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