Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 116
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 / 115
0.00% covered (danger)
0.00%
0 / 7
420
0.00% covered (danger)
0.00%
0 / 1
 getCoreUpdateList
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
2
 getInitialUpdateKeys
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getSchemaVars
0.00% covered (danger)
0.00%
0 / 8
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
 changeTableOption
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 migrateSearchindex
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 * MySQL-specific updater.
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 * @ingroup Installer
9 */
10
11namespace MediaWiki\Installer;
12
13use FixInconsistentRedirects;
14use FixWrongPasswordPrefixes;
15use MediaWiki\Maintenance\FixAutoblockLogTitles;
16use MigrateExternallinks;
17use MigrateRevisionCommentTemp;
18use PopulateUserIsTemp;
19
20/**
21 * Mysql update list and mysql-specific update functions.
22 *
23 * @ingroup Installer
24 * @since 1.17
25 * @property \Wikimedia\Rdbms\DatabaseMySQL $db
26 */
27class MysqlUpdater extends DatabaseUpdater {
28    /** @inheritDoc */
29    protected function getCoreUpdateList() {
30        return [
31            // 1.40
32            [ 'addField', 'externallinks', 'el_to_path', 'patch-externallinks-el_to_path.sql' ],
33
34            // 1.41
35            [ 'addField', 'user', 'user_is_temp', 'patch-user-user_is_temp.sql' ],
36            [ 'runMaintenance', MigrateRevisionCommentTemp::class ],
37            [ 'dropTable', 'revision_comment_temp' ],
38            [ 'runMaintenance', MigrateExternallinks::class ],
39            [ 'modifyField', 'externallinks', 'el_to', 'patch-externallinks-el_to_default.sql' ],
40            [ 'addField', 'pagelinks', 'pl_target_id', 'patch-pagelinks-target_id.sql' ],
41            [ 'dropField', 'externallinks', 'el_to', 'patch-externallinks-drop-el_to.sql' ],
42            [ 'runMaintenance', FixInconsistentRedirects::class ],
43            [ 'modifyField', 'image', 'img_size', 'patch-image-img_size_to_bigint.sql' ],
44            [ 'modifyField', 'filearchive', 'fa_size', 'patch-filearchive-fa_size_to_bigint.sql' ],
45            [ 'modifyField', 'oldimage', 'oi_size', 'patch-oldimage-oi_size_to_bigint.sql' ],
46            [ 'modifyField', 'uploadstash', 'us_size', 'patch-uploadstash-us_size_to_bigint.sql' ],
47
48            // 1.42
49            [ 'addField', 'user_autocreate_serial', 'uas_year', 'patch-user_autocreate_serial-uas_year.sql' ],
50            [ 'addTable', 'block_target', 'patch-block_target.sql' ],
51            [ 'dropIndex', 'categorylinks', 'cl_collation_ext', 'patch-drop-cl_collation_ext.sql' ],
52            [ 'runMaintenance', PopulateUserIsTemp::class ],
53            [ 'dropIndex', 'sites', 'site_type', 'patch-sites-drop_indexes.sql' ],
54            [ 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-iwlinks-drop-iwl_prefix_from_title.sql' ],
55
56            // 1.43
57            [ 'migratePagelinks' ],
58            [ 'modifyField', 'revision', 'rev_id', 'patch-revision-cleanup.sql' ],
59            [ 'modifyField', 'recentchanges', 'rc_id', 'patch-recentchanges-rc_id-bigint.sql' ],
60            [ 'modifyField', 'change_tag', 'ct_rc_id', 'patch-change_tag-ct_rc_id.sql' ],
61            [ 'runMaintenance', \MigrateBlocks::class ],
62            [ 'dropTable', 'ipblocks' ],
63            [ 'dropField', 'pagelinks', 'pl_title', 'patch-pagelinks-drop-pl_title.sql' ],
64            [ 'modifyField', 'page', 'page_links_updated', 'patch-page-page_links_updated-noinfinite.sql' ],
65            [ 'addPostDatabaseUpdateMaintenance', FixAutoblockLogTitles::class ],
66            [ 'changeTableOption', 'searchindex', 'CONVERT TO CHARACTER SET utf8mb4', 'utf8mb4' ],
67            [ 'migrateSearchindex' ],
68
69            // 1.44
70            [ 'addTable', 'file', 'patch-file.sql' ],
71            [ 'addField', 'categorylinks', 'cl_target_id', 'patch-categorylinks-target_id.sql' ],
72            [ 'addTable', 'collation', 'patch-collation.sql' ],
73            [ 'dropTable', 'module_deps' ],
74
75            // 1.45
76            [ 'addTable', 'existencelinks', 'patch-existencelinks.sql' ],
77            [ 'runMaintenance', FixWrongPasswordPrefixes::class ],
78            [ 'addIndex', 'categorylinks', 'cl_timestamp_id', 'patch-categorylinks-cl_timestamp_id.sql' ],
79            [ 'migrateCategorylinks' ],
80            [ 'normalizeCollation' ],
81            [ 'modifyPrimaryKey', 'categorylinks', [ 'cl_from', 'cl_target_id' ], 'patch-categorylinks-pk.sql' ],
82            [ 'addIndex', 'recentchanges', 'rc_source_name_timestamp',
83                'patch-recentchanges-rc_source_name_timestamp.sql' ],
84            [ 'addIndex', 'recentchanges', 'rc_name_source_patrolled_timestamp',
85                'patch-recentchanges-rc_name_source_patrolled_timestamp.sql' ],
86            [ 'dropField', 'recentchanges', 'rc_new', 'patch-recentchanges-drop-rc_new.sql' ],
87            [ 'dropField', 'categorylinks', 'cl_to', 'patch-categorylinks-drop-cl_to-cl_collation.sql' ],
88
89            // 1.46
90            [ 'addTable', 'watchlist_label', 'patch-watchlist_label.sql' ],
91            [ 'dropField', 'recentchanges', 'rc_type', 'patch-recentchanges-drop-rc_type.sql' ],
92            [ 'dropField', 'archive', 'ar_sha1', 'patch-archive-drop-ar_sha1.sql' ],
93            [ 'dropField', 'revision', 'rev_sha1', 'patch-revision-drop-rev_sha1.sql' ],
94            [ 'dropField', 'objectcache', 'modtoken', 'patch-objectcache-drop-modtoken.sql' ],
95            [ 'addField', 'imagelinks', 'il_target_id', 'patch-imagelinks-add-il_target_id.sql' ],
96            [ 'migrateImagelinks' ],
97            [ 'modifyPrimaryKey', 'imagelinks', [ 'il_from', 'il_target_id' ], 'patch-imagelinks-pk.sql' ],
98            [ 'addMissingTalkPageWatchlistLabels' ],
99            [ 'dropField', 'imagelinks', 'il_to', 'patch-imagelinks-drop-il_to.sql' ],
100        ];
101    }
102
103    /** @inheritDoc */
104    protected function getInitialUpdateKeys() {
105        return [
106            'filearchive-fa_major_mime-patch-fa_major_mime-chemical.sql',
107            'image-img_major_mime-patch-img_major_mime-chemical.sql',
108            'oldimage-oi_major_mime-patch-oi_major_mime-chemical.sql',
109            'user_groups-ug_group-patch-ug_group-length-increase-255.sql',
110            'user_former_groups-ufg_group-patch-ufg_group-length-increase-255.sql',
111            'user_properties-up_property-patch-up_property.sql',
112        ];
113    }
114
115    /** @inheritDoc */
116    public function getSchemaVars() {
117        global $wgDBTableOptions;
118
119        $vars = [];
120        $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions );
121        $vars['wgDBTableOptions'] = str_replace(
122            'CHARSET=mysql4',
123            'CHARSET=binary',
124            $vars['wgDBTableOptions']
125        );
126
127        return $vars;
128    }
129
130    /**
131     * Drops the default value from a field
132     *
133     * @since 1.36
134     * @param string $table
135     * @param string $field
136     */
137    protected function dropDefault( $table, $field ) {
138        $updateKey = "$table-$field-dropDefault";
139
140        if ( $this->updateRowExists( $updateKey ) ) {
141            return;
142        }
143
144        $info = $this->db->fieldInfo( $table, $field );
145        if ( $info && $info->defaultValue() !== false ) {
146            $this->output( "Removing '$table.$field' default value.\n" );
147            $table = $this->db->tableName( $table );
148            $ret = $this->db->query( "ALTER TABLE $table ALTER COLUMN $field DROP DEFAULT", __METHOD__ );
149
150            if ( $ret ) {
151                $this->insertUpdateRow( $updateKey );
152            }
153        }
154    }
155
156    /**
157     * Set a default value for a field
158     *
159     * @since 1.36
160     * @param string $table
161     * @param string $field
162     * @param mixed $default
163     */
164    protected function setDefault( $table, $field, $default ) {
165        $info = $this->db->fieldInfo( $table, $field );
166        if ( $info && $info->defaultValue() !== $default ) {
167            $this->output( "Changing '$table.$field' default value.\n" );
168            $table = $this->db->tableName( $table );
169            $this->db->query(
170                "ALTER TABLE $table ALTER COLUMN $field SET DEFAULT "
171                . $this->db->addQuotes( $default ), __METHOD__
172            );
173        }
174    }
175
176    /**
177     * Change the table options of a table
178     *
179     * @since 1.43
180     * @param string $table
181     * @param string $tableOption Raw table option that should already have been escaped !!!!
182     * @param string $updateName
183     */
184    protected function changeTableOption( string $table, string $tableOption, string $updateName ) {
185        $updateKey = "$table-tableoption-$updateName";
186        if ( $this->updateRowExists( $updateKey ) ) {
187            return;
188        }
189
190        $this->output( "Changing table options of '$table'.\n" );
191        $table = $this->db->tableName( $table );
192        $ret = $this->db->query(
193            "ALTER TABLE $table $tableOption",
194            __METHOD__
195        );
196
197        if ( $ret ) {
198            $this->insertUpdateRow( $updateKey );
199        }
200    }
201
202    protected function migrateSearchindex() {
203        $updateKey = 'searchindex-pk-titlelength';
204        if ( !$this->tableExists( 'searchindex' ) ) {
205            return;
206        }
207
208        $primaryIndexExists = $this->db->indexExists( 'searchindex', 'PRIMARY', __METHOD__ );
209        if ( $this->updateRowExists( $updateKey ) || $primaryIndexExists ) {
210            $this->outputApplied( "...searchindex table has already been migrated.\n" );
211            if ( !$this->updateRowExists( $updateKey ) ) {
212                $this->insertUpdateRow( $updateKey );
213            }
214            return;
215        }
216
217        $apply = $this->applyPatch( 'patch-searchindex-pk-titlelength.sql', false, '...migrating searchindex table' );
218
219        if ( $apply ) {
220            $this->insertUpdateRow( $updateKey );
221        }
222    }
223}
224
225/** @deprecated class alias since 1.42 */
226class_alias( MysqlUpdater::class, 'MysqlUpdater' );