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