drupal/core/modules/file/file.install

324 lines
11 KiB
Plaintext

<?php
/**
* @file
* Install, update and uninstall functions for File module.
*/
use Drupal\Core\Entity\DatabaseStorageController;
use Drupal\field\Entity\Field;
/**
* Implements hook_schema().
*/
function file_schema() {
$schema['file_managed'] = array(
'description' => 'Stores information for uploaded files.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uuid' => array(
'description' => 'Unique Key: Universally unique identifier for this entity.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'uid' => array(
'description' => 'The {users}.uid of the user who is associated with the file.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'filename' => array(
'description' => 'Name of the file with no path components. This may differ from the basename of the URI if the file is renamed to avoid overwriting an existing file.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'uri' => array(
'description' => 'The URI to access the file (either local or remote).',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'binary' => TRUE,
),
'langcode' => array(
'description' => 'The {language}.langcode of this file.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
'filemime' => array(
'description' => "The file's MIME type.",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'filesize' => array(
'description' => 'The size of the file in bytes.',
'type' => 'int',
'size' => 'big',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'description' => 'A field indicating the status of the file. Two status are defined in core: temporary (0) and permanent (1). Temporary files older than DRUPAL_MAXIMUM_TEMP_FILE_AGE will be removed during a cron run.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
),
'timestamp' => array(
'description' => 'UNIX timestamp for when the file was added.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'uid' => array('uid'),
'status' => array('status'),
'timestamp' => array('timestamp'),
),
'unique keys' => array(
'uuid' => array('uuid'),
'uri' => array('uri'),
),
'primary key' => array('fid'),
'foreign keys' => array(
'file_owner' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
$schema['file_usage'] = array(
'description' => 'Track where a file is used.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'module' => array(
'description' => 'The name of the module that is using the file.',
'type' => 'varchar',
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
'not null' => TRUE,
'default' => '',
),
'type' => array(
'description' => 'The name of the object type in which the file is used.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
),
'id' => array(
'description' => 'The primary key of the object using the file.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => 0,
),
'count' => array(
'description' => 'The number of times this file is used by this object.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('fid', 'type', 'id', 'module'),
'indexes' => array(
'type_id' => array('type', 'id'),
'fid_count' => array('fid', 'count'),
'fid_module' => array('fid', 'module'),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*
* Display information about getting upload progress bars working.
*/
function file_requirements($phase) {
$requirements = array();
// Check the server's ability to indicate upload progress.
if ($phase == 'runtime') {
$implementation = file_progress_implementation();
$server_software = Drupal::request()->server->get('SERVER_SOFTWARE');
$apache = strpos($server_software, 'Apache') !== FALSE;
$fastcgi = strpos($server_software, 'mod_fastcgi') !== FALSE || strpos($server_software, 'mod_fcgi') !== FALSE;
$description = NULL;
if (!$apache) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php.');
}
elseif ($fastcgi) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php and not as FastCGI.');
}
elseif (!$implementation && extension_loaded('apc')) {
$value = t('Not enabled');
$description = t('Your server is capable of displaying file upload progress through APC, but it is not enabled. Add <code>apc.rfc1867 = 1</code> to your php.ini configuration. Alternatively, it is recommended to use <a href="@url">PECL uploadprogress</a>, which supports more than one simultaneous upload.', array('@url' => 'http://pecl.php.net/package/uploadprogress'));
}
elseif (!$implementation) {
$value = t('Not enabled');
$description = t('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="@uploadprogress_url">PECL uploadprogress library</a> (preferred) or to install <a href="@apc_url">APC</a>.', array('@uploadprogress_url' => 'http://pecl.php.net/package/uploadprogress', '@apc_url' => 'http://php.net/apc'));
}
elseif ($implementation == 'apc') {
$value = t('Enabled (<a href="@url">APC RFC1867</a>)', array('@url' => 'http://php.net/manual/apc.configuration.php#ini.apc.rfc1867'));
$description = t('Your server is capable of displaying file upload progress using APC RFC1867. Note that only one upload at a time is supported. It is recommended to use the <a href="@url">PECL uploadprogress library</a> if possible.', array('@url' => 'http://pecl.php.net/package/uploadprogress'));
}
elseif ($implementation == 'uploadprogress') {
$value = t('Enabled (<a href="@url">PECL uploadprogress</a>)', array('@url' => 'http://pecl.php.net/package/uploadprogress'));
}
$requirements['file_progress'] = array(
'title' => t('Upload progress'),
'value' => $value,
'description' => $description,
);
}
return $requirements;
}
/**
* Implements hook_update_dependencies().
*/
function file_update_dependencies() {
// Convert fields and instances to ConfigEntities after the {file_usage}.id
// column has moved to varchar.
$dependencies['field'][8003] = array(
'file' => 8001,
);
// Convert the 'fid' column of file fields to 'target_id' after the field
// tables have been reorganized.
$dependencies['file'][8003] = array(
'field' => 8006,
);
return $dependencies;
}
/**
* Converts default_file_main variable to config.
*
* @ingroup config_upgrade
*/
function file_update_8000() {
update_variables_to_config('file.settings', array(
'file_description_type' => 'description.type',
'file_description_length'=>'description.length',
'file_icon_directory'=>'icon.directory',
));
}
/*
* Convert the 'id' column in {file_usage} to accept UUIDs.
*/
function file_update_8001() {
$spec = array(
'description' => 'The primary key of the object using the file.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
);
db_change_field('file_usage', 'id', 'id', $spec);
}
/**
* Convert the 'module' column in {file_usage} to the maximum shortname length.
*/
function file_update_8002() {
if (db_field_exists('file_usage', 'module')) {
$spec = array(
'description' => 'The name of the module that is using the file.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => '',
);
db_change_field('file_usage', 'module', 'module', $spec);
}
}
/**
* Update file field tables to use target_id instead of fid.
*/
function file_update_8003() {
foreach (config_get_storage_names_with_prefix('field.field.') as $config_name) {
$field_config = Drupal::config($config_name);
// Only update file fields that use the default SQL storage.
if (in_array($field_config->get('type'), array('file', 'image'))) {
$field = new Field($field_config->get());
if (db_table_exists(DatabaseStorageController::_fieldTableName($field))) {
$tables = array(
DatabaseStorageController::_fieldTableName($field),
DatabaseStorageController::_fieldRevisionTableName($field),
);
foreach ($tables as $table_name) {
// Skip fields which were created during the upgrade process.
if (!db_field_exists($table_name, $field->name . '_fid')) {
continue 2;
}
db_change_field($table_name, $field->name . '_fid', $field->name . '_target_id', array(
'description' => 'The ID of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
));
// Change the index.
db_drop_index($table_name, $field->name . '_fid');
db_add_index($table_name, $field->name . '_target_id', array($field->name . '_target_id'));
}
// Update the indexes in field config as well.
$indexes = $field_config->get('indexes');
unset($indexes['fid']);
$indexes['target_id'] = array('target_id');
$field_config->set('indexes', $indexes);
$field_config->save();
}
}
}
}
/*
* Convert the 'filesize' column in {file_managed} to a bigint.
*/
function file_update_8004() {
$spec = array(
'description' => 'The size of the file in bytes.',
'type' => 'int',
'size' => 'big',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
db_change_field('file_managed', 'filesize', 'filesize', $spec);
}