MediaWiki  master
MysqlUpdater.php
Go to the documentation of this file.
1 <?php
24 
33  protected function getCoreUpdateList() {
34  return [
35  // 1.31
36  [ 'addField', 'image', 'img_description_id', 'patch-image-img_description_id.sql' ],
37  [ 'migrateComments' ],
38 
39  [ 'addTable', 'slots', 'patch-slots.sql' ],
40  [ 'addField', 'slots', 'slot_origin', 'patch-slot-origin.sql' ],
41  [ 'addTable', 'content', 'patch-content.sql' ],
42  [ 'addTable', 'slot_roles', 'patch-slot_roles.sql' ],
43  [ 'addTable', 'content_models', 'patch-content_models.sql' ],
44  [ 'migrateArchiveText' ],
45  [ 'addTable', 'actor', 'patch-actor-table.sql' ],
46  [ 'addTable', 'revision_actor_temp', 'patch-revision_actor_temp-table.sql' ],
47  [ 'addField', 'archive', 'ar_actor', 'patch-archive-ar_actor.sql' ],
48  [ 'addField', 'ipblocks', 'ipb_by_actor', 'patch-ipblocks-ipb_by_actor.sql' ],
49  [ 'addField', 'image', 'img_actor', 'patch-image-img_actor.sql' ],
50  [ 'addField', 'oldimage', 'oi_actor', 'patch-oldimage-oi_actor.sql' ],
51  [ 'addField', 'filearchive', 'fa_actor', 'patch-filearchive-fa_actor.sql' ],
52  [ 'addField', 'recentchanges', 'rc_actor', 'patch-recentchanges-rc_actor.sql' ],
53  [ 'addField', 'logging', 'log_actor', 'patch-logging-log_actor.sql' ],
54  [ 'migrateActors' ],
55 
56  // Adds a default value to the rev_text_id field to allow Multi Content
57  // Revisions migration to happen where rows will have to be added to the
58  // revision table with no rev_text_id.
59  [ 'setDefault', 'revision', 'rev_text_id', 0 ],
60  [ 'modifyTable', 'site_stats', 'patch-site_stats-modify.sql' ],
61  [ 'populateArchiveRevId' ],
62  [ 'addIndex', 'recentchanges', 'rc_namespace_title_timestamp',
63  'patch-recentchanges-nttindex.sql' ],
64 
65  // 1.32
66  [ 'addTable', 'change_tag_def', 'patch-change_tag_def.sql' ],
67  [ 'populateExternallinksIndex60' ],
68  [ 'dropDefault', 'externallinks', 'el_index_60' ],
69  [ 'runMaintenance', DeduplicateArchiveRevId::class, 'maintenance/deduplicateArchiveRevId.php' ],
70  [ 'addField', 'change_tag', 'ct_tag_id', 'patch-change_tag-tag_id.sql' ],
71  [ 'addIndex', 'archive', 'ar_revid_uniq', 'patch-archive-ar_rev_id-unique.sql' ],
72  [ 'populateContentTables' ],
73  [ 'addIndex', 'logging', 'log_type_action', 'patch-logging-log-type-action-index.sql' ],
74  [ 'dropIndex', 'logging', 'type_action', 'patch-logging-drop-type-action-index.sql' ],
75  [ 'renameIndex', 'interwiki', 'iw_prefix', 'PRIMARY', false, 'patch-interwiki-fix-pk.sql' ],
76  [ 'renameIndex', 'page_props', 'pp_page_propname', 'PRIMARY', false,
77  'patch-page_props-fix-pk.sql' ],
78  [ 'renameIndex', 'protected_titles', 'pt_namespace_title', 'PRIMARY', false,
79  'patch-protected_titles-fix-pk.sql' ],
80  [ 'renameIndex', 'site_identifiers', 'site_ids_type', 'PRIMARY', false,
81  'patch-site_identifiers-fix-pk.sql' ],
82  [ 'addIndex', 'recentchanges', 'rc_this_oldid', 'patch-recentchanges-rc_this_oldid-index.sql' ],
83  [ 'dropTable', 'transcache' ],
84  [ 'runMaintenance', PopulateChangeTagDef::class, 'maintenance/populateChangeTagDef.php' ],
85  [ 'dropIndex', 'change_tag', 'change_tag_rc_tag', 'patch-change_tag-change_tag_rc_tag_id.sql' ],
86  [ 'addField', 'ipblocks', 'ipb_sitewide', 'patch-ipb_sitewide.sql' ],
87  [ 'addTable', 'ipblocks_restrictions', 'patch-ipblocks_restrictions-table.sql' ],
88  [ 'migrateImageCommentTemp' ],
89 
90  // 1.33
91  [ 'dropField', 'change_tag', 'ct_tag', 'patch-drop-ct_tag.sql' ],
92  [ 'dropTable', 'valid_tag' ],
93  [ 'dropTable', 'tag_summary' ],
94  [ 'dropField', 'archive', 'ar_comment', 'patch-archive-drop-ar_comment.sql' ],
95  [ 'dropField', 'ipblocks', 'ipb_reason', 'patch-ipblocks-drop-ipb_reason.sql' ],
96  [ 'dropField', 'image', 'img_description', 'patch-image-drop-img_description.sql' ],
97  [ 'dropField', 'oldimage', 'oi_description', 'patch-oldimage-drop-oi_description.sql' ],
98  [ 'dropField', 'filearchive', 'fa_description', 'patch-filearchive-drop-fa_description.sql' ],
99  [ 'dropField', 'recentchanges', 'rc_comment', 'patch-recentchanges-drop-rc_comment.sql' ],
100  [ 'dropField', 'logging', 'log_comment', 'patch-logging-drop-log_comment.sql' ],
101  [ 'dropField', 'protected_titles', 'pt_reason', 'patch-protected_titles-drop-pt_reason.sql' ],
102  [ 'modifyTable', 'job', 'patch-job-params-mediumblob.sql' ],
103 
104  // 1.34
105  [ 'dropIndex', 'archive', 'ar_usertext_timestamp',
106  'patch-drop-archive-ar_usertext_timestamp.sql' ],
107  [ 'dropIndex', 'archive', 'usertext_timestamp', 'patch-drop-archive-usertext_timestamp.sql' ],
108  [ 'dropField', 'archive', 'ar_user', 'patch-drop-archive-user-fields.sql' ],
109  [ 'dropField', 'ipblocks', 'ip_by', 'patch-drop-ipblocks-user-fields.sql' ],
110  [ 'dropIndex', 'image', 'img_user_timestamp', 'patch-drop-image-img_user_timestamp.sql' ],
111  [ 'dropField', 'image', 'img_user', 'patch-drop-image-user-fields.sql' ],
112  [ 'dropField', 'oldimage', 'oi_user', 'patch-drop-oldimage-user-fields.sql' ],
113  [ 'dropField', 'filearchive', 'fa_user', 'patch-drop-filearchive-user-fields.sql' ],
114  [ 'dropField', 'recentchanges', 'rc_user', 'patch-drop-recentchanges-user-fields.sql' ],
115  [ 'dropField', 'logging', 'log_user', 'patch-drop-logging-user-fields.sql' ],
116  [ 'addIndex', 'user_newtalk', 'un_user_ip', 'patch-rename-mysql-user_newtalk-indexes.sql' ],
117 
118  // 1.35
119  [ 'addTable', 'watchlist_expiry', 'patch-watchlist_expiry.sql' ],
120  [ 'modifyField', 'page', 'page_restrictions', 'patch-page_restrictions-null.sql' ],
121  [ 'renameIndex', 'ipblocks', 'ipb_address', 'ipb_address_unique', false,
122  'patch-ipblocks-rename-ipb_address.sql' ],
123  [ 'addField', 'revision', 'rev_actor', 'patch-revision-actor-comment-MCR.sql' ],
124  [ 'dropField', 'archive', 'ar_text_id', 'patch-archive-MCR.sql' ],
125  [ 'doLanguageLinksLengthSync' ],
126  [ 'doFixIpbAddressUniqueIndex' ],
127  [ 'modifyField', 'actor', 'actor_name', 'patch-actor-actor_name-varbinary.sql' ],
128  [ 'modifyField', 'sites', 'site_global_key', 'patch-sites-site_global_key.sql' ],
129  [ 'modifyField', 'iwlinks', 'iwl_prefix', 'patch-extend-iwlinks-iwl_prefix.sql' ],
130 
131  // 1.36
132  [ 'modifyField', 'redirect', 'rd_title', 'patch-redirect-rd_title-varbinary.sql' ],
133  [ 'modifyField', 'pagelinks', 'pl_title', 'patch-pagelinks-pl_title-varbinary.sql' ],
134  [ 'modifyField', 'templatelinks', 'tl_title', 'patch-templatelinks-tl_title-varbinary.sql' ],
135  [ 'modifyField', 'imagelinks', 'il_to', 'patch-imagelinks-il_to-varbinary.sql' ],
136  [ 'modifyField', 'langlinks', 'll_title', 'patch-langlinks-ll_title-varbinary.sql' ],
137  [ 'modifyField', 'iwlinks', 'iwl_title', 'patch-iwlinks-iwl_title-varbinary.sql' ],
138  [ 'modifyField', 'category', 'cat_title', 'patch-category-cat_title-varbinary.sql' ],
139  [ 'modifyField', 'querycache', 'qc_title', 'patch-querycache-qc_title-varbinary.sql' ],
140  [ 'modifyField', 'querycachetwo', 'qcc_title', 'patch-querycachetwo-qcc_title-varbinary.sql' ],
141  [ 'modifyField', 'watchlist', 'wl_title', 'patch-watchlist-wl_title-varbinary.sql' ],
142  [ 'modifyField', 'user_newtalk', 'user_last_timestamp',
143  'patch-user_newtalk-user_last_timestamp-binary.sql'
144  ],
145  [ 'modifyField', 'protected_titles', 'pt_title', 'patch-protected_titles-pt_title-varbinary.sql' ],
146  [ 'dropDefault', 'protected_titles', 'pt_expiry' ],
147  [ 'dropDefault', 'ip_changes', 'ipc_rev_timestamp' ],
148  [ 'dropDefault', 'revision_actor_temp', 'revactor_timestamp' ],
149  [ 'modifyField', 'ipblocks_restrictions', 'ir_type', 'patch-ipblocks_restrictions-ir_type.sql' ],
150  [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title', false,
151  'patch-watchlist-namespace_title-rename-index.sql' ],
152  [ 'modifyField', 'job', 'job_title', 'patch-job-job_title-varbinary.sql' ],
153  [ 'modifyField', 'job', 'job_timestamp', 'patch-job_job_timestamp.sql' ],
154  [ 'modifyField', 'job', 'job_token_timestamp', 'patch-job_job_token_timestamp.sql' ],
155  [ 'modifyField', 'watchlist', 'wl_notificationtimestamp', 'patch-watchlist-wl_notificationtimestamp.sql' ],
156  [ 'modifyField', 'slot_roles', 'role_id', 'patch-slot_roles-role_id.sql' ],
157  [ 'modifyField', 'content_models', 'model_id', 'patch-content_models-model_id.sql' ],
158  [ 'modifyField', 'categorylinks', 'cl_to', 'patch-categorylinks-cl_to-varbinary.sql' ],
159  [ 'modifyField', 'logging', 'log_title', 'patch-logging-log_title-varbinary.sql' ],
160  [ 'modifyField', 'uploadstash', 'us_timestamp', 'patch-uploadstash-us_timestamp.sql' ],
161  [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property', false,
162  'patch-user_properties-rename-index.sql' ],
163  [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key', false, 'patch-sites-rename-indexes.sql' ],
164  [ 'renameIndex', 'logging', 'type_time', 'log_type_time', false, 'patch-logging-rename-indexes.sql' ],
165  [ 'modifyField', 'filearchive', 'fa_name', 'patch-filearchive-fa_name.sql' ],
166  [ 'dropDefault', 'filearchive', 'fa_deleted_timestamp' ],
167  [ 'dropDefault', 'filearchive', 'fa_timestamp' ],
168  [ 'modifyField', 'oldimage', 'oi_name', 'patch-oldimage-oi_name-varbinary.sql' ],
169  [ 'dropDefault', 'oldimage', 'oi_timestamp' ],
170  [ 'modifyField', 'objectcache', 'exptime', 'patch-objectcache-exptime-notnull.sql' ],
171  [ 'dropDefault', 'ipblocks', 'ipb_timestamp' ],
172  [ 'dropDefault', 'ipblocks', 'ipb_expiry' ],
173  [ 'renameIndex', 'archive', 'name_title_timestamp', 'ar_name_title_timestamp', false,
174  'patch-archive-rename-name_title_timestamp-index.sql' ],
175  [ 'modifyField', 'image', 'img_name', 'patch-image-img_name-varbinary.sql' ],
176  [ 'dropDefault', 'image', 'img_timestamp' ],
177  [ 'modifyField', 'image', 'img_timestamp', 'patch-image-img_timestamp.sql' ],
178  [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key', false,
179  'patch-site_identifiers-rename-indexes.sql' ],
180  [ 'modifyField', 'recentchanges', 'rc_title', 'patch-recentchanges-rc_title-varbinary.sql' ],
181  [ 'dropDefault', 'recentchanges', 'rc_timestamp' ],
182  [ 'modifyField', 'recentchanges', 'rc_timestamp', 'patch-recentchanges-rc_timestamp.sql' ],
183  [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id.sql' ],
184  [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
185  'patch-recentchanges-rc_new_name_timestamp.sql' ],
186  [ 'dropDefault', 'archive', 'ar_timestamp' ],
187  [ 'modifyField', 'archive', 'ar_title', 'patch-archive-ar_title-varbinary.sql' ],
188  [ 'modifyField', 'page', 'page_title', 'patch-page-page_title-varbinary.sql' ],
189  [ 'dropDefault', 'page', 'page_touched' ],
190  [ 'modifyField', 'user', 'user_name', 'patch-user_table-updates.sql' ],
191 
192  // 1.37
193  [ 'renameIndex', 'revision', 'page_timestamp', 'rev_page_timestamp', false,
194  'patch-revision-rename-index.sql' ],
195  [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
196  [ 'dropDefault', 'revision', 'rev_timestamp' ],
197  [ 'addIndex', 'oldimage', 'oi_timestamp', 'patch-oldimage-oi_timestamp.sql' ],
198  [ 'renameIndex', 'page', 'name_title', 'page_name_title', false, 'patch-page-rename-name_title-index.sql' ],
199  [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id', false,
200  'patch-change_tag-rename-indexes.sql' ],
201 
202  // 1.38
203  [ 'doConvertDjvuMetadata' ],
204  [ 'dropField', 'page_restrictions', 'pr_user', 'patch-drop-page_restrictions-pr_user.sql' ],
205  [ 'modifyField', 'filearchive', 'fa_id', 'patch-filearchive-fa_id.sql' ],
206  [ 'modifyField', 'image', 'img_major_mime', 'patch-image-img_major_mime-default.sql' ],
207  [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
208  [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
209  [ 'modifyField', 'page_restrictions', 'pr_page', 'patch-page_restrictions-pr_page.sql' ],
210  [ 'modifyField', 'page_props', 'pp_page', 'patch-page_props-pp_page.sql' ],
211  [ 'modifyField', 'ipblocks_restrictions', 'ir_value', 'patch-ipblocks_restrictions-ir_value.sql' ],
212  [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
213 
214  // 1.39
215  [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
216  [ 'modifyField', 'ipblocks_restrictions', 'ir_ipb_id', 'patch-ipblocks_restrictions-ir_ipb_id.sql' ],
217  [ 'modifyField', 'ipblocks', 'ipb_id', 'patch-ipblocks-ipb_id.sql' ],
218  [ 'modifyField', 'user', 'user_editcount', 'patch-user-user_editcount.sql' ],
219  ];
220  }
221 
231  protected function checkBin( $table, $field, $patchFile ) {
232  if ( !$this->doTable( $table ) ) {
233  return true;
234  }
235 
237  $fieldInfo = $this->db->fieldInfo( $table, $field );
238  if ( $fieldInfo->isBinary() ) {
239  $this->output( "...$table table has correct $field encoding.\n" );
240  } else {
241  $this->applyPatch( $patchFile, false, "Fixing $field encoding on $table table" );
242  }
243  }
244 
253  protected function indexHasField( $table, $index, $field ) {
254  $info = $this->db->indexInfo( $table, $index, __METHOD__ );
255  if ( $info ) {
256  foreach ( $info as $row ) {
257  if ( $row->Column_name == $field ) {
258  $this->output( "...index $index on table $table includes field $field.\n" );
259  return true;
260  }
261  }
262  }
263  $this->output( "...index $index on table $table has no field $field; added.\n" );
264 
265  return false;
266  }
267 
268  protected function doLanguageLinksLengthSync() {
269  $sync = [
270  [ 'table' => 'l10n_cache', 'field' => 'lc_lang', 'file' => 'patch-l10n_cache-lc_lang-35.sql' ],
271  [ 'table' => 'langlinks', 'field' => 'll_lang', 'file' => 'patch-langlinks-ll_lang-35.sql' ],
272  [ 'table' => 'sites', 'field' => 'site_language', 'file' => 'patch-sites-site_language-35.sql' ],
273  ];
274 
275  foreach ( $sync as $s ) {
276  $table = $this->db->tableName( $s['table'] );
277  $field = $s['field'];
278  $res = $this->db->query( "SHOW COLUMNS FROM $table LIKE '$field'", __METHOD__ );
279  $row = $res->fetchObject();
280 
281  if ( $row && $row->Type !== "varbinary(35)" ) {
282  $this->applyPatch(
283  $s['file'],
284  false,
285  "Updating length of $field in $table"
286  );
287  } else {
288  $this->output( "...$field is up-to-date.\n" );
289  }
290  }
291  }
292 
293  protected function doFixIpbAddressUniqueIndex() {
294  if ( !$this->doTable( 'ipblocks' ) ) {
295  return;
296  }
297 
298  if ( !$this->indexHasField( 'ipblocks', 'ipb_address_unique', 'ipb_anon_only' ) ) {
299  $this->output( "...ipb_address_unique index up-to-date.\n" );
300  return;
301  }
302 
303  $this->applyPatch(
304  'patch-ipblocks-fix-ipb_address_unique.sql',
305  false,
306  'Removing ipb_anon_only column from ipb_address_unique index'
307  );
308  }
309 
310  public function getSchemaVars() {
311  global $wgDBTableOptions;
312 
313  $vars = [];
314  $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
315  $vars['wgDBTableOptions'] = str_replace(
316  'CHARSET=mysql4',
317  'CHARSET=binary',
318  $vars['wgDBTableOptions']
319  );
320 
321  return $vars;
322  }
323 
331  protected function dropDefault( $table, $field ) {
332  $updateKey = "$table-$field-dropDefault";
333 
334  if ( $this->updateRowExists( $updateKey ) ) {
335  return;
336  }
337 
338  $info = $this->db->fieldInfo( $table, $field );
339  if ( $info && $info->defaultValue() !== false ) {
340  $this->output( "Removing '$table.$field' default value.\n" );
341  $table = $this->db->tableName( $table );
342  $ret = $this->db->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__ );
343 
344  if ( $ret ) {
345  $this->insertUpdateRow( $updateKey );
346  }
347  }
348  }
349 
358  protected function setDefault( $table, $field, $default ) {
359  $info = $this->db->fieldInfo( $table, $field );
360  if ( $info && $info->defaultValue() !== $default ) {
361  $this->output( "Changing '$table.$field' default value.\n" );
362  $table = $this->db->tableName( $table );
363  $this->db->query(
364  "ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
365  . $this->db->addQuotes( $default ), __METHOD__
366  );
367  }
368  }
369 
370 }
Class for handling database updates.
updateRowExists( $key)
Helper function: check if the given key is present in the updatelog table.
output( $str)
Output some text.
insertUpdateRow( $key, $val=null)
Helper function: Add a key to the updatelog table.
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
doTable( $name)
Returns whether updates should be executed on the database table $name.
dropDefault( $table, $field)
Drop a default value from a field.
getSchemaVars()
Get appropriate schema variables in the current database connection.
setDefault( $table, $field, $default)
Set a default value for a field.
checkBin( $table, $field, $patchFile)
MW 1.4 betas were missing the 'binary' marker from logging.log_title, which caused a MySQL collation ...
getCoreUpdateList()
Get an array of updates to perform on the database.
indexHasField( $table, $index, $field)
Check whether an index contain a field.
doLanguageLinksLengthSync()
doFixIpbAddressUniqueIndex()
$wgDBTableOptions
Config variable stub for the DBTableOptions setting, for use by phpdoc and IDEs.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s