Merge "Fix two PHPCS warnings in NaiveForeignTitleFactory.php"
[lhc/web/wiklou.git] / includes / installer / PostgresUpdater.php
1 <?php
2 /**
3 * PostgreSQL-specific updater.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 /**
25 * Class for handling updates to Postgres databases.
26 *
27 * @ingroup Deployment
28 * @since 1.17
29 */
30 class PostgresUpdater extends DatabaseUpdater {
31
32 /**
33 * @var DatabasePostgres
34 */
35 protected $db;
36
37 /**
38 * @todo FIXME: Postgres should use sequential updates like Mysql, Sqlite
39 * and everybody else. It never got refactored like it should've.
40 * @return array
41 */
42 protected function getCoreUpdateList() {
43 return array(
44 # rename tables 1.7.3
45 # r15791 Change reserved word table names "user" and "text"
46 array( 'renameTable', 'user', 'mwuser' ),
47 array( 'renameTable', 'text', 'pagecontent' ),
48 array( 'renameIndex', 'mwuser', 'user_pkey', 'mwuser_pkey' ),
49 array( 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ),
50 array( 'renameIndex', 'pagecontent', 'text_pkey', 'pagecontent_pkey' ),
51
52 # renamed sequences
53 array( 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq' ),
54 array( 'renameSequence', 'rev_rev_id_val', 'revision_rev_id_seq' ),
55 array( 'renameSequence', 'text_old_id_val', 'text_old_id_seq' ),
56 array( 'renameSequence', 'rc_rc_id_seq', 'recentchanges_rc_id_seq' ),
57 array( 'renameSequence', 'log_log_id_seq', 'logging_log_id_seq' ),
58 array( 'renameSequence', 'pr_id_val', 'page_restrictions_pr_id_seq' ),
59 array( 'renameSequence', 'us_id_seq', 'uploadstash_us_id_seq' ),
60
61 # since r58263
62 array( 'renameSequence', 'category_id_seq', 'category_cat_id_seq' ),
63
64 # new sequences if not renamed above
65 array( 'addSequence', 'logging', false, 'logging_log_id_seq' ),
66 array( 'addSequence', 'page_restrictions', false, 'page_restrictions_pr_id_seq' ),
67 array( 'addSequence', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ),
68 array( 'addSequence', 'archive', false, 'archive_ar_id_seq' ),
69 array( 'addSequence', 'externallinks', false, 'externallinks_el_id_seq' ),
70
71 # new tables
72 array( 'addTable', 'category', 'patch-category.sql' ),
73 array( 'addTable', 'page', 'patch-page.sql' ),
74 array( 'addTable', 'querycachetwo', 'patch-querycachetwo.sql' ),
75 array( 'addTable', 'page_props', 'patch-page_props.sql' ),
76 array( 'addTable', 'page_restrictions', 'patch-page_restrictions.sql' ),
77 array( 'addTable', 'profiling', 'patch-profiling.sql' ),
78 array( 'addTable', 'protected_titles', 'patch-protected_titles.sql' ),
79 array( 'addTable', 'redirect', 'patch-redirect.sql' ),
80 array( 'addTable', 'updatelog', 'patch-updatelog.sql' ),
81 array( 'addTable', 'change_tag', 'patch-change_tag.sql' ),
82 array( 'addTable', 'tag_summary', 'patch-tag_summary.sql' ),
83 array( 'addTable', 'valid_tag', 'patch-valid_tag.sql' ),
84 array( 'addTable', 'user_properties', 'patch-user_properties.sql' ),
85 array( 'addTable', 'log_search', 'patch-log_search.sql' ),
86 array( 'addTable', 'l10n_cache', 'patch-l10n_cache.sql' ),
87 array( 'addTable', 'iwlinks', 'patch-iwlinks.sql' ),
88 array( 'addTable', 'msg_resource', 'patch-msg_resource.sql' ),
89 array( 'addTable', 'msg_resource_links', 'patch-msg_resource_links.sql' ),
90 array( 'addTable', 'module_deps', 'patch-module_deps.sql' ),
91 array( 'addTable', 'uploadstash', 'patch-uploadstash.sql' ),
92 array( 'addTable', 'user_former_groups', 'patch-user_former_groups.sql' ),
93 array( 'addTable', 'sites', 'patch-sites.sql' ),
94
95 # Needed before new field
96 array( 'convertArchive2' ),
97
98 # new fields
99 array( 'addPgField', 'updatelog', 'ul_value', 'TEXT' ),
100 array( 'addPgField', 'archive', 'ar_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
101 array( 'addPgField', 'archive', 'ar_len', 'INTEGER' ),
102 array( 'addPgField', 'archive', 'ar_page_id', 'INTEGER' ),
103 array( 'addPgField', 'archive', 'ar_parent_id', 'INTEGER' ),
104 array( 'addPgField', 'archive', 'ar_content_model', 'TEXT' ),
105 array( 'addPgField', 'archive', 'ar_content_format', 'TEXT' ),
106 array( 'addPgField', 'categorylinks', 'cl_sortkey_prefix', "TEXT NOT NULL DEFAULT ''" ),
107 array( 'addPgField', 'categorylinks', 'cl_collation', "TEXT NOT NULL DEFAULT 0" ),
108 array( 'addPgField', 'categorylinks', 'cl_type', "TEXT NOT NULL DEFAULT 'page'" ),
109 array( 'addPgField', 'image', 'img_sha1', "TEXT NOT NULL DEFAULT ''" ),
110 array( 'addPgField', 'ipblocks', 'ipb_allow_usertalk', 'SMALLINT NOT NULL DEFAULT 0' ),
111 array( 'addPgField', 'ipblocks', 'ipb_anon_only', 'SMALLINT NOT NULL DEFAULT 0' ),
112 array( 'addPgField', 'ipblocks', 'ipb_by_text', "TEXT NOT NULL DEFAULT ''" ),
113 array( 'addPgField', 'ipblocks', 'ipb_block_email', 'SMALLINT NOT NULL DEFAULT 0' ),
114 array( 'addPgField', 'ipblocks', 'ipb_create_account', 'SMALLINT NOT NULL DEFAULT 1' ),
115 array( 'addPgField', 'ipblocks', 'ipb_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
116 array( 'addPgField', 'ipblocks', 'ipb_enable_autoblock', 'SMALLINT NOT NULL DEFAULT 1' ),
117 array( 'addPgField', 'ipblocks', 'ipb_parent_block_id',
118 'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ' .
119 'ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ),
120 array( 'addPgField', 'filearchive', 'fa_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
121 array( 'addPgField', 'filearchive', 'fa_sha1', "TEXT NOT NULL DEFAULT ''" ),
122 array( 'addPgField', 'logging', 'log_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
123 array( 'addPgField', 'logging', 'log_id',
124 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ),
125 array( 'addPgField', 'logging', 'log_params', 'TEXT' ),
126 array( 'addPgField', 'mwuser', 'user_editcount', 'INTEGER' ),
127 array( 'addPgField', 'mwuser', 'user_newpass_time', 'TIMESTAMPTZ' ),
128 array( 'addPgField', 'oldimage', 'oi_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
129 array( 'addPgField', 'oldimage', 'oi_major_mime', "TEXT NOT NULL DEFAULT 'unknown'" ),
130 array( 'addPgField', 'oldimage', 'oi_media_type', 'TEXT' ),
131 array( 'addPgField', 'oldimage', 'oi_metadata', "BYTEA NOT NULL DEFAULT ''" ),
132 array( 'addPgField', 'oldimage', 'oi_minor_mime', "TEXT NOT NULL DEFAULT 'unknown'" ),
133 array( 'addPgField', 'oldimage', 'oi_sha1', "TEXT NOT NULL DEFAULT ''" ),
134 array( 'addPgField', 'page', 'page_content_model', 'TEXT' ),
135 array( 'addPgField', 'page_restrictions', 'pr_id',
136 "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_seq')" ),
137 array( 'addPgField', 'profiling', 'pf_memory', 'NUMERIC(18,10) NOT NULL DEFAULT 0' ),
138 array( 'addPgField', 'recentchanges', 'rc_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
139 array( 'addPgField', 'recentchanges', 'rc_log_action', 'TEXT' ),
140 array( 'addPgField', 'recentchanges', 'rc_log_type', 'TEXT' ),
141 array( 'addPgField', 'recentchanges', 'rc_logid', 'INTEGER NOT NULL DEFAULT 0' ),
142 array( 'addPgField', 'recentchanges', 'rc_new_len', 'INTEGER' ),
143 array( 'addPgField', 'recentchanges', 'rc_old_len', 'INTEGER' ),
144 array( 'addPgField', 'recentchanges', 'rc_params', 'TEXT' ),
145 array( 'addPgField', 'redirect', 'rd_interwiki', 'TEXT NULL' ),
146 array( 'addPgField', 'redirect', 'rd_fragment', 'TEXT NULL' ),
147 array( 'addPgField', 'revision', 'rev_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
148 array( 'addPgField', 'revision', 'rev_len', 'INTEGER' ),
149 array( 'addPgField', 'revision', 'rev_parent_id', 'INTEGER DEFAULT NULL' ),
150 array( 'addPgField', 'revision', 'rev_content_model', 'TEXT' ),
151 array( 'addPgField', 'revision', 'rev_content_format', 'TEXT' ),
152 array( 'addPgField', 'site_stats', 'ss_active_users', "INTEGER DEFAULT '-1'" ),
153 array( 'addPgField', 'user_newtalk', 'user_last_timestamp', 'TIMESTAMPTZ' ),
154 array( 'addPgField', 'logging', 'log_user_text', "TEXT NOT NULL DEFAULT ''" ),
155 array( 'addPgField', 'logging', 'log_page', 'INTEGER' ),
156 array( 'addPgField', 'interwiki', 'iw_api', "TEXT NOT NULL DEFAULT ''" ),
157 array( 'addPgField', 'interwiki', 'iw_wikiid', "TEXT NOT NULL DEFAULT ''" ),
158 array( 'addPgField', 'revision', 'rev_sha1', "TEXT NOT NULL DEFAULT ''" ),
159 array( 'addPgField', 'archive', 'ar_sha1', "TEXT NOT NULL DEFAULT ''" ),
160 array( 'addPgField', 'uploadstash', 'us_chunk_inx', "INTEGER NULL" ),
161 array( 'addPgField', 'job', 'job_timestamp', "TIMESTAMPTZ" ),
162 array( 'addPgField', 'job', 'job_random', "INTEGER NOT NULL DEFAULT 0" ),
163 array( 'addPgField', 'job', 'job_attempts', "INTEGER NOT NULL DEFAULT 0" ),
164 array( 'addPgField', 'job', 'job_token', "TEXT NOT NULL DEFAULT ''" ),
165 array( 'addPgField', 'job', 'job_token_timestamp', "TIMESTAMPTZ" ),
166 array( 'addPgField', 'job', 'job_sha1', "TEXT NOT NULL DEFAULT ''" ),
167 array( 'addPgField', 'archive', 'ar_id',
168 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('archive_ar_id_seq')" ),
169 array( 'addPgField', 'externallinks', 'el_id',
170 "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('externallinks_el_id_seq')" ),
171 array( 'addPgField', 'uploadstash', 'us_props', "BYTEA" ),
172
173 # type changes
174 array( 'changeField', 'archive', 'ar_deleted', 'smallint', '' ),
175 array( 'changeField', 'archive', 'ar_minor_edit', 'smallint',
176 'ar_minor_edit::smallint DEFAULT 0' ),
177 array( 'changeField', 'filearchive', 'fa_deleted', 'smallint', '' ),
178 array( 'changeField', 'filearchive', 'fa_height', 'integer', '' ),
179 array( 'changeField', 'filearchive', 'fa_metadata', 'bytea', "decode(fa_metadata,'escape')" ),
180 array( 'changeField', 'filearchive', 'fa_size', 'integer', '' ),
181 array( 'changeField', 'filearchive', 'fa_width', 'integer', '' ),
182 array( 'changeField', 'filearchive', 'fa_storage_group', 'text', '' ),
183 array( 'changeField', 'filearchive', 'fa_storage_key', 'text', '' ),
184 array( 'changeField', 'image', 'img_metadata', 'bytea', "decode(img_metadata,'escape')" ),
185 array( 'changeField', 'image', 'img_size', 'integer', '' ),
186 array( 'changeField', 'image', 'img_width', 'integer', '' ),
187 array( 'changeField', 'image', 'img_height', 'integer', '' ),
188 array( 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint' ),
189 array( 'changeField', 'interwiki', 'iw_trans', 'smallint', 'iw_trans::smallint DEFAULT 0' ),
190 array( 'changeField', 'ipblocks', 'ipb_auto', 'smallint', 'ipb_auto::smallint DEFAULT 0' ),
191 array( 'changeField', 'ipblocks', 'ipb_anon_only', 'smallint',
192 "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
193 array( 'changeField', 'ipblocks', 'ipb_create_account', 'smallint',
194 "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ),
195 array( 'changeField', 'ipblocks', 'ipb_enable_autoblock', 'smallint',
196 "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ),
197 array( 'changeField', 'ipblocks', 'ipb_block_email', 'smallint',
198 "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ),
199 array( 'changeField', 'ipblocks', 'ipb_address', 'text', 'ipb_address::text' ),
200 array( 'changeField', 'ipblocks', 'ipb_deleted', 'smallint', 'ipb_deleted::smallint DEFAULT 0' ),
201 array( 'changeField', 'mwuser', 'user_token', 'text', '' ),
202 array( 'changeField', 'mwuser', 'user_email_token', 'text', '' ),
203 array( 'changeField', 'objectcache', 'keyname', 'text', '' ),
204 array( 'changeField', 'oldimage', 'oi_height', 'integer', '' ),
205 array( 'changeField', 'oldimage', 'oi_metadata', 'bytea', "decode(img_metadata,'escape')" ),
206 array( 'changeField', 'oldimage', 'oi_size', 'integer', '' ),
207 array( 'changeField', 'oldimage', 'oi_width', 'integer', '' ),
208 array( 'changeField', 'page', 'page_is_redirect', 'smallint',
209 'page_is_redirect::smallint DEFAULT 0' ),
210 array( 'changeField', 'page', 'page_is_new', 'smallint', 'page_is_new::smallint DEFAULT 0' ),
211 array( 'changeField', 'querycache', 'qc_value', 'integer', '' ),
212 array( 'changeField', 'querycachetwo', 'qcc_value', 'integer', '' ),
213 array( 'changeField', 'recentchanges', 'rc_bot', 'smallint', 'rc_bot::smallint DEFAULT 0' ),
214 array( 'changeField', 'recentchanges', 'rc_deleted', 'smallint', '' ),
215 array( 'changeField', 'recentchanges', 'rc_minor', 'smallint', 'rc_minor::smallint DEFAULT 0' ),
216 array( 'changeField', 'recentchanges', 'rc_new', 'smallint', 'rc_new::smallint DEFAULT 0' ),
217 array( 'changeField', 'recentchanges', 'rc_type', 'smallint', 'rc_type::smallint DEFAULT 0' ),
218 array( 'changeField', 'recentchanges', 'rc_patrolled', 'smallint',
219 'rc_patrolled::smallint DEFAULT 0' ),
220 array( 'changeField', 'revision', 'rev_deleted', 'smallint', 'rev_deleted::smallint DEFAULT 0' ),
221 array( 'changeField', 'revision', 'rev_minor_edit', 'smallint',
222 'rev_minor_edit::smallint DEFAULT 0' ),
223 array( 'changeField', 'templatelinks', 'tl_namespace', 'smallint', 'tl_namespace::smallint' ),
224 array( 'changeField', 'user_newtalk', 'user_ip', 'text', 'host(user_ip)' ),
225 array( 'changeField', 'uploadstash', 'us_image_bits', 'smallint', '' ),
226 array( 'changeField', 'profiling', 'pf_time', 'float', '' ),
227 array( 'changeField', 'profiling', 'pf_memory', 'float', '' ),
228
229 # null changes
230 array( 'changeNullableField', 'oldimage', 'oi_bits', 'NULL' ),
231 array( 'changeNullableField', 'oldimage', 'oi_timestamp', 'NULL' ),
232 array( 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ),
233 array( 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ),
234 array( 'changeNullableField', 'image', 'img_metadata', 'NOT NULL' ),
235 array( 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL' ),
236 array( 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ),
237 array( 'changeNullableField', 'recentchanges', 'rc_cur_time', 'NULL' ),
238
239 array( 'checkOiDeleted' ),
240
241 # New indexes
242 array( 'addPgIndex', 'archive', 'archive_user_text', '(ar_user_text)' ),
243 array( 'addPgIndex', 'image', 'img_sha1', '(img_sha1)' ),
244 array( 'addPgIndex', 'ipblocks', 'ipb_parent_block_id', '(ipb_parent_block_id)' ),
245 array( 'addPgIndex', 'oldimage', 'oi_sha1', '(oi_sha1)' ),
246 array( 'addPgIndex', 'page', 'page_mediawiki_title', '(page_title) WHERE page_namespace = 8' ),
247 array( 'addPgIndex', 'pagelinks', 'pagelinks_title', '(pl_title)' ),
248 array( 'addPgIndex', 'page_props', 'pp_propname_page', '(pp_propname, pp_page)' ),
249 array( 'addPgIndex', 'revision', 'rev_text_id_idx', '(rev_text_id)' ),
250 array( 'addPgIndex', 'recentchanges', 'rc_timestamp_bot', '(rc_timestamp) WHERE rc_bot = 0' ),
251 array( 'addPgIndex', 'templatelinks', 'templatelinks_from', '(tl_from)' ),
252 array( 'addPgIndex', 'watchlist', 'wl_user', '(wl_user)' ),
253 array( 'addPgIndex', 'watchlist', 'wl_user_notificationtimestamp',
254 '(wl_user, wl_notificationtimestamp)' ),
255 array( 'addPgIndex', 'logging', 'logging_user_type_time',
256 '(log_user, log_type, log_timestamp)' ),
257 array( 'addPgIndex', 'logging', 'logging_page_id_time', '(log_page,log_timestamp)' ),
258 array( 'addPgIndex', 'iwlinks', 'iwl_prefix_from_title', '(iwl_prefix, iwl_from, iwl_title)' ),
259 array( 'addPgIndex', 'iwlinks', 'iwl_prefix_title_from', '(iwl_prefix, iwl_title, iwl_from)' ),
260 array( 'addPgIndex', 'job', 'job_timestamp_idx', '(job_timestamp)' ),
261 array( 'addPgIndex', 'job', 'job_sha1', '(job_sha1)' ),
262 array( 'addPgIndex', 'job', 'job_cmd_token', '(job_cmd, job_token, job_random)' ),
263 array( 'addPgIndex', 'job', 'job_cmd_token_id', '(job_cmd, job_token, job_id)' ),
264 array( 'addPgIndex', 'filearchive', 'fa_sha1', '(fa_sha1)' ),
265 array( 'addPgIndex', 'logging', 'logging_user_text_type_time',
266 '(log_user_text, log_type, log_timestamp)' ),
267 array( 'addPgIndex', 'logging', 'logging_user_text_time', '(log_user_text, log_timestamp)' ),
268
269 array( 'checkIndex', 'pagelink_unique', array(
270 array( 'pl_from', 'int4_ops', 'btree', 0 ),
271 array( 'pl_namespace', 'int2_ops', 'btree', 0 ),
272 array( 'pl_title', 'text_ops', 'btree', 0 ),
273 ),
274 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ),
275 array( 'checkIndex', 'cl_sortkey', array(
276 array( 'cl_to', 'text_ops', 'btree', 0 ),
277 array( 'cl_sortkey', 'text_ops', 'btree', 0 ),
278 array( 'cl_from', 'int4_ops', 'btree', 0 ),
279 ),
280 'CREATE INDEX cl_sortkey ON "categorylinks" ' .
281 'USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ),
282 array( 'checkIndex', 'iwl_prefix_title_from', array(
283 array( 'iwl_prefix', 'text_ops', 'btree', 0 ),
284 array( 'iwl_title', 'text_ops', 'btree', 0 ),
285 array( 'iwl_from', 'int4_ops', 'btree', 0 ),
286 ),
287 'CREATE INDEX iwl_prefix_title_from ON "iwlinks" ' .
288 'USING "btree" ("iwl_prefix", "iwl_title", "iwl_from")' ),
289 array( 'checkIndex', 'logging_times', array(
290 array( 'log_timestamp', 'timestamptz_ops', 'btree', 0 ),
291 ),
292 'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ),
293 array( 'dropIndex', 'oldimage', 'oi_name' ),
294 array( 'checkIndex', 'oi_name_archive_name', array(
295 array( 'oi_name', 'text_ops', 'btree', 0 ),
296 array( 'oi_archive_name', 'text_ops', 'btree', 0 ),
297 ),
298 'CREATE INDEX "oi_name_archive_name" ON "oldimage" ' .
299 'USING "btree" ("oi_name", "oi_archive_name")' ),
300 array( 'checkIndex', 'oi_name_timestamp', array(
301 array( 'oi_name', 'text_ops', 'btree', 0 ),
302 array( 'oi_timestamp', 'timestamptz_ops', 'btree', 0 ),
303 ),
304 'CREATE INDEX "oi_name_timestamp" ON "oldimage" ' .
305 'USING "btree" ("oi_name", "oi_timestamp")' ),
306 array( 'checkIndex', 'page_main_title', array(
307 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
308 ),
309 'CREATE INDEX "page_main_title" ON "page" ' .
310 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ),
311 array( 'checkIndex', 'page_mediawiki_title', array(
312 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
313 ),
314 'CREATE INDEX "page_mediawiki_title" ON "page" ' .
315 'USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ),
316 array( 'checkIndex', 'page_project_title', array(
317 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
318 ),
319 'CREATE INDEX "page_project_title" ON "page" ' .
320 'USING "btree" ("page_title" "text_pattern_ops") ' .
321 'WHERE ("page_namespace" = 4)' ),
322 array( 'checkIndex', 'page_talk_title', array(
323 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
324 ),
325 'CREATE INDEX "page_talk_title" ON "page" ' .
326 'USING "btree" ("page_title" "text_pattern_ops") ' .
327 'WHERE ("page_namespace" = 1)' ),
328 array( 'checkIndex', 'page_user_title', array(
329 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
330 ),
331 'CREATE INDEX "page_user_title" ON "page" ' .
332 'USING "btree" ("page_title" "text_pattern_ops") WHERE ' .
333 '("page_namespace" = 2)' ),
334 array( 'checkIndex', 'page_utalk_title', array(
335 array( 'page_title', 'text_pattern_ops', 'btree', 0 ),
336 ),
337 'CREATE INDEX "page_utalk_title" ON "page" ' .
338 'USING "btree" ("page_title" "text_pattern_ops") ' .
339 'WHERE ("page_namespace" = 3)' ),
340 array( 'checkIndex', 'ts2_page_text', array(
341 array( 'textvector', 'tsvector_ops', 'gist', 0 ),
342 ),
343 'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ),
344 array( 'checkIndex', 'ts2_page_title', array(
345 array( 'titlevector', 'tsvector_ops', 'gist', 0 ),
346 ),
347 'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ),
348
349 array( 'checkOiNameConstraint' ),
350 array( 'checkPageDeletedTrigger' ),
351 array( 'checkRevUserFkey' ),
352 array( 'dropIndex', 'ipblocks', 'ipb_address' ),
353 array( 'checkIndex', 'ipb_address_unique', array(
354 array( 'ipb_address', 'text_ops', 'btree', 0 ),
355 array( 'ipb_user', 'int4_ops', 'btree', 0 ),
356 array( 'ipb_auto', 'int2_ops', 'btree', 0 ),
357 array( 'ipb_anon_only', 'int2_ops', 'btree', 0 ),
358 ),
359 'CREATE UNIQUE INDEX ipb_address_unique ' .
360 'ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ),
361
362 array( 'checkIwlPrefix' ),
363
364 # All FK columns should be deferred
365 array( 'changeFkeyDeferrable', 'archive', 'ar_user', 'mwuser(user_id) ON DELETE SET NULL' ),
366 array( 'changeFkeyDeferrable', 'categorylinks', 'cl_from', 'page(page_id) ON DELETE CASCADE' ),
367 array( 'changeFkeyDeferrable', 'externallinks', 'el_from', 'page(page_id) ON DELETE CASCADE' ),
368 array( 'changeFkeyDeferrable', 'filearchive', 'fa_deleted_user',
369 'mwuser(user_id) ON DELETE SET NULL' ),
370 array( 'changeFkeyDeferrable', 'filearchive', 'fa_user', 'mwuser(user_id) ON DELETE SET NULL' ),
371 array( 'changeFkeyDeferrable', 'image', 'img_user', 'mwuser(user_id) ON DELETE SET NULL' ),
372 array( 'changeFkeyDeferrable', 'imagelinks', 'il_from', 'page(page_id) ON DELETE CASCADE' ),
373 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_by', 'mwuser(user_id) ON DELETE CASCADE' ),
374 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_user', 'mwuser(user_id) ON DELETE SET NULL' ),
375 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_parent_block_id',
376 'ipblocks(ipb_id) ON DELETE SET NULL' ),
377 array( 'changeFkeyDeferrable', 'langlinks', 'll_from', 'page(page_id) ON DELETE CASCADE' ),
378 array( 'changeFkeyDeferrable', 'logging', 'log_user', 'mwuser(user_id) ON DELETE SET NULL' ),
379 array( 'changeFkeyDeferrable', 'oldimage', 'oi_name',
380 'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ),
381 array( 'changeFkeyDeferrable', 'oldimage', 'oi_user', 'mwuser(user_id) ON DELETE SET NULL' ),
382 array( 'changeFkeyDeferrable', 'pagelinks', 'pl_from', 'page(page_id) ON DELETE CASCADE' ),
383 array( 'changeFkeyDeferrable', 'page_props', 'pp_page', 'page (page_id) ON DELETE CASCADE' ),
384 array( 'changeFkeyDeferrable', 'page_restrictions', 'pr_page',
385 'page(page_id) ON DELETE CASCADE' ),
386 array( 'changeFkeyDeferrable', 'protected_titles', 'pt_user',
387 'mwuser(user_id) ON DELETE SET NULL' ),
388 array( 'changeFkeyDeferrable', 'recentchanges', 'rc_user',
389 'mwuser(user_id) ON DELETE SET NULL' ),
390 array( 'changeFkeyDeferrable', 'redirect', 'rd_from', 'page(page_id) ON DELETE CASCADE' ),
391 array( 'changeFkeyDeferrable', 'revision', 'rev_page', 'page (page_id) ON DELETE CASCADE' ),
392 array( 'changeFkeyDeferrable', 'revision', 'rev_user', 'mwuser(user_id) ON DELETE RESTRICT' ),
393 array( 'changeFkeyDeferrable', 'templatelinks', 'tl_from', 'page(page_id) ON DELETE CASCADE' ),
394 array( 'changeFkeyDeferrable', 'user_groups', 'ug_user', 'mwuser(user_id) ON DELETE CASCADE' ),
395 array( 'changeFkeyDeferrable', 'user_newtalk', 'user_id', 'mwuser(user_id) ON DELETE CASCADE' ),
396 array( 'changeFkeyDeferrable', 'user_properties', 'up_user',
397 'mwuser(user_id) ON DELETE CASCADE' ),
398 array( 'changeFkeyDeferrable', 'watchlist', 'wl_user', 'mwuser(user_id) ON DELETE CASCADE' ),
399
400 # r81574
401 array( 'addInterwikiType' ),
402 # end
403 array( 'tsearchFixes' ),
404
405 // 1.23
406 array( 'addPgField', 'recentchanges', 'rc_source', "TEXT NOT NULL DEFAULT ''" ),
407 array( 'addPgField', 'page', 'page_links_updated', "TIMESTAMPTZ NULL" ),
408 array( 'addPgField', 'mwuser', 'user_password_expires', 'TIMESTAMPTZ NULL' ),
409 array( 'changeFieldPurgeTable', 'l10n_cache', 'lc_value', 'bytea',
410 "replace(lc_value,'\','\\\\')::bytea" ),
411 // 1.23.9
412 array( 'rebuildTextSearch' ),
413
414 // 1.24
415 array( 'addPgField', 'page_props', 'pp_sortkey', 'float NULL' ),
416 array( 'addPgIndex', 'page_props', 'pp_propname_sortkey_page',
417 '( pp_propname, pp_sortkey, pp_page ) WHERE ( pp_sortkey IS NOT NULL )' ),
418 array( 'addPgField', 'page', 'page_lang', 'TEXT default NULL' ),
419 array( 'addPgField', 'pagelinks', 'pl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ),
420 array( 'addPgField', 'templatelinks', 'tl_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ),
421 array( 'addPgField', 'imagelinks', 'il_from_namespace', 'INTEGER NOT NULL DEFAULT 0' ),
422
423 // 1.25
424 array( 'dropTable', 'hitcounter' ),
425 array( 'dropField', 'site_stats', 'ss_total_views', 'patch-drop-ss_total_views.sql' ),
426 array( 'dropField', 'page', 'page_counter', 'patch-drop-page_counter.sql' ),
427 array( 'dropFkey', 'recentchanges', 'rc_cur_id' )
428 );
429 }
430
431 protected function getOldGlobalUpdates() {
432 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
433
434 $updates = array();
435
436 # Add missing extension tables
437 foreach ( $wgExtNewTables as $tableRecord ) {
438 $updates[] = array(
439 'addTable', $tableRecord[0], $tableRecord[1], true
440 );
441 }
442
443 # Add missing extension fields
444 foreach ( $wgExtPGNewFields as $fieldRecord ) {
445 $updates[] = array(
446 'addPgField', $fieldRecord[0], $fieldRecord[1],
447 $fieldRecord[2]
448 );
449 }
450
451 # Change altered columns
452 foreach ( $wgExtPGAlteredFields as $fieldRecord ) {
453 $updates[] = array(
454 'changeField', $fieldRecord[0], $fieldRecord[1],
455 $fieldRecord[2]
456 );
457 }
458
459 # Add missing extension indexes
460 foreach ( $wgExtNewIndexes as $fieldRecord ) {
461 $updates[] = array(
462 'addPgExtIndex', $fieldRecord[0], $fieldRecord[1],
463 $fieldRecord[2]
464 );
465 }
466
467 return $updates;
468 }
469
470 protected function describeTable( $table ) {
471 $q = <<<END
472 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
473 WHERE pg_class.relnamespace = pg_namespace.oid
474 AND attrelid=pg_class.oid AND attnum > 0
475 AND relname=%s AND nspname=%s
476 END;
477 $res = $this->db->query( sprintf( $q,
478 $this->db->addQuotes( $table ),
479 $this->db->addQuotes( $this->db->getCoreSchema() ) ) );
480 if ( !$res ) {
481 return null;
482 }
483
484 $cols = array();
485 foreach ( $res as $r ) {
486 $cols[] = array(
487 "name" => $r[0],
488 "ord" => $r[1],
489 );
490 }
491
492 return $cols;
493 }
494
495 function describeIndex( $idx ) {
496 // first fetch the key (which is a list of columns ords) and
497 // the table the index applies to (an oid)
498 $q = <<<END
499 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
500 WHERE nspname=%s
501 AND pg_class.relnamespace = pg_namespace.oid
502 AND relname=%s
503 AND indexrelid=pg_class.oid
504 END;
505 $res = $this->db->query(
506 sprintf(
507 $q,
508 $this->db->addQuotes( $this->db->getCoreSchema() ),
509 $this->db->addQuotes( $idx )
510 )
511 );
512 if ( !$res ) {
513 return null;
514 }
515 if ( !( $r = $this->db->fetchRow( $res ) ) ) {
516 return null;
517 }
518
519 $indkey = $r[0];
520 $relid = intval( $r[1] );
521 $indkeys = explode( ' ', $indkey );
522
523 $colnames = array();
524 foreach ( $indkeys as $rid ) {
525 $query = <<<END
526 SELECT attname FROM pg_class, pg_attribute
527 WHERE attrelid=$relid
528 AND attnum=%d
529 AND attrelid=pg_class.oid
530 END;
531 $r2 = $this->db->query( sprintf( $query, $rid ) );
532 if ( !$r2 ) {
533 return null;
534 }
535 if ( !( $row2 = $this->db->fetchRow( $r2 ) ) ) {
536 return null;
537 }
538 $colnames[] = $row2[0];
539 }
540
541 return $colnames;
542 }
543
544 function fkeyDeltype( $fkey ) {
545 $q = <<<END
546 SELECT confdeltype FROM pg_constraint, pg_namespace
547 WHERE connamespace=pg_namespace.oid
548 AND nspname=%s
549 AND conname=%s;
550 END;
551 $r = $this->db->query(
552 sprintf(
553 $q,
554 $this->db->addQuotes( $this->db->getCoreSchema() ),
555 $this->db->addQuotes( $fkey )
556 )
557 );
558 if ( !( $row = $this->db->fetchRow( $r ) ) ) {
559 return null;
560 }
561
562 return $row[0];
563 }
564
565 function ruleDef( $table, $rule ) {
566 $q = <<<END
567 SELECT definition FROM pg_rules
568 WHERE schemaname = %s
569 AND tablename = %s
570 AND rulename = %s
571 END;
572 $r = $this->db->query(
573 sprintf(
574 $q,
575 $this->db->addQuotes( $this->db->getCoreSchema() ),
576 $this->db->addQuotes( $table ),
577 $this->db->addQuotes( $rule )
578 )
579 );
580 $row = $this->db->fetchRow( $r );
581 if ( !$row ) {
582 return null;
583 }
584 $d = $row[0];
585
586 return $d;
587 }
588
589 protected function addSequence( $table, $pkey, $ns ) {
590 if ( !$this->db->sequenceExists( $ns ) ) {
591 $this->output( "Creating sequence $ns\n" );
592 $this->db->query( "CREATE SEQUENCE $ns" );
593 if ( $pkey !== false ) {
594 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
595 }
596 }
597 }
598
599 protected function renameSequence( $old, $new ) {
600 if ( $this->db->sequenceExists( $new ) ) {
601 $this->output( "...sequence $new already exists.\n" );
602
603 return;
604 }
605 if ( $this->db->sequenceExists( $old ) ) {
606 $this->output( "Renaming sequence $old to $new\n" );
607 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" );
608 }
609 }
610
611 protected function renameTable( $old, $new, $patch = false ) {
612 if ( $this->db->tableExists( $old ) ) {
613 $this->output( "Renaming table $old to $new\n" );
614 $old = $this->db->realTableName( $old, "quoted" );
615 $new = $this->db->realTableName( $new, "quoted" );
616 $this->db->query( "ALTER TABLE $old RENAME TO $new" );
617 if ( $patch !== false ) {
618 $this->applyPatch( $patch );
619 }
620 }
621 }
622
623 protected function renameIndex(
624 $table, $old, $new, $skipBothIndexExistWarning = false, $a = false, $b = false
625 ) {
626 // First requirement: the table must exist
627 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
628 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
629
630 return;
631 }
632
633 // Second requirement: the new index must be missing
634 if ( $this->db->indexExists( $table, $new, __METHOD__ ) ) {
635 $this->output( "...index $new already set on $table table.\n" );
636 if ( !$skipBothIndexExistWarning
637 && $this->db->indexExists( $table, $old, __METHOD__ )
638 ) {
639 $this->output( "...WARNING: $old still exists, despite it has been " .
640 "renamed into $new (which also exists).\n" .
641 " $old should be manually removed if not needed anymore.\n" );
642 }
643
644 return;
645 }
646
647 // Third requirement: the old index must exist
648 if ( !$this->db->indexExists( $table, $old, __METHOD__ ) ) {
649 $this->output( "...skipping: index $old doesn't exist.\n" );
650
651 return;
652 }
653
654 $this->db->query( "ALTER INDEX $old RENAME TO $new" );
655 }
656
657 protected function addPgField( $table, $field, $type ) {
658 $fi = $this->db->fieldInfo( $table, $field );
659 if ( !is_null( $fi ) ) {
660 $this->output( "...column '$table.$field' already exists\n" );
661
662 return;
663 } else {
664 $this->output( "Adding column '$table.$field'\n" );
665 $this->db->query( "ALTER TABLE $table ADD $field $type" );
666 }
667 }
668
669 protected function changeField( $table, $field, $newtype, $default ) {
670 $fi = $this->db->fieldInfo( $table, $field );
671 if ( is_null( $fi ) ) {
672 $this->output( "...ERROR: expected column $table.$field to exist\n" );
673 exit( 1 );
674 }
675
676 if ( $fi->type() === $newtype ) {
677 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
678 } else {
679 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
680 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
681 if ( strlen( $default ) ) {
682 $res = array();
683 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
684 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
685 $this->db->query( $sqldef );
686 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
687 }
688 $sql .= " USING $default";
689 }
690 $this->db->query( $sql );
691 }
692 }
693
694 protected function changeFieldPurgeTable( $table, $field, $newtype, $default ) {
695 # # For a cache table, empty it if the field needs to be changed, because the old contents
696 # # may be corrupted. If the column is already the desired type, refrain from purging.
697 $fi = $this->db->fieldInfo( $table, $field );
698 if ( is_null( $fi ) ) {
699 $this->output( "...ERROR: expected column $table.$field to exist\n" );
700 exit( 1 );
701 }
702
703 if ( $fi->type() === $newtype ) {
704 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
705 } else {
706 $this->output( "Purging data from cache table '$table'\n" );
707 $this->db->query( "DELETE from $table" );
708 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
709 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
710 if ( strlen( $default ) ) {
711 $res = array();
712 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
713 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
714 $this->db->query( $sqldef );
715 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
716 }
717 $sql .= " USING $default";
718 }
719 $this->db->query( $sql );
720 }
721 }
722
723 protected function setDefault( $table, $field, $default ) {
724
725 $info = $this->db->fieldInfo( $table, $field );
726 if ( $info->defaultValue() !== $default ) {
727 $this->output( "Changing '$table.$field' default value\n" );
728 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
729 }
730 }
731
732 protected function changeNullableField( $table, $field, $null ) {
733 $fi = $this->db->fieldInfo( $table, $field );
734 if ( is_null( $fi ) ) {
735 $this->output( "...ERROR: expected column $table.$field to exist\n" );
736 exit( 1 );
737 }
738 if ( $fi->isNullable() ) {
739 # # It's NULL - does it need to be NOT NULL?
740 if ( 'NOT NULL' === $null ) {
741 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
742 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" );
743 } else {
744 $this->output( "...column '$table.$field' is already set as NULL\n" );
745 }
746 } else {
747 # # It's NOT NULL - does it need to be NULL?
748 if ( 'NULL' === $null ) {
749 $this->output( "Changing '$table.$field' to allow NULLs\n" );
750 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" );
751 } else {
752 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
753 }
754 }
755 }
756
757 public function addPgIndex( $table, $index, $type ) {
758 if ( $this->db->indexExists( $table, $index ) ) {
759 $this->output( "...index '$index' on table '$table' already exists\n" );
760 } else {
761 $this->output( "Creating index '$index' on table '$table' $type\n" );
762 $this->db->query( "CREATE INDEX $index ON $table $type" );
763 }
764 }
765
766 public function addPgExtIndex( $table, $index, $type ) {
767 if ( $this->db->indexExists( $table, $index ) ) {
768 $this->output( "...index '$index' on table '$table' already exists\n" );
769 } else {
770 if ( preg_match( '/^\(/', $type ) ) {
771 $this->output( "Creating index '$index' on table '$table'\n" );
772 $this->db->query( "CREATE INDEX $index ON $table $type" );
773 } else {
774 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
775 }
776 }
777 }
778
779 protected function dropFkey( $table, $field ) {
780 $fi = $this->db->fieldInfo( $table, $field );
781 if ( is_null( $fi ) ) {
782 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
783 "Please report this.\n" );
784 return;
785 }
786 $conname = $fi->conname();
787 if ( $fi->conname() ) {
788 $this->output( "Dropping foreign key constraint on '$table.$field'\n" );
789 $conclause = "CONSTRAINT \"$conname\"";
790 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
791 $this->db->query( $command );
792 } else {
793 $this->output( "...foreign key constraint on '$table.$field' already does not exist\n" );
794 };
795 }
796
797 protected function changeFkeyDeferrable( $table, $field, $clause ) {
798 $fi = $this->db->fieldInfo( $table, $field );
799 if ( is_null( $fi ) ) {
800 $this->output( "WARNING! Column '$table.$field' does not exist but it should! " .
801 "Please report this.\n" );
802
803 return;
804 }
805 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
806 return;
807 }
808 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
809 $conname = $fi->conname();
810 if ( $fi->conname() ) {
811 $conclause = "CONSTRAINT \"$conname\"";
812 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
813 $this->db->query( $command );
814 } else {
815 $this->output( "Column '$table.$field' does not have a foreign key " .
816 "constraint, will be added\n" );
817 $conclause = "";
818 }
819 $command =
820 "ALTER TABLE $table ADD $conclause " .
821 "FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
822 $this->db->query( $command );
823 }
824
825 protected function convertArchive2() {
826 if ( $this->db->tableExists( "archive2" ) ) {
827 if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
828 $this->output( "Dropping rule 'archive_insert'\n" );
829 $this->db->query( 'DROP RULE archive_insert ON archive' );
830 }
831 if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) {
832 $this->output( "Dropping rule 'archive_delete'\n" );
833 $this->db->query( 'DROP RULE archive_delete ON archive' );
834 }
835 $this->applyPatch(
836 'patch-remove-archive2.sql',
837 false,
838 "Converting 'archive2' back to normal archive table"
839 );
840 } else {
841 $this->output( "...obsolete table 'archive2' does not exist\n" );
842 }
843 }
844
845 protected function checkOiDeleted() {
846 if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
847 $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" );
848 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
849 $this->db->query(
850 "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
851 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
852 } else {
853 $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" );
854 }
855 }
856
857 protected function checkOiNameConstraint() {
858 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
859 $this->output( "...table 'oldimage' has correct cascading delete/update " .
860 "foreign key to image\n" );
861 } else {
862 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) {
863 $this->db->query(
864 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
865 }
866 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
867 $this->db->query(
868 "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
869 }
870 $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" );
871 $this->db->query(
872 "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
873 "FOREIGN KEY (oi_name) REFERENCES image(img_name) " .
874 "ON DELETE CASCADE ON UPDATE CASCADE" );
875 }
876 }
877
878 protected function checkPageDeletedTrigger() {
879 if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
880 $this->applyPatch(
881 'patch-page_deleted.sql',
882 false,
883 "Adding function and trigger 'page_deleted' to table 'page'"
884 );
885 } else {
886 $this->output( "...table 'page' has 'page_deleted' trigger\n" );
887 }
888 }
889
890 protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
891 if ( $this->db->indexExists( $table, $index ) ) {
892 $this->output( "Dropping obsolete index '$index'\n" );
893 $this->db->query( "DROP INDEX \"" . $index . "\"" );
894 }
895 }
896
897 protected function checkIndex( $index, $should_be, $good_def ) {
898 $pu = $this->db->indexAttributes( $index );
899 if ( !empty( $pu ) && $pu != $should_be ) {
900 $this->output( "Dropping obsolete version of index '$index'\n" );
901 $this->db->query( "DROP INDEX \"" . $index . "\"" );
902 $pu = array();
903 } else {
904 $this->output( "...no need to drop index '$index'\n" );
905 }
906
907 if ( empty( $pu ) ) {
908 $this->output( "Creating index '$index'\n" );
909 $this->db->query( $good_def );
910 } else {
911 $this->output( "...index '$index' exists\n" );
912 }
913 }
914
915 protected function checkRevUserFkey() {
916 if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
917 $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
918 } else {
919 $this->applyPatch(
920 'patch-revision_rev_user_fkey.sql',
921 false,
922 "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT"
923 );
924 }
925 }
926
927 protected function checkIwlPrefix() {
928 if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
929 $this->applyPatch(
930 'patch-rename-iwl_prefix.sql',
931 false,
932 "Replacing index 'iwl_prefix' with 'iwl_prefix_title_from'"
933 );
934 }
935 }
936
937 protected function addInterwikiType() {
938 $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
939 }
940
941 protected function tsearchFixes() {
942 # Tweak the page_title tsearch2 trigger to filter out slashes
943 # This is create or replace, so harmless to call if not needed
944 $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
945
946 # If the server is 8.3 or higher, rewrite the tsearch2 triggers
947 # in case they have the old 'default' versions
948 # Gather version numbers in case we need them
949 if ( $this->db->getServerVersion() >= 8.3 ) {
950 $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
951 }
952 }
953
954 protected function rebuildTextSearch() {
955 if ( $this->updateRowExists( 'patch-textsearch_bug66650.sql' ) ) {
956 $this->output( "...bug 66650 already fixed or not applicable.\n" );
957 return true;
958 };
959 $this->applyPatch( 'patch-textsearch_bug66650.sql', false,
960 'Rebuilding text search for bug 66650' );
961 }
962 }