metatag.install 90 KB

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