MediaWiki  master
MysqlUpdater.php
Go to the documentation of this file.
1 <?php
24 
33  protected function getCoreUpdateList() {
34  return [
35  // 1.35 but it must come first
36  [ 'addField', 'revision', 'rev_actor', 'patch-revision-actor-comment-MCR.sql' ],
37 
38  // 1.34
39  [ 'dropIndex', 'archive', 'ar_usertext_timestamp',
40  'patch-drop-archive-ar_usertext_timestamp.sql' ],
41  [ 'dropIndex', 'archive', 'usertext_timestamp', 'patch-drop-archive-usertext_timestamp.sql' ],
42  [ 'dropField', 'archive', 'ar_user', 'patch-drop-archive-user-fields.sql' ],
43  [ 'dropField', 'ipblocks', 'ip_by', 'patch-drop-ipblocks-user-fields.sql' ],
44  [ 'dropIndex', 'image', 'img_user_timestamp', 'patch-drop-image-img_user_timestamp.sql' ],
45  [ 'dropField', 'image', 'img_user', 'patch-drop-image-user-fields.sql' ],
46  [ 'dropField', 'oldimage', 'oi_user', 'patch-drop-oldimage-user-fields.sql' ],
47  [ 'dropField', 'filearchive', 'fa_user', 'patch-drop-filearchive-user-fields.sql' ],
48  [ 'dropField', 'recentchanges', 'rc_user', 'patch-drop-recentchanges-user-fields.sql' ],
49  [ 'dropField', 'logging', 'log_user', 'patch-drop-logging-user-fields.sql' ],
50  [ 'addIndex', 'user_newtalk', 'un_user_ip', 'patch-rename-mysql-user_newtalk-indexes.sql' ],
51 
52  // 1.35
53  [ 'addTable', 'watchlist_expiry', 'patch-watchlist_expiry.sql' ],
54  [ 'modifyField', 'page', 'page_restrictions', 'patch-page_restrictions-null.sql' ],
55  [ 'renameIndex', 'ipblocks', 'ipb_address', 'ipb_address_unique', false,
56  'patch-ipblocks-rename-ipb_address.sql' ],
57  [ 'dropField', 'archive', 'ar_text_id', 'patch-archive-MCR.sql' ],
58  [ 'doLanguageLinksLengthSync' ],
59  [ 'doFixIpbAddressUniqueIndex' ],
60  [ 'modifyField', 'actor', 'actor_name', 'patch-actor-actor_name-varbinary.sql' ],
61  [ 'modifyField', 'sites', 'site_global_key', 'patch-sites-site_global_key.sql' ],
62  [ 'modifyField', 'iwlinks', 'iwl_prefix', 'patch-extend-iwlinks-iwl_prefix.sql' ],
63 
64  // 1.36
65  [ 'modifyField', 'redirect', 'rd_title', 'patch-redirect-rd_title-varbinary.sql' ],
66  [ 'modifyField', 'pagelinks', 'pl_title', 'patch-pagelinks-pl_title-varbinary.sql' ],
67  [ 'modifyField', 'templatelinks', 'tl_title', 'patch-templatelinks-tl_title-varbinary.sql' ],
68  [ 'modifyField', 'imagelinks', 'il_to', 'patch-imagelinks-il_to-varbinary.sql' ],
69  [ 'modifyField', 'langlinks', 'll_title', 'patch-langlinks-ll_title-varbinary.sql' ],
70  [ 'modifyField', 'iwlinks', 'iwl_title', 'patch-iwlinks-iwl_title-varbinary.sql' ],
71  [ 'modifyField', 'category', 'cat_title', 'patch-category-cat_title-varbinary.sql' ],
72  [ 'modifyField', 'querycache', 'qc_title', 'patch-querycache-qc_title-varbinary.sql' ],
73  [ 'modifyField', 'querycachetwo', 'qcc_title', 'patch-querycachetwo-qcc_title-varbinary.sql' ],
74  [ 'modifyField', 'watchlist', 'wl_title', 'patch-watchlist-wl_title-varbinary.sql' ],
75  [ 'modifyField', 'user_newtalk', 'user_last_timestamp',
76  'patch-user_newtalk-user_last_timestamp-binary.sql'
77  ],
78  [ 'modifyField', 'protected_titles', 'pt_title', 'patch-protected_titles-pt_title-varbinary.sql' ],
79  [ 'dropDefault', 'protected_titles', 'pt_expiry' ],
80  [ 'dropDefault', 'ip_changes', 'ipc_rev_timestamp' ],
81  [ 'modifyField', 'ipblocks_restrictions', 'ir_type', 'patch-ipblocks_restrictions-ir_type.sql' ],
82  [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title', false,
83  'patch-watchlist-namespace_title-rename-index.sql' ],
84  [ 'modifyField', 'job', 'job_title', 'patch-job-job_title-varbinary.sql' ],
85  [ 'modifyField', 'job', 'job_timestamp', 'patch-job_job_timestamp.sql' ],
86  [ 'modifyField', 'job', 'job_token_timestamp', 'patch-job_job_token_timestamp.sql' ],
87  [ 'modifyField', 'watchlist', 'wl_notificationtimestamp', 'patch-watchlist-wl_notificationtimestamp.sql' ],
88  [ 'modifyField', 'slot_roles', 'role_id', 'patch-slot_roles-role_id.sql' ],
89  [ 'modifyField', 'content_models', 'model_id', 'patch-content_models-model_id.sql' ],
90  [ 'modifyField', 'categorylinks', 'cl_to', 'patch-categorylinks-cl_to-varbinary.sql' ],
91  [ 'modifyField', 'logging', 'log_title', 'patch-logging-log_title-varbinary.sql' ],
92  [ 'modifyField', 'uploadstash', 'us_timestamp', 'patch-uploadstash-us_timestamp.sql' ],
93  [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property', false,
94  'patch-user_properties-rename-index.sql' ],
95  [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key', false, 'patch-sites-rename-indexes.sql' ],
96  [ 'renameIndex', 'logging', 'type_time', 'log_type_time', false, 'patch-logging-rename-indexes.sql' ],
97  [ 'modifyField', 'filearchive', 'fa_name', 'patch-filearchive-fa_name.sql' ],
98  [ 'dropDefault', 'filearchive', 'fa_deleted_timestamp' ],
99  [ 'dropDefault', 'filearchive', 'fa_timestamp' ],
100  [ 'modifyField', 'oldimage', 'oi_name', 'patch-oldimage-oi_name-varbinary.sql' ],
101  [ 'dropDefault', 'oldimage', 'oi_timestamp' ],
102  [ 'modifyField', 'objectcache', 'exptime', 'patch-objectcache-exptime-notnull.sql' ],
103  [ 'dropDefault', 'ipblocks', 'ipb_timestamp' ],
104  [ 'dropDefault', 'ipblocks', 'ipb_expiry' ],
105  [ 'renameIndex', 'archive', 'name_title_timestamp', 'ar_name_title_timestamp', false,
106  'patch-archive-rename-name_title_timestamp-index.sql' ],
107  [ 'modifyField', 'image', 'img_name', 'patch-image-img_name-varbinary.sql' ],
108  [ 'dropDefault', 'image', 'img_timestamp' ],
109  [ 'modifyField', 'image', 'img_timestamp', 'patch-image-img_timestamp.sql' ],
110  [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key', false,
111  'patch-site_identifiers-rename-indexes.sql' ],
112  [ 'modifyField', 'recentchanges', 'rc_title', 'patch-recentchanges-rc_title-varbinary.sql' ],
113  [ 'dropDefault', 'recentchanges', 'rc_timestamp' ],
114  [ 'modifyField', 'recentchanges', 'rc_timestamp', 'patch-recentchanges-rc_timestamp.sql' ],
115  [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id.sql' ],
116  [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
117  'patch-recentchanges-rc_new_name_timestamp.sql' ],
118  [ 'dropDefault', 'archive', 'ar_timestamp' ],
119  [ 'modifyField', 'archive', 'ar_title', 'patch-archive-ar_title-varbinary.sql' ],
120  [ 'modifyField', 'page', 'page_title', 'patch-page-page_title-varbinary.sql' ],
121  [ 'dropDefault', 'page', 'page_touched' ],
122  [ 'modifyField', 'user', 'user_name', 'patch-user_table-updates.sql' ],
123 
124  // 1.37
125  [ 'renameIndex', 'revision', 'page_timestamp', 'rev_page_timestamp', false,
126  'patch-revision-rename-index.sql' ],
127  [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
128  [ 'dropDefault', 'revision', 'rev_timestamp' ],
129  [ 'addIndex', 'oldimage', 'oi_timestamp', 'patch-oldimage-oi_timestamp.sql' ],
130  [ 'renameIndex', 'page', 'name_title', 'page_name_title', false, 'patch-page-rename-name_title-index.sql' ],
131  [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id', false,
132  'patch-change_tag-rename-indexes.sql' ],
133 
134  // 1.38
135  [ 'doConvertDjvuMetadata' ],
136  [ 'dropField', 'page_restrictions', 'pr_user', 'patch-drop-page_restrictions-pr_user.sql' ],
137  [ 'modifyField', 'filearchive', 'fa_id', 'patch-filearchive-fa_id.sql' ],
138  [ 'modifyField', 'image', 'img_major_mime', 'patch-image-img_major_mime-default.sql' ],
139  [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
140  [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
141  [ 'modifyField', 'page_restrictions', 'pr_page', 'patch-page_restrictions-pr_page.sql' ],
142  [ 'modifyField', 'page_props', 'pp_page', 'patch-page_props-pp_page.sql' ],
143  [ 'modifyField', 'ipblocks_restrictions', 'ir_value', 'patch-ipblocks_restrictions-ir_value.sql' ],
144  [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
145 
146  // 1.39
147  [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
148  [ 'modifyField', 'ipblocks_restrictions', 'ir_ipb_id', 'patch-ipblocks_restrictions-ir_ipb_id.sql' ],
149  [ 'modifyField', 'ipblocks', 'ipb_id', 'patch-ipblocks-ipb_id.sql' ],
150  [ 'modifyField', 'user', 'user_editcount', 'patch-user-user_editcount.sql' ],
151  [ 'runMaintenance', MigrateRevisionActorTemp::class, 'maintenance/migrateRevisionActorTemp.php' ],
152  [ 'dropTable', 'revision_actor_temp' ],
153  [ 'runMaintenance', UpdateRestrictions::class, 'maintenance/updateRestrictions.php' ],
154  [ 'dropField', 'page', 'page_restrictions', 'patch-page-drop-page_restrictions.sql' ],
155  [ 'migrateTemplatelinks' ],
156  [ 'modifyField', 'templatelinks', 'tl_namespace', 'patch-templatelinks-tl_title-nullable.sql' ],
157  [ 'dropField', 'templatelinks', 'tl_title', 'patch-templatelinks-drop-tl_title.sql' ],
158 
159  // 1.40
160  [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
161  ];
162  }
163 
173  protected function checkBin( $table, $field, $patchFile ) {
174  if ( !$this->doTable( $table ) ) {
175  return true;
176  }
177 
179  $fieldInfo = $this->db->fieldInfo( $table, $field );
180  if ( $fieldInfo->isBinary() ) {
181  $this->output( "...$table table has correct $field encoding.\n" );
182  } else {
183  $this->applyPatch( $patchFile, false, "Fixing $field encoding on $table table" );
184  }
185  }
186 
195  protected function indexHasField( $table, $index, $field ) {
196  $info = $this->db->indexInfo( $table, $index, __METHOD__ );
197  if ( $info ) {
198  foreach ( $info as $row ) {
199  if ( $row->Column_name == $field ) {
200  $this->output( "...index $index on table $table includes field $field.\n" );
201  return true;
202  }
203  }
204  }
205  $this->output( "...index $index on table $table has no field $field; added.\n" );
206 
207  return false;
208  }
209 
210  protected function doLanguageLinksLengthSync() {
211  $sync = [
212  [ 'table' => 'l10n_cache', 'field' => 'lc_lang', 'file' => 'patch-l10n_cache-lc_lang-35.sql' ],
213  [ 'table' => 'langlinks', 'field' => 'll_lang', 'file' => 'patch-langlinks-ll_lang-35.sql' ],
214  [ 'table' => 'sites', 'field' => 'site_language', 'file' => 'patch-sites-site_language-35.sql' ],
215  ];
216 
217  foreach ( $sync as $s ) {
218  $table = $this->db->tableName( $s['table'] );
219  $field = $s['field'];
220  $res = $this->db->query( "SHOW COLUMNS FROM $table LIKE '$field'", __METHOD__ );
221  $row = $res->fetchObject();
222 
223  if ( $row && $row->Type !== "varbinary(35)" ) {
224  $this->applyPatch(
225  $s['file'],
226  false,
227  "Updating length of $field in $table"
228  );
229  } else {
230  $this->output( "...$field is up-to-date.\n" );
231  }
232  }
233  }
234 
235  protected function doFixIpbAddressUniqueIndex() {
236  if ( !$this->doTable( 'ipblocks' ) ) {
237  return;
238  }
239 
240  if ( !$this->indexHasField( 'ipblocks', 'ipb_address_unique', 'ipb_anon_only' ) ) {
241  $this->output( "...ipb_address_unique index up-to-date.\n" );
242  return;
243  }
244 
245  $this->applyPatch(
246  'patch-ipblocks-fix-ipb_address_unique.sql',
247  false,
248  'Removing ipb_anon_only column from ipb_address_unique index'
249  );
250  }
251 
252  public function getSchemaVars() {
253  global $wgDBTableOptions;
254 
255  $vars = [];
256  $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
257  $vars['wgDBTableOptions'] = str_replace(
258  'CHARSET=mysql4',
259  'CHARSET=binary',
260  $vars['wgDBTableOptions']
261  );
262 
263  return $vars;
264  }
265 
273  protected function dropDefault( $table, $field ) {
274  $updateKey = "$table-$field-dropDefault";
275 
276  if ( $this->updateRowExists( $updateKey ) ) {
277  return;
278  }
279 
280  $info = $this->db->fieldInfo( $table, $field );
281  if ( $info && $info->defaultValue() !== false ) {
282  $this->output( "Removing '$table.$field' default value.\n" );
283  $table = $this->db->tableName( $table );
284  $ret = $this->db->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__ );
285 
286  if ( $ret ) {
287  $this->insertUpdateRow( $updateKey );
288  }
289  }
290  }
291 
300  protected function setDefault( $table, $field, $default ) {
301  $info = $this->db->fieldInfo( $table, $field );
302  if ( $info && $info->defaultValue() !== $default ) {
303  $this->output( "Changing '$table.$field' default value.\n" );
304  $table = $this->db->tableName( $table );
305  $this->db->query(
306  "ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
307  . $this->db->addQuotes( $default ), __METHOD__
308  );
309  }
310  }
311 
312 }
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