MysqlUpdater.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. <?php
  2. /**
  3. * MySQL-specific updater.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Deployment
  22. */
  23. use Wikimedia\Rdbms\MySQLField;
  24. use Wikimedia\Rdbms\IDatabase;
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Mysql update list and mysql-specific update functions.
  28. *
  29. * @ingroup Deployment
  30. * @since 1.17
  31. * @property Wikimedia\Rdbms\DatabaseMysqlBase $db
  32. */
  33. class MysqlUpdater extends DatabaseUpdater {
  34. protected function getCoreUpdateList() {
  35. return [
  36. [ 'disableContentHandlerUseDB' ],
  37. // 1.2
  38. [ 'addField', 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ],
  39. [ 'addField', 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ],
  40. [ 'doInterwikiUpdate' ],
  41. [ 'doIndexUpdate' ],
  42. [ 'addField', 'recentchanges', 'rc_type', 'patch-rc_type.sql' ],
  43. [ 'addIndex', 'recentchanges', 'new_name_timestamp', 'patch-rc-newindex.sql' ],
  44. // 1.3
  45. [ 'addField', 'user', 'user_real_name', 'patch-user-realname.sql' ],
  46. [ 'addTable', 'querycache', 'patch-querycache.sql' ],
  47. [ 'addTable', 'objectcache', 'patch-objectcache.sql' ],
  48. [ 'addTable', 'categorylinks', 'patch-categorylinks.sql' ],
  49. [ 'doOldLinksUpdate' ],
  50. [ 'doFixAncientImagelinks' ],
  51. [ 'addField', 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ],
  52. // 1.4
  53. [ 'addIndex', 'image', 'PRIMARY', 'patch-image_name_primary.sql' ],
  54. [ 'addField', 'recentchanges', 'rc_id', 'patch-rc_id.sql' ],
  55. [ 'addField', 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ],
  56. [ 'addTable', 'logging', 'patch-logging.sql' ],
  57. [ 'addField', 'user', 'user_token', 'patch-user_token.sql' ],
  58. [ 'addField', 'watchlist', 'wl_notificationtimestamp', 'patch-email-notification.sql' ],
  59. [ 'doWatchlistUpdate' ],
  60. [ 'dropField', 'user', 'user_emailauthenticationtimestamp',
  61. 'patch-email-authentication.sql' ],
  62. // 1.5
  63. [ 'doSchemaRestructuring' ],
  64. [ 'addField', 'logging', 'log_params', 'patch-log_params.sql' ],
  65. [ 'checkBin', 'logging', 'log_title', 'patch-logging-title.sql', ],
  66. [ 'addField', 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ],
  67. [ 'addField', 'page', 'page_len', 'patch-page_len.sql' ],
  68. [ 'dropField', 'revision', 'inverse_timestamp', 'patch-inverse_timestamp.sql' ],
  69. [ 'addField', 'revision', 'rev_text_id', 'patch-rev_text_id.sql' ],
  70. [ 'addField', 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ],
  71. [ 'addField', 'image', 'img_width', 'patch-img_width.sql' ],
  72. [ 'addField', 'image', 'img_metadata', 'patch-img_metadata.sql' ],
  73. [ 'addField', 'user', 'user_email_token', 'patch-user_email_token.sql' ],
  74. [ 'addField', 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ],
  75. [ 'doNamespaceSize' ],
  76. [ 'addField', 'image', 'img_media_type', 'patch-img_media_type.sql' ],
  77. [ 'doPagelinksUpdate' ],
  78. [ 'dropField', 'image', 'img_type', 'patch-drop_img_type.sql' ],
  79. [ 'doUserUniqueUpdate' ],
  80. [ 'doUserGroupsUpdate' ],
  81. [ 'addField', 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ],
  82. [ 'addTable', 'user_newtalk', 'patch-usernewtalk.sql' ],
  83. [ 'addField', 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ],
  84. // 1.6
  85. [ 'doWatchlistNull' ],
  86. [ 'addIndex', 'logging', 'times', 'patch-logging-times-index.sql' ],
  87. [ 'addField', 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ],
  88. [ 'doPageRandomUpdate' ],
  89. [ 'addField', 'user', 'user_registration', 'patch-user_registration.sql' ],
  90. [ 'doTemplatelinksUpdate' ],
  91. [ 'addTable', 'externallinks', 'patch-externallinks.sql' ],
  92. [ 'addTable', 'job', 'patch-job.sql' ],
  93. [ 'addField', 'site_stats', 'ss_images', 'patch-ss_images.sql' ],
  94. [ 'addTable', 'langlinks', 'patch-langlinks.sql' ],
  95. [ 'addTable', 'querycache_info', 'patch-querycacheinfo.sql' ],
  96. [ 'addTable', 'filearchive', 'patch-filearchive.sql' ],
  97. [ 'addField', 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ],
  98. [ 'ifNoActorTable', 'addIndex', 'recentchanges', 'rc_ns_usertext',
  99. 'patch-recentchanges-utindex.sql' ],
  100. [ 'ifNoActorTable', 'addIndex', 'recentchanges', 'rc_user_text',
  101. 'patch-rc_user_text-index.sql' ],
  102. // 1.9
  103. [ 'addField', 'user', 'user_newpass_time', 'patch-user_newpass_time.sql' ],
  104. [ 'addTable', 'redirect', 'patch-redirect.sql' ],
  105. [ 'addTable', 'querycachetwo', 'patch-querycachetwo.sql' ],
  106. [ 'addField', 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ],
  107. [ 'doBacklinkingIndicesUpdate' ],
  108. [ 'addField', 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ],
  109. [ 'addField', 'user', 'user_editcount', 'patch-user_editcount.sql' ],
  110. // 1.10
  111. [ 'doRestrictionsUpdate' ],
  112. [ 'addField', 'logging', 'log_id', 'patch-log_id.sql' ],
  113. [ 'addField', 'revision', 'rev_parent_id', 'patch-rev_parent_id.sql' ],
  114. [ 'addField', 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ],
  115. [ 'addField', 'revision', 'rev_len', 'patch-rev_len.sql' ],
  116. [ 'addField', 'recentchanges', 'rc_deleted', 'patch-rc_deleted.sql' ],
  117. [ 'addField', 'logging', 'log_deleted', 'patch-log_deleted.sql' ],
  118. [ 'addField', 'archive', 'ar_deleted', 'patch-ar_deleted.sql' ],
  119. [ 'addField', 'ipblocks', 'ipb_deleted', 'patch-ipb_deleted.sql' ],
  120. [ 'addField', 'filearchive', 'fa_deleted', 'patch-fa_deleted.sql' ],
  121. [ 'addField', 'archive', 'ar_len', 'patch-ar_len.sql' ],
  122. // 1.11
  123. [ 'addField', 'ipblocks', 'ipb_block_email', 'patch-ipb_emailban.sql' ],
  124. [ 'doCategorylinksIndicesUpdate' ],
  125. [ 'addField', 'oldimage', 'oi_metadata', 'patch-oi_metadata.sql' ],
  126. [ 'ifNoActorTable', 'addIndex', 'archive', 'usertext_timestamp',
  127. 'patch-archive-user-index.sql' ],
  128. [ 'ifNoActorTable', 'addIndex', 'image', 'img_usertext_timestamp',
  129. 'patch-image-user-index.sql' ],
  130. [ 'ifNoActorTable', 'addIndex', 'oldimage', 'oi_usertext_timestamp',
  131. 'patch-oldimage-user-index.sql' ],
  132. [ 'addField', 'archive', 'ar_page_id', 'patch-archive-page_id.sql' ],
  133. [ 'addField', 'image', 'img_sha1', 'patch-img_sha1.sql' ],
  134. // 1.12
  135. [ 'addTable', 'protected_titles', 'patch-protected_titles.sql' ],
  136. // 1.13
  137. [ 'ifNoActorTable', 'addField', 'ipblocks', 'ipb_by_text', 'patch-ipb_by_text.sql' ],
  138. [ 'addTable', 'page_props', 'patch-page_props.sql' ],
  139. [ 'addTable', 'updatelog', 'patch-updatelog.sql' ],
  140. [ 'addTable', 'category', 'patch-category.sql' ],
  141. [ 'doCategoryPopulation' ],
  142. [ 'addField', 'archive', 'ar_parent_id', 'patch-ar_parent_id.sql' ],
  143. [ 'addField', 'user_newtalk', 'user_last_timestamp', 'patch-user_last_timestamp.sql' ],
  144. [ 'doPopulateParentId' ],
  145. [ 'checkBin', 'protected_titles', 'pt_title', 'patch-pt_title-encoding.sql', ],
  146. [ 'doMaybeProfilingMemoryUpdate' ],
  147. [ 'ifNoActorTable', 'doFilearchiveIndicesUpdate' ],
  148. // 1.14
  149. [ 'addField', 'site_stats', 'ss_active_users', 'patch-ss_active_users.sql' ],
  150. [ 'doActiveUsersInit' ],
  151. [ 'addField', 'ipblocks', 'ipb_allow_usertalk', 'patch-ipb_allow_usertalk.sql' ],
  152. // 1.15
  153. [ 'addTable', 'change_tag', 'patch-change_tag.sql' ],
  154. // 1.16
  155. [ 'addTable', 'user_properties', 'patch-user_properties.sql' ],
  156. [ 'addTable', 'log_search', 'patch-log_search.sql' ],
  157. [ 'ifNoActorTable', 'addField', 'logging', 'log_user_text', 'patch-log_user_text.sql' ],
  158. # listed separately from the previous update because 1.16 was released without this update
  159. [ 'ifNoActorTable', 'doLogUsertextPopulation' ],
  160. [ 'doLogSearchPopulation' ],
  161. [ 'addTable', 'l10n_cache', 'patch-l10n_cache.sql' ],
  162. [ 'dropIndex', 'change_tag', 'ct_rc_id', 'patch-change_tag-indexes.sql' ],
  163. [ 'addField', 'redirect', 'rd_interwiki', 'patch-rd_interwiki.sql' ],
  164. [ 'doUpdateMimeMinorField' ],
  165. // 1.17
  166. [ 'addTable', 'iwlinks', 'patch-iwlinks.sql' ],
  167. [ 'addIndex', 'iwlinks', 'iwl_prefix_title_from', 'patch-rename-iwl_prefix.sql' ],
  168. [ 'addField', 'updatelog', 'ul_value', 'patch-ul_value.sql' ],
  169. [ 'addField', 'interwiki', 'iw_api', 'patch-iw_api_and_wikiid.sql' ],
  170. [ 'dropIndex', 'iwlinks', 'iwl_prefix', 'patch-kill-iwl_prefix.sql' ],
  171. [ 'addField', 'categorylinks', 'cl_collation', 'patch-categorylinks-better-collation.sql' ],
  172. [ 'doClFieldsUpdate' ],
  173. [ 'addTable', 'module_deps', 'patch-module_deps.sql' ],
  174. [ 'dropIndex', 'archive', 'ar_page_revid', 'patch-archive_kill_ar_page_revid.sql' ],
  175. [ 'addIndexIfNoneExist',
  176. 'archive', [ 'ar_revid', 'ar_revid_uniq' ], 'patch-archive_ar_revid.sql' ],
  177. [ 'doLangLinksLengthUpdate' ],
  178. // 1.18
  179. [ 'doUserNewTalkTimestampNotNull' ],
  180. [ 'addIndex', 'user', 'user_email', 'patch-user_email_index.sql' ],
  181. [ 'modifyField', 'user_properties', 'up_property', 'patch-up_property.sql' ],
  182. [ 'addTable', 'uploadstash', 'patch-uploadstash.sql' ],
  183. [ 'addTable', 'user_former_groups', 'patch-user_former_groups.sql' ],
  184. // 1.19
  185. [ 'addField', 'revision', 'rev_sha1', 'patch-rev_sha1.sql' ],
  186. [ 'doMigrateUserOptions' ],
  187. [ 'dropField', 'user', 'user_options', 'patch-drop-user_options.sql' ],
  188. [ 'addField', 'archive', 'ar_sha1', 'patch-ar_sha1.sql' ],
  189. [ 'addIndex', 'page', 'page_redirect_namespace_len',
  190. 'patch-page_redirect_namespace_len.sql' ],
  191. [ 'addField', 'uploadstash', 'us_chunk_inx', 'patch-uploadstash_chunk.sql' ],
  192. [ 'addfield', 'job', 'job_timestamp', 'patch-jobs-add-timestamp.sql' ],
  193. // 1.20
  194. [ 'addIndex', 'revision', 'page_user_timestamp', 'patch-revision-user-page-index.sql' ],
  195. [ 'addField', 'ipblocks', 'ipb_parent_block_id', 'patch-ipb-parent-block-id.sql' ],
  196. [ 'addIndex', 'ipblocks', 'ipb_parent_block_id', 'patch-ipb-parent-block-id-index.sql' ],
  197. [ 'dropField', 'category', 'cat_hidden', 'patch-cat_hidden.sql' ],
  198. // 1.21
  199. [ 'addField', 'revision', 'rev_content_format', 'patch-revision-rev_content_format.sql' ],
  200. [ 'addField', 'revision', 'rev_content_model', 'patch-revision-rev_content_model.sql' ],
  201. [ 'addField', 'archive', 'ar_content_format', 'patch-archive-ar_content_format.sql' ],
  202. [ 'addField', 'archive', 'ar_content_model', 'patch-archive-ar_content_model.sql' ],
  203. [ 'addField', 'page', 'page_content_model', 'patch-page-page_content_model.sql' ],
  204. [ 'enableContentHandlerUseDB' ],
  205. [ 'dropField', 'site_stats', 'ss_admins', 'patch-drop-ss_admins.sql' ],
  206. [ 'dropField', 'recentchanges', 'rc_moved_to_title', 'patch-rc_moved.sql' ],
  207. [ 'addTable', 'sites', 'patch-sites.sql' ],
  208. [ 'addField', 'filearchive', 'fa_sha1', 'patch-fa_sha1.sql' ],
  209. [ 'addField', 'job', 'job_token', 'patch-job_token.sql' ],
  210. [ 'addField', 'job', 'job_attempts', 'patch-job_attempts.sql' ],
  211. [ 'doEnableProfiling' ],
  212. [ 'addField', 'uploadstash', 'us_props', 'patch-uploadstash-us_props.sql' ],
  213. [ 'modifyField', 'user_groups', 'ug_group', 'patch-ug_group-length-increase-255.sql' ],
  214. [ 'modifyField', 'user_former_groups', 'ufg_group',
  215. 'patch-ufg_group-length-increase-255.sql' ],
  216. [ 'addIndex', 'page_props', 'pp_propname_page',
  217. 'patch-page_props-propname-page-index.sql' ],
  218. [ 'addIndex', 'image', 'img_media_mime', 'patch-img_media_mime-index.sql' ],
  219. // 1.22
  220. [ 'doIwlinksIndexNonUnique' ],
  221. [ 'addIndex', 'iwlinks', 'iwl_prefix_from_title',
  222. 'patch-iwlinks-from-title-index.sql' ],
  223. [ 'addField', 'archive', 'ar_id', 'patch-archive-ar_id.sql' ],
  224. [ 'addField', 'externallinks', 'el_id', 'patch-externallinks-el_id.sql' ],
  225. // 1.23
  226. [ 'addField', 'recentchanges', 'rc_source', 'patch-rc_source.sql' ],
  227. [ 'ifNoActorTable', 'addIndex', 'logging', 'log_user_text_type_time',
  228. 'patch-logging_user_text_type_time_index.sql' ],
  229. [ 'ifNoActorTable', 'addIndex', 'logging', 'log_user_text_time',
  230. 'patch-logging_user_text_time_index.sql' ],
  231. [ 'addField', 'page', 'page_links_updated', 'patch-page_links_updated.sql' ],
  232. [ 'addField', 'user', 'user_password_expires', 'patch-user_password_expire.sql' ],
  233. // 1.24
  234. [ 'addField', 'page_props', 'pp_sortkey', 'patch-pp_sortkey.sql' ],
  235. [ 'dropField', 'recentchanges', 'rc_cur_time', 'patch-drop-rc_cur_time.sql' ],
  236. [ 'addIndex', 'watchlist', 'wl_user_notificationtimestamp',
  237. 'patch-watchlist-user-notificationtimestamp-index.sql' ],
  238. [ 'addField', 'page', 'page_lang', 'patch-page_lang.sql' ],
  239. [ 'addField', 'pagelinks', 'pl_from_namespace', 'patch-pl_from_namespace.sql' ],
  240. [ 'addField', 'templatelinks', 'tl_from_namespace', 'patch-tl_from_namespace.sql' ],
  241. [ 'addField', 'imagelinks', 'il_from_namespace', 'patch-il_from_namespace.sql' ],
  242. [ 'modifyField', 'image', 'img_major_mime',
  243. 'patch-img_major_mime-chemical.sql' ],
  244. [ 'modifyField', 'oldimage', 'oi_major_mime',
  245. 'patch-oi_major_mime-chemical.sql' ],
  246. [ 'modifyField', 'filearchive', 'fa_major_mime',
  247. 'patch-fa_major_mime-chemical.sql' ],
  248. // 1.25
  249. // note this patch covers other _comment and _description fields too
  250. [ 'doExtendCommentLengths' ],
  251. // 1.26
  252. [ 'dropTable', 'hitcounter' ],
  253. [ 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ],
  254. [ 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ],
  255. // 1.27
  256. [ 'dropTable', 'msg_resource_links' ],
  257. [ 'dropTable', 'msg_resource' ],
  258. [ 'addTable', 'bot_passwords', 'patch-bot_passwords.sql' ],
  259. [ 'addField', 'watchlist', 'wl_id', 'patch-watchlist-wl_id.sql' ],
  260. [ 'dropIndex', 'categorylinks', 'cl_collation', 'patch-kill-cl_collation_index.sql' ],
  261. [ 'addIndex', 'categorylinks', 'cl_collation_ext',
  262. 'patch-add-cl_collation_ext_index.sql' ],
  263. [ 'doCollationUpdate' ],
  264. // 1.28
  265. [ 'addIndex', 'recentchanges', 'rc_name_type_patrolled_timestamp',
  266. 'patch-add-rc_name_type_patrolled_timestamp_index.sql' ],
  267. [ 'doRevisionPageRevIndexNonUnique' ],
  268. [ 'doNonUniquePlTlIl' ],
  269. [ 'addField', 'change_tag', 'ct_id', 'patch-change_tag-ct_id.sql' ],
  270. [ 'modifyField', 'recentchanges', 'rc_ip', 'patch-rc_ip_modify.sql' ],
  271. [ 'ifNoActorTable', 'addIndex', 'archive', 'usertext_timestamp',
  272. 'patch-rename-ar_usertext_timestamp.sql' ],
  273. // 1.29
  274. [ 'addField', 'externallinks', 'el_index_60', 'patch-externallinks-el_index_60.sql' ],
  275. [ 'dropIndex', 'user_groups', 'ug_user_group', 'patch-user_groups-primary-key.sql' ],
  276. [ 'addField', 'user_groups', 'ug_expiry', 'patch-user_groups-ug_expiry.sql' ],
  277. [ 'ifNoActorTable', 'addIndex', 'image', 'img_user_timestamp', 'patch-image-user-index-2.sql' ],
  278. // 1.30
  279. [ 'modifyField', 'image', 'img_media_type', 'patch-add-3d.sql' ],
  280. [ 'addTable', 'ip_changes', 'patch-ip_changes.sql' ],
  281. [ 'renameIndex', 'categorylinks', 'cl_from', 'PRIMARY', false,
  282. 'patch-categorylinks-fix-pk.sql' ],
  283. [ 'renameIndex', 'templatelinks', 'tl_from', 'PRIMARY', false,
  284. 'patch-templatelinks-fix-pk.sql' ],
  285. [ 'renameIndex', 'pagelinks', 'pl_from', 'PRIMARY', false, 'patch-pagelinks-fix-pk.sql' ],
  286. [ 'renameIndex', 'text', 'old_id', 'PRIMARY', false, 'patch-text-fix-pk.sql' ],
  287. [ 'renameIndex', 'imagelinks', 'il_from', 'PRIMARY', false, 'patch-imagelinks-fix-pk.sql' ],
  288. [ 'renameIndex', 'iwlinks', 'iwl_from', 'PRIMARY', false, 'patch-iwlinks-fix-pk.sql' ],
  289. [ 'renameIndex', 'langlinks', 'll_from', 'PRIMARY', false, 'patch-langlinks-fix-pk.sql' ],
  290. [ 'renameIndex', 'log_search', 'ls_field_val', 'PRIMARY', false, 'patch-log_search-fix-pk.sql' ],
  291. [ 'renameIndex', 'module_deps', 'md_module_skin', 'PRIMARY', false,
  292. 'patch-module_deps-fix-pk.sql' ],
  293. [ 'renameIndex', 'objectcache', 'keyname', 'PRIMARY', false, 'patch-objectcache-fix-pk.sql' ],
  294. [ 'renameIndex', 'querycache_info', 'qci_type', 'PRIMARY', false,
  295. 'patch-querycache_info-fix-pk.sql' ],
  296. [ 'renameIndex', 'site_stats', 'ss_row_id', 'PRIMARY', false, 'patch-site_stats-fix-pk.sql' ],
  297. [ 'renameIndex', 'user_former_groups', 'ufg_user_group', 'PRIMARY', false,
  298. 'patch-user_former_groups-fix-pk.sql' ],
  299. [ 'renameIndex', 'user_properties', 'user_properties_user_property', 'PRIMARY', false,
  300. 'patch-user_properties-fix-pk.sql' ],
  301. [ 'addTable', 'comment', 'patch-comment-table.sql' ],
  302. [ 'addTable', 'revision_comment_temp', 'patch-revision_comment_temp-table.sql' ],
  303. // image_comment_temp is no longer needed when upgrading to MW 1.31 or newer,
  304. // as it is dropped later in the update process as part of 'migrateImageCommentTemp'.
  305. // File kept on disk and the updater entry here for historical purposes.
  306. // [ 'addTable', 'image_comment_temp', 'patch-image_comment_temp-table.sql' ],
  307. [ 'addField', 'archive', 'ar_comment_id', 'patch-archive-ar_comment_id.sql' ],
  308. [ 'addField', 'filearchive', 'fa_description_id', 'patch-filearchive-fa_description_id.sql' ],
  309. [ 'modifyField', 'image', 'img_description', 'patch-image-img_description-default.sql' ],
  310. [ 'addField', 'ipblocks', 'ipb_reason_id', 'patch-ipblocks-ipb_reason_id.sql' ],
  311. [ 'addField', 'logging', 'log_comment_id', 'patch-logging-log_comment_id.sql' ],
  312. [ 'addField', 'oldimage', 'oi_description_id', 'patch-oldimage-oi_description_id.sql' ],
  313. [ 'addField', 'protected_titles', 'pt_reason_id', 'patch-protected_titles-pt_reason_id.sql' ],
  314. [ 'addField', 'recentchanges', 'rc_comment_id', 'patch-recentchanges-rc_comment_id.sql' ],
  315. [ 'modifyField', 'revision', 'rev_comment', 'patch-revision-rev_comment-default.sql' ],
  316. // This field was added in 1.31, but is put here so it can be used by 'migrateComments'
  317. [ 'addField', 'image', 'img_description_id', 'patch-image-img_description_id.sql' ],
  318. [ 'migrateComments' ],
  319. [ 'renameIndex', 'l10n_cache', 'lc_lang_key', 'PRIMARY', false,
  320. 'patch-l10n_cache-primary-key.sql' ],
  321. [ 'doUnsignedSyncronisation' ],
  322. // 1.31
  323. [ 'addTable', 'slots', 'patch-slots.sql' ],
  324. [ 'addField', 'slots', 'slot_origin', 'patch-slot-origin.sql' ],
  325. [ 'addTable', 'content', 'patch-content.sql' ],
  326. [ 'addTable', 'slot_roles', 'patch-slot_roles.sql' ],
  327. [ 'addTable', 'content_models', 'patch-content_models.sql' ],
  328. [ 'migrateArchiveText' ],
  329. [ 'addTable', 'actor', 'patch-actor-table.sql' ],
  330. [ 'addTable', 'revision_actor_temp', 'patch-revision_actor_temp-table.sql' ],
  331. [ 'addField', 'archive', 'ar_actor', 'patch-archive-ar_actor.sql' ],
  332. [ 'addField', 'ipblocks', 'ipb_by_actor', 'patch-ipblocks-ipb_by_actor.sql' ],
  333. [ 'addField', 'image', 'img_actor', 'patch-image-img_actor.sql' ],
  334. [ 'addField', 'oldimage', 'oi_actor', 'patch-oldimage-oi_actor.sql' ],
  335. [ 'addField', 'filearchive', 'fa_actor', 'patch-filearchive-fa_actor.sql' ],
  336. [ 'addField', 'recentchanges', 'rc_actor', 'patch-recentchanges-rc_actor.sql' ],
  337. [ 'addField', 'logging', 'log_actor', 'patch-logging-log_actor.sql' ],
  338. [ 'migrateActors' ],
  339. [ 'modifyField', 'revision', 'rev_text_id', 'patch-rev_text_id-default.sql' ],
  340. [ 'modifyTable', 'site_stats', 'patch-site_stats-modify.sql' ],
  341. [ 'populateArchiveRevId' ],
  342. [ 'addIndex', 'recentchanges', 'rc_namespace_title_timestamp',
  343. 'patch-recentchanges-nttindex.sql' ],
  344. // 1.32
  345. [ 'addTable', 'change_tag_def', 'patch-change_tag_def.sql' ],
  346. [ 'populateExternallinksIndex60' ],
  347. [ 'modifyfield', 'externallinks', 'el_index_60',
  348. 'patch-externallinks-el_index_60-drop-default.sql' ],
  349. [ 'runMaintenance', DeduplicateArchiveRevId::class, 'maintenance/deduplicateArchiveRevId.php' ],
  350. [ 'addField', 'change_tag', 'ct_tag_id', 'patch-change_tag-tag_id.sql' ],
  351. [ 'addIndex', 'archive', 'ar_revid_uniq', 'patch-archive-ar_rev_id-unique.sql' ],
  352. [ 'populateContentTables' ],
  353. [ 'addIndex', 'logging', 'log_type_action', 'patch-logging-log-type-action-index.sql' ],
  354. [ 'dropIndex', 'logging', 'type_action', 'patch-logging-drop-type-action-index.sql' ],
  355. [ 'renameIndex', 'interwiki', 'iw_prefix', 'PRIMARY', false, 'patch-interwiki-fix-pk.sql' ],
  356. [ 'renameIndex', 'page_props', 'pp_page_propname', 'PRIMARY', false,
  357. 'patch-page_props-fix-pk.sql' ],
  358. [ 'renameIndex', 'protected_titles', 'pt_namespace_title', 'PRIMARY', false,
  359. 'patch-protected_titles-fix-pk.sql' ],
  360. [ 'renameIndex', 'site_identifiers', 'site_ids_type', 'PRIMARY', false,
  361. 'patch-site_identifiers-fix-pk.sql' ],
  362. [ 'addIndex', 'recentchanges', 'rc_this_oldid', 'patch-recentchanges-rc_this_oldid-index.sql' ],
  363. [ 'dropTable', 'transcache' ],
  364. [ 'runMaintenance', PopulateChangeTagDef::class, 'maintenance/populateChangeTagDef.php' ],
  365. [ 'addIndex', 'change_tag', 'change_tag_rc_tag_id',
  366. 'patch-change_tag-change_tag_rc_tag_id.sql' ],
  367. [ 'addField', 'ipblocks', 'ipb_sitewide', 'patch-ipb_sitewide.sql' ],
  368. [ 'addTable', 'ipblocks_restrictions', 'patch-ipblocks_restrictions-table.sql' ],
  369. [ 'migrateImageCommentTemp' ],
  370. // 1,33
  371. [ 'dropField', 'change_tag', 'ct_tag', 'patch-drop-ct_tag.sql' ],
  372. [ 'dropTable', 'valid_tag' ],
  373. [ 'dropTable', 'tag_summary' ],
  374. [ 'dropField', 'protected_titles', 'pt_reason', 'patch-drop-comment-fields.sql' ],
  375. [ 'modifyTable', 'job', 'patch-job-params-mediumblob.sql' ],
  376. // 1.34
  377. [ 'dropIndex', 'archive', 'ar_usertext_timestamp',
  378. 'patch-drop-archive-ar_usertext_timestamp.sql' ],
  379. [ 'dropIndex', 'archive', 'usertext_timestamp', 'patch-drop-archive-usertext_timestamp.sql' ],
  380. [ 'dropField', 'logging', 'log_user', 'patch-drop-user-fields.sql' ],
  381. [ 'addIndex', 'user_newtalk', 'un_user_ip', 'patch-rename-mysql-user_newtalk-indexes.sql' ],
  382. ];
  383. }
  384. /**
  385. * 1.4 betas were missing the 'binary' marker from logging.log_title,
  386. * which causes a collation mismatch error on joins in MySQL 4.1.
  387. *
  388. * @param string $table Table name
  389. * @param string $field Field name to check
  390. * @param string $patchFile Path to the patch to correct the field
  391. * @return bool
  392. */
  393. protected function checkBin( $table, $field, $patchFile ) {
  394. if ( !$this->doTable( $table ) ) {
  395. return true;
  396. }
  397. /** @var MySQLField $fieldInfo */
  398. $fieldInfo = $this->db->fieldInfo( $table, $field );
  399. if ( $fieldInfo->isBinary() ) {
  400. $this->output( "...$table table has correct $field encoding.\n" );
  401. } else {
  402. $this->applyPatch( $patchFile, false, "Fixing $field encoding on $table table" );
  403. }
  404. }
  405. /**
  406. * Check whether an index contain a field
  407. *
  408. * @param string $table Table name
  409. * @param string $index Index name to check
  410. * @param string $field Field that should be in the index
  411. * @return bool
  412. */
  413. protected function indexHasField( $table, $index, $field ) {
  414. if ( !$this->doTable( $table ) ) {
  415. return true;
  416. }
  417. $info = $this->db->indexInfo( $table, $index, __METHOD__ );
  418. if ( $info ) {
  419. foreach ( $info as $row ) {
  420. if ( $row->Column_name == $field ) {
  421. $this->output( "...index $index on table $table includes field $field.\n" );
  422. return true;
  423. }
  424. }
  425. }
  426. $this->output( "...index $index on table $table has no field $field; added.\n" );
  427. return false;
  428. }
  429. /**
  430. * Check that interwiki table exists; if it doesn't source it
  431. */
  432. protected function doInterwikiUpdate() {
  433. global $IP;
  434. if ( !$this->doTable( 'interwiki' ) ) {
  435. return;
  436. }
  437. if ( $this->db->tableExists( "interwiki", __METHOD__ ) ) {
  438. $this->output( "...already have interwiki table\n" );
  439. return;
  440. }
  441. $this->applyPatch( 'patch-interwiki.sql', false, 'Creating interwiki table' );
  442. $this->applyPatch(
  443. "$IP/maintenance/interwiki.sql",
  444. true,
  445. 'Adding default interwiki definitions'
  446. );
  447. }
  448. /**
  449. * Check that proper indexes are in place
  450. */
  451. protected function doIndexUpdate() {
  452. $meta = $this->db->fieldInfo( 'recentchanges', 'rc_timestamp' );
  453. if ( $meta === false ) {
  454. throw new MWException( 'Missing rc_timestamp field of recentchanges table. Should not happen.' );
  455. }
  456. if ( $meta->isMultipleKey() ) {
  457. $this->output( "...indexes seem up to 20031107 standards.\n" );
  458. return;
  459. }
  460. $this->applyPatch( 'patch-indexes.sql', true, "Updating indexes to 20031107" );
  461. }
  462. protected function doOldLinksUpdate() {
  463. $cl = $this->maintenance->runChild( ConvertLinks::class );
  464. $cl->execute();
  465. }
  466. protected function doFixAncientImagelinks() {
  467. $info = $this->db->fieldInfo( 'imagelinks', 'il_from' );
  468. if ( !$info || $info->type() !== 'string' ) {
  469. $this->output( "...il_from OK\n" );
  470. return;
  471. }
  472. $applied = $this->applyPatch(
  473. 'patch-fix-il_from.sql',
  474. false,
  475. 'Fixing ancient broken imagelinks table.'
  476. );
  477. if ( $applied ) {
  478. $this->output( "NOTE: you will have to run maintenance/refreshLinks.php after this." );
  479. }
  480. }
  481. /**
  482. * Check if we need to add talk page rows to the watchlist
  483. */
  484. function doWatchlistUpdate() {
  485. $talk = $this->db->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', __METHOD__ );
  486. $nontalk = $this->db->selectField(
  487. 'watchlist',
  488. 'count(*)',
  489. 'NOT (wl_namespace & 1)',
  490. __METHOD__
  491. );
  492. if ( $talk == $nontalk ) {
  493. $this->output( "...watchlist talk page rows already present.\n" );
  494. return;
  495. }
  496. $insertOpts = [ 'IGNORE' ];
  497. $selectOpts = [];
  498. // If wl_id exists, make insertSelect() more replication-safe by
  499. // ordering on that column. If not, hint that it should be safe anyway.
  500. if ( $this->db->fieldExists( 'watchlist', 'wl_id', __METHOD__ ) ) {
  501. $selectOpts['ORDER BY'] = 'wl_id';
  502. } else {
  503. $insertOpts[] = 'NO_AUTO_COLUMNS';
  504. }
  505. $this->output( "Adding missing watchlist talk page rows... " );
  506. $this->db->insertSelect( 'watchlist', 'watchlist',
  507. [
  508. 'wl_user' => 'wl_user',
  509. 'wl_namespace' => 'wl_namespace | 1',
  510. 'wl_title' => 'wl_title',
  511. 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
  512. ], [ 'NOT (wl_namespace & 1)' ], __METHOD__, $insertOpts, $selectOpts );
  513. $this->output( "done.\n" );
  514. $this->output( "Adding missing watchlist subject page rows... " );
  515. $this->db->insertSelect( 'watchlist', 'watchlist',
  516. [
  517. 'wl_user' => 'wl_user',
  518. 'wl_namespace' => 'wl_namespace & ~1',
  519. 'wl_title' => 'wl_title',
  520. 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
  521. ], [ 'wl_namespace & 1' ], __METHOD__, $insertOpts, $selectOpts );
  522. $this->output( "done.\n" );
  523. }
  524. function doSchemaRestructuring() {
  525. if ( $this->db->tableExists( 'page', __METHOD__ ) ) {
  526. $this->output( "...page table already exists.\n" );
  527. return;
  528. }
  529. $this->output( "...converting from cur/old to page/revision/text DB structure.\n" );
  530. $this->output( wfTimestamp( TS_DB ) );
  531. $this->output( "......checking for duplicate entries.\n" );
  532. list( $cur, $old, $page, $revision, $text ) = $this->db->tableNamesN(
  533. 'cur',
  534. 'old',
  535. 'page',
  536. 'revision',
  537. 'text'
  538. );
  539. $rows = $this->db->query( "
  540. SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
  541. FROM $cur
  542. GROUP BY cur_title, cur_namespace
  543. HAVING c>1",
  544. __METHOD__
  545. );
  546. if ( $rows->numRows() > 0 ) {
  547. $this->output( wfTimestamp( TS_DB ) );
  548. $this->output( "......<b>Found duplicate entries</b>\n" );
  549. $this->output( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
  550. $duplicate = [];
  551. foreach ( $rows as $row ) {
  552. if ( !isset( $duplicate[$row->cur_namespace] ) ) {
  553. $duplicate[$row->cur_namespace] = [];
  554. }
  555. $duplicate[$row->cur_namespace][] = $row->cur_title;
  556. $this->output( sprintf(
  557. " %-60s %3s %5s\n",
  558. $row->cur_title, $row->cur_namespace,
  559. $row->c
  560. ) );
  561. }
  562. $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
  563. $dupeTitles = [];
  564. foreach ( $duplicate as $ns => $titles ) {
  565. $dupeTitles[] = "( cur_namespace = {$ns} AND cur_title in ("
  566. . $this->db->makeList( $titles ) . ") ) \n";
  567. }
  568. $sql .= $this->db->makeList( $dupeTitles, IDatabase::LIST_OR );
  569. # By sorting descending, the most recent entry will be the first in the list.
  570. # All following entries will be deleted by the next while-loop.
  571. $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
  572. $rows = $this->db->query( $sql, __METHOD__ );
  573. $prev_title = $prev_namespace = false;
  574. $deleteId = [];
  575. foreach ( $rows as $row ) {
  576. if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
  577. $deleteId[] = (int)$row->cur_id;
  578. }
  579. $prev_title = $row->cur_title;
  580. $prev_namespace = $row->cur_namespace;
  581. }
  582. $sql = "DELETE FROM $cur WHERE cur_id IN ( " . implode( ',', $deleteId ) . ')';
  583. $this->db->query( $sql, __METHOD__ );
  584. $this->output( wfTimestamp( TS_DB ) );
  585. $this->output( "......<b>Deleted</b> " . $this->db->affectedRows() . " records.\n" );
  586. }
  587. $this->output( wfTimestamp( TS_DB ) );
  588. $this->output( "......Creating tables.\n" );
  589. $this->db->query( "CREATE TABLE $page (
  590. page_id int(8) unsigned NOT NULL auto_increment,
  591. page_namespace int NOT NULL,
  592. page_title varchar(255) binary NOT NULL,
  593. page_restrictions tinyblob NOT NULL,
  594. page_is_redirect tinyint(1) unsigned NOT NULL default '0',
  595. page_is_new tinyint(1) unsigned NOT NULL default '0',
  596. page_random real unsigned NOT NULL,
  597. page_touched char(14) binary NOT NULL default '',
  598. page_latest int(8) unsigned NOT NULL,
  599. page_len int(8) unsigned NOT NULL,
  600. PRIMARY KEY page_id (page_id),
  601. UNIQUE INDEX name_title (page_namespace,page_title),
  602. INDEX (page_random),
  603. INDEX (page_len)
  604. ) ENGINE=InnoDB", __METHOD__ );
  605. $this->db->query( "CREATE TABLE $revision (
  606. rev_id int(8) unsigned NOT NULL auto_increment,
  607. rev_page int(8) unsigned NOT NULL,
  608. rev_comment tinyblob NOT NULL,
  609. rev_user int(5) unsigned NOT NULL default '0',
  610. rev_user_text varchar(255) binary NOT NULL default '',
  611. rev_timestamp char(14) binary NOT NULL default '',
  612. rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
  613. rev_deleted tinyint(1) unsigned NOT NULL default '0',
  614. rev_len int(8) unsigned,
  615. rev_parent_id int(8) unsigned default NULL,
  616. PRIMARY KEY rev_page_id (rev_page, rev_id),
  617. UNIQUE INDEX rev_id (rev_id),
  618. INDEX rev_timestamp (rev_timestamp),
  619. INDEX page_timestamp (rev_page,rev_timestamp),
  620. INDEX user_timestamp (rev_user,rev_timestamp),
  621. INDEX usertext_timestamp (rev_user_text,rev_timestamp)
  622. ) ENGINE=InnoDB", __METHOD__ );
  623. $this->output( wfTimestamp( TS_DB ) );
  624. $this->output( "......Locking tables.\n" );
  625. $this->db->query(
  626. "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE",
  627. __METHOD__
  628. );
  629. $maxold = intval( $this->db->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
  630. $this->output( wfTimestamp( TS_DB ) );
  631. $this->output( "......maxold is {$maxold}\n" );
  632. $this->output( wfTimestamp( TS_DB ) );
  633. global $wgLegacySchemaConversion;
  634. if ( $wgLegacySchemaConversion ) {
  635. // Create HistoryBlobCurStub entries.
  636. // Text will be pulled from the leftover 'cur' table at runtime.
  637. $this->output( "......Moving metadata from cur; using blob references to text in cur table.\n" );
  638. $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
  639. $cur_flags = "'object'";
  640. } else {
  641. // Copy all cur text in immediately: this may take longer but avoids
  642. // having to keep an extra table around.
  643. $this->output( "......Moving text from cur.\n" );
  644. $cur_text = 'cur_text';
  645. $cur_flags = "''";
  646. }
  647. $this->db->query(
  648. "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user,
  649. old_user_text, old_timestamp, old_minor_edit, old_flags)
  650. SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text,
  651. cur_timestamp, cur_minor_edit, $cur_flags
  652. FROM $cur",
  653. __METHOD__
  654. );
  655. $this->output( wfTimestamp( TS_DB ) );
  656. $this->output( "......Setting up revision table.\n" );
  657. $this->db->query(
  658. "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user,
  659. rev_user_text, rev_timestamp, rev_minor_edit)
  660. SELECT old_id, cur_id, old_comment, old_user, old_user_text,
  661. old_timestamp, old_minor_edit
  662. FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
  663. __METHOD__
  664. );
  665. $this->output( wfTimestamp( TS_DB ) );
  666. $this->output( "......Setting up page table.\n" );
  667. $this->db->query(
  668. "INSERT INTO $page (page_id, page_namespace, page_title,
  669. page_restrictions, page_is_redirect, page_is_new, page_random,
  670. page_touched, page_latest, page_len)
  671. SELECT cur_id, cur_namespace, cur_title, cur_restrictions,
  672. cur_is_redirect, cur_is_new, cur_random, cur_touched, rev_id, LENGTH(cur_text)
  673. FROM $cur,$revision
  674. WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}",
  675. __METHOD__
  676. );
  677. $this->output( wfTimestamp( TS_DB ) );
  678. $this->output( "......Unlocking tables.\n" );
  679. $this->db->query( "UNLOCK TABLES", __METHOD__ );
  680. $this->output( wfTimestamp( TS_DB ) );
  681. $this->output( "......Renaming old.\n" );
  682. $this->db->query( "ALTER TABLE $old RENAME TO $text", __METHOD__ );
  683. $this->output( wfTimestamp( TS_DB ) );
  684. $this->output( "...done.\n" );
  685. }
  686. protected function doNamespaceSize() {
  687. $tables = [
  688. 'page' => 'page',
  689. 'archive' => 'ar',
  690. 'recentchanges' => 'rc',
  691. 'watchlist' => 'wl',
  692. 'querycache' => 'qc',
  693. 'logging' => 'log',
  694. ];
  695. foreach ( $tables as $table => $prefix ) {
  696. $field = $prefix . '_namespace';
  697. $tablename = $this->db->tableName( $table );
  698. $result = $this->db->query( "SHOW COLUMNS FROM $tablename LIKE '$field'", __METHOD__ );
  699. $info = $this->db->fetchObject( $result );
  700. if ( substr( $info->Type, 0, 3 ) == 'int' ) {
  701. $this->output( "...$field is already a full int ($info->Type).\n" );
  702. } else {
  703. $this->output( "Promoting $field from $info->Type to int... " );
  704. $this->db->query( "ALTER TABLE $tablename MODIFY $field int NOT NULL", __METHOD__ );
  705. $this->output( "done.\n" );
  706. }
  707. }
  708. }
  709. protected function doPagelinksUpdate() {
  710. if ( $this->db->tableExists( 'pagelinks', __METHOD__ ) ) {
  711. $this->output( "...already have pagelinks table.\n" );
  712. return;
  713. }
  714. $this->applyPatch(
  715. 'patch-pagelinks.sql',
  716. false,
  717. 'Converting links and brokenlinks tables to pagelinks'
  718. );
  719. foreach (
  720. MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces() as $ns => $name
  721. ) {
  722. if ( $ns == 0 ) {
  723. continue;
  724. }
  725. $this->output( "Cleaning up broken links for namespace $ns... " );
  726. $this->db->update( 'pagelinks',
  727. [
  728. 'pl_namespace' => $ns,
  729. "pl_title = TRIM(LEADING {$this->db->addQuotes( "$name:" )} FROM pl_title)",
  730. ],
  731. [
  732. 'pl_namespace' => 0,
  733. 'pl_title' . $this->db->buildLike( "$name:", $this->db->anyString() ),
  734. ],
  735. __METHOD__
  736. );
  737. $this->output( "done.\n" );
  738. }
  739. }
  740. protected function doUserUniqueUpdate() {
  741. if ( !$this->doTable( 'user' ) ) {
  742. return true;
  743. }
  744. $duper = new UserDupes( $this->db, [ $this, 'output' ] );
  745. if ( $duper->hasUniqueIndex() ) {
  746. $this->output( "...already have unique user_name index.\n" );
  747. return;
  748. }
  749. if ( !$duper->clearDupes() ) {
  750. $this->output( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
  751. }
  752. $this->applyPatch( 'patch-user_nameindex.sql', false, "Adding unique index on user_name" );
  753. }
  754. protected function doUserGroupsUpdate() {
  755. if ( !$this->doTable( 'user_groups' ) ) {
  756. return true;
  757. }
  758. if ( $this->db->tableExists( 'user_groups', __METHOD__ ) ) {
  759. $info = $this->db->fieldInfo( 'user_groups', 'ug_group' );
  760. if ( $info->type() == 'int' ) {
  761. $oldug = $this->db->tableName( 'user_groups' );
  762. $newug = $this->db->tableName( 'user_groups_bogus' );
  763. $this->output( "user_groups table exists but is in bogus intermediate " .
  764. "format. Renaming to $newug... " );
  765. $this->db->query( "ALTER TABLE $oldug RENAME TO $newug", __METHOD__ );
  766. $this->output( "done.\n" );
  767. $this->applyPatch( 'patch-user_groups.sql', false, "Re-adding fresh user_groups table" );
  768. $this->output( "***\n" );
  769. $this->output( "*** WARNING: You will need to manually fix up user " .
  770. "permissions in the user_groups\n" );
  771. $this->output( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
  772. $this->output( "***\n" );
  773. } else {
  774. $this->output( "...user_groups table exists and is in current format.\n" );
  775. }
  776. return;
  777. }
  778. $this->applyPatch( 'patch-user_groups.sql', false, "Adding user_groups table" );
  779. if ( !$this->db->tableExists( 'user_rights', __METHOD__ ) ) {
  780. if ( $this->db->fieldExists( 'user', 'user_rights', __METHOD__ ) ) {
  781. $this->applyPatch(
  782. 'patch-user_rights.sql',
  783. false,
  784. 'Upgrading from a 1.3 or older database? Breaking out user_rights for conversion'
  785. );
  786. } else {
  787. $this->output( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
  788. $this->output( "*** You may need to manually configure some sysops by manipulating\n" );
  789. $this->output( "*** the user_groups table.\n" );
  790. return;
  791. }
  792. }
  793. $this->output( "Converting user_rights table to user_groups... " );
  794. $result = $this->db->select( 'user_rights',
  795. [ 'ur_user', 'ur_rights' ],
  796. [ "ur_rights != ''" ],
  797. __METHOD__ );
  798. foreach ( $result as $row ) {
  799. $groups = array_unique(
  800. array_map( 'trim',
  801. explode( ',', $row->ur_rights ) ) );
  802. foreach ( $groups as $group ) {
  803. $this->db->insert( 'user_groups',
  804. [
  805. 'ug_user' => $row->ur_user,
  806. 'ug_group' => $group ],
  807. __METHOD__ );
  808. }
  809. }
  810. $this->output( "done.\n" );
  811. }
  812. /**
  813. * Make sure wl_notificationtimestamp can be NULL,
  814. * and update old broken items.
  815. */
  816. protected function doWatchlistNull() {
  817. $info = $this->db->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
  818. if ( !$info ) {
  819. return;
  820. }
  821. if ( $info->isNullable() ) {
  822. $this->output( "...wl_notificationtimestamp is already nullable.\n" );
  823. return;
  824. }
  825. $this->applyPatch(
  826. 'patch-watchlist-null.sql',
  827. false,
  828. 'Making wl_notificationtimestamp nullable'
  829. );
  830. }
  831. /**
  832. * Set page_random field to a random value where it is equals to 0.
  833. *
  834. * @see T5946
  835. */
  836. protected function doPageRandomUpdate() {
  837. $page = $this->db->tableName( 'page' );
  838. $this->db->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", __METHOD__ );
  839. $rows = $this->db->affectedRows();
  840. if ( $rows ) {
  841. $this->output( "Set page_random to a random value on $rows rows where it was set to 0\n" );
  842. } else {
  843. $this->output( "...no page_random rows needed to be set\n" );
  844. }
  845. }
  846. protected function doTemplatelinksUpdate() {
  847. if ( $this->db->tableExists( 'templatelinks', __METHOD__ ) ) {
  848. $this->output( "...templatelinks table already exists\n" );
  849. return;
  850. }
  851. $this->applyPatch( 'patch-templatelinks.sql', false, "Creating templatelinks table" );
  852. $this->output( "Populating...\n" );
  853. $services = MediaWikiServices::getInstance();
  854. if ( $services->getDBLoadBalancer()->getServerCount() > 1 ) {
  855. // Slow, replication-friendly update
  856. $res = $this->db->select( 'pagelinks', [ 'pl_from', 'pl_namespace', 'pl_title' ],
  857. [ 'pl_namespace' => NS_TEMPLATE ], __METHOD__ );
  858. $count = 0;
  859. foreach ( $res as $row ) {
  860. $count = ( $count + 1 ) % 100;
  861. if ( $count == 0 ) {
  862. $lbFactory = $services->getDBLoadBalancerFactory();
  863. $lbFactory->waitForReplication( [
  864. 'domain' => $lbFactory->getLocalDomainID(),
  865. 'timeout' => self::REPLICATION_WAIT_TIMEOUT
  866. ] );
  867. }
  868. $this->db->insert( 'templatelinks',
  869. [
  870. 'tl_from' => $row->pl_from,
  871. 'tl_namespace' => $row->pl_namespace,
  872. 'tl_title' => $row->pl_title,
  873. ], __METHOD__
  874. );
  875. }
  876. } else {
  877. // Fast update
  878. $this->db->insertSelect( 'templatelinks', 'pagelinks',
  879. [
  880. 'tl_from' => 'pl_from',
  881. 'tl_namespace' => 'pl_namespace',
  882. 'tl_title' => 'pl_title'
  883. ], [
  884. 'pl_namespace' => 10
  885. ], __METHOD__,
  886. [ 'NO_AUTO_COLUMNS' ] // There's no "tl_id" auto-increment field
  887. );
  888. }
  889. $this->output( "Done. Please run maintenance/refreshLinks.php for a more " .
  890. "thorough templatelinks update.\n" );
  891. }
  892. protected function doBacklinkingIndicesUpdate() {
  893. if ( !$this->indexHasField( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
  894. !$this->indexHasField( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
  895. !$this->indexHasField( 'imagelinks', 'il_to', 'il_from' )
  896. ) {
  897. $this->applyPatch( 'patch-backlinkindexes.sql', false, "Updating backlinking indices" );
  898. }
  899. }
  900. /**
  901. * Adding page_restrictions table, obsoleting page.page_restrictions.
  902. * Migrating old restrictions to new table
  903. * -- Andrew Garrett, January 2007.
  904. */
  905. protected function doRestrictionsUpdate() {
  906. if ( $this->db->tableExists( 'page_restrictions', __METHOD__ ) ) {
  907. $this->output( "...page_restrictions table already exists.\n" );
  908. return;
  909. }
  910. $this->applyPatch(
  911. 'patch-page_restrictions.sql',
  912. false,
  913. 'Creating page_restrictions table (1/2)'
  914. );
  915. $this->applyPatch(
  916. 'patch-page_restrictions_sortkey.sql',
  917. false,
  918. 'Creating page_restrictions table (2/2)'
  919. );
  920. $this->output( "done.\n" );
  921. $this->output( "Migrating old restrictions to new table...\n" );
  922. $task = $this->maintenance->runChild( UpdateRestrictions::class );
  923. $task->execute();
  924. }
  925. protected function doCategorylinksIndicesUpdate() {
  926. if ( !$this->indexHasField( 'categorylinks', 'cl_sortkey', 'cl_from' ) ) {
  927. $this->applyPatch( 'patch-categorylinksindex.sql', false, "Updating categorylinks Indices" );
  928. }
  929. }
  930. protected function doCategoryPopulation() {
  931. if ( $this->updateRowExists( 'populate category' ) ) {
  932. $this->output( "...category table already populated.\n" );
  933. return;
  934. }
  935. $this->output(
  936. "Populating category table, printing progress markers. " .
  937. "For large databases, you\n" .
  938. "may want to hit Ctrl-C and do this manually with maintenance/\n" .
  939. "populateCategory.php.\n"
  940. );
  941. $task = $this->maintenance->runChild( PopulateCategory::class );
  942. $task->execute();
  943. $this->output( "Done populating category table.\n" );
  944. }
  945. protected function doPopulateParentId() {
  946. if ( !$this->updateRowExists( 'populate rev_parent_id' ) ) {
  947. $this->output(
  948. "Populating rev_parent_id fields, printing progress markers. For large\n" .
  949. "databases, you may want to hit Ctrl-C and do this manually with\n" .
  950. "maintenance/populateParentId.php.\n" );
  951. $task = $this->maintenance->runChild( PopulateParentId::class );
  952. $task->execute();
  953. }
  954. }
  955. protected function doMaybeProfilingMemoryUpdate() {
  956. if ( !$this->doTable( 'profiling' ) ) {
  957. return true;
  958. }
  959. if ( !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
  960. return true;
  961. } elseif ( $this->db->fieldExists( 'profiling', 'pf_memory', __METHOD__ ) ) {
  962. $this->output( "...profiling table has pf_memory field.\n" );
  963. return true;
  964. }
  965. return $this->applyPatch(
  966. 'patch-profiling-memory.sql',
  967. false,
  968. 'Adding pf_memory field to table profiling'
  969. );
  970. }
  971. protected function doFilearchiveIndicesUpdate() {
  972. $info = $this->db->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
  973. if ( !$info ) {
  974. $this->applyPatch( 'patch-filearchive-user-index.sql', false, "Updating filearchive indices" );
  975. }
  976. return true;
  977. }
  978. protected function doNonUniquePlTlIl() {
  979. $info = $this->db->indexInfo( 'pagelinks', 'pl_namespace' );
  980. if ( is_array( $info ) && $info[0]->Non_unique ) {
  981. $this->output( "...pl_namespace, tl_namespace, il_to indices are already non-UNIQUE.\n" );
  982. return true;
  983. }
  984. if ( $this->skipSchema ) {
  985. $this->output( "...skipping schema change (making pl_namespace, tl_namespace " .
  986. "and il_to indices non-UNIQUE).\n" );
  987. return false;
  988. }
  989. return $this->applyPatch(
  990. 'patch-pl-tl-il-nonunique.sql',
  991. false,
  992. 'Making pl_namespace, tl_namespace and il_to indices non-UNIQUE'
  993. );
  994. }
  995. protected function doUpdateMimeMinorField() {
  996. if ( $this->updateRowExists( 'mime_minor_length' ) ) {
  997. $this->output( "...*_mime_minor fields are already long enough.\n" );
  998. return;
  999. }
  1000. $this->applyPatch(
  1001. 'patch-mime_minor_length.sql',
  1002. false,
  1003. 'Altering all *_mime_minor fields to 100 bytes in size'
  1004. );
  1005. }
  1006. protected function doClFieldsUpdate() {
  1007. if ( $this->updateRowExists( 'cl_fields_update' ) ) {
  1008. $this->output( "...categorylinks up-to-date.\n" );
  1009. return;
  1010. }
  1011. $this->applyPatch(
  1012. 'patch-categorylinks-better-collation2.sql',
  1013. false,
  1014. 'Updating categorylinks (again)'
  1015. );
  1016. }
  1017. protected function doLangLinksLengthUpdate() {
  1018. $langlinks = $this->db->tableName( 'langlinks' );
  1019. $res = $this->db->query( "SHOW COLUMNS FROM $langlinks LIKE 'll_lang'" );
  1020. $row = $this->db->fetchObject( $res );
  1021. if ( $row && $row->Type == "varbinary(10)" ) {
  1022. $this->applyPatch(
  1023. 'patch-langlinks-ll_lang-20.sql',
  1024. false,
  1025. 'Updating length of ll_lang in langlinks'
  1026. );
  1027. } else {
  1028. $this->output( "...ll_lang is up-to-date.\n" );
  1029. }
  1030. }
  1031. protected function doUserNewTalkTimestampNotNull() {
  1032. if ( !$this->doTable( 'user_newtalk' ) ) {
  1033. return true;
  1034. }
  1035. $info = $this->db->fieldInfo( 'user_newtalk', 'user_last_timestamp' );
  1036. if ( $info === false ) {
  1037. return;
  1038. }
  1039. if ( $info->isNullable() ) {
  1040. $this->output( "...user_last_timestamp is already nullable.\n" );
  1041. return;
  1042. }
  1043. $this->applyPatch(
  1044. 'patch-user-newtalk-timestamp-null.sql',
  1045. false,
  1046. 'Making user_last_timestamp nullable'
  1047. );
  1048. }
  1049. protected function doIwlinksIndexNonUnique() {
  1050. $info = $this->db->indexInfo( 'iwlinks', 'iwl_prefix_title_from' );
  1051. if ( is_array( $info ) && $info[0]->Non_unique ) {
  1052. $this->output( "...iwl_prefix_title_from index is already non-UNIQUE.\n" );
  1053. return true;
  1054. }
  1055. if ( $this->skipSchema ) {
  1056. $this->output( "...skipping schema change (making iwl_prefix_title_from index non-UNIQUE).\n" );
  1057. return false;
  1058. }
  1059. return $this->applyPatch(
  1060. 'patch-iwl_prefix_title_from-non-unique.sql',
  1061. false,
  1062. 'Making iwl_prefix_title_from index non-UNIQUE'
  1063. );
  1064. }
  1065. protected function doUnsignedSyncronisation() {
  1066. $sync = [
  1067. [ 'table' => 'bot_passwords', 'field' => 'bp_user' ],
  1068. [ 'table' => 'change_tag', 'field' => 'ct_log_id' ],
  1069. [ 'table' => 'change_tag', 'field' => 'ct_rev_id' ],
  1070. [ 'table' => 'page_restrictions', 'field' => 'pr_user' ],
  1071. [ 'table' => 'user_newtalk', 'field' => 'user_id' ],
  1072. [ 'table' => 'user_properties', 'field' => 'up_user' ],
  1073. ];
  1074. foreach ( $sync as $s ) {
  1075. if ( !$this->doTable( $s['table'] ) ) {
  1076. continue;
  1077. }
  1078. $info = $this->db->fieldInfo( $s['table'], $s['field'] );
  1079. if ( $info === false ) {
  1080. continue;
  1081. }
  1082. $fullName = "{$s['table']}.{$s['field']}";
  1083. if ( $info->isUnsigned() ) {
  1084. $this->output( "...$fullName is already unsigned int.\n" );
  1085. continue;
  1086. }
  1087. $this->applyPatch(
  1088. "patch-{$s['table']}-{$s['field']}-unsigned.sql",
  1089. false,
  1090. "Making $fullName into an unsigned int"
  1091. );
  1092. }
  1093. return true;
  1094. }
  1095. protected function doRevisionPageRevIndexNonUnique() {
  1096. if ( !$this->doTable( 'revision' ) ) {
  1097. return true;
  1098. } elseif ( !$this->db->indexExists( 'revision', 'rev_page_id' ) ) {
  1099. $this->output( "...rev_page_id index not found on revision.\n" );
  1100. return true;
  1101. }
  1102. if ( !$this->db->indexUnique( 'revision', 'rev_page_id' ) ) {
  1103. $this->output( "...rev_page_id index already non-unique.\n" );
  1104. return true;
  1105. }
  1106. return $this->applyPatch(
  1107. 'patch-revision-page-rev-index-nonunique.sql',
  1108. false,
  1109. 'Making rev_page_id index non-unique'
  1110. );
  1111. }
  1112. protected function doExtendCommentLengths() {
  1113. $table = $this->db->tableName( 'revision' );
  1114. $res = $this->db->query( "SHOW COLUMNS FROM $table LIKE 'rev_comment'" );
  1115. $row = $this->db->fetchObject( $res );
  1116. if ( $row && ( $row->Type !== "varbinary(767)" || $row->Default !== "" ) ) {
  1117. $this->applyPatch(
  1118. 'patch-editsummary-length.sql',
  1119. false,
  1120. 'Extending edit summary lengths (and setting defaults)'
  1121. );
  1122. } else {
  1123. $this->output( "...comment fields are up to date.\n" );
  1124. }
  1125. }
  1126. public function getSchemaVars() {
  1127. global $wgDBTableOptions;
  1128. $vars = [];
  1129. $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
  1130. $vars['wgDBTableOptions'] = str_replace(
  1131. 'CHARSET=mysql4',
  1132. 'CHARSET=binary',
  1133. $vars['wgDBTableOptions']
  1134. );
  1135. return $vars;
  1136. }
  1137. }