metatag.install 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the metatag module.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function metatag_requirements($phase) {
  10. $requirements = array();
  11. // Ensure translations don't break during installation.
  12. $t = get_t();
  13. if ($phase == 'install') {
  14. // Handle scenarios where the site had the legacy "metatags" module
  15. // installed but then had Metatag installed on top of it.
  16. if (function_exists('db_table_exists') && function_exists('db_field_exists') && function_exists('db_query') && Database::isActiveConnection()) {
  17. // Check if the primary table already exists.
  18. if (db_table_exists('metatag')) {
  19. // Check to see if all of the fields exist in the table. If one of the
  20. // fields does not exist, proceed with the fix.
  21. $fields = array(
  22. 'entity_type',
  23. 'entity_id',
  24. 'revision_id',
  25. 'language',
  26. 'data',
  27. );
  28. foreach ($fields as $field) {
  29. // This field doesn't exist, so determine what to do.
  30. if (!db_field_exists('metatag', $field)) {
  31. // The table contains data, so rename it.
  32. if (db_query("SELECT COUNT(*) FROM {metatag}")->fetchField() > 0) {
  33. db_query("RENAME TABLE {metatag} TO {metatag}_legacy");
  34. $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.';
  35. }
  36. // The table is empty, so delete it.
  37. else {
  38. db_query("DROP TABLE {metatag}");
  39. $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.';
  40. }
  41. $requirements['metatag'] = array(
  42. 'severity' => REQUIREMENT_WARNING,
  43. 'title' => 'Metatag',
  44. 'value' => $t('Legacy data discovered.'),
  45. 'description' => $t($message),
  46. );
  47. drupal_set_message($t($message), 'warning');
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. elseif ($phase == 'runtime') {
  55. // Complete data dump of all installed modules, used later.
  56. $module_data = system_rebuild_module_data();
  57. // Work out the release of D7 that is currently running.
  58. list($major, $minor) = explode('.', VERSION);
  59. // Strip off any suffixes on the version string, e.g. "17-dev".
  60. if (strpos('-', $minor)) {
  61. list($minor, $suffix) = explode('-', $minor);
  62. }
  63. // Releases of Drupal older than 7.40 support did not project:module syntax
  64. // for dependencies.
  65. if ($minor < 40) {
  66. $requirements['metatag'] = array(
  67. 'severity' => REQUIREMENT_WARNING,
  68. 'title' => 'Metatag',
  69. 'value' => $t('Upgrade Drupal core to v7.40 or newer'),
  70. '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."),
  71. );
  72. }
  73. // Add a note if Page Title is also installed.
  74. if (module_exists('page_title')) {
  75. $requirements['metatag_page_title'] = array(
  76. 'severity' => REQUIREMENT_WARNING,
  77. 'title' => 'Metatag',
  78. 'value' => $t('Page Title module should be removed'),
  79. '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')),
  80. );
  81. }
  82. // Add a note if the deprecated metatag.entity_translation.inc file still
  83. // exists.
  84. $filename = 'metatag.entity_translation.inc';
  85. if (file_exists(dirname(__FILE__) . '/' . $filename)) {
  86. $requirements['metatag_deprecated_et_file'] = array(
  87. 'severity' => REQUIREMENT_ERROR,
  88. 'title' => 'Metatag',
  89. 'value' => $t('Unwanted :filename file found', array(':filename' => $filename)),
  90. '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)),
  91. );
  92. }
  93. // Check that Entity_Translation is current.
  94. if (module_exists('entity_translation')
  95. && !empty($module_data['entity_translation'])
  96. && !empty($module_data['entity_translation']->schema_version)
  97. && $module_data['entity_translation']->schema_version < 7004) {
  98. $requirements['metatag_et_version'] = array(
  99. 'severity' => REQUIREMENT_ERROR,
  100. 'title' => 'Metatag',
  101. 'value' => $t('<a href="@url">Entity_Translation</a> is out of date and requires updating', array('@url' => 'https://www.drupal.org/project/entity_translation')),
  102. 'description' => $t('The Entity_Translation module is out of date and needs to be updated in order to be compatible with Metatag.'),
  103. );
  104. }
  105. // It's recommended to install the Transliteration module to clean up file
  106. // paths for use with image meta tags.
  107. if (!module_exists('transliteration')) {
  108. $requirements['metatag_transliteration'] = array(
  109. 'severity' => REQUIREMENT_INFO,
  110. 'title' => 'Metatag',
  111. 'value' => $t('The Transliteration module is recommended.'),
  112. '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')),
  113. );
  114. }
  115. // It's recommended to install the Imagecache Token module to make image
  116. // tokens easier to do.
  117. if (!module_exists('imagecache_token')) {
  118. $requirements['metatag_imagecache_token'] = array(
  119. 'severity' => REQUIREMENT_INFO,
  120. 'title' => 'Metatag',
  121. 'value' => $t('The Imagecache Token module is recommended.'),
  122. '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')),
  123. );
  124. }
  125. // The Admin Language module can cause problems.
  126. if (module_exists('admin_language') && variable_get('admin_language_force_neutral', 0)) {
  127. $requirements['metatag_admin_language'] = array(
  128. 'severity' => REQUIREMENT_WARNING,
  129. 'title' => 'Metatag',
  130. 'value' => $t('Conflict with Admin Language module.'),
  131. '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'))),
  132. );
  133. }
  134. // Token v7.x-1.6 is *highly* recommended.
  135. $token_module = $module_data['token'];
  136. // If the version string is not present then it means the module is running
  137. // from git, which means it can't be compared against. Alternatively, look
  138. // for the test file, which was the last commit of the 1.6 release.
  139. if (!empty($token_module->info['version'])
  140. || empty($token_module->info['files'])) {
  141. // If there's no test file then this is older than v1.6.
  142. if (empty($token_module->info['files'])) {
  143. $minor = 5;
  144. }
  145. // Otherwise check the version string.
  146. else {
  147. // Versions are in the format 7.x-1.y, so split the string up to find
  148. // the 'y' portion.
  149. $version = explode('-', $token_module->info['version']);
  150. if (isset($version[1])) {
  151. list($major, $minor) = explode('.', $version[1]);
  152. // Strip off any suffixes on the version string, e.g. "17-dev".
  153. if (strpos('-', $minor)) {
  154. list($minor, $suffix) = explode('-', $minor);
  155. }
  156. }
  157. // If the version string couldn't be extracted correctly, assume that
  158. // an incorrect version is installed.
  159. else {
  160. $minor = 0;
  161. }
  162. }
  163. // If v1.6 is not installed, give a warning.
  164. if ($minor < 6) {
  165. $requirements['metatag_token_version'] = array(
  166. 'severity' => REQUIREMENT_WARNING,
  167. 'title' => 'Metatag',
  168. 'value' => $t('Token module is out of date.'),
  169. '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.'),
  170. );
  171. }
  172. }
  173. // If Workbench Moderation is installed, show a message if it is out of
  174. // date.
  175. if (module_exists('workbench_moderation')) {
  176. $wm_module = $module_data['workbench_moderation'];
  177. // If the version string is not present then it means the module is
  178. // running from git, which means it can't be compared against.
  179. if (!empty($wm_module->info['version'])) {
  180. // Versions are in the format 7.x-1.y, so split the string up to find
  181. // the 'y' portion.
  182. $version = explode('-', $wm_module->info['version']);
  183. if (isset($version[1])) {
  184. list($major, $minor) = explode('.', $version[1]);
  185. }
  186. // If the version string couldn't be extracted correctly, assume that
  187. // an incorrect version is installed.
  188. else {
  189. $major = 0;
  190. }
  191. }
  192. // If v3.x is not installed, give a message.
  193. if ($major < 3) {
  194. $requirements['metatag_wm_version'] = array(
  195. 'severity' => REQUIREMENT_INFO,
  196. 'title' => 'Metatag',
  197. 'value' => $t('Workbench Moderation module is out of date.'),
  198. '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.'),
  199. );
  200. }
  201. }
  202. // Recommend the Schema.org Metatag module.
  203. if (!module_exists('schema_metatag')) {
  204. $requirements['metatag_schema'] = array(
  205. 'severity' => REQUIREMENT_INFO,
  206. 'title' => 'Metatag',
  207. 'value' => $t('Schema.org Metatag is recommended'),
  208. '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')),
  209. );
  210. }
  211. else {
  212. $requirements['metatag_schema'] = array(
  213. 'severity' => REQUIREMENT_OK,
  214. 'title' => 'Metatag',
  215. 'value' => $t('Schema.org Metatag is installed'),
  216. 'description' => $t('The <a href="@module">Schema.org Metatag</a> module is installed.', array('@module' => 'https://www.drupal.org/project/schema_metatag')),
  217. );
  218. }
  219. }
  220. return $requirements;
  221. }
  222. /**
  223. * Implements hook_schema().
  224. */
  225. function metatag_schema() {
  226. $schema['metatag_config'] = array(
  227. 'description' => 'Storage of meta tag configuration and defaults.',
  228. 'export' => array(
  229. 'key' => 'instance',
  230. 'key name' => 'Instance',
  231. 'primary key' => 'cid',
  232. 'identifier' => 'config',
  233. 'default hook' => 'metatag_config_default',
  234. 'api' => array(
  235. 'owner' => 'metatag',
  236. 'api' => 'metatag',
  237. 'minimum_version' => 1,
  238. 'current_version' => 1,
  239. ),
  240. 'cache defaults' => TRUE,
  241. 'default cache bin' => 'cache_metatag',
  242. ),
  243. 'fields' => array(
  244. 'cid' => array(
  245. 'type' => 'serial',
  246. 'unsigned' => TRUE,
  247. 'not null' => TRUE,
  248. 'description' => 'The primary identifier for a metatag configuration set.',
  249. 'no export' => TRUE,
  250. ),
  251. 'instance' => array(
  252. 'type' => 'varchar',
  253. 'length' => 255,
  254. 'not null' => TRUE,
  255. 'default' => '',
  256. 'description' => 'The machine-name of the configuration, typically entity-type:bundle.',
  257. ),
  258. 'config' => array(
  259. 'type' => 'blob',
  260. 'size' => 'big',
  261. 'not null' => TRUE,
  262. 'serialize' => TRUE,
  263. 'description' => 'Serialized data containing the meta tag configuration.',
  264. 'translatable' => TRUE,
  265. ),
  266. ),
  267. 'primary key' => array('cid'),
  268. 'unique keys' => array(
  269. 'instance' => array('instance'),
  270. ),
  271. );
  272. $schema['metatag'] = array(
  273. 'fields' => array(
  274. 'entity_type' => array(
  275. 'type' => 'varchar',
  276. 'length' => 32,
  277. 'not null' => TRUE,
  278. 'default' => '',
  279. 'description' => 'The entity type this data is attached to.',
  280. ),
  281. 'entity_id' => array(
  282. 'type' => 'int',
  283. 'unsigned' => TRUE,
  284. 'not null' => TRUE,
  285. 'default' => 0,
  286. 'description' => 'The entity id this data is attached to.',
  287. ),
  288. 'revision_id' => array(
  289. 'type' => 'int',
  290. 'unsigned' => TRUE,
  291. 'not null' => TRUE,
  292. 'default' => 0,
  293. 'description' => 'The revision_id for the entity object this data is attached to.',
  294. ),
  295. 'language' => array(
  296. 'type' => 'varchar',
  297. 'length' => 32,
  298. 'not null' => TRUE,
  299. 'default' => '',
  300. 'description' => 'The language of the tag.',
  301. ),
  302. 'data' => array(
  303. 'type' => 'blob',
  304. 'size' => 'big',
  305. 'not null' => TRUE,
  306. 'serialize' => TRUE,
  307. ),
  308. ),
  309. 'indexes' => array(
  310. 'type_revision' => array(
  311. 'entity_type',
  312. 'revision_id',
  313. ),
  314. ),
  315. 'primary key' => array(
  316. 'entity_type',
  317. 'entity_id',
  318. 'revision_id',
  319. 'language',
  320. ),
  321. );
  322. $schema['cache_metatag'] = drupal_get_schema_unprocessed('system', 'cache');
  323. $schema['cache_metatag']['description'] = 'Cache table for the generated meta tag output.';
  324. return $schema;
  325. }
  326. /**
  327. * Implements hook_install().
  328. */
  329. function metatag_install() {
  330. 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'))));
  331. // Always enable the node, taxonomy term and user entities.
  332. foreach (array('node', 'taxonomy_term', 'user') as $entity_type) {
  333. // Enable the main entity type.
  334. $variable_name = 'metatag_enable_' . $entity_type;
  335. variable_set($variable_name, TRUE);
  336. // Update each entity bundle too.
  337. $entity_info = entity_get_info($entity_type);
  338. if (!empty($entity_info['bundles'])) {
  339. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  340. $variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
  341. variable_set($variable_name, TRUE);
  342. }
  343. }
  344. }
  345. // Possibly enable other entities, if they're specifically requested.
  346. foreach (entity_get_info() as $entity_type => $entity_info) {
  347. // Skip the three entity types that were already enabled.
  348. if (in_array($entity_type, array('node', 'taxonomy_term', 'user'))) {
  349. continue;
  350. }
  351. $variable_name = 'metatag_enable_' . $entity_type;
  352. // Configuration entities are skipped.
  353. if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) {
  354. continue;
  355. }
  356. // Entities must have bundles.
  357. if (empty($entity_info['bundles'])) {
  358. continue;
  359. }
  360. // Entities must be fieldable.
  361. elseif (empty($entity_info['fieldable'])) {
  362. continue;
  363. }
  364. // Ignore some view modes that are automatically added by certain modules.
  365. unset($entity_info['view modes']['ical']);
  366. unset($entity_info['view modes']['diff_standard']);
  367. unset($entity_info['view modes']['token']);
  368. // Entities without view modes are skipped.
  369. if (empty($entity_info['view modes'])) {
  370. continue;
  371. }
  372. // At this point, disable the entity by default.
  373. $entity_enabled = FALSE;
  374. // Anything that was specifically enabled via hook_entity_info() from older
  375. // versions will be enabled if not configured already.
  376. if (!empty($entity_info['metatag']) || !empty($entity_info['metatags'])) {
  377. $entity_enabled = variable_get($variable_name, 'monkey');
  378. if ($entity_enabled === 'monkey') {
  379. $entity_enabled = TRUE;
  380. }
  381. }
  382. variable_set($variable_name, $entity_enabled);
  383. // Loop through the bundles, but only if the entity is enabled.
  384. if ($entity_enabled) {
  385. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  386. $variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
  387. // If it wasn't specifically disabled before, enable it.
  388. $bundle_enabled = variable_get($variable_name, 'monkey');
  389. if ($bundle_name != FALSE) {
  390. variable_set($variable_name, TRUE);
  391. }
  392. }
  393. }
  394. }
  395. 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'))));
  396. }
  397. /**
  398. * Implements hook_uninstall().
  399. */
  400. function metatag_uninstall() {
  401. // This variable is created via hook_enable.
  402. variable_del('metatag_schema_installed');
  403. // Used to control whether 403/404 pages are cached.
  404. variable_del('metatag_cache_error_pages');
  405. // Used to make meta tags display on admin pages.
  406. variable_del('metatag_tag_admin_pages');
  407. // Temp variables, just in case they weren't removed already.
  408. variable_del('metatag_skip_update_7017');
  409. // Used to note that the schema for the main {metatag} table were sufficiently
  410. // updated.
  411. variable_del('metatag_has_revision_id');
  412. // Used to force an entity's default language values to be used if nothing
  413. // else matched.
  414. variable_del('metatag_entity_no_lang_default');
  415. // Controls which page region is used to trigger output of the meta tags.
  416. variable_del('metatag_page_region');
  417. // Optionally disable the default configurations.
  418. variable_del('metatag_load_defaults');
  419. // Optionally disables the output cache.
  420. variable_del('metatag_cache_output');
  421. // Customizable pager string.
  422. variable_del('metatag_pager_string');
  423. // Optionally enable translations of final output.
  424. variable_del('metatag_i18n_translate_output');
  425. // Optionally enable the automatic watchdog logging of i18n strings.
  426. variable_del('metatag_i18n_enable_watchdog');
  427. // Optionally disable the i18n integration.
  428. variable_del('metatag_i18n_disabled');
  429. // Optionally output core's meta tags.
  430. variable_del('metatag_leave_core_tags');
  431. // Optionally enable individual permissions for each meta tag.
  432. variable_del('metatag_extended_permissions');
  433. // Optionally load meta tags on admin pages.
  434. variable_del('metatag_load_all_pages');
  435. // Sanitize token replacement output.
  436. variable_del('metatag_token_sanitize');
  437. // Remove all possible 'enable' variables.
  438. foreach (entity_get_info() as $entity_type => $entity_info) {
  439. variable_del('metatag_enable_' . $entity_type);
  440. if (!empty($entity_info['bundles'])) {
  441. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  442. variable_del('metatag_enable_' . $entity_type . '__' . $bundle_name);
  443. }
  444. }
  445. }
  446. // Remove all of the maxlength variables.
  447. variable_del('metatag_maxlength_title');
  448. variable_del('metatag_maxlength_description');
  449. variable_del('metatag_maxlength_abstract');
  450. variable_del('metatag_maxlength_keywords');
  451. variable_del('metatag_maxlength_news_keywords');
  452. }
  453. /**
  454. * Implements hook_enable().
  455. */
  456. function metatag_enable() {
  457. variable_set('metatag_schema_installed', TRUE);
  458. }
  459. /**
  460. * Replace one meta tag's with another in the entity records.
  461. *
  462. * @param array $sandbox
  463. * A Batch API sandbox, passed by reference.
  464. * @param string $old_tag
  465. * The meta tag that is to be replaced.
  466. * @param string $new_tag
  467. * The meta tag that replaces the old one.
  468. */
  469. function metatag_update_replace_entity_tag(&$sandbox, $old_tag, $new_tag) {
  470. if (!isset($sandbox['progress'])) {
  471. // Count of all {metatag} records that contained an entry for the old meta
  472. // tag.
  473. $records_count = db_select('metatag', 'm')
  474. ->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
  475. ->countQuery()
  476. ->execute()
  477. ->fetchField();
  478. if (empty($records_count)) {
  479. return t('No Metatag entity records needed to have the "@tag" meta tag renamed.', array('@tag' => $old_tag));
  480. }
  481. $sandbox['max'] = $records_count;
  482. $sandbox['progress'] = 0;
  483. }
  484. // Count of rows that will be processed per iteration.
  485. $limit = 100;
  486. // Fetches a part of records.
  487. $records = db_select('metatag', 'm')
  488. ->fields('m', array())
  489. ->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
  490. ->range(0, $limit)
  491. ->execute();
  492. $count = 0;
  493. $keys = array('entity_type', 'entity_id', 'revision_id', 'language');
  494. // Loop over the values and correct them.
  495. foreach ($records as $record) {
  496. $record->data = unserialize($record->data);
  497. if (isset($record->data[$old_tag])) {
  498. $record->data[$new_tag] = $record->data[$old_tag];
  499. unset($record->data[$old_tag]);
  500. drupal_write_record('metatag', $record, $keys);
  501. // Clear the cache for the entity this belongs to.
  502. entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
  503. }
  504. $count++;
  505. }
  506. if (!empty($count)) {
  507. $sandbox['progress'] += $count;
  508. // In some cases the query yields results that cannot be fixed and we would
  509. // run into an infinite loop. Stop immediately if we processed all records.
  510. if ($sandbox['progress'] >= $sandbox['max']) {
  511. $sandbox['#finished'] = TRUE;
  512. }
  513. else {
  514. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  515. }
  516. }
  517. else {
  518. $sandbox['#finished'] = TRUE;
  519. 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']));
  520. }
  521. }
  522. /**
  523. * Replace one meta tag's value in the entity records.
  524. *
  525. * @param array $sandbox
  526. * A Batch API sandbox, passed by reference.
  527. * @param string $meta_tag
  528. * The meta tag that needs its value replaced.
  529. * @param string $old_value
  530. * The meta tag value that is to be replaced.
  531. * @param string $new_value
  532. * The meta tag value that replaces the old one.
  533. */
  534. function metatag_update_replace_entity_value(&$sandbox, $meta_tag, $old_value, $new_value) {
  535. // The condition used for both queries.
  536. $db_and = db_and();
  537. $db_and->condition('m.data', '%' . db_like('"' . $meta_tag . '"') . '%', 'LIKE');
  538. $db_and->condition('m.data', '%' . db_like('"' . $old_value . '"') . '%', 'LIKE');
  539. if (!isset($sandbox['progress'])) {
  540. // Count of all {metatag} records that contained an entry for the old meta
  541. // tag value.
  542. $records_count = db_select('metatag', 'm')
  543. ->condition($db_and)
  544. ->countQuery()
  545. ->execute()
  546. ->fetchField();
  547. if (empty($records_count)) {
  548. 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));
  549. }
  550. $sandbox['max'] = $records_count;
  551. $sandbox['progress'] = 0;
  552. // Keep track of the number of replaced values separately.
  553. $sandbox['count'] = 0;
  554. }
  555. // Count of rows that will be processed per iteration.
  556. $limit = 100;
  557. // Fetches a part of records.
  558. $records = db_select('metatag', 'm')
  559. ->fields('m', array())
  560. ->condition($db_and)
  561. ->range(0, $limit)
  562. ->execute();
  563. $count = 0;
  564. $keys = array('entity_type', 'entity_id', 'revision_id', 'language');
  565. // Loop over the values and correct them.
  566. foreach ($records as $record) {
  567. $record->data = unserialize($record->data);
  568. if (isset($record->data[$meta_tag])) {
  569. $record->data[$meta_tag]['value'] = str_replace($old_value, $new_value, $record->data[$meta_tag]['value']);
  570. drupal_write_record('metatag', $record, $keys);
  571. // Clear the cache for the entity this belongs to.
  572. entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
  573. $sandbox['count']++;
  574. }
  575. $sandbox['progress']++;
  576. }
  577. if (!empty($count)) {
  578. // In some cases the query yields results that cannot be fixed and we would
  579. // run into an infinite loop. Stop immediately if we processed all records.
  580. if ($sandbox['progress'] >= $sandbox['max']) {
  581. $sandbox['#finished'] = TRUE;
  582. }
  583. else {
  584. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  585. }
  586. }
  587. else {
  588. $sandbox['#finished'] = TRUE;
  589. return t('Replaced the value of @count entity records for the "@meta_tag" meta tag.', array('@meta_tag' => $meta_tag, '@count' => $sandbox['count']));
  590. }
  591. }
  592. /**
  593. * Replace one meta tag with another in the configs.
  594. *
  595. * @param string $old_tag
  596. * The meta tag that is to be replaced.
  597. * @param string $new_tag
  598. * The meta tag that replaces the old one.
  599. */
  600. function metatag_update_replace_config_tag($old_tag, $new_tag) {
  601. // Find all {metatag_config} records that contained an entry for the old meta
  602. // tag.
  603. $records = db_select('metatag_config', 'm')
  604. ->fields('m', array('cid', 'config'))
  605. ->condition('m.config', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
  606. ->execute();
  607. // This message will be returned if nothing needed to be updated.
  608. $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));
  609. // Loop over the values and correct them.
  610. if ($records->rowCount() == 0) {
  611. $message = $none_message;
  612. }
  613. else {
  614. $keys = array('cid');
  615. // Loop over the values and correct them.
  616. $counter = 0;
  617. foreach ($records as $record) {
  618. $record->config = unserialize($record->config);
  619. if (isset($record->config[$old_tag])) {
  620. $record->config[$new_tag] = $record->config[$old_tag];
  621. unset($record->config[$old_tag]);
  622. drupal_write_record('metatag_config', $record, $keys);
  623. $counter++;
  624. }
  625. }
  626. if ($counter == 0) {
  627. $message = $none_message;
  628. }
  629. else {
  630. $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));
  631. }
  632. }
  633. // Clear all Metatag caches.
  634. cache_clear_all('*', 'cache_metatag', TRUE);
  635. drupal_static_reset('metatag_config_load_with_defaults');
  636. drupal_static_reset('metatag_entity_supports_metatags');
  637. drupal_static_reset('metatag_config_instance_info');
  638. drupal_static_reset('metatag_get_info');
  639. ctools_include('export');
  640. ctools_export_load_object_reset('metatag_config');
  641. return $message;
  642. }
  643. /**
  644. * Replace one meta tag with another in the configs.
  645. *
  646. * @param string $meta_tag
  647. * The meta tag that needs its value replaced.
  648. * @param string $old_value
  649. * The meta tag value that is to be replaced.
  650. * @param string $new_value
  651. * The meta tag value that replaces the old one.
  652. */
  653. function metatag_update_replace_config_value($meta_tag, $old_value, $new_value) {
  654. // Find all {metatag_config} records that contained an entry for the old meta
  655. // tag.
  656. $db_and = db_and();
  657. $db_and->condition('m.config', '%' . db_like('"' . $meta_tag . '"') . '%', 'LIKE');
  658. $db_and->condition('m.config', '%' . db_like('"' . $old_value . '"') . '%', 'LIKE');
  659. $records = db_select('metatag_config', 'm')
  660. ->fields('m', array('cid', 'config'))
  661. ->condition($db_and)
  662. ->execute();
  663. // This message will be returned if nothing needed to be updated.
  664. $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));
  665. // Loop over the values and correct them.
  666. if ($records->rowCount() == 0) {
  667. $message = $none_message;
  668. }
  669. else {
  670. $keys = array('cid');
  671. // Loop over the values and correct them.
  672. $counter = 0;
  673. foreach ($records as $record) {
  674. $record->config = unserialize($record->config);
  675. if (isset($record->config[$meta_tag])) {
  676. $record->config[$meta_tag]['value'] = str_replace($old_value, $new_value, $record->config[$meta_tag]['value']);
  677. drupal_write_record('metatag_config', $record, $keys);
  678. $counter++;
  679. }
  680. }
  681. if ($counter == 0) {
  682. $message = $none_message;
  683. }
  684. else {
  685. $message = t('Replaced the value of @count entity records for the "@meta_tag" meta tag.', array('@meta_tag' => $meta_tag, '@count' => $counter));
  686. }
  687. }
  688. // Clear all Metatag caches.
  689. cache_clear_all('*', 'cache_metatag', TRUE);
  690. drupal_static_reset('metatag_config_load_with_defaults');
  691. drupal_static_reset('metatag_entity_supports_metatags');
  692. drupal_static_reset('metatag_config_instance_info');
  693. drupal_static_reset('metatag_get_info');
  694. ctools_include('export');
  695. ctools_export_load_object_reset('metatag_config');
  696. return $message;
  697. }
  698. /**
  699. * Remove a specific meta tag from all configs.
  700. *
  701. * @param string $$tag_name
  702. * The meta tag that is to be removed.
  703. */
  704. function metatag_update_delete_config($tag_name) {
  705. // Find all {metatag_config} records that contained an entry for the meta tag.
  706. $records = db_select('metatag_config', 'm')
  707. ->fields('m', array('cid', 'config'))
  708. ->condition('m.config', '%' . db_like('"' . $tag_name . '"') . '%', 'LIKE')
  709. ->execute();
  710. // This message will be returned if nothing needed to be updated.
  711. $none_message = t('No Metatag configuration records needed to have the "@tag" meta tag removed.', array('@tag' => $tag_name));
  712. // Loop over the values and correct them.
  713. if ($records->rowCount() == 0) {
  714. drupal_set_message($none_message);
  715. }
  716. else {
  717. // Loop over the values and correct them.
  718. $counter = 0;
  719. foreach ($records as $record) {
  720. $record->config = unserialize($record->config);
  721. if (isset($record->config[$tag_name])) {
  722. unset($record->config[$tag_name]);
  723. drupal_write_record('metatag_config', $record, array('cid'));
  724. $counter++;
  725. }
  726. }
  727. if ($counter == 0) {
  728. drupal_set_message($none_message);
  729. }
  730. else {
  731. drupal_set_message(t('Removed the "@tag" meta tag for @count configurations.', array('@tag' => $tag_name, '@count' => $counter)));
  732. }
  733. }
  734. // Clear all Metatag caches.
  735. cache_clear_all('*', 'cache_metatag', TRUE);
  736. drupal_static_reset('metatag_config_load_with_defaults');
  737. drupal_static_reset('metatag_entity_supports_metatags');
  738. drupal_static_reset('metatag_config_instance_info');
  739. drupal_static_reset('metatag_get_info');
  740. ctools_include('export');
  741. ctools_export_load_object_reset('metatag_config');
  742. }
  743. /**
  744. * Disable the deprecated metatag_ui module, which has been merged into metatag.
  745. */
  746. function metatag_update_7000() {
  747. if (module_exists('metatag_ui')) {
  748. module_disable(array('metatag_ui'), FALSE);
  749. drupal_uninstall_modules(array('metatag_ui'), FALSE);
  750. }
  751. }
  752. /**
  753. * Fix the "{metatag_config}.cid column cannot be NULL" error.
  754. */
  755. function metatag_update_7001() {
  756. $table_name = 'metatag_config';
  757. $field_name = 'cid';
  758. $field_spec = array(
  759. 'type' => 'serial',
  760. 'unsigned' => TRUE,
  761. 'not null' => TRUE,
  762. 'description' => 'The primary identifier for a metatag configuration set.',
  763. );
  764. $keys = array('primary key' => array($field_name));
  765. // Before making any changes, drop the existing primary key.
  766. // Let's add a temporary unique key for cid so MySQL will let it go.
  767. // Hint taken from https://drupal.org/node/2064305#comment-7753197.
  768. db_add_unique_key($table_name, 'temp_key', array($field_name, 'instance'));
  769. // Unforunately there is no API way to check if a primary key exists, so if
  770. // it doesn't exist the db_drop_primary_key() call will fail.
  771. try {
  772. db_drop_primary_key($table_name);
  773. }
  774. catch (Exception $e) {
  775. drupal_set_message('Caught an exception: ', $e->getMessage());
  776. }
  777. // Rejig the field, and turn on the primary key again.
  778. db_change_field($table_name, $field_name, $field_name, $field_spec, $keys);
  779. // Finally, remove the temporary unique key because it's no longer useful.
  780. db_drop_unique_key($table_name, 'temp_key');
  781. }
  782. /**
  783. * Disable the deprecated metatag_ui module which has been merged into metatag,
  784. * again.
  785. */
  786. function metatag_update_7002() {
  787. if (module_exists('metatag_ui')) {
  788. module_disable(array('metatag_ui'), FALSE);
  789. drupal_uninstall_modules(array('metatag_ui'), FALSE);
  790. drupal_set_message(t('The deprecated Metatag UI module has been disabled.'));
  791. }
  792. }
  793. /**
  794. * Add the {metatag}.language field.
  795. */
  796. function metatag_update_7003() {
  797. // Set the target table and field name.
  798. $table_name = 'metatag';
  799. $field_name = 'language';
  800. // Don't add the new field if it already exists.
  801. if (!db_field_exists($table_name, $field_name)) {
  802. // Describe the new field.
  803. $field_spec = array(
  804. 'type' => 'varchar',
  805. 'length' => 32,
  806. 'not null' => TRUE,
  807. 'default' => '',
  808. 'description' => 'The language of the tag',
  809. );
  810. // Add it and update the primary key.
  811. db_add_field($table_name, $field_name, $field_spec);
  812. db_drop_primary_key($table_name);
  813. db_add_primary_key($table_name, array('entity_type', 'entity_id', 'language'));
  814. }
  815. }
  816. /**
  817. * Replaced by updates 7009, 7010, 7011, 7012 and 7013.
  818. */
  819. function metatag_update_7004() {
  820. // Do nothing.
  821. }
  822. /**
  823. * Removing wrong metatag watchdog entries that break the admin/reports/dblog
  824. * page.
  825. */
  826. function metatag_update_7005() {
  827. if (db_table_exists('watchdog')) {
  828. db_delete('watchdog')
  829. ->condition('type', 'metatag')
  830. ->condition('variables', serialize('info'))
  831. ->execute();
  832. }
  833. }
  834. /**
  835. * Remove {metatag} records that were added by old versions of the module for
  836. * entities that don't actually support Metatag. A more complete version of
  837. * this will be added later on after it's (hopefully) guaranteed that all
  838. * modules have updated to the correct API usage.
  839. */
  840. function metatag_update_7006() {
  841. $entity_types = array(
  842. // Core.
  843. 'comment',
  844. 'menu_link',
  845. 'taxonomy_vocabulary',
  846. // Some contrib entities.
  847. 'mailchimp_list',
  848. 'profile2',
  849. 'profile2_type',
  850. 'redirect',
  851. 'rules_config',
  852. 'wysiwyg_profile',
  853. );
  854. foreach ($entity_types as $entity_type) {
  855. $num_deleted = db_delete('metatag')
  856. ->condition('entity_type', $entity_type)
  857. ->execute();
  858. if ($num_deleted > 0) {
  859. 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)));
  860. }
  861. }
  862. }
  863. /**
  864. * Remove {metatag} records for nodes, users and taxonomy terms that have been
  865. * deleted; older versions of Metatag may have failed to purge these.
  866. */
  867. function metatag_update_7007() {
  868. $nodes = db_query("SELECT m.entity_id
  869. FROM {metatag} m
  870. LEFT OUTER JOIN {node} n
  871. ON m.entity_id=n.nid
  872. WHERE m.entity_type='node'
  873. AND n.nid IS NULL")
  874. ->fetchCol();
  875. if (count($nodes) > 0) {
  876. $deleted = db_delete('metatag')
  877. ->condition('entity_type', 'node')
  878. ->condition('entity_id', $nodes)
  879. ->execute();
  880. if ($deleted > 0) {
  881. drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array('@count' => $deleted)));
  882. }
  883. else {
  884. drupal_set_message(t('There were no meta tag records to purge for removed nodes. This is a good thing :)'));
  885. }
  886. }
  887. $users = db_query("SELECT m.entity_id
  888. FROM {metatag} m
  889. LEFT OUTER JOIN {users} u
  890. ON m.entity_id=u.uid
  891. WHERE m.entity_type='user'
  892. AND u.uid IS NULL")
  893. ->fetchCol();
  894. if (count($users) > 0) {
  895. $deleted = db_delete('metatag')
  896. ->condition('entity_type', 'user')
  897. ->condition('entity_id', $users)
  898. ->execute();
  899. if ($deleted > 0) {
  900. drupal_set_message(t('Removed @count meta tag record(s) for users that had been purged.', array('@count' => $deleted)));
  901. }
  902. else {
  903. drupal_set_message(t('There were no meta tag records to purge for removed users. This is a good thing :)'));
  904. }
  905. }
  906. // Only run this if the Taxonomy module is enabled.
  907. if (module_exists('taxonomy')) {
  908. $terms = db_query("SELECT m.entity_id
  909. FROM {metatag} m
  910. LEFT OUTER JOIN {taxonomy_term_data} t
  911. ON m.entity_id=t.tid
  912. WHERE m.entity_type='taxonomy_term'
  913. AND t.tid IS NULL")
  914. ->fetchCol();
  915. if (count($terms) > 0) {
  916. $deleted = db_delete('metatag')
  917. ->condition('entity_type', 'taxonomy_term')
  918. ->condition('entity_id', $terms)
  919. ->execute();
  920. if ($deleted > 0) {
  921. drupal_set_message(t('Removed @count meta tag record(s) for taxonomy terms that had been purged.', array('@count' => $deleted)));
  922. }
  923. else {
  924. drupal_set_message(t('There were no meta tag records to purge for removed taxonomy terms. This is a good thing :)'));
  925. }
  926. }
  927. }
  928. }
  929. /**
  930. * Remove any empty records that may be hanging around from old releases.
  931. */
  932. function metatag_update_7008() {
  933. $conditions = db_or()
  934. ->isNull('data')
  935. ->condition('data', '')
  936. ->condition('data', serialize(array()));
  937. $deleted = db_delete("metatag")
  938. ->condition($conditions)
  939. ->execute();
  940. if ($deleted > 0) {
  941. drupal_set_message(t('Purged @count empty meta tag record(s).', array('@count' => $deleted)));
  942. }
  943. }
  944. /**
  945. * Fix {metatag} records for taxonomy terms.
  946. */
  947. function metatag_update_7009() {
  948. if (module_exists('taxonomy')) {
  949. // Fix the {metatag} table first.
  950. metatag_update_7015();
  951. // Remove duplicates.
  952. _metatag_remove_dupes('taxonomy_term');
  953. }
  954. // The taxonomy term entity doesn't support a 'language' option, so reset it
  955. // to LANGUAGE_NONE.
  956. $result = db_query("UPDATE {metatag} SET language = :language WHERE entity_type='taxonomy_term'", array(':language' => LANGUAGE_NONE));
  957. if ($result->rowCount() > 0) {
  958. drupal_set_message(t('Fixed language values for @count taxonomy terms.', array('@count' => $result->rowCount())));
  959. }
  960. }
  961. /**
  962. * Fix {metatag} records for users.
  963. */
  964. function metatag_update_7010() {
  965. // Fix the {metatag} table first.
  966. metatag_update_7015();
  967. // Remove duplicates.
  968. _metatag_remove_dupes('user');
  969. // Update User values.
  970. $result = db_query("UPDATE {metatag} SET language = :language WHERE entity_type='user'", array(':language' => LANGUAGE_NONE));
  971. if ($result->rowCount() > 0) {
  972. drupal_set_message(t('Fixed language values for @count user records.', array('@count' => $result->rowCount())));
  973. }
  974. }
  975. /**
  976. * Fix {metatag} records for nodes.
  977. */
  978. function metatag_update_7011(&$sandbox) {
  979. // Fix the {metatag} table first.
  980. metatag_update_7015();
  981. // Only proceed if Entity_Translation is not enabled as it allows each node
  982. // record to have multiple languages available.
  983. if (module_exists('entity_translation')) {
  984. drupal_set_message(t("Entity Translation is enabled, so node meta tags will not be updated, to avoid accidental dataloss."));
  985. return;
  986. }
  987. // Process records by groups of 10 (arbitrary value).
  988. // When a group is processed, the batch update engine determines whether it
  989. // should continue processing in the same request or provide progress
  990. // feedback to the user and wait for the next request.
  991. $limit = 10;
  992. // When ran through Drush it's Ok to process a larger number of objects at a
  993. // time.
  994. if (drupal_is_cli()) {
  995. $limit = 100;
  996. }
  997. // Use the sandbox at your convenience to store the information needed
  998. // to track progression between successive calls to the function.
  999. if (!isset($sandbox['progress'])) {
  1000. // The count of records visited so far.
  1001. $sandbox['progress'] = 0;
  1002. // Remove duplicates.
  1003. _metatag_remove_dupes('node');
  1004. // Update Node values.
  1005. $nodes = db_query("SELECT n.nid, n.language FROM {node} n INNER JOIN {metatag} m ON n.nid = m.entity_id WHERE m.entity_type = 'node' AND n.language != m.language ORDER BY nid");
  1006. $sandbox['records'] = array();
  1007. foreach ($nodes as $record) {
  1008. $sandbox['records'][] = $record;
  1009. }
  1010. // If there's no data, don't bother with the extra work.
  1011. if (empty($sandbox['records'])) {
  1012. watchdog('metatag', 'Update 7011: No nodes need the Metatag language values fixed.', array(), WATCHDOG_INFO);
  1013. if (drupal_is_cli()) {
  1014. drupal_set_message(t('Update 7011: No nodes need the Metatag language values fixed.'));
  1015. }
  1016. return t('No nodes need the Metatag language values fixed.');
  1017. }
  1018. // Total records that must be visited.
  1019. $sandbox['max'] = count($sandbox['records']);
  1020. // A place to store messages during the run.
  1021. $sandbox['messages'] = array();
  1022. // An initial record of the number of records to be updated.
  1023. watchdog('metatag', 'Update 7011: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  1024. if (drupal_is_cli()) {
  1025. drupal_set_message(t('Update 7011: !count records to update.', array('!count' => $sandbox['max'])));
  1026. }
  1027. // Last record processed.
  1028. $sandbox['current_record'] = -1;
  1029. // Because a lot of other processing happens on the first iteration, just do
  1030. // one.
  1031. $limit = 1;
  1032. }
  1033. // Set default values.
  1034. for ($ctr = 0; $ctr < $limit; $ctr++) {
  1035. $sandbox['current_record']++;
  1036. if (empty($sandbox['records'][$sandbox['current_record']])) {
  1037. break;
  1038. }
  1039. // Shortcuts for later.
  1040. $langcode = $sandbox['records'][$sandbox['current_record']]->language;
  1041. $nid = $sandbox['records'][$sandbox['current_record']]->nid;
  1042. db_update('metatag')
  1043. ->fields(array('language' => $langcode))
  1044. ->condition('entity_type', 'node')
  1045. ->condition('entity_id', $nid)
  1046. ->execute();
  1047. // Update our progress information.
  1048. $sandbox['progress']++;
  1049. }
  1050. // Set the "finished" status, to tell batch engine whether this function
  1051. // needs to run again. If you set a float, this will indicate the progress of
  1052. // the batch so the progress bar will update.
  1053. if ($sandbox['progress'] >= $sandbox['max']) {
  1054. $sandbox['#finished'] = TRUE;
  1055. }
  1056. else {
  1057. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  1058. }
  1059. if ($sandbox['#finished']) {
  1060. // Clear all caches so the fixed data will be reloaded.
  1061. cache_clear_all('*', 'cache_metatag', TRUE);
  1062. // A final log of the number of records that were converted.
  1063. watchdog('metatag', 'Update 7011: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  1064. if (drupal_is_cli()) {
  1065. drupal_set_message(t('Update 7011: !count records were updated.', array('!count' => $sandbox['progress'])));
  1066. }
  1067. // hook_update_N() may optionally return a string which will be displayed
  1068. // to the user.
  1069. return t('Fixed the Metatag language values for @count nodes.', array('@count' => $sandbox['progress']));
  1070. }
  1071. }
  1072. /**
  1073. * Remove duplicate {metatag} records for non-core entities.
  1074. */
  1075. function metatag_update_7012() {
  1076. // Fix the {metatag} table first.
  1077. metatag_update_7015();
  1078. if (module_exists('entity_translation')) {
  1079. drupal_set_message(t("Entity Translation is enabled, duplicate meta tags will not be removed for custom entities, to avoid accidental dataloss."));
  1080. return;
  1081. }
  1082. $records = db_select('metatag', 'm')
  1083. ->fields('m', array('entity_type'))
  1084. ->condition('m.entity_type', array('node', 'taxonomy_term', 'user'), 'NOT IN')
  1085. ->orderBy('m.entity_type', 'ASC')
  1086. ->orderBy('m.entity_id', 'ASC')
  1087. ->distinct()
  1088. ->execute();
  1089. $entity_types = array();
  1090. foreach ($records as $record) {
  1091. $entity_types[] = $record->entity_type;
  1092. // Remove duplicates.
  1093. _metatag_remove_dupes($record->entity_type);
  1094. }
  1095. if (empty($entity_types)) {
  1096. drupal_set_message(t('There were no other records to fix.'));
  1097. }
  1098. }
  1099. /**
  1100. * Fix the {metatag} language value for all non-core entity records. This might
  1101. * take a while, depending on how much data needs to be converted.
  1102. */
  1103. function metatag_update_7013(&$sandbox) {
  1104. // Fix the {metatag} table first.
  1105. metatag_update_7015();
  1106. if (module_exists('entity_translation')) {
  1107. drupal_set_message(t("Entity Translation is enabled, meta tags will not be updated for custom entities, to avoid accidental dataloss."));
  1108. return;
  1109. }
  1110. // Use the sandbox at your convenience to store the information needed
  1111. // to track progression between successive calls to the function.
  1112. if (!isset($sandbox['progress'])) {
  1113. // The count of records visited so far.
  1114. $sandbox['progress'] = 0;
  1115. // Because the {metatag} table uses multiple primary keys, there's no easy
  1116. // way to do this, so we're going to cache all record keys and manually
  1117. // step through them.
  1118. $records = db_select('metatag', 'm')
  1119. ->fields('m', array('entity_type', 'entity_id'))
  1120. ->condition('m.entity_type', array('node', 'taxonomy_term', 'user'), 'NOT IN')
  1121. ->orderBy('m.entity_type', 'ASC')
  1122. ->orderBy('m.entity_id', 'ASC')
  1123. ->execute();
  1124. $sandbox['records'] = array();
  1125. foreach ($records as $record) {
  1126. $sandbox['records'][] = $record;
  1127. }
  1128. // If there's no data, don't bother with the extra work.
  1129. if (empty($sandbox['records'])) {
  1130. watchdog('metatag', 'Update 7013: No meta tag records need updating.', array(), WATCHDOG_INFO);
  1131. if (drupal_is_cli()) {
  1132. drupal_set_message(t('Update 7013: No meta tag records need updating.'));
  1133. }
  1134. return t('No meta tag records need updating.');
  1135. }
  1136. // Total records that must be visited.
  1137. $sandbox['max'] = count($sandbox['records']);
  1138. // A place to store messages during the run.
  1139. $sandbox['messages'] = array();
  1140. // An initial record of the number of records to be updated.
  1141. watchdog('metatag', 'Update 7013: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  1142. if (drupal_is_cli()) {
  1143. drupal_set_message(t('Update 7013: !count records to update.', array('!count' => $sandbox['max'])));
  1144. }
  1145. // Last record processed.
  1146. $sandbox['current_record'] = -1;
  1147. }
  1148. // Process records by groups of 10 (arbitrary value).
  1149. // When a group is processed, the batch update engine determines whether it
  1150. // should continue processing in the same request or provide progress
  1151. // feedback to the user and wait for the next request.
  1152. $limit = 10;
  1153. // When ran through Drush it's Ok to process a larger number of objects at a
  1154. // time.
  1155. if (drupal_is_cli()) {
  1156. $limit = 100;
  1157. }
  1158. // Set default values.
  1159. for ($ctr = 0; $ctr < $limit; $ctr++) {
  1160. $sandbox['current_record']++;
  1161. if (empty($sandbox['records'][$sandbox['current_record']])) {
  1162. break;
  1163. }
  1164. // Shortcuts for later.
  1165. $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type;
  1166. $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id;
  1167. // Load the entity.
  1168. $entities = entity_load($entity_type, array($entity_id));
  1169. if (!empty($entities)) {
  1170. $entity = array_pop($entities);
  1171. // Make sure that the entity has a language set.
  1172. if (!empty($entity)) {
  1173. // If there's a (non-empty) language value, use it.
  1174. $new_language = entity_language($entity_type, $entity);
  1175. if (empty($new_language)) {
  1176. $new_language = LANGUAGE_NONE;
  1177. }
  1178. // Update the 'language' value.
  1179. db_update('metatag')
  1180. ->fields(array('language' => $new_language))
  1181. ->condition('entity_type', $entity_type)
  1182. ->condition('entity_id', $entity_id)
  1183. ->execute();
  1184. }
  1185. }
  1186. // Update our progress information.
  1187. $sandbox['progress']++;
  1188. }
  1189. // Set the "finished" status, to tell batch engine whether this function
  1190. // needs to run again. If you set a float, this will indicate the progress of
  1191. // the batch so the progress bar will update.
  1192. if ($sandbox['progress'] >= $sandbox['max']) {
  1193. $sandbox['#finished'] = TRUE;
  1194. }
  1195. else {
  1196. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  1197. }
  1198. if ($sandbox['#finished']) {
  1199. // Clear all caches so the fixed data will be reloaded.
  1200. cache_clear_all('*', 'cache_metatag', TRUE);
  1201. // A final log of the number of records that were converted.
  1202. watchdog('metatag', 'Update 7013: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  1203. if (drupal_is_cli()) {
  1204. drupal_set_message(t('Update 7013: !count records were updated.', array('!count' => $sandbox['progress'])));
  1205. }
  1206. // hook_update_N() may optionally return a string which will be displayed
  1207. // to the user.
  1208. return t('!count records were updated in total.', array('!count' => $sandbox['progress']));
  1209. }
  1210. }
  1211. /**
  1212. * Remove duplicate records for a given entity.
  1213. *
  1214. * It should be OK to run this without doing a separate batch process as there
  1215. * shouldn't be many records that have this problem. Hopefully.
  1216. *
  1217. * @param $entity_type
  1218. * The name of an entity type to check for.
  1219. */
  1220. function _metatag_remove_dupes($entity_type) {
  1221. $purge_count = 0;
  1222. // First step: fix the records. There should not be multiple records for the
  1223. // same entity_id with different languages.
  1224. $dupe_records = db_query("SELECT m.entity_id, count(m.language) AS the_count
  1225. FROM {metatag} m
  1226. WHERE
  1227. m.entity_type = :type
  1228. GROUP BY m.entity_id
  1229. HAVING count(m.language) > 1", array(':type' => $entity_type));
  1230. if (!empty($dupe_records)) {
  1231. foreach ($dupe_records as $record) {
  1232. $entity_id = $record->entity_id;
  1233. $langs = db_query("SELECT m.entity_id, m.language, m.data FROM {metatag} m WHERE m.entity_type = :type AND m.entity_id = :id", array(':type' => $entity_type, ':id' => $entity_id))->fetchAll();
  1234. // Work out which language record to remove. Will need to store this as
  1235. // an array incase there are multiple records to purge.
  1236. $langs_to_remove = array();
  1237. // Check for duplicate records.
  1238. // Outer loop starts from the beginning.
  1239. for ($outer = 0; $outer < count($langs); $outer++) {
  1240. // This record may have been removed already.
  1241. if (isset($langs[$outer])) {
  1242. // Inner loop starts from the end.
  1243. for ($inner = count($langs) - 1; $inner > 0; $inner--) {
  1244. // Work out if the outer loop's data is the same as the inner
  1245. // loop's.
  1246. if (isset($langs[$inner]) && $langs[$outer]->data == $langs[$inner]->data) {
  1247. // Remove the second record.
  1248. $langs_to_remove[] = $langs[$inner]->language;
  1249. unset($langs[$inner]);
  1250. }
  1251. }
  1252. }
  1253. }
  1254. // Only one record left.
  1255. if (count($langs) == 1) {
  1256. // This is how it should be, this record is fine.
  1257. }
  1258. // More than one record, work out which one to keep.
  1259. elseif (count($langs) > 1) {
  1260. // Work out the entity's language.
  1261. $entity = entity_load($entity_type, $entity_id);
  1262. $entity_language = entity_language($entity_type, $entity);
  1263. if (empty($language)) {
  1264. $entity_language = LANGUAGE_NONE;
  1265. }
  1266. // Work out if the entity's language record exists.
  1267. $lang_pos = NULL;
  1268. foreach ($langs as $key => $record) {
  1269. if ($record->language == $entity_language) {
  1270. $lang_pos = $key;
  1271. break;
  1272. }
  1273. }
  1274. // If the language record exists, delete the others.
  1275. if (isset($lang_pos)) {
  1276. foreach ($langs as $key => $record) {
  1277. if ($record->language != $entity_language) {
  1278. $langs_to_remove[] = $record->language;
  1279. }
  1280. }
  1281. }
  1282. // Otherwise look for a record for the site's default language.
  1283. else {
  1284. foreach ($langs as $key => $record) {
  1285. if ($record->language == $GLOBALS['language']->language) {
  1286. $lang_pos = $key;
  1287. break;
  1288. }
  1289. }
  1290. if (isset($lang_pos)) {
  1291. foreach ($langs as $key => $record) {
  1292. if ($record->language != $GLOBALS['language']->language) {
  1293. $langs_to_remove[] = $record->language;
  1294. }
  1295. }
  1296. }
  1297. // Finally check for LANGUAGE_NONE.
  1298. else {
  1299. foreach ($langs as $key => $record) {
  1300. if ($record->language == LANGUAGE_NONE) {
  1301. $lang_pos = $key;
  1302. break;
  1303. }
  1304. }
  1305. if (isset($lang_pos)) {
  1306. foreach ($langs as $key => $record) {
  1307. if ($record->language != LANGUAGE_NONE) {
  1308. $langs_to_remove[] = $record->language;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. }
  1314. }
  1315. // Purge the redundant records.
  1316. if (!empty($langs_to_remove)) {
  1317. $purge_count += db_delete('metatag')
  1318. ->condition('entity_type', $entity_type)
  1319. ->condition('entity_id', $entity_id)
  1320. ->condition('language', $langs_to_remove)
  1321. ->execute();
  1322. }
  1323. }
  1324. }
  1325. if (empty($purge_count)) {
  1326. drupal_set_message(t('No duplicate :entity_type records were found (this is a good thing).', array(':entity_type' => $entity_type)));
  1327. watchdog('metatag', 'No duplicate :entity_type records were found (this is a good thing).', array(':entity_type' => $entity_type));
  1328. }
  1329. else {
  1330. drupal_set_message(t('Purged :count duplicate :entity_type record(s).', array(':count' => $purge_count, ':entity_type' => $entity_type)));
  1331. watchdog('metatag', 'Purged :count duplicate :entity_type record(s).', array(':count' => $purge_count, ':entity_type' => $entity_type));
  1332. return;
  1333. }
  1334. }
  1335. /**
  1336. * Fix {metatag} records that may have been corrupted by #1871020.
  1337. */
  1338. function metatag_update_7014() {
  1339. $records = db_query("SELECT *
  1340. FROM {metatag} m
  1341. WHERE
  1342. m.data LIKE :nolang
  1343. OR m.data LIKE :lang
  1344. OR m.data LIKE :und",
  1345. array(
  1346. ':nolang' => 'a:1:{s:0:"";a:%:{s:%;a:%:{%;}}}',
  1347. ':lang' => 'a:1:{s:2:"__";a:%:{s:%;a:%:{%;}}}',
  1348. ':und' => 'a:1:{s:3:"___";a:%:{s:%;a:%:{%;}}}',
  1349. ));
  1350. // Nothing to fix.
  1351. if ($records->rowCount() == 0) {
  1352. drupal_set_message(t('No corrupt records to fix, this is good news :-)'));
  1353. }
  1354. // Fix the faulty records.
  1355. else {
  1356. foreach ($records as $record) {
  1357. // Extract the data and get the first element of the array, this should be
  1358. // valid data.
  1359. $record->data = reset(unserialize($record->data));
  1360. // Update the record.
  1361. drupal_write_record('metatag', $record, array('entity_type', 'entity_id', 'language'));
  1362. }
  1363. drupal_set_message(t('Fixed @count corrupt meta tag record(s).', array('@count' => $records->rowCount())));
  1364. }
  1365. }
  1366. /**
  1367. * Add the revision_id from the entity into metatag schema, adjust the primary
  1368. * keys accordingly.
  1369. */
  1370. function metatag_update_7015() {
  1371. if (!db_field_exists('metatag', 'revision_id')) {
  1372. // Leave a note for metatag_metatags_load_multiple() that the revision_id
  1373. // field has been added.
  1374. variable_set('metatag_has_revision_id', TRUE);
  1375. // Tell update 7017 that it isn't needed.
  1376. variable_set('metatag_skip_update_7017', TRUE);
  1377. // Add the new field.
  1378. db_add_field('metatag', 'revision_id', array(
  1379. 'type' => 'int',
  1380. 'unsigned' => TRUE,
  1381. 'not null' => TRUE,
  1382. 'default' => 0,
  1383. 'description' => 'The revision_id for the entity object this data is attached to.',
  1384. ));
  1385. // Remove the existing primary key. This may take some work so it can be
  1386. // database agnostic, i.e. some databases will not like it.
  1387. db_drop_primary_key('metatag');
  1388. // Add the new primary key.
  1389. db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
  1390. drupal_set_message(t('Added the {metatag}.revision_id field.'));
  1391. }
  1392. else {
  1393. drupal_set_message(t('The {metatag}.revision_id field has already been added, nothing to do.'));
  1394. }
  1395. }
  1396. /**
  1397. * Update the revision ID to fix the NULL values, help avoid problems with
  1398. * update 7017.
  1399. */
  1400. function metatag_update_7016() {
  1401. // It's possible that 7015 was not executed if the site had been updated to
  1402. // an early dev release, so make sure the revision_id field exists.
  1403. metatag_update_7015();
  1404. // Run the update.
  1405. db_query("UPDATE {metatag} SET revision_id = 0 WHERE revision_id IS NULL");
  1406. }
  1407. /**
  1408. * The {metatag}.revision_id field is required.
  1409. */
  1410. function metatag_update_7017() {
  1411. if (!variable_get('metatag_skip_update_7017', FALSE)) {
  1412. // Let's add a temporary unique key so MySQL will let it go.
  1413. db_add_unique_key('metatag', 'temp_key', array('entity_type', 'entity_id', 'revision_id', 'language'));
  1414. // Now remove the PK before changing a field from serial.
  1415. db_drop_primary_key('metatag');
  1416. // Change the field.
  1417. db_change_field('metatag', 'revision_id', 'revision_id', array(
  1418. 'type' => 'int',
  1419. 'unsigned' => TRUE,
  1420. 'not null' => TRUE,
  1421. 'default' => 0,
  1422. 'description' => 'The revision_id for the entity object this data is attached to.',
  1423. ));
  1424. // Manually re-add the PK.
  1425. db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
  1426. // Finally, remove the temporary unique key because it's no longer useful.
  1427. db_drop_unique_key('metatag', 'temp_key');
  1428. drupal_set_message(t('Fixed the {metatag}.revision_id field.'));
  1429. }
  1430. else {
  1431. drupal_set_message(t("Didn't need to fix the {metatag}.revision_id field; please disperse, nothing to see here."));
  1432. }
  1433. // Delete the temporary variable.
  1434. variable_del('metatag_skip_update_7017');
  1435. }
  1436. /**
  1437. * Update the revision ID for each record. This may take some time. Should any
  1438. * nodes be discovered with a meta tag record for both revision_id 0 and the
  1439. * correct revision_id, the "0" value will be deleted; if this is not the
  1440. * desired result the {metatag} table must be manually pruned to have the
  1441. * correct records prior to letting this update run.
  1442. */
  1443. function metatag_update_7018(&$sandbox) {
  1444. // Process records in small groups.
  1445. // When a group is processed, the batch update engine determines whether it
  1446. // should continue processing in the same request or provide progress
  1447. // feedback to the user and wait for the next request.
  1448. $limit = 10;
  1449. // When ran through Drush it's Ok to process a larger number of objects at a
  1450. // time.
  1451. if (drupal_is_cli()) {
  1452. $limit = 100;
  1453. }
  1454. // Use the sandbox at your convenience to store the information needed
  1455. // to track progression between successive calls to the function.
  1456. if (!isset($sandbox['progress'])) {
  1457. // The count of records visited so far.
  1458. $sandbox['progress'] = 0;
  1459. // Get a list of all records affected.
  1460. $sandbox['records'] = db_query("SELECT entity_type, entity_id, language
  1461. FROM {metatag}
  1462. WHERE revision_id = 0")
  1463. ->fetchAll();
  1464. // If there's no data, don't bother with the extra work.
  1465. if (empty($sandbox['records'])) {
  1466. watchdog('metatag', 'Update 7018: No {metatag} records needed to have the revision_id value fixed.', array(), WATCHDOG_INFO);
  1467. if (drupal_is_cli()) {
  1468. drupal_set_message(t('Update 7018: No {metatag} records needed to have the revision_id value fixed.'));
  1469. }
  1470. return t('No {metatag} records needed to have the revision_id value fixed.');
  1471. }
  1472. // Total records that must be visited.
  1473. $sandbox['max'] = count($sandbox['records']);
  1474. // A place to store messages during the run.
  1475. $sandbox['messages'] = array();
  1476. // An initial record of the number of records to be updated.
  1477. watchdog('metatag', 'Update 7018: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  1478. if (drupal_is_cli()) {
  1479. drupal_set_message(t('Update 7018: !count records to update.', array('!count' => $sandbox['max'])));
  1480. }
  1481. // Last record processed.
  1482. $sandbox['current_record'] = -1;
  1483. // Because a lot of other processing happens on the first iteration, just do
  1484. // one.
  1485. $limit = 1;
  1486. }
  1487. // Work out which entities support languages and revisions.
  1488. $has_language = array();
  1489. $has_revisions = array();
  1490. foreach (entity_get_info() as $entity_type => $info) {
  1491. $has_language[$entity_type] = FALSE;
  1492. $has_revisions[$entity_type] = FALSE;
  1493. if (!empty($info['entity keys']['language'])) {
  1494. $has_language[$entity_type] = $info['entity keys']['language'];
  1495. }
  1496. if (!empty($info['entity keys']['revision'])) {
  1497. $has_revisions[$entity_type] = $info['entity keys']['revision'];
  1498. }
  1499. }
  1500. // Set default values.
  1501. for ($ctr = 0; $ctr < $limit; $ctr++) {
  1502. $sandbox['current_record']++;
  1503. if (empty($sandbox['records'][$sandbox['current_record']])) {
  1504. break;
  1505. }
  1506. // Shortcuts for later.
  1507. $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type;
  1508. $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id;
  1509. // Make sure to load the correct language record.
  1510. $language = $sandbox['records'][$sandbox['current_record']]->language;
  1511. $conditions = array();
  1512. // Some entities don't include a language value.
  1513. if (!empty($has_language[$entity_type])) {
  1514. $conditions['language'] = $language;
  1515. }
  1516. $records = entity_load($entity_type, array($entity_id), $conditions);
  1517. // Try to fallback to default language if no record was found. This may
  1518. // happen when using entity_translation as the parent entity table only
  1519. // contains one record.
  1520. if (!empty($conditions) && empty($records)) {
  1521. $records = entity_load($entity_type, array($entity_id));
  1522. }
  1523. // Fix this record.
  1524. if (!empty($records)) {
  1525. $entity = reset($records);
  1526. // Speed up the handling of entities that don't support revisions.
  1527. $revision_id = 0;
  1528. if (!empty($has_revisions[$entity_type])) {
  1529. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  1530. $revision_id = intval($revision_id);
  1531. }
  1532. // Don't bother updating records if the revision_id is 0.
  1533. if (!empty($revision_id)) {
  1534. $exists = db_query("SELECT entity_id
  1535. FROM {metatag}
  1536. WHERE entity_type = :entity_type
  1537. AND entity_id = :entity_id
  1538. AND revision_id = :revision_id
  1539. AND language = :language",
  1540. array(
  1541. ':entity_type' => $entity_type,
  1542. ':entity_id' => $entity_id,
  1543. ':revision_id' => $revision_id,
  1544. ':language' => $language,
  1545. ))->fetchObject();
  1546. // There isn't already a record for the revision_id, so update the
  1547. // metatag record.
  1548. if (!$exists) {
  1549. db_update('metatag')
  1550. ->fields(array('revision_id' => $revision_id))
  1551. ->condition('entity_type', $entity_type)
  1552. ->condition('entity_id', $entity_id)
  1553. ->condition('revision_id', 0)
  1554. ->condition('language', $language)
  1555. ->execute();
  1556. }
  1557. // The record exists, so delete the old one under the grounds that the
  1558. // one with a revision_id is newer.
  1559. // Disclaimer: this is completely arbitrary, without providing a UI to
  1560. // let the site maintainer/builder choose which of the two records to
  1561. // keep, we're stuck with a bad scenario. Thankfully this should not
  1562. // happen very often and would only affect sites that were running a
  1563. // dev release. Also, sorry :(
  1564. else {
  1565. db_delete('metatag')
  1566. ->condition('entity_type', $entity_type)
  1567. ->condition('entity_id', $entity_id)
  1568. ->condition('revision_id', 0)
  1569. ->condition('language', $language)
  1570. ->execute();
  1571. }
  1572. // Nodes can have multiple revisions, so create new {metatag} records
  1573. // for each of the other revisions.
  1574. if ($entity_type == 'node') {
  1575. $revisions = node_revision_list($entity);
  1576. if (count($revisions) > 1) {
  1577. $metatags = db_query("SELECT data
  1578. FROM {metatag}
  1579. WHERE entity_type = :entity_type
  1580. AND entity_id = :entity_id
  1581. AND language = :language",
  1582. array(
  1583. ':entity_type' => $entity_type,
  1584. ':entity_id' => $entity_id,
  1585. ':language' => $language,
  1586. ));
  1587. if (!empty($metatags) && isset($metatags->data) && !empty($metatags->data)) {
  1588. foreach ($revisions as $vid => $revision) {
  1589. // Only one record per nid/vid/langcode, thank you.
  1590. if ($vid != $revision_id) {
  1591. // Check that there isn't already a record for this revision.
  1592. $exists = db_query("SELECT entity_id
  1593. FROM {metatag}
  1594. WHERE entity_type = :entity_type
  1595. AND entity_id = :entity_id
  1596. AND revision_id = :revision_id
  1597. AND language = :language",
  1598. array(
  1599. ':entity_type' => $entity_type,
  1600. ':entity_id' => $entity_id,
  1601. ':revision_id' => $vid,
  1602. ':language' => $language,
  1603. ))->fetchObject();
  1604. if (!$exists) {
  1605. $node = node_load($entity_id, $vid);
  1606. $record = new StdClass();
  1607. $record->entity_type = $entity_type;
  1608. $record->entity_id = $entity_id;
  1609. $record->revision_id = $vid;
  1610. $record->language = $language;
  1611. $record->data = $metatags->data;
  1612. drupal_write_record('metatag', $record);
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. }
  1619. // Other entity types.
  1620. else {
  1621. drupal_set_message(t('Metatag records for @type objects have not been checked for revisions.', array('@type' => $entity_type)), 'status', FALSE);
  1622. }
  1623. }
  1624. }
  1625. // Update our progress information.
  1626. $sandbox['progress']++;
  1627. }
  1628. // Set the "finished" status, to tell batch engine whether this function
  1629. // needs to run again. If you set a float, this will indicate the progress of
  1630. // the batch so the progress bar will update.
  1631. if ($sandbox['progress'] >= $sandbox['max']) {
  1632. $sandbox['#finished'] = TRUE;
  1633. }
  1634. else {
  1635. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  1636. }
  1637. if ($sandbox['#finished']) {
  1638. // Clear all caches so the fixed data will be reloaded.
  1639. cache_clear_all('*', 'cache_metatag', TRUE);
  1640. // A final log of the number of records that were converted.
  1641. watchdog('metatag', 'Update 7018: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  1642. if (drupal_is_cli()) {
  1643. drupal_set_message(t('Update 7018: !count records were updated.', array('!count' => $sandbox['progress'])));
  1644. }
  1645. // hook_update_N() may optionally return a string which will be displayed
  1646. // to the user.
  1647. return t('Fixed the revision_id values for !count {metatag} records.', array('!count' => $sandbox['progress']));
  1648. }
  1649. }
  1650. /**
  1651. * Clear the entity_cache bins.
  1652. */
  1653. function metatag_update_7019() {
  1654. if (module_exists('entitycache')) {
  1655. foreach (drupal_get_schema() as $table_name => $spec) {
  1656. if (strpos($table_name, 'cache_entity_') === 0) {
  1657. cache_clear_all('*', $table_name, TRUE);
  1658. drupal_set_message(t("Cleared the @table cache bin", array('@table' => $table_name)));
  1659. }
  1660. }
  1661. }
  1662. else {
  1663. drupal_set_message(t("The EntityCache module is not installed, nothing to do."));
  1664. }
  1665. }
  1666. /**
  1667. * Clear the Metatag cache.
  1668. */
  1669. function metatag_update_7020() {
  1670. cache_clear_all('*', 'cache_metatag', TRUE);
  1671. return t('All Metatag caches cleared.');
  1672. }
  1673. /**
  1674. * Clear the existing Metatag cache so all unwanted 403/404 paths can be
  1675. * purged.
  1676. */
  1677. function metatag_update_7021() {
  1678. cache_clear_all('*', 'cache_metatag', TRUE);
  1679. return t('All Metatag caches cleared.');
  1680. }
  1681. /**
  1682. * A minor bit of tidy-up after update 7015.
  1683. */
  1684. function metatag_update_7022() {
  1685. variable_del('metatag_skip_update_7015');
  1686. }
  1687. /**
  1688. * Clear the Metatag cache because $cid_parts was changed.
  1689. */
  1690. function metatag_update_7023() {
  1691. cache_clear_all('*', 'cache_metatag', TRUE);
  1692. return t('All Metatag caches cleared.');
  1693. }
  1694. /**
  1695. * No-op update. Renaming twitter:image to twitter:image:src no longer needed.
  1696. */
  1697. function metatag_update_7024() {
  1698. // Do nothing.
  1699. }
  1700. /**
  1701. * Replaced by update 7030.
  1702. */
  1703. function metatag_update_7025() {
  1704. // Do nothing.
  1705. }
  1706. /**
  1707. * Rename the 'copyright' meta tag to 'rights', part 1.
  1708. */
  1709. function metatag_update_7026(&$sandbox) {
  1710. $old_tag = 'copyright';
  1711. $new_tag = 'rights';
  1712. return metatag_update_replace_entity_tag($sandbox, $old_tag, $new_tag);
  1713. }
  1714. /**
  1715. * Replaced by update 7031.
  1716. */
  1717. function metatag_update_7027() {
  1718. // Do nothing.
  1719. }
  1720. /**
  1721. * Clear the menu cache so the new Settings page will be picked up.
  1722. */
  1723. function metatag_update_7028() {
  1724. variable_set('menu_rebuild_needed', TRUE);
  1725. }
  1726. /**
  1727. * Add an index to the {metatag} table to speed up some queries.
  1728. */
  1729. function metatag_update_7029() {
  1730. if (!db_index_exists('metatag', 'type_revision')) {
  1731. db_add_index('metatag', 'type_revision', array('entity_type', 'revision_id'));
  1732. return t('Added an index to the main Metatag table that will hopefully improve performance a little.');
  1733. }
  1734. else {
  1735. return t('Did not add the index, it already existed.');
  1736. }
  1737. }
  1738. /**
  1739. * No-op update. Renaming twitter:image to twitter:image:src no longer needed.
  1740. */
  1741. function metatag_update_7030() {
  1742. // Do nothing.
  1743. }
  1744. /**
  1745. * Rename the 'copyright' meta tag to 'rights', part 2.
  1746. */
  1747. function metatag_update_7031() {
  1748. $old_tag = 'copyright';
  1749. $new_tag = 'rights';
  1750. return metatag_update_replace_config_tag($old_tag, $new_tag);
  1751. }
  1752. /**
  1753. * Clear the Metatag cache.
  1754. */
  1755. function metatag_update_7032() {
  1756. cache_clear_all('*', 'cache_metatag', TRUE);
  1757. return t('All Metatag caches cleared.');
  1758. }
  1759. /**
  1760. * These originally removed the 'author' meta tag, but it was subsequently
  1761. * decided that this was not the correct approach, that the meta tag should not
  1762. * be removed after all.
  1763. *
  1764. * @see https://www.drupal.org/node/2330823
  1765. */
  1766. function metatag_update_7033() {
  1767. }
  1768. function metatag_update_7034() {
  1769. }
  1770. function metatag_update_7035() {
  1771. }
  1772. /**
  1773. * Update variables to indicate which entities should be supported.
  1774. */
  1775. function metatag_update_7036() {
  1776. foreach (entity_get_info() as $entity_type => $entity_info) {
  1777. $variable_name = 'metatag_enable_' . $entity_type;
  1778. // Configuration entities are skipped.
  1779. if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) {
  1780. continue;
  1781. }
  1782. // Entities without view modes are skipped.
  1783. elseif (empty($entity_info['view modes'])) {
  1784. continue;
  1785. }
  1786. // Basic core entities or "normal" entities that have been enabled via the
  1787. // API.
  1788. elseif (in_array($entity_type, array('node', 'taxonomy_term', 'user')) ||
  1789. !empty($entity_info['metatag']) || !empty($entity_info['metatags'])) {
  1790. // Check if the entity type has been enabled or disabled previously; if
  1791. // the variable equals a junk value then it was not previously set,
  1792. // therefore we'll set a default.
  1793. if (variable_get($variable_name, 'monkey') == 'monkey') {
  1794. // By default these entity types are enabled.
  1795. variable_set($variable_name, TRUE);
  1796. // Check each entity bundle.
  1797. if (!empty($entity_info['bundles'])) {
  1798. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  1799. $variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
  1800. // Check if the bundle has been enabled or disabled previously; if
  1801. // the variable equals a junk value then it was not previously set,
  1802. // therefore we'll set a default.
  1803. if (variable_get($variable_name, 'monkey') == 'monkey') {
  1804. if (!empty($bundle_info['metatag']) || !empty($bundle_info['metatags'])) {
  1805. variable_set($variable_name, TRUE);
  1806. }
  1807. else {
  1808. variable_set($variable_name, FALSE);
  1809. }
  1810. }
  1811. // This variable was set before.
  1812. else {
  1813. // Do nothing.
  1814. }
  1815. }
  1816. }
  1817. }
  1818. // This variable was set before.
  1819. else {
  1820. // Do nothing.
  1821. }
  1822. }
  1823. // Disable this entity type.
  1824. else {
  1825. variable_set($variable_name, FALSE);
  1826. }
  1827. }
  1828. // Clear the caches.
  1829. cache_clear_all('*', 'cache_metatag', TRUE);
  1830. drupal_static_reset('metatag_config_load_with_defaults');
  1831. drupal_static_reset('metatag_entity_supports_metatags');
  1832. ctools_include('export');
  1833. ctools_export_load_object_reset('metatag_config');
  1834. drupal_set_message(t('The way that Metatag tracks which entity types are compatible has changed. Please review the <a href="@url">Settings page</a> to ensure that all of the entity types are enabled correctly.', array('@url' => 'admin/config/search/metatags/settings')));
  1835. }
  1836. /**
  1837. * Clear the menu cache so the renamed Settings page will be picked up.
  1838. */
  1839. function metatag_update_7037() {
  1840. variable_set('menu_rebuild_needed', TRUE);
  1841. }
  1842. /**
  1843. * Manually enable all content types, vocabularies and the user entity to help
  1844. * resolve issues from 1.5's architecture change.
  1845. */
  1846. function metatag_update_7038() {
  1847. foreach (array('node', 'taxonomy_term', 'user') as $entity_type) {
  1848. // Enable the main entity type.
  1849. $variable_name = 'metatag_enable_' . $entity_type;
  1850. variable_set($variable_name, TRUE);
  1851. // Update each entity bundle too.
  1852. $entity_info = entity_get_info($entity_type);
  1853. if (!empty($entity_info['bundles'])) {
  1854. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  1855. $variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name;
  1856. variable_set($variable_name, TRUE);
  1857. }
  1858. }
  1859. }
  1860. }
  1861. /**
  1862. * Reload some meta tag caches.
  1863. */
  1864. function metatag_update_7039() {
  1865. foreach (language_list() as $langcode => $language) {
  1866. cache_clear_all('info:' . $langcode, 'cache_metatag');
  1867. }
  1868. }
  1869. /**
  1870. * Fix robots meta tags that might have been broken when they were imported
  1871. * from Nodewords.
  1872. */
  1873. function metatag_update_7040(&$sandbox) {
  1874. // Process records by groups of 10 (arbitrary value).
  1875. // When a group is processed, the batch update engine determines whether it
  1876. // should continue processing in the same request or provide progress
  1877. // feedback to the user and wait for the next request.
  1878. $limit = 10;
  1879. // When ran through Drush it's Ok to process a larger number of objects at a
  1880. // time.
  1881. if (drupal_is_cli()) {
  1882. $limit = 100;
  1883. }
  1884. // Use the sandbox at your convenience to store the information needed
  1885. // to track progression between successive calls to the function.
  1886. if (!isset($sandbox['progress'])) {
  1887. // The count of records visited so far.
  1888. $sandbox['progress'] = 0;
  1889. // Get a count of {metatag} records that have a robots meta tag definition
  1890. // that is not in the correct format.
  1891. $records = db_select('metatag', 'm')
  1892. ->fields('m')
  1893. ->condition('m.data', '%:6:"robots";%', 'LIKE')
  1894. ->condition('m.data', '%:6:"robots";a:1:{s:5:"value";%', 'NOT LIKE')
  1895. ->countQuery()
  1896. ->execute()
  1897. ->fetchField();
  1898. // If there's no data, don't bother with the extra work.
  1899. if (empty($records)) {
  1900. watchdog('metatag', 'Update 7040: No robots meta tags need to be fixed.', array(), WATCHDOG_INFO);
  1901. if (drupal_is_cli()) {
  1902. drupal_set_message(t('Update 7040: No robots meta tags need to be fixed.'));
  1903. }
  1904. return t('No robots meta tags need to be fixed.');
  1905. }
  1906. // Total records that must be visited.
  1907. $sandbox['max'] = $records;
  1908. // A place to store messages during the run.
  1909. $sandbox['messages'] = array();
  1910. // An initial record of the number of records to be updated.
  1911. watchdog('metatag', 'Update 7040: !count records to examine.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  1912. if (drupal_is_cli()) {
  1913. drupal_set_message(t('Update 7040: !count records to examine.', array('!count' => $sandbox['max'])));
  1914. }
  1915. // Log how many records are fixed.
  1916. $sandbox['fixed'] = 0;
  1917. // Because a lot of other processing happens on the first iteration, just do
  1918. // ten.
  1919. $limit = 10;
  1920. }
  1921. // Get a list of records that need to be fixed.
  1922. $records = db_select('metatag', 'm')
  1923. ->fields('m')
  1924. ->condition('m.data', '%:6:"robots";%', 'LIKE')
  1925. ->condition('m.data', '%:6:"robots";a:1:{s:5:"value";%', 'NOT LIKE')
  1926. ->range(0, $limit)
  1927. ->execute();
  1928. // Set default values.
  1929. foreach ($records as $record) {
  1930. // Extract the record.
  1931. $record->data = unserialize($record->data);
  1932. // See if the record needs to be fixed.
  1933. if (!empty($record->data['robots']) && empty($record->data['robots']['value'])) {
  1934. // Fix the record.
  1935. $robots = $record->data['robots'];
  1936. $record->data['robots'] = array(
  1937. 'value' => $robots,
  1938. );
  1939. // Update the database.
  1940. db_update('metatag')
  1941. ->fields(array('data' => serialize($record->data)))
  1942. ->condition('entity_type', $record->entity_type)
  1943. ->condition('entity_id', $record->entity_id)
  1944. ->condition('revision_id', $record->revision_id)
  1945. ->condition('language', $record->language)
  1946. ->execute();
  1947. // Clear the cache for this entity.
  1948. entity_get_controller($record->entity_type)->resetCache(array($record->entity_id));
  1949. // Update our progress information.
  1950. $sandbox['fixed']++;
  1951. }
  1952. // Update our progress information.
  1953. $sandbox['progress']++;
  1954. }
  1955. // Set the "finished" status, to tell batch engine whether this function
  1956. // needs to run again. If you set a float, this will indicate the progress of
  1957. // the batch so the progress bar will update.
  1958. if ($sandbox['progress'] >= $sandbox['max']) {
  1959. $sandbox['#finished'] = TRUE;
  1960. }
  1961. else {
  1962. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  1963. }
  1964. // Only display a status message if process finished.
  1965. if ($sandbox['#finished'] === TRUE) {
  1966. // Clear all caches so the fixed data will be reloaded.
  1967. cache_clear_all('*', 'cache_metatag', TRUE);
  1968. // A final log of the number of records that were converted.
  1969. watchdog('metatag', 'Update 7040: !count records were fixed.', array('!count' => $sandbox['fixed']), WATCHDOG_INFO);
  1970. if (drupal_is_cli()) {
  1971. drupal_set_message(t('Update 7040: !count records were fixed.', array('!count' => $sandbox['fixed'])));
  1972. }
  1973. // hook_update_N() may optionally return a string which will be displayed
  1974. // to the user.
  1975. return t('Fixed the Metatag robots values for @count nodes.', array('@count' => $sandbox['fixed']));
  1976. }
  1977. }
  1978. /**
  1979. * Rerun update 7018 that was fixed to run correctly when using Entity
  1980. * Translation. This may take some time.
  1981. */
  1982. function metatag_update_7041(&$sandbox) {
  1983. metatag_update_7018($sandbox);
  1984. }
  1985. /**
  1986. * Delete a deprecated variable and clear all Metatag caches.
  1987. */
  1988. function metatag_update_7100() {
  1989. variable_del('metatag_translate_final_values');
  1990. // Clear all caches so that i18n support can be activated, if necessary.
  1991. cache_clear_all('*', 'cache_metatag', TRUE);
  1992. }
  1993. /**
  1994. * Update i18n strings for all meta tags to use the new format.
  1995. */
  1996. function metatag_update_7101() {
  1997. if (!module_exists('locale') || !db_table_exists('locales_source')) {
  1998. return t('No translations to fix as the locale system is not enabled.');
  1999. }
  2000. // Loop through each entity type. Not going to bother filtering this list,
  2001. // it'll only be a difference of a few entities anyway and the queries
  2002. // should be fairly quick.
  2003. foreach (entity_get_info() as $entity_type => $entity_info) {
  2004. // Entities must have bundles.
  2005. if (empty($entity_info['bundles'])) {
  2006. $entity_info['bundles'] = array(
  2007. $entity_type => $entity_type,
  2008. );
  2009. }
  2010. foreach ($entity_info['bundles'] as $bundle_type => $bundle_info) {
  2011. // Update {locales_source}, replace 'metatag:ENTITYTYPE:BUNDLE:tag' with
  2012. // 'metatag:metatag_config:ENTITYTYPE:BUNDLE:tag'.
  2013. db_query("UPDATE {locales_source}
  2014. SET location = REPLACE(location, 'metatag:{$entity_type}:{$bundle_type}:', 'metatag:metatag_config:{$entity_type}:{$bundle_type}:'),
  2015. context = REPLACE(context, '{$entity_type}:{$bundle_type}:', 'metatag_config:{$entity_type}:{$bundle_type}:')
  2016. WHERE textgroup = 'metatag'
  2017. AND location LIKE 'metatag:{$entity_type}:{$bundle_type}:%'");
  2018. // Update {locales_source}, replace 'metatag:ENTITYTYPE:tag' with
  2019. // 'metatag:metatag_config:ENTITYTYPE:tag'.
  2020. db_query("UPDATE {locales_source}
  2021. SET location = REPLACE(location, 'metatag:{$entity_type}:', 'metatag:metatag_config:{$entity_type}:'),
  2022. context = REPLACE(context, '{$entity_type}:', 'metatag_config:{$entity_type}:')
  2023. WHERE textgroup = 'metatag'
  2024. AND location LIKE 'metatag:{$entity_type}:%'");
  2025. }
  2026. }
  2027. // Update {locales_source}, replace 'metatag:metatag:' with
  2028. // 'metatag:output:'.
  2029. db_query("UPDATE {locales_source}
  2030. SET location = REPLACE(location, 'metatag:metatag:', 'metatag:output:'),
  2031. context = REPLACE(context, 'metatag:', 'output:')
  2032. WHERE textgroup = 'metatag'
  2033. AND location LIKE 'metatag:metatag:%'");
  2034. }
  2035. /**
  2036. * Re-run update 7101.
  2037. */
  2038. function metatag_update_7102() {
  2039. metatag_update_7101();
  2040. }
  2041. /**
  2042. * Clear all metatag caches so the new entity caching structure takes over.
  2043. */
  2044. function metatag_update_7103() {
  2045. cache_clear_all('*', 'cache_metatag', TRUE);
  2046. }
  2047. /**
  2048. * Remove the entity revision ID from the translation strings.
  2049. */
  2050. function metatag_update_7104(&$sandbox) {
  2051. // Verify that locales are being used in the first place.
  2052. if (!module_exists('locale') || !db_table_exists('locales_source')) {
  2053. return t('Metatag: No translations to fix as the locale system is not enabled.');
  2054. }
  2055. // No need to do anything if output translation is disabled.
  2056. if (!variable_get('metatag_i18n_translate_output', FALSE)) {
  2057. return t('Metatag: Output translation is disabled, so no need to update anything.');
  2058. }
  2059. // Process records by groups of 10 (arbitrary value).
  2060. // When a group is processed, the batch update engine determines whether it
  2061. // should continue processing in the same request or provide progress
  2062. // feedback to the user and wait for the next request.
  2063. $limit = 10;
  2064. // When ran through Drush it's Ok to process a larger number of objects at a
  2065. // time.
  2066. if (drupal_is_cli()) {
  2067. $limit = 100;
  2068. }
  2069. // Use the sandbox at your convenience to store the information needed
  2070. // to track progression between successive calls to the function.
  2071. if (!isset($sandbox['progress'])) {
  2072. // The count of records visited so far.
  2073. $sandbox['progress'] = 0;
  2074. // Total records that must be visited.
  2075. $sandbox['max'] = db_query("SELECT COUNT(lid)
  2076. FROM {locales_source}
  2077. WHERE textgroup = 'metatag'
  2078. AND context LIKE 'output:%:%:%:%'")->fetchField();
  2079. // If there's no data, don't bother with the extra work.
  2080. if (empty($sandbox['max'])) {
  2081. watchdog('metatag', 'Update 7104: No nodes need the translation entity string fixed.', array(), WATCHDOG_INFO);
  2082. if (drupal_is_cli()) {
  2083. drupal_set_message(t('Update 7104: No nodes need the translation entity string fixed.'));
  2084. }
  2085. return t('No nodes need the Metatag language values fixed.');
  2086. }
  2087. // A place to store messages during the run.
  2088. $sandbox['messages'] = array();
  2089. // An initial record of the number of records to be updated.
  2090. watchdog('metatag', 'Update 7104: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  2091. if (drupal_is_cli()) {
  2092. drupal_set_message(t('Update 7104: !count records to update.', array('!count' => $sandbox['max'])));
  2093. }
  2094. }
  2095. // Get a batch of records that need to be fixed.
  2096. $records = db_query_range("SELECT lid, location, context
  2097. FROM {locales_source}
  2098. WHERE textgroup = 'metatag'
  2099. AND context LIKE 'output:%:%:%:%'", 0, $limit);
  2100. // Update each of the records.
  2101. foreach ($records as $record) {
  2102. $old_location = '/metatag:output:([^:]*):([^:]*):([^:]*):([^:]*)/';
  2103. $new_location = 'metatag:output:$1:$2:$4';
  2104. $location = preg_replace($old_location, $new_location, $record->location);
  2105. $old_context = '/output:([^:]*):([^:]*):([^:]*):([^:]*)/';
  2106. $new_context = 'output:$1:$2:$4';
  2107. $context = preg_replace($old_context, $new_context, $record->context);
  2108. drupal_set_message($location);
  2109. drupal_set_message($context);
  2110. db_update('locales_source')
  2111. ->fields(array(
  2112. 'location' => $location,
  2113. 'context' => $context,
  2114. ))
  2115. ->condition('lid', $record->lid)
  2116. ->execute();
  2117. // Update our progress information.
  2118. $sandbox['progress']++;
  2119. }
  2120. // Set the "finished" status, to tell batch engine whether this function
  2121. // needs to run again. If you set a float, this will indicate the progress of
  2122. // the batch so the progress bar will update.
  2123. if ($sandbox['progress'] >= $sandbox['max']) {
  2124. $sandbox['#finished'] = TRUE;
  2125. }
  2126. else {
  2127. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  2128. }
  2129. if ($sandbox['#finished']) {
  2130. // Clear all caches so the fixed data will be reloaded.
  2131. cache_clear_all('*', 'cache_metatag', TRUE);
  2132. // hook_update_N() may optionally return a string which will be displayed
  2133. // to the user.
  2134. return t('Fixed the Metatag language values for @count nodes.', array('@count' => $sandbox['progress']));
  2135. }
  2136. }
  2137. /**
  2138. * Fix the output translation strings.
  2139. */
  2140. function metatag_update_7105() {
  2141. if (!module_exists('locale') || !db_table_exists('locales_source')) {
  2142. return t('No translations to fix as the locale system is not enabled.');
  2143. }
  2144. db_query("UPDATE {locales_source}
  2145. SET location = REPLACE(location, 'metatag:metatag:', 'metatag:output:'),
  2146. context = REPLACE(context, 'metatag:', 'output:')
  2147. WHERE textgroup = 'metatag'
  2148. AND location LIKE 'metatag:metatag:%'");
  2149. return t('Fixed the Metatag output strings.');
  2150. }
  2151. /**
  2152. * The output translation strings were renamed to something shorter, so rerun
  2153. * update 7105.
  2154. */
  2155. function metatag_update_7106() {
  2156. return metatag_update_7105();
  2157. }
  2158. /**
  2159. * Fix the global config translation strings.
  2160. */
  2161. function metatag_update_7107() {
  2162. if (!module_exists('locale') || !db_table_exists('locales_source')) {
  2163. return t('No translations to fix as the locale system is not enabled.');
  2164. }
  2165. db_query("UPDATE {locales_source}
  2166. SET location = REPLACE(location, 'metatag:global:', 'metatag:metatag_config:global:'),
  2167. context = REPLACE(context, 'global:', 'metatag_config:global:')
  2168. WHERE textgroup = 'metatag'
  2169. AND location LIKE 'metatag:global:%'");
  2170. return t('Fixed the Metatag global config string translations.');
  2171. }
  2172. /**
  2173. * Delete output translations if it's disabled.
  2174. */
  2175. function metatag_update_7108(&$sandbox) {
  2176. if (!module_exists('locale') || !db_table_exists('locales_source')) {
  2177. return t('No translations to fix as the locale system is not enabled.');
  2178. }
  2179. // If the output-translation option is enabled then don't delete anything.
  2180. if (variable_get('metatag_i18n_translate_output', FALSE)) {
  2181. return t("Metatag: Not deleting output translations because that option is enabled.");
  2182. }
  2183. $limit = 100;
  2184. // When ran through Drush it's Ok to process a larger number of objects at a
  2185. // time.
  2186. if (drupal_is_cli()) {
  2187. $limit = 500;
  2188. }
  2189. // Use the sandbox at your convenience to store the information needed to
  2190. // track progression between successive calls to the function.
  2191. if (!isset($sandbox['progress'])) {
  2192. // The count of records visited so far.
  2193. $sandbox['progress'] = 0;
  2194. $sandbox['max'] = db_query("SELECT COUNT(lid)
  2195. FROM {locales_source}
  2196. WHERE textgroup = 'metatag'
  2197. AND context LIKE 'output:%'")->fetchField();
  2198. // If there's no data, don't bother with the extra work.
  2199. if (empty($sandbox['max'])) {
  2200. watchdog('metatag', 'Update 7108: No nodes need the translation entity string fixed.', array(), WATCHDOG_INFO);
  2201. if (drupal_is_cli()) {
  2202. drupal_set_message(t('Update 7108: No nodes need the translation entity string fixed.'));
  2203. }
  2204. return t('No nodes need the Metatag language values fixed.');
  2205. }
  2206. // A place to store messages during the run.
  2207. $sandbox['messages'] = array();
  2208. // An initial record of the number of records to be updated.
  2209. watchdog('metatag', 'Update 7108: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  2210. if (drupal_is_cli()) {
  2211. drupal_set_message(t('Update 7108: !count records to update.', array('!count' => $sandbox['max'])));
  2212. }
  2213. }
  2214. // Get a batch of records that need to be fixed.
  2215. $records = db_query_range("SELECT lid
  2216. FROM {locales_source}
  2217. WHERE textgroup = 'metatag'
  2218. AND context LIKE 'output:%'", 0, $limit);
  2219. $lids = $records->fetchCol();
  2220. $count = count($lids);
  2221. // Delete records in the tables in reverse order, so that if the query fails
  2222. // and has to be reran it'll still find records. But it should be ok.
  2223. if (db_table_exists('i18n_string')) {
  2224. db_delete('i18n_string')
  2225. ->condition('lid', $lids, 'IN')
  2226. ->execute();
  2227. }
  2228. db_delete('locales_target')
  2229. ->condition('lid', $lids, 'IN')
  2230. ->execute();
  2231. db_delete('locales_source')
  2232. ->condition('lid', $lids, 'IN')
  2233. ->execute();
  2234. $sandbox['progress'] = $sandbox['progress'] + $count;
  2235. // Set the "finished" status, to tell batch engine whether this function
  2236. // needs to run again. If you set a float, this will indicate the progress of
  2237. // the batch so the progress bar will update.
  2238. if ($sandbox['progress'] >= $sandbox['max']) {
  2239. $sandbox['#finished'] = TRUE;
  2240. }
  2241. else {
  2242. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  2243. }
  2244. if ($sandbox['#finished']) {
  2245. // Clear all caches so the fixed data will be reloaded.
  2246. cache_clear_all('*', 'cache_metatag', TRUE);
  2247. // hook_update_N() may optionally return a string which will be displayed
  2248. // to the user.
  2249. return t('Deleted output translation if disabled for @count items.', array('@count' => $sandbox['progress']));
  2250. }
  2251. }
  2252. /**
  2253. * Rename the 'icon_any' meta tag to 'mask-icon' in the entity records.
  2254. */
  2255. function metatag_update_7109(&$sandbox) {
  2256. module_load_include('install', 'metatag');
  2257. $old_tag = 'icon_any';
  2258. $new_tag = 'mask-icon';
  2259. return metatag_update_replace_entity_tag($sandbox, $old_tag, $new_tag);
  2260. }
  2261. /**
  2262. * Rename the 'icon_any' meta tag to 'mask-icon' in the configs.
  2263. */
  2264. function metatag_update_7110() {
  2265. module_load_include('install', 'metatag');
  2266. $old_tag = 'icon_any';
  2267. $new_tag = 'mask-icon';
  2268. return metatag_update_replace_config_tag($old_tag, $new_tag);
  2269. }
  2270. /**
  2271. * Remove the "metatag_ui" record from the {system} table.
  2272. */
  2273. function metatag_update_7111() {
  2274. db_delete('system')
  2275. ->condition('name', 'metatag_ui')
  2276. ->execute();
  2277. }
  2278. /**
  2279. * The Publisher meta tag is now part of the Google Plus submodule.
  2280. */
  2281. function metatag_update_7112() {
  2282. cache_clear_all('*', 'cache_metatag', TRUE);
  2283. drupal_set_message(t('The Publisher meta tag is now part of the Google Plus submodule.'));
  2284. }
  2285. /**
  2286. * Clear all metatag output caches if output caching is disabled.
  2287. */
  2288. function metatag_update_7113() {
  2289. if (variable_get('metatag_cache_output', TRUE) == FALSE) {
  2290. cache_clear_all('output:', 'cache_metatag', TRUE);
  2291. }
  2292. }
  2293. /**
  2294. * Disable output caching.
  2295. */
  2296. function metatag_update_7114() {
  2297. variable_del('metatag_cache_output');
  2298. drupal_set_message(t('Note: Output caching of meta tags has been disabled, it may be reenabled from the Metatag settings page if needed.'));
  2299. }
  2300. /**
  2301. * Clear all metatag output caches, it will be rebuild if needed.
  2302. */
  2303. function metatag_update_7115() {
  2304. cache_clear_all('output:', 'cache_metatag', TRUE);
  2305. }