Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / simple_sitemap / simple_sitemap.install
1 <?php
2
3 /**
4  * @file
5  * Module install and update procedures.
6  */
7
8 /**
9  * Implements hook_requirements().
10  */
11 function simple_sitemap_requirements($phase) {
12   $requirements = [];
13
14   if (!extension_loaded('xmlwriter')) {
15     $requirements['simple_sitemap_php_extensions'] = [
16       'title' => t('Simple XML sitemap PHP extensions'),
17       'value' => t('Missing PHP xmlwriter extension'),
18       'description' => t("In order to be able to generate sitemaps, the Simple XML sitemap module requires the <em>xmlwriter</em> PHP extension to be enabled."),
19       'severity' => REQUIREMENT_ERROR,
20     ];
21   }
22
23   switch ($phase) {
24
25     case 'runtime':
26
27       $generator = \Drupal::service('simple_sitemap.generator');
28       $generated_ago = $generator->getGeneratedAgo();
29       $cron_generation = $generator->getSetting('cron_generate');
30
31       if (!$generated_ago) {
32         $value = t('Not available');
33         $description = t($cron_generation
34           ? "Run cron, or <a href='@generate'>generate</a> the sitemap manually."
35           : "Generation on cron run is disabled. <a href='@generate'>Generate</a> the sitemap manually.", [
36             '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
37           ]
38         );
39         $severity = REQUIREMENT_WARNING;
40       }
41       else {
42         $value = t('XML sitemap is available');
43         $description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago."
44           . ' ' . ($cron_generation
45             ? "Run cron, or <a href='@generate'>regenerate</a> the sitemap manually."
46             : "Generation on cron run is disabled. <a href='@generate'>Regenerate</a> the sitemap manually."), [
47               '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml',
48               '@ago' => $generated_ago,
49               '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
50             ]
51           );
52         $severity = REQUIREMENT_INFO;
53       }
54
55       $requirements['simple_sitemap_generated'] = [
56         'title' => 'Simple XML sitemap',
57         'value' => $value,
58         'description' => $description,
59         'severity' => $severity,
60       ];
61       break;
62   }
63   return $requirements;
64 }
65
66 /**
67  * Implements hook_schema().
68  */
69 function simple_sitemap_schema() {
70   $schema['simple_sitemap'] = [
71     'description' => 'Holds XML sitemaps as strings for quick retrieval.',
72     'fields' => [
73       'id' => [
74         'description' => 'Sitemap chunk unique identifier.',
75         'type' => 'int',
76         'size' => 'small',
77         'not null' => TRUE,
78         'unsigned' => TRUE,
79       ],
80       'sitemap_string' => [
81         'description' => 'XML sitemap chunk string.',
82         'type' => 'text',
83         'size' => 'big',
84         'not null' => TRUE,
85       ],
86       'sitemap_created' => [
87         'description' => 'Timestamp of sitemap chunk generation.',
88         'type' => 'int',
89         'default' => 0,
90         'not null' => TRUE,
91         'unsigned' => TRUE,
92       ],
93     ],
94     'primary key' => ['id'],
95   ];
96
97   $schema['simple_sitemap_entity_overrides'] = [
98     'description' => 'Holds sitemap settings overridden by entities.',
99     'fields' => [
100       'id' => [
101         'description' => 'Override unique identifier.',
102         'type' => 'serial',
103         'unsigned' => TRUE,
104         'not null' => TRUE,
105       ],
106       'entity_type' => [
107         'description' => 'Entity type of the overriding entity.',
108         'type' => 'varchar',
109         'length' => 32,
110         'not null' => TRUE,
111       ],
112       'entity_id' => [
113         'description' => 'ID of the overriding entity.',
114         'type' => 'varchar',
115         'length' => 32,
116         'not null' => TRUE,
117       ],
118       'inclusion_settings' => [
119         'description' => 'Setting for the overriding entity.',
120         'type' => 'blob',
121       ],
122     ],
123     'primary key' => ['id'],
124   ];
125   return $schema;
126 }
127
128 /**
129  * Implements hook_install().
130  */
131 function simple_sitemap_install() {
132   $base_url = $GLOBALS['base_url'];
133   drupal_set_message(t("You can now include content into the sitemap by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url'>node type edit pages</a>).<br/>Support for additional entity types and custom links can be added on <a href='@config_url'>the module's configuration pages</a>.", ['@content_type_url' => "$base_url/admin/structure/types", '@config_url' => "$base_url/admin/config/search/simplesitemap"]));
134 }
135
136 /**
137  * Changing the data structure of the module's configuration.
138  */
139 function simple_sitemap_update_8201() {
140   $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
141   $entity_types = is_array($entity_types) ? $entity_types : [];
142   $naming_changes = [
143     'node_type' => 'node',
144     'taxonomy_vocabulary' => 'taxonomy_term',
145     'menu' => 'menu_link_content',
146     'commerce_product_type' => 'commerce_product',
147     'media_bundle' => 'media',
148   ];
149   foreach ($entity_types as $entity_type_name => $settings) {
150     if (isset($naming_changes[$entity_type_name])) {
151       $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
152       unset($entity_types[$entity_type_name]);
153     }
154   }
155   \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
156     ->set('entity_types', $entity_types)->save();
157 }
158
159 /**
160  * Moving entity overrides from configuration to database table.
161  */
162 function simple_sitemap_update_8202() {
163   $database = \Drupal::database();
164
165   // Create database table.
166   if (!$database->schema()->tableExists('simple_sitemap_entity_overrides')) {
167     $database->schema()->createTable('simple_sitemap_entity_overrides', [
168       'description' => 'Holds sitemap settings overridden by entities.',
169       'fields' => [
170         'id' => [
171           'description' => 'Override unique identifier.',
172           'type' => 'serial',
173           'unsigned' => TRUE,
174           'not null' => TRUE,
175         ],
176         'entity_type' => [
177           'description' => 'Entity type of the overriding entity.',
178           'type' => 'varchar',
179           'length' => 32,
180           'not null' => TRUE,
181         ],
182         'entity_id' => [
183           'description' => 'ID of the overriding entity.',
184           'type' => 'int',
185           'unsigned' => TRUE,
186           'not null' => TRUE,
187         ],
188         'inclusion_settings' => [
189           'description' => 'Setting for the overriding entity.',
190           'type' => 'blob',
191         ],
192       ],
193       'primary key' => ['id'],
194     ]);
195   }
196
197   // Populate database table with config values.
198   $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
199   $entity_types = is_array($entity_types) ? $entity_types : [];
200
201   foreach ($entity_types as $entity_type_name => &$entity_type) {
202     if (is_array($entity_type)) {
203       foreach($entity_type as $bundle_name => &$bundle) {
204         if (isset($bundle['entities'])) {
205           foreach($bundle['entities'] as $entity_id => $entity_settings) {
206             $database->insert('simple_sitemap_entity_overrides')
207               ->fields([
208                 'entity_type' => $entity_type_name,
209                 'entity_id' => $entity_id,
210                 'inclusion_settings' => serialize($entity_settings),
211               ])
212               ->execute();
213           }
214           // Remove entity overrides from configuration.
215           unset($bundle['entities']);
216         }
217       }
218     }
219   }
220
221   \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
222     ->set('entity_types', $entity_types)->save();
223 }
224
225 /**
226  * Splitting simple_sitemap.settings configuration into simple_sitemap.settings,
227  * simple_sitemap.entity_types and simple_sitemap.custom.
228  */
229 function simple_sitemap_update_8203() {
230   $old_config = $config = \Drupal::config('simple_sitemap.settings');
231   foreach (['entity_types', 'custom'] as $config_name) {
232     if (!$config = $old_config->get($config_name)) {
233       continue;
234     }
235     \Drupal::service('config.factory')->getEditable("simple_sitemap.$config_name")
236       ->setData($config)->save();
237   }
238   $settings = $old_config->get('settings');
239   \Drupal::service('config.factory')->getEditable("simple_sitemap.settings")
240     ->setData($settings)->save();
241 }
242
243 /**
244  * Removing entity type settings for entity types which do not have the canonical
245  * link template.
246  */
247 function simple_sitemap_update_8204() {
248   $sitemap_entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
249   $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
250   unset($entity_types['_core']);
251   foreach($entity_types as $entity_type_id => $entity_type) {
252     if (!isset($sitemap_entity_types[$entity_type_id])
253       || !$sitemap_entity_types[$entity_type_id]->hasLinkTemplate('canonical')) {
254
255       // Delete entity overrides.
256       \Drupal::database()->delete('simple_sitemap_entity_overrides')
257         ->condition('entity_type', $entity_type_id)
258         ->execute();
259
260       // Delete entity type settings.
261       unset($entity_types[$entity_type_id]);
262     }
263   }
264   \Drupal::service('config.factory')->getEditable("simple_sitemap.entity_types")
265     ->setData($entity_types)->save();
266 }
267
268 /**
269  * Splitting simple_sitemap.entity_types into individual configuration objects
270  * for each bundle.
271  */
272 function simple_sitemap_update_8205() {
273   $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
274   unset($entity_types['_core']);
275   $enabled_entity_types = [];
276   foreach($entity_types as $entity_type_id => $bundles) {
277     $enabled_entity_types[] = $entity_type_id;
278     foreach($bundles as $bundle_name => $bundle_settings) {
279       \Drupal::service('config.factory')
280         ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
281         ->setData($bundle_settings)->save();
282     }
283   }
284
285   // Add enabled entity type settings.
286   \Drupal::service('config.factory')
287     ->getEditable('simple_sitemap.settings')
288     ->set('enabled_entity_types', $enabled_entity_types)
289     ->save();
290
291   // Remove old configuration object.
292   \Drupal::service('config.factory')
293     ->getEditable('simple_sitemap.entity_types')
294     ->delete();
295 }
296
297 /**
298  * Placing custom links in a subkey of simple_sitemap.custom configuration.
299  */
300 function simple_sitemap_update_8206() {
301   $custom_links = \Drupal::config('simple_sitemap.custom')->get();
302   foreach($custom_links as $i => $custom_link) {
303     if (!isset($custom_link['path'])) {
304       unset($custom_links[$i]);
305     }
306   }
307   \Drupal::service('config.factory')->getEditable("simple_sitemap.custom")
308     ->setData(['links' => $custom_links])->save();
309 }
310
311 /**
312  * Updating entity_id field of simple_sitemap_entity_overrides table to varchar(32).
313  */
314 function simple_sitemap_update_8207() {
315   \Drupal::database()->schema()->changeField(
316     'simple_sitemap_entity_overrides',
317     'entity_id',
318     'entity_id', [
319       'description' => 'ID of the overriding entity.',
320       'type' => 'varchar',
321       'length' => 32,
322       'not null' => TRUE,
323     ]
324   );
325 }