6274efc7c912f52ffe9697085ce4b45e3a8dc8f9
[yaffs-website] / web / core / modules / file / file.install
1 <?php
2
3 /**
4  * @file
5  * Install, update and uninstall functions for File module.
6  */
7
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9
10 /**
11  * Implements hook_schema().
12  */
13 function file_schema() {
14   $schema['file_usage'] = [
15     'description' => 'Track where a file is used.',
16     'fields' => [
17       'fid' => [
18         'description' => 'File ID.',
19         'type' => 'int',
20         'unsigned' => TRUE,
21         'not null' => TRUE,
22       ],
23       'module' => [
24         'description' => 'The name of the module that is using the file.',
25         'type' => 'varchar_ascii',
26         'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
27         'not null' => TRUE,
28         'default' => '',
29       ],
30       'type' => [
31         'description' => 'The name of the object type in which the file is used.',
32         'type' => 'varchar_ascii',
33         'length' => 64,
34         'not null' => TRUE,
35         'default' => '',
36       ],
37       'id' => [
38         'description' => 'The primary key of the object using the file.',
39         'type' => 'varchar_ascii',
40         'length' => 64,
41         'not null' => TRUE,
42         'default' => 0,
43       ],
44       'count' => [
45         'description' => 'The number of times this file is used by this object.',
46         'type' => 'int',
47         'unsigned' => TRUE,
48         'not null' => TRUE,
49         'default' => 0,
50       ],
51     ],
52     'primary key' => ['fid', 'type', 'id', 'module'],
53     'indexes' => [
54       'type_id' => ['type', 'id'],
55       'fid_count' => ['fid', 'count'],
56       'fid_module' => ['fid', 'module'],
57     ],
58   ];
59   return $schema;
60 }
61
62 /**
63  * Implements hook_requirements().
64  *
65  * Display information about getting upload progress bars working.
66  */
67 function file_requirements($phase) {
68   $requirements = [];
69
70   // Check the server's ability to indicate upload progress.
71   if ($phase == 'runtime') {
72     $description = NULL;
73     $implementation = file_progress_implementation();
74     $server_software = \Drupal::request()->server->get('SERVER_SOFTWARE');
75
76     // Test the web server identity.
77     if (preg_match("/Nginx/i", $server_software)) {
78       $is_nginx = TRUE;
79       $is_apache = FALSE;
80       $fastcgi = FALSE;
81     }
82     elseif (preg_match("/Apache/i", $server_software)) {
83       $is_nginx = FALSE;
84       $is_apache = TRUE;
85       $fastcgi = strpos($server_software, 'mod_fastcgi') !== FALSE || strpos($server_software, 'mod_fcgi') !== FALSE;
86     }
87     else {
88       $is_nginx = FALSE;
89       $is_apache = FALSE;
90       $fastcgi = FALSE;
91     }
92
93     if (!$is_apache && !$is_nginx) {
94       $value = t('Not enabled');
95       $description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php or Nginx with PHP-FPM.');
96     }
97     elseif ($fastcgi) {
98       $value = t('Not enabled');
99       $description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php or PHP-FPM and not as FastCGI.');
100     }
101     elseif (!$implementation) {
102       $value = t('Not enabled');
103       $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="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.');
104     }
105     elseif ($implementation == 'apc') {
106       $value = t('Enabled (<a href="http://php.net/manual/apcu.configuration.php#ini.apcu.rfc1867">APC RFC1867</a>)');
107       $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="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a> if possible.');
108     }
109     elseif ($implementation == 'uploadprogress') {
110       $value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
111     }
112     $requirements['file_progress'] = [
113       'title' => t('Upload progress'),
114       'value' => $value,
115       'description' => $description,
116     ];
117   }
118
119   return $requirements;
120 }
121
122 /**
123  * Prevent unused files from being deleted.
124  */
125 function file_update_8300() {
126   // Disable deletion of unused permanent files.
127   \Drupal::configFactory()->getEditable('file.settings')
128     ->set('make_unused_managed_files_temporary', FALSE)
129     ->save();
130
131   return t('Files that have no remaining usages are no longer deleted by default.');
132 }
133
134 /**
135  * Add 'use_description_as_link_text' setting to file field formatters.
136  */
137 function file_update_8001() {
138   $displays = EntityViewDisplay::loadMultiple();
139   foreach ($displays as $display) {
140     /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
141     $fields_settings = $display->get('content');
142     $changed = FALSE;
143     foreach ($fields_settings as $field_name => $settings) {
144       if (!empty($settings['type'])) {
145         switch ($settings['type']) {
146           // The file_table formatter never displayed available descriptions
147           // before, so we disable this option to ensure backward compatibility.
148           case 'file_table':
149             $fields_settings[$field_name]['settings']['use_description_as_link_text'] = FALSE;
150             $changed = TRUE;
151             break;
152
153           // The file_default formatter always displayed available descriptions
154           // before, so we enable this option to ensure backward compatibility.
155           case 'file_default':
156             $fields_settings[$field_name]['settings']['use_description_as_link_text'] = TRUE;
157             $changed = TRUE;
158             break;
159         }
160       }
161     }
162     if ($changed === TRUE) {
163       $display->set('content', $fields_settings)->save();
164     }
165   }
166 }