-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetatag.install
2614 lines (2305 loc) · 91 KB
/
metatag.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Install, update, and uninstall functions for the metatag module.
*/
/**
* Implements hook_requirements().
*/
function metatag_requirements($phase) {
$requirements = array();
// Ensure translations don't break during installation.
$t = get_t();
if ($phase == 'install') {
// Handle scenarios where the site had the legacy "metatags" module
// installed but then had Metatag installed on top of it.
if (function_exists('db_table_exists') && function_exists('db_field_exists') && function_exists('db_query') && Database::isActiveConnection()) {
// Check if the primary table already exists.
if (db_table_exists('metatag')) {
// Check to see if all of the fields exist in the table. If one of the
// fields does not exist, proceed with the fix.
$fields = array(
'entity_type',
'entity_id',
'revision_id',
'language',
'data',
);
foreach ($fields as $field) {
// This field doesn't exist, so determine what to do.
if (!db_field_exists('metatag', $field)) {
// The table contains data, so rename it.
if (db_query("SELECT COUNT(*) FROM {metatag}")->fetchField() > 0) {
db_query("RENAME TABLE {metatag} TO {metatag}_legacy");
$message = 'An out-of-date version of the {metatag} table was discovered. As the table contained data it was renamed with a suffix of "_legacy". This will not prevent installation from continuing, but will need to be dealt with later. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
}
// The table is empty, so delete it.
else {
db_query("DROP TABLE {metatag}");
$message = 'An out-of-date version of the {metatag} table was discovered. As the table was empty it was simply removed so that it could be recreated in the correct format. Installation may now proceed. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
}
$requirements['metatag'] = array(
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => $t('Legacy data discovered.'),
'description' => $t($message),
);
drupal_set_message($t($message), 'warning');
break;
}
}
}
}
}
elseif ($phase == 'runtime') {
// Complete data dump of all installed modules, used later.
$module_data = system_rebuild_module_data();
// Work out the release of D7 that is currently running.
list($major, $minor) = explode('.', VERSION);
// Strip off any suffixes on the version string, e.g. "17-dev".
if (strpos('-', $minor)) {
list($minor, $suffix) = explode('-', $minor);
}
// Releases of Drupal older than 7.40 support did not project:module syntax
// for dependencies.
if ($minor < 40) {
$requirements['metatag'] = array(
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => $t('Upgrade Drupal core to v7.40 or newer'),
'description' => $t("This older version of Drupal core is missing functionality necessary for Metatag to work correctly, it must be upgraded to version 7.40 or newer."),
);
}
// Add a note if Page Title is also installed.
if (module_exists('page_title')) {
$requirements['metatag_page_title'] = array(
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => $t('Page Title module should be removed'),
'description' => $t('The Metatag module is able to customize page titles, so running the Page Title module simultaneously can lead to complications. Please follow the instructions to <a href="@page">convert the Page Title settings</a> and uninstall the module.', array('@page' => 'https://www.drupal.org/node/2774833')),
);
}
// Add a note if the deprecated metatag.entity_translation.inc file still
// exists.
$filename = 'metatag.entity_translation.inc';
if (file_exists(dirname(__FILE__) . '/' . $filename)) {
$requirements['metatag_deprecated_et_file'] = array(
'severity' => REQUIREMENT_ERROR,
'title' => 'Metatag',
'value' => $t('Unwanted :filename file found', array(':filename' => $filename)),
'description' => $t("The :filename file was removed in v7.x-1.0-beta5 but it still exists in the site's Metatag module's directory and will cause problems. This file needs to be removed. The file's path in the Drupal directory structure is:<br /><code>!short_path</code><br />The file's full path is:<br /><code>!full_path</code>", array(':filename' => $filename, '!short_path' => drupal_get_path('module', 'metatag') . '/' . $filename, '!full_path' => dirname(__FILE__) . $filename)),
);
}
// Check that Entity_Translation is current.
if (module_exists('entity_translation')
&& !empty($module_data['entity_translation'])
&& !empty($module_data['entity_translation']->schema_version)
&& $module_data['entity_translation']->schema_version < 7004) {
$requirements['metatag_et_version'] = array(
'severity' => REQUIREMENT_ERROR,
'title' => 'Metatag',
'value' => $t('<a href="@url">Entity_Translation</a> is out of date and requires updating', array('@url' => 'https://www.drupal.org/project/entity_translation')),
'description' => $t('The Entity_Translation module is out of date and needs to be updated in order to be compatible with Metatag.'),
);
}
// It's recommended to install the Transliteration module to clean up file
// paths for use with image meta tags.
if (!module_exists('transliteration')) {
$requirements['metatag_transliteration'] = array(
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => $t('The Transliteration module is recommended.'),
'description' => $t("It is recommended to install the <a href=\"@url\">Transliteration module</a> to clean up filenames of uploaded files that may be used with image meta tags.", array('@url' => 'https://drupal.org/project/transliteration')),
);
}
// It's recommended to install the Imagecache Token module to make image
// tokens easier to do.
if (!module_exists('imagecache_token')) {
$requirements['metatag_imagecache_token'] = array(
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => $t('The Imagecache Token module is recommended.'),
'description' => $t("It is recommended to install the <a href=\"@url\">Imagecache Token module</a> to make it easier to control image meta tags, e.g. og:image. See the Metatag module's README.txt for details.", array('@url' => 'https://drupal.org/project/imagecache_token')),
);
}
// The Admin Language module can cause problems.
if (module_exists('admin_language') && variable_get('admin_language_force_neutral', 0)) {
$requirements['metatag_admin_language'] = array(
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => $t('Conflict with Admin Language module.'),
'description' => $t("Using the \"@option\" with Metatag can lead to data loss, so it is recommended to <a href=\"@url\">disable that option</a>.", array('@option' => t('Force language neutral aliases'), '@url' => url('admin/config/regional/language/admin_language'))),
);
}
// Token v7.x-1.6 is *highly* recommended.
$token_module = $module_data['token'];
// If the version string is not present then it means the module is running
// from git, which means it can't be compared against. Alternatively, look
// for the test file, which was the last commit of the 1.6 release.
if (!empty($token_module->info['version'])
|| empty($token_module->info['files'])) {
// If there's no test file then this is older than v1.6.
if (empty($token_module->info['files'])) {
$minor = 5;
}
// Otherwise check the version string.
else {
// Versions are in the format 7.x-1.y, so split the string up to find
// the 'y' portion.
$version = explode('-', $token_module->info['version']);
if (isset($version[1])) {
list($major, $minor) = explode('.', $version[1]);
// Strip off any suffixes on the version string, e.g. "17-dev".
if (strpos('-', $minor)) {
list($minor, $suffix) = explode('-', $minor);
}
}
// If the version string couldn't be extracted correctly, assume that
// an incorrect version is installed.
else {
$minor = 0;
}
}
// If v1.6 is not installed, give a warning.
if ($minor < 6) {
$requirements['metatag_token_version'] = array(
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => $t('Token module is out of date.'),
'description' => $t('It is highly recommended to install <a href="https://www.drupal.org/project/token">Token module</a> v7.x-1.6 or newer, otherwise there may be problems using certain meta tags.'),
);
}
}
// If Workbench Moderation is installed, show a message if it is out of
// date.
if (module_exists('workbench_moderation')) {
$wm_module = $module_data['workbench_moderation'];
// If the version string is not present then it means the module is
// running from git, which means it can't be compared against.
if (!empty($wm_module->info['version'])) {
// Versions are in the format 7.x-1.y, so split the string up to find
// the 'y' portion.
$version = explode('-', $wm_module->info['version']);
if (isset($version[1])) {
list($major, $minor) = explode('.', $version[1]);
}
// If the version string couldn't be extracted correctly, assume that
// an incorrect version is installed.
else {
$major = 0;
}
}
// If v3.x is not installed, give a message.
if ($major < 3) {
$requirements['metatag_wm_version'] = array(
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => $t('Workbench Moderation module is out of date.'),
'description' => $t('It is recommended to use <a href="https://www.drupal.org/project/workbench_moderation">Workbench Moderation module</a> v7.x-3.0 or newer.'),
);
}
}
// Recommend the Schema.org Metatag module.
if (!module_exists('schema_metatag')) {
$requirements['metatag_schema'] = array(
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => $t('Schema.org Metatag is recommended'),
'description' => $t('The <a href="@module">Schema.org Metatag</a> module is highly recommended to add <a href="@jsonld">JSON-LD</a> -formatted <a href="@schema">schema.org</a> compatible data structures to the site.', array('@module' => 'https://www.drupal.org/project/schema_metatag', '@jsonld' => 'https://json-ld.org', '@schema' => 'http://schema.org')),
);
}
else {
$requirements['metatag_schema'] = array(
'severity' => REQUIREMENT_OK,
'title' => 'Metatag',
'value' => $t('Schema.org Metatag is installed'),
'description' => $t('The <a href="@module">Schema.org Metatag</a> module is installed.', array('@module' => 'https://www.drupal.org/project/schema_metatag')),
);
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function metatag_schema() {
$schema['metatag_config'] = array(
'description' => 'Storage of meta tag configuration and defaults.',
'export' => array(
'key' => 'instance',
'key name' => 'Instance',
'primary key' => 'cid',
'identifier' => 'config',
'default hook' => 'metatag_config_default',
'api' => array(
'owner' => 'metatag',
'api' => 'metatag',
'minimum_version' => 1,
'current_version' => 1,
),
'cache defaults' => TRUE,
'default cache bin' => 'cache_metatag',
),
'fields' => array(
'cid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The primary identifier for a metatag configuration set.',
'no export' => TRUE,
),
'instance' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The machine-name of the configuration, typically entity-type:bundle.',
),
'config' => array(
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'Serialized data containing the meta tag configuration.',
'translatable' => TRUE,
),
),
'primary key' => array('cid'),
'unique keys' => array(
'instance' => array('instance'),
),
);
$schema['metatag'] = array(
'fields' => array(
'entity_type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'The entity type this data is attached to.',
),
'entity_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The entity id this data is attached to.',
),
'revision_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The revision_id for the entity object this data is attached to.',
),
'language' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'The language of the tag.',
),
'data' => array(
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'serialize' => TRUE,
),
),
'indexes' => array(
'type_revision' => array(
'entity_type',
'revision_id',
),
),
'primary key' => array(
'entity_type',
'entity_id',
'revision_id',
'language',
),
);
$schema['cache_metatag'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_metatag']['description'] = 'Cache table for the generated meta tag output.';
return $schema;
}
/**
* Implements hook_install().
*/
function metatag_install() {
drupal_set_message(t("Thank you for installing the Metatag module. It is recommended to read the module's <a href=\"!url\" title=\"Read the Metatag module's documentation\">README.txt</a> file as there are some known issues that may affect this site.", array('!url' => url(drupal_get_path('module', 'metatag') . '/README.txt'))));
// Always enable the node, taxonomy term and user entities.
foreach (array('node', 'taxonomy_term', 'user') as $entity_type) {
// Enable the main entity type.
$variable_name = 'metatag_enable_' . $entity_type;
variable_set($variable_name, TRUE);
// Update each entity bundle too.
$entity_info = entity_get_info($entity_type);
if (!empty($entity_info['bundles'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
variable_set($variable_name, TRUE);
}
}
}
// Possibly enable other entities, if they're specifically requested.
foreach (entity_get_info() as $entity_type => $entity_info) {
// Skip the three entity types that were already enabled.
if (in_array($entity_type, array('node', 'taxonomy_term', 'user'))) {
continue;
}
$variable_name = 'metatag_enable_' . $entity_type;
// Configuration entities are skipped.
if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) {
continue;
}
// Entities must have bundles.
if (empty($entity_info['bundles'])) {
continue;
}
// Entities must be fieldable.
elseif (empty($entity_info['fieldable'])) {
continue;
}
// Ignore some view modes that are automatically added by certain modules.
unset($entity_info['view modes']['ical']);
unset($entity_info['view modes']['diff_standard']);
unset($entity_info['view modes']['token']);
// Entities without view modes are skipped.
if (empty($entity_info['view modes'])) {
continue;
}
// At this point, disable the entity by default.
$entity_enabled = FALSE;
// Anything that was specifically enabled via hook_entity_info() from older
// versions will be enabled if not configured already.
if (!empty($entity_info['metatag']) || !empty($entity_info['metatags'])) {
$entity_enabled = variable_get($variable_name, 'monkey');
if ($entity_enabled === 'monkey') {
$entity_enabled = TRUE;
}
}
variable_set($variable_name, $entity_enabled);
// Loop through the bundles, but only if the entity is enabled.
if ($entity_enabled) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
// If it wasn't specifically disabled before, enable it.
$bundle_enabled = variable_get($variable_name, 'monkey');
if ($bundle_name != FALSE) {
variable_set($variable_name, TRUE);
}
}
}
}
drupal_set_message(t('It may be worth verifying on the <a href="@url">Settings page</a> which types of content on the site should allow meta tags.', array('@url' => url('admin/config/search/metatags/settings'))));
}
/**
* Implements hook_uninstall().
*/
function metatag_uninstall() {
// This variable is created via hook_enable.
variable_del('metatag_schema_installed');
// Used to control whether 403/404 pages are cached.
variable_del('metatag_cache_error_pages');
// Used to make meta tags display on admin pages.
variable_del('metatag_tag_admin_pages');
// Temp variables, just in case they weren't removed already.
variable_del('metatag_skip_update_7017');
// Used to note that the schema for the main {metatag} table were sufficiently
// updated.
variable_del('metatag_has_revision_id');
// Used to force an entity's default language values to be used if nothing
// else matched.
variable_del('metatag_entity_no_lang_default');
// Controls which page region is used to trigger output of the meta tags.
variable_del('metatag_page_region');
// Optionally disable the default configurations.
variable_del('metatag_load_defaults');
// Optionally disables the output cache.
variable_del('metatag_cache_output');
// Customizable pager string.
variable_del('metatag_pager_string');
// Optionally enable translations of final output.
variable_del('metatag_i18n_translate_output');
// Optionally enable the automatic watchdog logging of i18n strings.
variable_del('metatag_i18n_enable_watchdog');
// Optionally disable the i18n integration.
variable_del('metatag_i18n_disabled');
// Optionally output core's meta tags.
variable_del('metatag_leave_core_tags');
// Optionally enable individual permissions for each meta tag.
variable_del('metatag_extended_permissions');
// Optionally load meta tags on admin pages.
variable_del('metatag_load_all_pages');
// Sanitize token replacement output.
variable_del('metatag_token_sanitize');
// Remove all possible 'enable' variables.
foreach (entity_get_info() as $entity_type => $entity_info) {
variable_del('metatag_enable_' . $entity_type);
if (!empty($entity_info['bundles'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
variable_del('metatag_enable_' . $entity_type . '__' . $bundle_name);
}
}
}
// Remove all of the maxlength variables.
variable_del('metatag_maxlength_title');
variable_del('metatag_maxlength_description');
variable_del('metatag_maxlength_abstract');
variable_del('metatag_maxlength_keywords');
variable_del('metatag_maxlength_news_keywords');
}
/**
* Implements hook_enable().
*/
function metatag_enable() {
variable_set('metatag_schema_installed', TRUE);
}
/**
* Replace one meta tag's with another in the entity records.
*
* @param array $sandbox
* A Batch API sandbox, passed by reference.
* @param string $old_tag
* The meta tag that is to be replaced.
* @param string $new_tag
* The meta tag that replaces the old one.
*/
function metatag_update_replace_entity_tag(&$sandbox, $old_tag, $new_tag) {
if (!isset($sandbox['progress'])) {
// Count of all {metatag} records that contained an entry for the old meta
// tag.
$records_count = db_select('metatag', 'm')
->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
->countQuery()
->execute()
->fetchField();
if (empty($records_count)) {
return t('No Metatag entity records needed to have the "@tag" meta tag renamed.', array('@tag' => $old_tag));
}
$sandbox['max'] = $records_count;
$sandbox['progress'] = 0;
}
// Count of rows that will be processed per iteration.
$limit = 100;
// Fetches a part of records.
$records = db_select('metatag', 'm')
->fields('m', array())
->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
->range(0, $limit)
->execute();
$count = 0;
$keys = array('entity_type', 'entity_id', 'revision_id', 'language');
// Loop over the values and correct them.
foreach ($records as $record) {
$record->data = unserialize($record->data);
if (isset($record->data[$old_tag])) {
$record->data[$new_tag] = $record->data[$old_tag];
unset($record->data[$old_tag]);
drupal_write_record('metatag', $record, $keys);
// Clear the cache for the entity this belongs to.
entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
}
$count++;
}
if (!empty($count)) {
$sandbox['progress'] += $count;
// In some cases the query yields results that cannot be fixed and we would
// run into an infinite loop. Stop immediately if we processed all records.
if ($sandbox['progress'] >= $sandbox['max']) {
$sandbox['#finished'] = TRUE;
}
else {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
}
else {
$sandbox['#finished'] = TRUE;
return t('Converted the "@old_tag" meta tag for @count entity records to "@new_tag" meta tag.', array('@old_tag' => $old_tag, '@new_tag' => $new_tag, '@count' => $sandbox['progress']));
}
}
/**
* Replace one meta tag's value in the entity records.
*
* @param array $sandbox
* A Batch API sandbox, passed by reference.
* @param string $meta_tag
* The meta tag that needs its value replaced.
* @param string $old_value
* The meta tag value that is to be replaced.
* @param string $new_value
* The meta tag value that replaces the old one.
*/
function metatag_update_replace_entity_value(&$sandbox, $meta_tag, $old_value, $new_value) {
// The condition used for both queries.
$db_and = db_and();
$db_and->condition('m.data', '%' . db_like('"' . $meta_tag . '"') . '%', 'LIKE');
$db_and->condition('m.data', '%' . db_like('"' . $old_value . '"') . '%', 'LIKE');
if (!isset($sandbox['progress'])) {
// Count of all {metatag} records that contained an entry for the old meta
// tag value.
$records_count = db_select('metatag', 'm')
->condition($db_and)
->countQuery()
->execute()
->fetchField();
if (empty($records_count)) {
return t('No Metatag entity records needed to have the "@tag" meta tag "@old_value" value replaced.', array('@tag' => $meta_tag, '@old_value' => $old_value));
}
$sandbox['max'] = $records_count;
$sandbox['progress'] = 0;
// Keep track of the number of replaced values separately.
$sandbox['count'] = 0;
}
// Count of rows that will be processed per iteration.
$limit = 100;
// Fetches a part of records.
$records = db_select('metatag', 'm')
->fields('m', array())
->condition($db_and)
->range(0, $limit)
->execute();
$count = 0;
$keys = array('entity_type', 'entity_id', 'revision_id', 'language');
// Loop over the values and correct them.
foreach ($records as $record) {
$record->data = unserialize($record->data);
if (isset($record->data[$meta_tag])) {
$record->data[$meta_tag]['value'] = str_replace($old_value, $new_value, $record->data[$meta_tag]['value']);
drupal_write_record('metatag', $record, $keys);
// Clear the cache for the entity this belongs to.
entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
$sandbox['count']++;
}
$sandbox['progress']++;
}
if (!empty($count)) {
// In some cases the query yields results that cannot be fixed and we would
// run into an infinite loop. Stop immediately if we processed all records.
if ($sandbox['progress'] >= $sandbox['max']) {
$sandbox['#finished'] = TRUE;
}
else {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
}
else {
$sandbox['#finished'] = TRUE;
return t('Replaced the value of @count entity records for the "@meta_tag" meta tag.', array('@meta_tag' => $meta_tag, '@count' => $sandbox['count']));
}
}
/**
* Replace one meta tag with another in the configs.
*
* @param string $old_tag
* The meta tag that is to be replaced.
* @param string $new_tag
* The meta tag that replaces the old one.
*/
function metatag_update_replace_config_tag($old_tag, $new_tag) {
// Find all {metatag_config} records that contained an entry for the old meta
// tag.
$records = db_select('metatag_config', 'm')
->fields('m', array('cid', 'config'))
->condition('m.config', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
->execute();
// This message will be returned if nothing needed to be updated.
$none_message = t('No Metatag configuration records needed to have the "@tag" meta tag fixed. That said, there may be other configurations elsewhere that do need updating.', array('@tag' => $old_tag));
// Loop over the values and correct them.
if ($records->rowCount() == 0) {
$message = $none_message;
}
else {
$keys = array('cid');
// Loop over the values and correct them.
$counter = 0;
foreach ($records as $record) {
$record->config = unserialize($record->config);
if (isset($record->config[$old_tag])) {
$record->config[$new_tag] = $record->config[$old_tag];
unset($record->config[$old_tag]);
drupal_write_record('metatag_config', $record, $keys);
$counter++;
}
}
if ($counter == 0) {
$message = $none_message;
}
else {
$message = t('Converted the "@old_tag" meta tag for @count configurations to the new "@new_tag" meta tag.', array('@old_tag' => $old_tag, '@new_tag' => $new_tag, '@count' => $counter));
}
}
// Clear all Metatag caches.
cache_clear_all('*', 'cache_metatag', TRUE);
drupal_static_reset('metatag_config_load_with_defaults');
drupal_static_reset('metatag_entity_supports_metatags');
drupal_static_reset('metatag_config_instance_info');
drupal_static_reset('metatag_get_info');
ctools_include('export');
ctools_export_load_object_reset('metatag_config');
return $message;
}
/**
* Replace one meta tag with another in the configs.
*
* @param string $meta_tag
* The meta tag that needs its value replaced.
* @param string $old_value
* The meta tag value that is to be replaced.
* @param string $new_value
* The meta tag value that replaces the old one.
*/
function metatag_update_replace_config_value($meta_tag, $old_value, $new_value) {
// Find all {metatag_config} records that contained an entry for the old meta
// tag.
$db_and = db_and();
$db_and->condition('m.config', '%' . db_like('"' . $meta_tag . '"') . '%', 'LIKE');
$db_and->condition('m.config', '%' . db_like('"' . $old_value . '"') . '%', 'LIKE');
$records = db_select('metatag_config', 'm')
->fields('m', array('cid', 'config'))
->condition($db_and)
->execute();
// This message will be returned if nothing needed to be updated.
$none_message = t('No Metatag configuration records needed to have the "@tag" meta tag values updated. That said, there may be other configurations elsewhere that do need updating.', array('@tag' => $meta_tag));
// Loop over the values and correct them.
if ($records->rowCount() == 0) {
$message = $none_message;
}
else {
$keys = array('cid');
// Loop over the values and correct them.
$counter = 0;
foreach ($records as $record) {
$record->config = unserialize($record->config);
if (isset($record->config[$meta_tag])) {
$record->config[$meta_tag]['value'] = str_replace($old_value, $new_value, $record->config[$meta_tag]['value']);
drupal_write_record('metatag_config', $record, $keys);
$counter++;
}
}
if ($counter == 0) {
$message = $none_message;
}
else {
$message = t('Replaced the value of @count entity records for the "@meta_tag" meta tag.', array('@meta_tag' => $meta_tag, '@count' => $counter));
}
}
// Clear all Metatag caches.
cache_clear_all('*', 'cache_metatag', TRUE);
drupal_static_reset('metatag_config_load_with_defaults');
drupal_static_reset('metatag_entity_supports_metatags');
drupal_static_reset('metatag_config_instance_info');
drupal_static_reset('metatag_get_info');
ctools_include('export');
ctools_export_load_object_reset('metatag_config');
return $message;
}
/**
* Remove a specific meta tag from all configs.
*
* @param string $$tag_name
* The meta tag that is to be removed.
*/
function metatag_update_delete_config($tag_name) {
// Find all {metatag_config} records that contained an entry for the meta tag.
$records = db_select('metatag_config', 'm')
->fields('m', array('cid', 'config'))
->condition('m.config', '%' . db_like('"' . $tag_name . '"') . '%', 'LIKE')
->execute();
// This message will be returned if nothing needed to be updated.
$none_message = t('No Metatag configuration records needed to have the "@tag" meta tag removed.', array('@tag' => $tag_name));
// Loop over the values and correct them.
if ($records->rowCount() == 0) {
drupal_set_message($none_message);
}
else {
// Loop over the values and correct them.
$counter = 0;
foreach ($records as $record) {
$record->config = unserialize($record->config);
if (isset($record->config[$tag_name])) {
unset($record->config[$tag_name]);
drupal_write_record('metatag_config', $record, array('cid'));
$counter++;
}
}
if ($counter == 0) {
drupal_set_message($none_message);
}
else {
drupal_set_message(t('Removed the "@tag" meta tag for @count configurations.', array('@tag' => $tag_name, '@count' => $counter)));
}
}
// Clear all Metatag caches.
cache_clear_all('*', 'cache_metatag', TRUE);
drupal_static_reset('metatag_config_load_with_defaults');
drupal_static_reset('metatag_entity_supports_metatags');
drupal_static_reset('metatag_config_instance_info');
drupal_static_reset('metatag_get_info');
ctools_include('export');
ctools_export_load_object_reset('metatag_config');
}
/**
* Disable the deprecated metatag_ui module, which has been merged into metatag.
*/
function metatag_update_7000() {
if (module_exists('metatag_ui')) {
module_disable(array('metatag_ui'), FALSE);
drupal_uninstall_modules(array('metatag_ui'), FALSE);
}
}
/**
* Fix the "{metatag_config}.cid column cannot be NULL" error.
*/
function metatag_update_7001() {
$table_name = 'metatag_config';
$field_name = 'cid';
$field_spec = array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The primary identifier for a metatag configuration set.',
);
$keys = array('primary key' => array($field_name));
// Before making any changes, drop the existing primary key.
// Let's add a temporary unique key for cid so MySQL will let it go.
// Hint taken from https://drupal.org/node/2064305#comment-7753197.
db_add_unique_key($table_name, 'temp_key', array($field_name, 'instance'));
// Unforunately there is no API way to check if a primary key exists, so if
// it doesn't exist the db_drop_primary_key() call will fail.
try {
db_drop_primary_key($table_name);
}
catch (Exception $e) {
drupal_set_message('Caught an exception: ', $e->getMessage());
}
// Rejig the field, and turn on the primary key again.
db_change_field($table_name, $field_name, $field_name, $field_spec, $keys);
// Finally, remove the temporary unique key because it's no longer useful.
db_drop_unique_key($table_name, 'temp_key');
}
/**
* Disable the deprecated metatag_ui module which has been merged into metatag,
* again.
*/
function metatag_update_7002() {
if (module_exists('metatag_ui')) {
module_disable(array('metatag_ui'), FALSE);
drupal_uninstall_modules(array('metatag_ui'), FALSE);
drupal_set_message(t('The deprecated Metatag UI module has been disabled.'));
}
}
/**
* Add the {metatag}.language field.
*/
function metatag_update_7003() {
// Set the target table and field name.
$table_name = 'metatag';
$field_name = 'language';
// Don't add the new field if it already exists.
if (!db_field_exists($table_name, $field_name)) {
// Describe the new field.
$field_spec = array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
'description' => 'The language of the tag',
);
// Add it and update the primary key.
db_add_field($table_name, $field_name, $field_spec);
db_drop_primary_key($table_name);
db_add_primary_key($table_name, array('entity_type', 'entity_id', 'language'));
}
}
/**
* Replaced by updates 7009, 7010, 7011, 7012 and 7013.
*/
function metatag_update_7004() {
// Do nothing.
}
/**
* Removing wrong metatag watchdog entries that break the admin/reports/dblog
* page.
*/
function metatag_update_7005() {
if (db_table_exists('watchdog')) {
db_delete('watchdog')
->condition('type', 'metatag')
->condition('variables', serialize('info'))
->execute();
}
}
/**
* Remove {metatag} records that were added by old versions of the module for
* entities that don't actually support Metatag. A more complete version of
* this will be added later on after it's (hopefully) guaranteed that all
* modules have updated to the correct API usage.
*/
function metatag_update_7006() {
$entity_types = array(
// Core.
'comment',
'menu_link',
'taxonomy_vocabulary',
// Some contrib entities.
'mailchimp_list',
'profile2',
'profile2_type',
'redirect',
'rules_config',
'wysiwyg_profile',
);
foreach ($entity_types as $entity_type) {
$num_deleted = db_delete('metatag')
->condition('entity_type', $entity_type)
->execute();
if ($num_deleted > 0) {
drupal_set_message(t('Removed @count meta tag record(s) for the @type entity type, it does not support meta tags.', array('@count' => $num_deleted, '@type' => $entity_type)));
}
}
}
/**
* Remove {metatag} records for nodes, users and taxonomy terms that have been
* deleted; older versions of Metatag may have failed to purge these.
*/
function metatag_update_7007() {
$nodes = db_query("SELECT m.entity_id
FROM {metatag} m
LEFT OUTER JOIN {node} n
ON m.entity_id=n.nid
WHERE m.entity_type='node'
AND n.nid IS NULL")
->fetchCol();
if (count($nodes) > 0) {
$deleted = db_delete('metatag')
->condition('entity_type', 'node')
->condition('entity_id', $nodes)
->execute();
if ($deleted > 0) {
drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array('@count' => $deleted)));
}
else {
drupal_set_message(t('There were no meta tag records to purge for removed nodes. This is a good thing :)'));
}
}
$users = db_query("SELECT m.entity_id
FROM {metatag} m
LEFT OUTER JOIN {users} u
ON m.entity_id=u.uid
WHERE m.entity_type='user'
AND u.uid IS NULL")