Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[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  * @param $phase
12  * @return array
13  */
14 function simple_sitemap_requirements($phase) {
15   $requirements = [];
16
17   if (!extension_loaded('xmlwriter')) {
18     $requirements['simple_sitemap_php_extensions'] = [
19       'title' => t('Simple XML sitemap PHP extensions'),
20       'value' => t('Missing PHP xmlwriter extension'),
21       '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.'),
22       'severity' => REQUIREMENT_ERROR,
23     ];
24   }
25
26   switch ($phase) {
27
28     case 'runtime':
29
30       $generator = \Drupal::service('simple_sitemap.generator');
31       $generated_ago = $generator->getGeneratedAgo();
32       $cron_generation = $generator->getSetting('cron_generate');
33
34       if (!$generated_ago) {
35         $value = t('Not available');
36         $description = t($cron_generation
37           ? 'Run cron, or <a href="@generate">generate</a> the sitemap manually.'
38           : 'Generation on cron run is disabled. <a href="@generate">Generate</a> the sitemap manually.', [
39             '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
40           ]
41         );
42         $severity = REQUIREMENT_WARNING;
43       }
44       else {
45         $value = t('XML sitemap is available');
46         $description = t('The <a href="@sitemap">XML sitemap</a> was generated @ago ago.'
47           . ' ' . ($cron_generation
48             ? 'Run cron, or <a href="@generate">regenerate</a> the sitemap manually.'
49             : 'Generation on cron run is disabled. <a href="@generate">Regenerate</a> the sitemap manually.'), [
50               '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml',
51               '@ago' => $generated_ago,
52               '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
53             ]
54           );
55         $severity = REQUIREMENT_INFO;
56       }
57
58       $requirements['simple_sitemap_generated'] = [
59         'title' => 'Simple XML sitemap',
60         'value' => $value,
61         'description' => $description,
62         'severity' => $severity,
63       ];
64       break;
65   }
66   return $requirements;
67 }
68
69 /**
70  * Implements hook_schema().
71  */
72 function simple_sitemap_schema() {
73   $schema['simple_sitemap'] = [
74     'description' => 'Holds XML sitemaps as strings for quick retrieval.',
75     'fields' => [
76       'id' => [
77         'description' => 'Sitemap chunk unique identifier.',
78         'type' => 'int',
79         'size' => 'small',
80         'not null' => TRUE,
81         'unsigned' => TRUE,
82       ],
83       'sitemap_string' => [
84         'description' => 'XML sitemap chunk string.',
85         'type' => 'text',
86         'size' => 'big',
87         'not null' => TRUE,
88       ],
89       'sitemap_created' => [
90         'description' => 'Timestamp of sitemap chunk generation.',
91         'type' => 'int',
92         'default' => 0,
93         'not null' => TRUE,
94         'unsigned' => TRUE,
95       ],
96     ],
97     'primary key' => ['id'],
98   ];
99
100   $schema['simple_sitemap_entity_overrides'] = [
101     'description' => 'Holds sitemap settings overridden by entities.',
102     'fields' => [
103       'id' => [
104         'description' => 'Override unique identifier.',
105         'type' => 'serial',
106         'unsigned' => TRUE,
107         'not null' => TRUE,
108       ],
109       'entity_type' => [
110         'description' => 'Entity type of the overriding entity.',
111         'type' => 'varchar',
112         'length' => 32,
113         'not null' => TRUE,
114       ],
115       'entity_id' => [
116         'description' => 'ID of the overriding entity.',
117         'type' => 'varchar',
118         'length' => 32,
119         'not null' => TRUE,
120       ],
121       'inclusion_settings' => [
122         'description' => 'Setting for the overriding entity.',
123         'type' => 'blob',
124       ],
125     ],
126     'primary key' => ['id'],
127   ];
128   return $schema;
129 }
130
131 /**
132  * Implements hook_install().
133  */
134 function simple_sitemap_install() {
135   $base_url = $GLOBALS['base_url'];
136   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"]));
137 }
138
139 /**
140  * Changing the data structure of the module's configuration.
141  */
142 function simple_sitemap_update_8201() {
143   $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
144   $entity_types = is_array($entity_types) ? $entity_types : [];
145   $naming_changes = [
146     'node_type' => 'node',
147     'taxonomy_vocabulary' => 'taxonomy_term',
148     'menu' => 'menu_link_content',
149     'commerce_product_type' => 'commerce_product',
150     'media_bundle' => 'media',
151   ];
152   foreach ($entity_types as $entity_type_name => $settings) {
153     if (isset($naming_changes[$entity_type_name])) {
154       $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
155       unset($entity_types[$entity_type_name]);
156     }
157   }
158   \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
159     ->set('entity_types', $entity_types)->save();
160 }
161
162 /**
163  * Moving entity overrides from configuration to database table.
164  */
165 function simple_sitemap_update_8202() {
166   $database = \Drupal::database();
167
168   // Create database table.
169   if (!$database->schema()->tableExists('simple_sitemap_entity_overrides')) {
170     $database->schema()->createTable('simple_sitemap_entity_overrides', [
171       'description' => 'Holds sitemap settings overridden by entities.',
172       'fields' => [
173         'id' => [
174           'description' => 'Override unique identifier.',
175           'type' => 'serial',
176           'unsigned' => TRUE,
177           'not null' => TRUE,
178         ],
179         'entity_type' => [
180           'description' => 'Entity type of the overriding entity.',
181           'type' => 'varchar',
182           'length' => 32,
183           'not null' => TRUE,
184         ],
185         'entity_id' => [
186           'description' => 'ID of the overriding entity.',
187           'type' => 'int',
188           'unsigned' => TRUE,
189           'not null' => TRUE,
190         ],
191         'inclusion_settings' => [
192           'description' => 'Setting for the overriding entity.',
193           'type' => 'blob',
194         ],
195       ],
196       'primary key' => ['id'],
197     ]);
198   }
199
200   // Populate database table with config values.
201   $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
202   $entity_types = is_array($entity_types) ? $entity_types : [];
203
204   foreach ($entity_types as $entity_type_name => &$entity_type) {
205     if (is_array($entity_type)) {
206       foreach ($entity_type as $bundle_name => &$bundle) {
207         if (isset($bundle['entities'])) {
208           foreach ($bundle['entities'] as $entity_id => $entity_settings) {
209             $database->insert('simple_sitemap_entity_overrides')
210               ->fields([
211                 'entity_type' => $entity_type_name,
212                 'entity_id' => $entity_id,
213                 'inclusion_settings' => serialize($entity_settings),
214               ])
215               ->execute();
216           }
217           // Remove entity overrides from configuration.
218           unset($bundle['entities']);
219         }
220       }
221     }
222   }
223
224   \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
225     ->set('entity_types', $entity_types)->save();
226 }
227
228 /**
229  * Splitting simple_sitemap.settings configuration into simple_sitemap.settings,
230  * simple_sitemap.entity_types and simple_sitemap.custom.
231  */
232 function simple_sitemap_update_8203() {
233   $old_config = $config = \Drupal::config('simple_sitemap.settings');
234   foreach (['entity_types', 'custom'] as $config_name) {
235     if (!$config = $old_config->get($config_name)) {
236       continue;
237     }
238     \Drupal::service('config.factory')->getEditable("simple_sitemap.$config_name")
239       ->setData($config)->save();
240   }
241   $settings = $old_config->get('settings');
242   \Drupal::service('config.factory')->getEditable("simple_sitemap.settings")
243     ->setData($settings)->save();
244 }
245
246 /**
247  * Removing entity type settings for entity types which do not have the canonical
248  * link template.
249  */
250 function simple_sitemap_update_8204() {
251   $sitemap_entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
252   $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
253   unset($entity_types['_core']);
254   foreach ($entity_types as $entity_type_id => $entity_type) {
255     if (!isset($sitemap_entity_types[$entity_type_id])
256       || !$sitemap_entity_types[$entity_type_id]->hasLinkTemplate('canonical')) {
257
258       // Delete entity overrides.
259       \Drupal::database()->delete('simple_sitemap_entity_overrides')
260         ->condition('entity_type', $entity_type_id)
261         ->execute();
262
263       // Delete entity type settings.
264       unset($entity_types[$entity_type_id]);
265     }
266   }
267   \Drupal::service('config.factory')->getEditable("simple_sitemap.entity_types")
268     ->setData($entity_types)->save();
269 }
270
271 /**
272  * Splitting simple_sitemap.entity_types into individual configuration objects
273  * for each bundle.
274  */
275 function simple_sitemap_update_8205() {
276   $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
277   unset($entity_types['_core']);
278   $enabled_entity_types = [];
279   foreach ($entity_types as $entity_type_id => $bundles) {
280     $enabled_entity_types[] = $entity_type_id;
281     foreach ($bundles as $bundle_name => $bundle_settings) {
282       \Drupal::service('config.factory')
283         ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
284         ->setData($bundle_settings)->save();
285     }
286   }
287
288   // Add enabled entity type settings.
289   \Drupal::service('config.factory')
290     ->getEditable('simple_sitemap.settings')
291     ->set('enabled_entity_types', $enabled_entity_types)
292     ->save();
293
294   // Remove old configuration object.
295   \Drupal::service('config.factory')
296     ->getEditable('simple_sitemap.entity_types')
297     ->delete();
298 }
299
300 /**
301  * Placing custom links in a subkey of simple_sitemap.custom configuration.
302  */
303 function simple_sitemap_update_8206() {
304   $custom_links = \Drupal::config('simple_sitemap.custom')->get();
305   foreach ($custom_links as $i => $custom_link) {
306     if (!isset($custom_link['path'])) {
307       unset($custom_links[$i]);
308     }
309   }
310   \Drupal::service('config.factory')->getEditable('simple_sitemap.custom')
311     ->setData(['links' => $custom_links])->save();
312 }
313
314 /**
315  * Updating entity_id field of simple_sitemap_entity_overrides table to varchar(32).
316  */
317 function simple_sitemap_update_8207() {
318   \Drupal::database()->schema()->changeField(
319     'simple_sitemap_entity_overrides',
320     'entity_id',
321     'entity_id', [
322       'description' => 'ID of the overriding entity.',
323       'type' => 'varchar',
324       'length' => 32,
325       'not null' => TRUE,
326     ]
327   );
328 }
329
330 /**
331  * Adding changefreq setting to all existing bundle and entity instance settings.
332  */
333 function simple_sitemap_update_8208() {
334
335   // Update existing bundle settings.
336   $config_factory = \Drupal::service('config.factory');
337   $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
338
339   foreach ($entity_types as $entity_type) {
340     $config = $config_factory->get($entity_type)->get();
341     if (!isset($config['changefreq'])) {
342       $config_factory->getEditable($entity_type)
343         ->setData($config + ['changefreq' => ''])
344         ->save();
345     }
346   }
347
348   // Update existing entity override data.
349   $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
350     ->fields('o', ['id', 'inclusion_settings'])
351     ->execute()->fetchAll(\PDO::FETCH_OBJ);
352
353   foreach ($results as $row) {
354     $settings = unserialize($row->inclusion_settings);
355     if (!isset($settings['changefreq'])) {
356       \Drupal::database()->update('simple_sitemap_entity_overrides')
357         ->fields(['inclusion_settings' => serialize($settings + ['changefreq' => '']),])
358         ->condition('id', $row->id)
359         ->execute();
360     }
361   }
362
363   return t('You may now want to configure the new changefreq setting for sitemap entities and custom links.');
364 }
365
366 /**
367  * Adding image inclusion setting to all existing bundle and entity instance settings.
368  */
369 function simple_sitemap_update_8209() {
370
371   // Update existing bundle settings.
372   $config_factory = \Drupal::service('config.factory');
373   $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
374
375   foreach ($entity_types as $entity_type) {
376     $config = $config_factory->get($entity_type)->get();
377     if (!isset($config['include_images'])) {
378       $config_factory->getEditable($entity_type)
379         ->setData($config + ['include_images' => 0])
380         ->save();
381     }
382   }
383
384   // Update existing entity override data.
385   $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
386     ->fields('o', ['id', 'inclusion_settings'])
387     ->execute()->fetchAll(\PDO::FETCH_OBJ);
388
389   foreach ($results as $row) {
390     $settings = unserialize($row->inclusion_settings);
391     if (!isset($settings['include_images'])) {
392       \Drupal::database()->update('simple_sitemap_entity_overrides')
393         ->fields(['inclusion_settings' => serialize($settings + ['include_images' => 0]),])
394         ->condition('id', $row->id)
395         ->execute();
396     }
397   }
398
399   return t('You may now want to configure your sitemap entities to include images.');
400 }