4 * MySQL-specific updater.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 namespace MediaWiki\Installer
;
27 use FixInconsistentRedirects
;
28 use MediaWiki\Maintenance\FixAutoblockLogTitles
;
29 use MigrateExternallinks
;
30 use MigrateRevisionActorTemp
;
31 use MigrateRevisionCommentTemp
;
32 use PopulateUserIsTemp
;
33 use UpdateRestrictions
;
36 * Mysql update list and mysql-specific update functions.
40 * @property \Wikimedia\Rdbms\DatabaseMySQL $db
42 class MysqlUpdater
extends DatabaseUpdater
{
43 protected function getCoreUpdateList() {
46 [ 'modifyField', 'redirect', 'rd_title', 'patch-redirect-rd_title-varbinary.sql' ],
47 [ 'modifyField', 'pagelinks', 'pl_title', 'patch-pagelinks-pl_title-varbinary.sql' ],
48 [ 'modifyField', 'templatelinks', 'tl_title', 'patch-templatelinks-tl_title-varbinary.sql' ],
49 [ 'modifyField', 'imagelinks', 'il_to', 'patch-imagelinks-il_to-varbinary.sql' ],
50 [ 'modifyField', 'langlinks', 'll_title', 'patch-langlinks-ll_title-varbinary.sql' ],
51 [ 'modifyField', 'iwlinks', 'iwl_title', 'patch-iwlinks-iwl_title-varbinary.sql' ],
52 [ 'modifyField', 'category', 'cat_title', 'patch-category-cat_title-varbinary.sql' ],
53 [ 'modifyField', 'querycache', 'qc_title', 'patch-querycache-qc_title-varbinary.sql' ],
54 [ 'modifyField', 'querycachetwo', 'qcc_title', 'patch-querycachetwo-qcc_title-varbinary.sql' ],
55 [ 'modifyField', 'watchlist', 'wl_title', 'patch-watchlist-wl_title-varbinary.sql' ],
56 [ 'modifyField', 'user_newtalk', 'user_last_timestamp',
57 'patch-user_newtalk-user_last_timestamp-binary.sql'
59 [ 'modifyField', 'protected_titles', 'pt_title', 'patch-protected_titles-pt_title-varbinary.sql' ],
60 [ 'dropDefault', 'protected_titles', 'pt_expiry' ],
61 [ 'dropDefault', 'ip_changes', 'ipc_rev_timestamp' ],
62 [ 'modifyField', 'ipblocks_restrictions', 'ir_type', 'patch-ipblocks_restrictions-ir_type.sql' ],
63 [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title', false,
64 'patch-watchlist-namespace_title-rename-index.sql' ],
65 [ 'modifyField', 'job', 'job_title', 'patch-job-job_title-varbinary.sql' ],
66 [ 'modifyField', 'job', 'job_timestamp', 'patch-job_job_timestamp.sql' ],
67 [ 'modifyField', 'job', 'job_token_timestamp', 'patch-job_job_token_timestamp.sql' ],
68 [ 'modifyField', 'watchlist', 'wl_notificationtimestamp', 'patch-watchlist-wl_notificationtimestamp.sql' ],
69 [ 'modifyField', 'slot_roles', 'role_id', 'patch-slot_roles-role_id.sql' ],
70 [ 'modifyField', 'content_models', 'model_id', 'patch-content_models-model_id.sql' ],
71 [ 'modifyField', 'categorylinks', 'cl_to', 'patch-categorylinks-cl_to-varbinary.sql' ],
72 [ 'modifyField', 'logging', 'log_title', 'patch-logging-log_title-varbinary.sql' ],
73 [ 'modifyField', 'uploadstash', 'us_timestamp', 'patch-uploadstash-us_timestamp.sql' ],
74 [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property', false,
75 'patch-user_properties-rename-index.sql' ],
76 [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key', false, 'patch-sites-rename-indexes.sql' ],
77 [ 'renameIndex', 'logging', 'type_time', 'log_type_time', false, 'patch-logging-rename-indexes.sql' ],
78 [ 'modifyField', 'filearchive', 'fa_name', 'patch-filearchive-fa_name.sql' ],
79 [ 'dropDefault', 'filearchive', 'fa_deleted_timestamp' ],
80 [ 'dropDefault', 'filearchive', 'fa_timestamp' ],
81 [ 'modifyField', 'oldimage', 'oi_name', 'patch-oldimage-oi_name-varbinary.sql' ],
82 [ 'dropDefault', 'oldimage', 'oi_timestamp' ],
83 [ 'modifyField', 'objectcache', 'exptime', 'patch-objectcache-exptime-notnull.sql' ],
84 [ 'dropDefault', 'ipblocks', 'ipb_timestamp' ],
85 [ 'dropDefault', 'ipblocks', 'ipb_expiry' ],
86 [ 'renameIndex', 'archive', 'name_title_timestamp', 'ar_name_title_timestamp', false,
87 'patch-archive-rename-name_title_timestamp-index.sql' ],
88 [ 'modifyField', 'image', 'img_name', 'patch-image-img_name-varbinary.sql' ],
89 [ 'dropDefault', 'image', 'img_timestamp' ],
90 [ 'modifyField', 'image', 'img_timestamp', 'patch-image-img_timestamp.sql' ],
91 [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key', false,
92 'patch-site_identifiers-rename-indexes.sql' ],
93 [ 'modifyField', 'recentchanges', 'rc_title', 'patch-recentchanges-rc_title-varbinary.sql' ],
94 [ 'dropDefault', 'recentchanges', 'rc_timestamp' ],
95 [ 'modifyField', 'recentchanges', 'rc_timestamp', 'patch-recentchanges-rc_timestamp.sql' ],
96 [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id.sql' ],
97 [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
98 'patch-recentchanges-rc_new_name_timestamp.sql' ],
99 [ 'dropDefault', 'archive', 'ar_timestamp' ],
100 [ 'modifyField', 'archive', 'ar_title', 'patch-archive-ar_title-varbinary.sql' ],
101 [ 'modifyField', 'page', 'page_title', 'patch-page-page_title-varbinary.sql' ],
102 [ 'dropDefault', 'page', 'page_touched' ],
103 [ 'modifyField', 'user', 'user_name', 'patch-user_table-updates.sql' ],
106 [ 'renameIndex', 'revision', 'page_timestamp', 'rev_page_timestamp', false,
107 'patch-revision-rename-index.sql' ],
108 [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
109 [ 'dropDefault', 'revision', 'rev_timestamp' ],
110 [ 'addIndex', 'oldimage', 'oi_timestamp', 'patch-oldimage-oi_timestamp.sql' ],
111 [ 'renameIndex', 'page', 'name_title', 'page_name_title', false, 'patch-page-rename-name_title-index.sql' ],
112 [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id', false,
113 'patch-change_tag-rename-indexes.sql' ],
116 [ 'doConvertDjvuMetadata' ],
117 [ 'dropField', 'page_restrictions', 'pr_user', 'patch-drop-page_restrictions-pr_user.sql' ],
118 [ 'modifyField', 'filearchive', 'fa_id', 'patch-filearchive-fa_id.sql' ],
119 [ 'modifyField', 'image', 'img_major_mime', 'patch-image-img_major_mime-default.sql' ],
120 [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
121 [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
122 [ 'modifyField', 'page_restrictions', 'pr_page', 'patch-page_restrictions-pr_page.sql' ],
123 [ 'modifyField', 'page_props', 'pp_page', 'patch-page_props-pp_page.sql' ],
124 [ 'modifyField', 'ipblocks_restrictions', 'ir_value', 'patch-ipblocks_restrictions-ir_value.sql' ],
125 [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
128 [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
129 [ 'modifyField', 'ipblocks_restrictions', 'ir_ipb_id', 'patch-ipblocks_restrictions-ir_ipb_id.sql' ],
130 [ 'modifyField', 'ipblocks', 'ipb_id', 'patch-ipblocks-ipb_id.sql' ],
131 [ 'modifyField', 'user', 'user_editcount', 'patch-user-user_editcount.sql' ],
132 [ 'runMaintenance', MigrateRevisionActorTemp
::class ],
133 [ 'dropTable', 'revision_actor_temp' ],
134 [ 'runMaintenance', UpdateRestrictions
::class ],
135 [ 'dropField', 'page', 'page_restrictions', 'patch-page-drop-page_restrictions.sql' ],
136 [ 'migrateTemplatelinks' ],
137 [ 'modifyField', 'templatelinks', 'tl_namespace', 'patch-templatelinks-tl_title-nullable.sql' ],
138 [ 'dropField', 'templatelinks', 'tl_title', 'patch-templatelinks-drop-tl_title.sql' ],
141 [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
144 [ 'addField', 'user', 'user_is_temp', 'patch-user-user_is_temp.sql' ],
145 [ 'runMaintenance', MigrateRevisionCommentTemp
::class ],
146 [ 'dropTable', 'revision_comment_temp' ],
147 [ 'runMaintenance', MigrateExternallinks
::class ],
148 [ 'modifyField', 'externallinks', 'el_to', 'patch-externallinks-el_to_default.sql' ],
149 [ 'addField', 'pagelinks', 'pl_target_id', 'patch-pagelinks-target_id.sql' ],
150 [ 'dropField', 'externallinks', 'el_to', 'patch-externallinks-drop-el_to.sql' ],
151 [ 'runMaintenance', FixInconsistentRedirects
::class ],
152 [ 'modifyField', 'image', 'img_size', 'patch-image-img_size_to_bigint.sql' ],
153 [ 'modifyField', 'filearchive', 'fa_size', 'patch-filearchive-fa_size_to_bigint.sql' ],
154 [ 'modifyField', 'oldimage', 'oi_size', 'patch-oldimage-oi_size_to_bigint.sql' ],
155 [ 'modifyField', 'uploadstash', 'us_size', 'patch-uploadstash-us_size_to_bigint.sql' ],
158 [ 'addField', 'user_autocreate_serial', 'uas_year', 'patch-user_autocreate_serial-uas_year.sql' ],
159 [ 'addTable', 'block_target', 'patch-block_target.sql' ],
160 [ 'dropIndex', 'categorylinks', 'cl_collation_ext', 'patch-drop-cl_collation_ext.sql' ],
161 [ 'runMaintenance', PopulateUserIsTemp
::class ],
162 [ 'dropIndex', 'sites', 'site_type', 'patch-sites-drop_indexes.sql' ],
163 [ 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-iwlinks-drop-iwl_prefix_from_title.sql' ],
166 [ 'migratePagelinks' ],
167 [ 'modifyField', 'revision', 'rev_id', 'patch-revision-cleanup.sql' ],
168 [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id-bigint.sql' ],
169 [ 'modifyField', 'change_tag', 'ct_rc_id', 'patch-change_tag-ct_rc_id.sql' ],
170 [ 'runMaintenance', \MigrateBlocks
::class ],
171 [ 'dropTable', 'ipblocks' ],
172 [ 'dropField', 'pagelinks', 'pl_title', 'patch-pagelinks-drop-pl_title.sql' ],
173 [ 'modifyField', 'page', 'page_links_updated', 'patch-page-page_links_updated-noinfinite.sql' ],
174 [ 'addPostDatabaseUpdateMaintenance', FixAutoblockLogTitles
::class ],
175 [ 'changeTableOption', 'searchindex', 'CONVERT TO CHARACTER SET utf8mb4', 'utf8mb4' ],
176 [ 'migrateSearchindex' ],
179 [ 'addTable', 'file', 'patch-file.sql' ],
180 [ 'addField', 'categorylinks', 'cl_target_id', 'patch-categorylinks-target_id.sql' ],
181 [ 'addTable', 'collation', 'patch-collation.sql' ],
185 protected function getInitialUpdateKeys() {
187 'filearchive-fa_major_mime-patch-fa_major_mime-chemical.sql',
188 'image-img_major_mime-patch-img_major_mime-chemical.sql',
189 'oldimage-oi_major_mime-patch-oi_major_mime-chemical.sql',
190 'user_groups-ug_group-patch-ug_group-length-increase-255.sql',
191 'user_former_groups-ufg_group-patch-ufg_group-length-increase-255.sql',
192 'user_properties-up_property-patch-up_property.sql',
196 public function getSchemaVars() {
197 global $wgDBTableOptions;
200 $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
201 $vars['wgDBTableOptions'] = str_replace(
204 $vars['wgDBTableOptions']
211 * Drops the default value from a field
214 * @param string $table
215 * @param string $field
217 protected function dropDefault( $table, $field ) {
218 $updateKey = "$table-$field-dropDefault";
220 if ( $this->updateRowExists( $updateKey ) ) {
224 $info = $this->db
->fieldInfo( $table, $field );
225 if ( $info && $info->defaultValue() !== false ) {
226 $this->output( "Removing '$table.$field' default value.\n" );
227 $table = $this->db
->tableName( $table );
228 $ret = $this->db
->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__
);
231 $this->insertUpdateRow( $updateKey );
237 * Set a default value for a field
240 * @param string $table
241 * @param string $field
242 * @param mixed $default
244 protected function setDefault( $table, $field, $default ) {
245 $info = $this->db
->fieldInfo( $table, $field );
246 if ( $info && $info->defaultValue() !== $default ) {
247 $this->output( "Changing '$table.$field' default value.\n" );
248 $table = $this->db
->tableName( $table );
250 "ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
251 . $this->db
->addQuotes( $default ), __METHOD__
257 * Change the table options of a table
260 * @param string $table
261 * @param string $tableOption Raw table option that should already have been escaped !!!!
262 * @param string $updateName
264 protected function changeTableOption( string $table, string $tableOption, string $updateName ) {
265 $updateKey = "$table-tableoption-$updateName";
266 if ( $this->updateRowExists( $updateKey ) ) {
270 $this->output( "Changing table options of '$table'.\n" );
271 $table = $this->db
->tableName( $table );
272 $ret = $this->db
->query(
273 "ALTER TABLE $table $tableOption",
278 $this->insertUpdateRow( $updateKey );
282 protected function migrateSearchindex() {
283 $updateKey = 'searchindex-pk-titlelength';
284 if ( !$this->tableExists( 'searchindex' ) ) {
288 $primaryIndexExists = $this->db
->indexExists( 'searchindex', 'PRIMARY' );
289 if ( $this->updateRowExists( $updateKey ) ||
$primaryIndexExists ) {
290 $this->output( "...searchindex table has already been migrated.\n" );
291 if ( !$this->updateRowExists( $updateKey ) ) {
292 $this->insertUpdateRow( $updateKey );
297 $apply = $this->applyPatch( 'patch-searchindex-pk-titlelength.sql', false, '...migrating searchindex table' );
300 $this->insertUpdateRow( $updateKey );
305 /** @deprecated class alias since 1.42 */
306 class_alias( MysqlUpdater
::class, 'MysqlUpdater' );