Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 182
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
MysqlUpdater
0.00% covered (danger)
0.00%
0 / 181
0.00% covered (danger)
0.00%
0 / 7
462
0.00% covered (danger)
0.00%
0 / 1
 getCoreUpdateList
0.00% covered (danger)
0.00%
0 / 119
0.00% covered (danger)
0.00%
0 / 1
2
 indexHasField
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 doLanguageLinksLengthSync
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 doFixIpbAddressUniqueIndex
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getSchemaVars
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 dropDefault
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 setDefault
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * MySQL-specific updater.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Installer
23 */
24
25namespace MediaWiki\Installer;
26
27use FixInconsistentRedirects;
28use MigrateExternallinks;
29use MigrateRevisionActorTemp;
30use MigrateRevisionCommentTemp;
31use PopulateUserIsTemp;
32use UpdateRestrictions;
33
34/**
35 * Mysql update list and mysql-specific update functions.
36 *
37 * @ingroup Installer
38 * @since 1.17
39 * @property \Wikimedia\Rdbms\DatabaseMySQL $db
40 */
41class MysqlUpdater extends DatabaseUpdater {
42    protected function getCoreUpdateList() {
43        return [
44            // 1.35
45            [ 'addField', 'revision', 'rev_actor', 'patch-revision-actor-comment-MCR.sql' ],
46            [ 'addTable', 'watchlist_expiry', 'patch-watchlist_expiry.sql' ],
47            [ 'modifyField', 'page', 'page_restrictions', 'patch-page_restrictions-null.sql' ],
48            [ 'renameIndex', 'ipblocks', 'ipb_address', 'ipb_address_unique', false,
49                'patch-ipblocks-rename-ipb_address.sql' ],
50            [ 'dropField', 'archive', 'ar_text_id', 'patch-archive-MCR.sql' ],
51            [ 'doLanguageLinksLengthSync' ],
52            [ 'doFixIpbAddressUniqueIndex' ],
53            [ 'modifyField', 'actor', 'actor_name', 'patch-actor-actor_name-varbinary.sql' ],
54            [ 'modifyField', 'sites', 'site_global_key', 'patch-sites-site_global_key.sql' ],
55            [ 'modifyField', 'iwlinks', 'iwl_prefix', 'patch-extend-iwlinks-iwl_prefix.sql' ],
56
57            // 1.36
58            [ 'modifyField', 'redirect', 'rd_title', 'patch-redirect-rd_title-varbinary.sql' ],
59            [ 'modifyField', 'pagelinks', 'pl_title', 'patch-pagelinks-pl_title-varbinary.sql' ],
60            [ 'modifyField', 'templatelinks', 'tl_title', 'patch-templatelinks-tl_title-varbinary.sql' ],
61            [ 'modifyField', 'imagelinks', 'il_to', 'patch-imagelinks-il_to-varbinary.sql' ],
62            [ 'modifyField', 'langlinks', 'll_title', 'patch-langlinks-ll_title-varbinary.sql' ],
63            [ 'modifyField', 'iwlinks', 'iwl_title', 'patch-iwlinks-iwl_title-varbinary.sql' ],
64            [ 'modifyField', 'category', 'cat_title', 'patch-category-cat_title-varbinary.sql' ],
65            [ 'modifyField', 'querycache', 'qc_title', 'patch-querycache-qc_title-varbinary.sql' ],
66            [ 'modifyField', 'querycachetwo', 'qcc_title', 'patch-querycachetwo-qcc_title-varbinary.sql' ],
67            [ 'modifyField', 'watchlist', 'wl_title', 'patch-watchlist-wl_title-varbinary.sql' ],
68            [ 'modifyField', 'user_newtalk', 'user_last_timestamp',
69                'patch-user_newtalk-user_last_timestamp-binary.sql'
70            ],
71            [ 'modifyField', 'protected_titles', 'pt_title', 'patch-protected_titles-pt_title-varbinary.sql' ],
72            [ 'dropDefault', 'protected_titles', 'pt_expiry' ],
73            [ 'dropDefault', 'ip_changes', 'ipc_rev_timestamp' ],
74            [ 'modifyField', 'ipblocks_restrictions', 'ir_type', 'patch-ipblocks_restrictions-ir_type.sql' ],
75            [ 'renameIndex', 'watchlist', 'namespace_title', 'wl_namespace_title', false,
76                'patch-watchlist-namespace_title-rename-index.sql' ],
77            [ 'modifyField', 'job', 'job_title', 'patch-job-job_title-varbinary.sql' ],
78            [ 'modifyField', 'job', 'job_timestamp', 'patch-job_job_timestamp.sql' ],
79            [ 'modifyField', 'job', 'job_token_timestamp', 'patch-job_job_token_timestamp.sql' ],
80            [ 'modifyField', 'watchlist', 'wl_notificationtimestamp', 'patch-watchlist-wl_notificationtimestamp.sql' ],
81            [ 'modifyField', 'slot_roles', 'role_id', 'patch-slot_roles-role_id.sql' ],
82            [ 'modifyField', 'content_models', 'model_id', 'patch-content_models-model_id.sql' ],
83            [ 'modifyField', 'categorylinks', 'cl_to', 'patch-categorylinks-cl_to-varbinary.sql' ],
84            [ 'modifyField', 'logging', 'log_title', 'patch-logging-log_title-varbinary.sql' ],
85            [ 'modifyField', 'uploadstash', 'us_timestamp', 'patch-uploadstash-us_timestamp.sql' ],
86            [ 'renameIndex', 'user_properties', 'user_properties_property', 'up_property', false,
87                'patch-user_properties-rename-index.sql' ],
88            [ 'renameIndex', 'sites', 'sites_global_key', 'site_global_key', false, 'patch-sites-rename-indexes.sql' ],
89            [ 'renameIndex', 'logging', 'type_time', 'log_type_time', false, 'patch-logging-rename-indexes.sql' ],
90            [ 'modifyField', 'filearchive', 'fa_name', 'patch-filearchive-fa_name.sql' ],
91            [ 'dropDefault', 'filearchive', 'fa_deleted_timestamp' ],
92            [ 'dropDefault', 'filearchive', 'fa_timestamp' ],
93            [ 'modifyField', 'oldimage', 'oi_name', 'patch-oldimage-oi_name-varbinary.sql' ],
94            [ 'dropDefault', 'oldimage', 'oi_timestamp' ],
95            [ 'modifyField', 'objectcache', 'exptime', 'patch-objectcache-exptime-notnull.sql' ],
96            [ 'dropDefault', 'ipblocks', 'ipb_timestamp' ],
97            [ 'dropDefault', 'ipblocks', 'ipb_expiry' ],
98            [ 'renameIndex', 'archive', 'name_title_timestamp', 'ar_name_title_timestamp', false,
99                'patch-archive-rename-name_title_timestamp-index.sql' ],
100            [ 'modifyField', 'image', 'img_name', 'patch-image-img_name-varbinary.sql' ],
101            [ 'dropDefault', 'image', 'img_timestamp' ],
102            [ 'modifyField', 'image', 'img_timestamp', 'patch-image-img_timestamp.sql' ],
103            [ 'renameIndex', 'site_identifiers', 'site_ids_key', 'si_key', false,
104                'patch-site_identifiers-rename-indexes.sql' ],
105            [ 'modifyField', 'recentchanges', 'rc_title', 'patch-recentchanges-rc_title-varbinary.sql' ],
106            [ 'dropDefault', 'recentchanges', 'rc_timestamp' ],
107            [ 'modifyField', 'recentchanges', 'rc_timestamp', 'patch-recentchanges-rc_timestamp.sql' ],
108            [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id.sql' ],
109            [ 'renameIndex', 'recentchanges', 'new_name_timestamp', 'rc_new_name_timestamp', false,
110                'patch-recentchanges-rc_new_name_timestamp.sql' ],
111            [ 'dropDefault', 'archive', 'ar_timestamp' ],
112            [ 'modifyField', 'archive', 'ar_title', 'patch-archive-ar_title-varbinary.sql' ],
113            [ 'modifyField', 'page', 'page_title', 'patch-page-page_title-varbinary.sql' ],
114            [ 'dropDefault', 'page', 'page_touched' ],
115            [ 'modifyField', 'user', 'user_name', 'patch-user_table-updates.sql' ],
116
117            // 1.37
118            [ 'renameIndex', 'revision', 'page_timestamp', 'rev_page_timestamp', false,
119                'patch-revision-rename-index.sql' ],
120            [ 'addField', 'objectcache', 'modtoken', 'patch-objectcache-modtoken.sql' ],
121            [ 'dropDefault', 'revision', 'rev_timestamp' ],
122            [ 'addIndex', 'oldimage', 'oi_timestamp', 'patch-oldimage-oi_timestamp.sql' ],
123            [ 'renameIndex', 'page', 'name_title', 'page_name_title', false, 'patch-page-rename-name_title-index.sql' ],
124            [ 'renameIndex', 'change_tag', 'change_tag_rc_tag_id', 'ct_rc_tag_id', false,
125                'patch-change_tag-rename-indexes.sql' ],
126
127            // 1.38
128            [ 'doConvertDjvuMetadata' ],
129            [ 'dropField', 'page_restrictions', 'pr_user', 'patch-drop-page_restrictions-pr_user.sql' ],
130            [ 'modifyField', 'filearchive', 'fa_id', 'patch-filearchive-fa_id.sql' ],
131            [ 'modifyField', 'image', 'img_major_mime', 'patch-image-img_major_mime-default.sql' ],
132            [ 'addTable', 'linktarget', 'patch-linktarget.sql' ],
133            [ 'dropIndex', 'revision', 'rev_page_id', 'patch-drop-rev_page_id.sql' ],
134            [ 'modifyField', 'page_restrictions', 'pr_page', 'patch-page_restrictions-pr_page.sql' ],
135            [ 'modifyField', 'page_props', 'pp_page', 'patch-page_props-pp_page.sql' ],
136            [ 'modifyField', 'ipblocks_restrictions', 'ir_value', 'patch-ipblocks_restrictions-ir_value.sql' ],
137            [ 'addField', 'templatelinks', 'tl_target_id', 'patch-templatelinks-target_id.sql' ],
138
139            // 1.39
140            [ 'addTable', 'user_autocreate_serial', 'patch-user_autocreate_serial.sql' ],
141            [ 'modifyField', 'ipblocks_restrictions', 'ir_ipb_id', 'patch-ipblocks_restrictions-ir_ipb_id.sql' ],
142            [ 'modifyField', 'ipblocks', 'ipb_id', 'patch-ipblocks-ipb_id.sql' ],
143            [ 'modifyField', 'user', 'user_editcount', 'patch-user-user_editcount.sql' ],
144            [ 'runMaintenance', MigrateRevisionActorTemp::class, 'maintenance/migrateRevisionActorTemp.php' ],
145            [ 'dropTable', 'revision_actor_temp' ],
146            [ 'runMaintenance', UpdateRestrictions::class, 'maintenance/updateRestrictions.php' ],
147            [ 'dropField', 'page', 'page_restrictions', 'patch-page-drop-page_restrictions.sql' ],
148            [ 'migrateTemplatelinks' ],
149            [ 'modifyField', 'templatelinks', 'tl_namespace', 'patch-templatelinks-tl_title-nullable.sql' ],
150            [ 'dropField', 'templatelinks', 'tl_title', 'patch-templatelinks-drop-tl_title.sql' ],
151
152            // 1.40
153            [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
154
155            // 1.41
156            [ 'addField', 'user', 'user_is_temp', 'patch-user-user_is_temp.sql' ],
157            [ 'runMaintenance', MigrateRevisionCommentTemp::class, 'maintenance/migrateRevisionCommentTemp.php' ],
158            [ 'dropTable', 'revision_comment_temp' ],
159            [ 'runMaintenance', MigrateExternallinks::class, 'maintenance/migrateExternallinks.php' ],
160            [ 'modifyField', 'externallinks', 'el_to', 'patch-externallinks-el_to_default.sql' ],
161            [ 'addField', 'pagelinks', 'pl_target_id', 'patch-pagelinks-target_id.sql' ],
162            [ 'dropField', 'externallinks', 'el_to', 'patch-externallinks-drop-el_to.sql' ],
163            [ 'runMaintenance', FixInconsistentRedirects::class, 'maintenance/fixInconsistentRedirects.php' ],
164            [ 'modifyField', 'image', 'img_size', 'patch-image-img_size_to_bigint.sql' ],
165            [ 'modifyField', 'filearchive', 'fa_size', 'patch-filearchive-fa_size_to_bigint.sql' ],
166            [ 'modifyField', 'oldimage', 'oi_size', 'patch-oldimage-oi_size_to_bigint.sql' ],
167            [ 'modifyField', 'uploadstash', 'us_size', 'patch-uploadstash-us_size_to_bigint.sql' ],
168
169            // 1.42
170            [ 'addField', 'user_autocreate_serial', 'uas_year', 'patch-user_autocreate_serial-uas_year.sql' ],
171            [ 'addTable', 'block_target', 'patch-block_target.sql' ],
172            [ 'dropIndex', 'categorylinks', 'cl_collation_ext', 'patch-drop-cl_collation_ext.sql' ],
173            [ 'runMaintenance', PopulateUserIsTemp::class, 'maintenance/populateUserIsTemp.php' ],
174            [ 'dropIndex', 'sites', 'site_type', 'patch-sites-drop_indexes.sql' ],
175            [ 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-iwlinks-drop-iwl_prefix_from_title.sql' ],
176        ];
177    }
178
179    /**
180     * Check whether an index contains a field
181     *
182     * @param string $table Table name
183     * @param string $index Index name to check
184     * @param string $field Field that should be in the index
185     * @return bool
186     */
187    protected function indexHasField( $table, $index, $field ) {
188        $info = $this->db->indexInfo( $table, $index, __METHOD__ );
189        if ( $info ) {
190            foreach ( $info as $row ) {
191                if ( $row->Column_name == $field ) {
192                    $this->output( "...index $index on table $table includes field $field.\n" );
193                    return true;
194                }
195            }
196        }
197        $this->output( "...index $index on table $table has no field $field; added.\n" );
198
199        return false;
200    }
201
202    protected function doLanguageLinksLengthSync() {
203        $sync = [
204            [ 'table' => 'l10n_cache', 'field' => 'lc_lang', 'file' => 'patch-l10n_cache-lc_lang-35.sql' ],
205            [ 'table' => 'langlinks', 'field' => 'll_lang', 'file' => 'patch-langlinks-ll_lang-35.sql' ],
206            [ 'table' => 'sites', 'field' => 'site_language', 'file' => 'patch-sites-site_language-35.sql' ],
207        ];
208
209        foreach ( $sync as $s ) {
210            $table = $this->db->tableName( $s['table'] );
211            $field = $s['field'];
212            $res = $this->db->query( "SHOW COLUMNS FROM $table LIKE '$field'", __METHOD__ );
213            $row = $res->fetchObject();
214
215            if ( $row && $row->Type !== "varbinary(35)" ) {
216                $this->applyPatch(
217                    $s['file'],
218                    false,
219                    "Updating length of $field in $table"
220                );
221            } else {
222                $this->output( "...$field is up-to-date.\n" );
223            }
224        }
225    }
226
227    protected function doFixIpbAddressUniqueIndex() {
228        if ( !$this->doTable( 'ipblocks' ) ) {
229            return;
230        }
231
232        if ( !$this->indexHasField( 'ipblocks', 'ipb_address_unique', 'ipb_anon_only' ) ) {
233            $this->output( "...ipb_address_unique index up-to-date.\n" );
234            return;
235        }
236
237        $this->applyPatch(
238            'patch-ipblocks-fix-ipb_address_unique.sql',
239            false,
240            'Removing ipb_anon_only column from ipb_address_unique index'
241        );
242    }
243
244    public function getSchemaVars() {
245        global $wgDBTableOptions;
246
247        $vars = [];
248        $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
249        $vars['wgDBTableOptions'] = str_replace(
250            'CHARSET=mysql4',
251            'CHARSET=binary',
252            $vars['wgDBTableOptions']
253        );
254
255        return $vars;
256    }
257
258    /**
259     * Drops the default value from a field
260     *
261     * @since 1.36
262     * @param string $table
263     * @param string $field
264     */
265    protected function dropDefault( $table, $field ) {
266        $updateKey = "$table-$field-dropDefault";
267
268        if ( $this->updateRowExists( $updateKey ) ) {
269            return;
270        }
271
272        $info = $this->db->fieldInfo( $table, $field );
273        if ( $info && $info->defaultValue() !== false ) {
274            $this->output( "Removing '$table.$field' default value.\n" );
275            $table = $this->db->tableName( $table );
276            $ret = $this->db->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__ );
277
278            if ( $ret ) {
279                $this->insertUpdateRow( $updateKey );
280            }
281        }
282    }
283
284    /**
285     * Set a default value for a field
286     *
287     * @since 1.36
288     * @param string $table
289     * @param string $field
290     * @param mixed $default
291     */
292    protected function setDefault( $table, $field, $default ) {
293        $info = $this->db->fieldInfo( $table, $field );
294        if ( $info && $info->defaultValue() !== $default ) {
295            $this->output( "Changing '$table.$field' default value.\n" );
296            $table = $this->db->tableName( $table );
297            $this->db->query(
298                "ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
299                . $this->db->addQuotes( $default ), __METHOD__
300            );
301        }
302    }
303
304}
305
306/** @deprecated class alias since 1.41 */
307class_alias( MysqlUpdater::class, 'MysqlUpdater' );