(bug 37755) Set robot meta tags for 'view source' pages
[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', 'config', 'patch-config.sql' ),
93 array( 'addTable', 'external_user', 'patch-external_user.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', 'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ),
118 array( 'addPgField', 'filearchive', 'fa_deleted', 'SMALLINT NOT NULL DEFAULT 0' ),
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
158 # type changes
159 array( 'changeField', 'archive', 'ar_deleted', 'smallint', '' ),
160 array( 'changeField', 'archive', 'ar_minor_edit', 'smallint', 'ar_minor_edit::smallint DEFAULT 0' ),
161 array( 'changeField', 'filearchive', 'fa_deleted', 'smallint', '' ),
162 array( 'changeField', 'filearchive', 'fa_height', 'integer', '' ),
163 array( 'changeField', 'filearchive', 'fa_metadata', 'bytea', "decode(fa_metadata,'escape')" ),
164 array( 'changeField', 'filearchive', 'fa_size', 'integer', '' ),
165 array( 'changeField', 'filearchive', 'fa_width', 'integer', '' ),
166 array( 'changeField', 'filearchive', 'fa_storage_group', 'text', '' ),
167 array( 'changeField', 'filearchive', 'fa_storage_key', 'text', '' ),
168 array( 'changeField', 'image', 'img_metadata', 'bytea', "decode(img_metadata,'escape')" ),
169 array( 'changeField', 'image', 'img_size', 'integer', '' ),
170 array( 'changeField', 'image', 'img_width', 'integer', '' ),
171 array( 'changeField', 'image', 'img_height', 'integer', '' ),
172 array( 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint' ),
173 array( 'changeField', 'interwiki', 'iw_trans', 'smallint', 'iw_trans::smallint DEFAULT 0' ),
174 array( 'changeField', 'ipblocks', 'ipb_auto', 'smallint', 'ipb_auto::smallint DEFAULT 0' ),
175 array( 'changeField', 'ipblocks', 'ipb_anon_only', 'smallint', "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
176 array( 'changeField', 'ipblocks', 'ipb_create_account', 'smallint', "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ),
177 array( 'changeField', 'ipblocks', 'ipb_enable_autoblock', 'smallint', "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ),
178 array( 'changeField', 'ipblocks', 'ipb_block_email', 'smallint', "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ),
179 array( 'changeField', 'ipblocks', 'ipb_address', 'text', 'ipb_address::text' ),
180 array( 'changeField', 'ipblocks', 'ipb_deleted', 'smallint', 'ipb_deleted::smallint DEFAULT 0' ),
181 array( 'changeField', 'mwuser', 'user_token', 'text', '' ),
182 array( 'changeField', 'mwuser', 'user_email_token', 'text', '' ),
183 array( 'changeField', 'objectcache', 'keyname', 'text', '' ),
184 array( 'changeField', 'oldimage', 'oi_height', 'integer', '' ),
185 array( 'changeField', 'oldimage', 'oi_metadata', 'bytea', "decode(img_metadata,'escape')" ),
186 array( 'changeField', 'oldimage', 'oi_size', 'integer', '' ),
187 array( 'changeField', 'oldimage', 'oi_width', 'integer', '' ),
188 array( 'changeField', 'page', 'page_is_redirect', 'smallint', 'page_is_redirect::smallint DEFAULT 0' ),
189 array( 'changeField', 'page', 'page_is_new', 'smallint', 'page_is_new::smallint DEFAULT 0' ),
190 array( 'changeField', 'querycache', 'qc_value', 'integer', '' ),
191 array( 'changeField', 'querycachetwo', 'qcc_value', 'integer', '' ),
192 array( 'changeField', 'recentchanges', 'rc_bot', 'smallint', 'rc_bot::smallint DEFAULT 0' ),
193 array( 'changeField', 'recentchanges', 'rc_deleted', 'smallint', '' ),
194 array( 'changeField', 'recentchanges', 'rc_minor', 'smallint', 'rc_minor::smallint DEFAULT 0' ),
195 array( 'changeField', 'recentchanges', 'rc_new', 'smallint', 'rc_new::smallint DEFAULT 0' ),
196 array( 'changeField', 'recentchanges', 'rc_type', 'smallint', 'rc_type::smallint DEFAULT 0' ),
197 array( 'changeField', 'recentchanges', 'rc_patrolled', 'smallint', 'rc_patrolled::smallint DEFAULT 0' ),
198 array( 'changeField', 'revision', 'rev_deleted', 'smallint', 'rev_deleted::smallint DEFAULT 0' ),
199 array( 'changeField', 'revision', 'rev_minor_edit', 'smallint', 'rev_minor_edit::smallint DEFAULT 0' ),
200 array( 'changeField', 'templatelinks', 'tl_namespace', 'smallint', 'tl_namespace::smallint' ),
201 array( 'changeField', 'user_newtalk', 'user_ip', 'text', 'host(user_ip)' ),
202 array( 'changeField', 'uploadstash', 'us_image_bits', 'smallint', '' ),
203
204 # null changes
205 array( 'changeNullableField', 'oldimage', 'oi_bits', 'NULL' ),
206 array( 'changeNullableField', 'oldimage', 'oi_timestamp', 'NULL' ),
207 array( 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ),
208 array( 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ),
209 array( 'changeNullableField', 'image', 'img_metadata', 'NOT NULL'),
210 array( 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL'),
211 array( 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ),
212
213 array( 'checkOiDeleted' ),
214
215 # New indexes
216 array( 'addPgIndex', 'archive', 'archive_user_text', '(ar_user_text)' ),
217 array( 'addPgIndex', 'image', 'img_sha1', '(img_sha1)' ),
218 array( 'addPgIndex', 'ipblocks', 'ipb_parent_block_id', '(ipb_parent_block_id)' ),
219 array( 'addPgIndex', 'oldimage', 'oi_sha1', '(oi_sha1)' ),
220 array( 'addPgIndex', 'page', 'page_mediawiki_title', '(page_title) WHERE page_namespace = 8' ),
221 array( 'addPgIndex', 'pagelinks', 'pagelinks_title', '(pl_title)' ),
222 array( 'addPgIndex', 'revision', 'rev_text_id_idx', '(rev_text_id)' ),
223 array( 'addPgIndex', 'recentchanges', 'rc_timestamp_bot', '(rc_timestamp) WHERE rc_bot = 0' ),
224 array( 'addPgIndex', 'templatelinks', 'templatelinks_from', '(tl_from)' ),
225 array( 'addPgIndex', 'watchlist', 'wl_user', '(wl_user)' ),
226 array( 'addPgIndex', 'logging', 'logging_user_type_time', '(log_user, log_type, log_timestamp)' ),
227 array( 'addPgIndex', 'logging', 'logging_page_id_time', '(log_page,log_timestamp)' ),
228 array( 'addPgIndex', 'iwlinks', 'iwl_prefix_title_from', '(iwl_prefix, iwl_title, iwl_from)' ),
229 array( 'addPgIndex', 'job', 'job_timestamp_idx', '(job_timestamp)' ),
230
231 array( 'checkIndex', 'pagelink_unique', array(
232 array('pl_from', 'int4_ops', 'btree', 0),
233 array('pl_namespace', 'int2_ops', 'btree', 0),
234 array('pl_title', 'text_ops', 'btree', 0),
235 ),
236 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ),
237 array( 'checkIndex', 'cl_sortkey', array(
238 array('cl_to', 'text_ops', 'btree', 0),
239 array('cl_sortkey', 'text_ops', 'btree', 0),
240 array('cl_from', 'int4_ops', 'btree', 0),
241 ),
242 'CREATE INDEX cl_sortkey ON "categorylinks" USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ),
243 array( 'checkIndex', 'logging_times', array(
244 array('log_timestamp', 'timestamptz_ops', 'btree', 0),
245 ),
246 'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ),
247 array( 'dropIndex', 'oldimage', 'oi_name' ),
248 array( 'checkIndex', 'oi_name_archive_name', array(
249 array('oi_name', 'text_ops', 'btree', 0),
250 array('oi_archive_name', 'text_ops', 'btree', 0),
251 ),
252 'CREATE INDEX "oi_name_archive_name" ON "oldimage" USING "btree" ("oi_name", "oi_archive_name")' ),
253 array( 'checkIndex', 'oi_name_timestamp', array(
254 array('oi_name', 'text_ops', 'btree', 0),
255 array('oi_timestamp', 'timestamptz_ops', 'btree', 0),
256 ),
257 'CREATE INDEX "oi_name_timestamp" ON "oldimage" USING "btree" ("oi_name", "oi_timestamp")' ),
258 array( 'checkIndex', 'page_main_title', array(
259 array('page_title', 'text_pattern_ops', 'btree', 0),
260 ),
261 'CREATE INDEX "page_main_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ),
262 array( 'checkIndex', 'page_mediawiki_title', array(
263 array('page_title', 'text_pattern_ops', 'btree', 0),
264 ),
265 'CREATE INDEX "page_mediawiki_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ),
266 array( 'checkIndex', 'page_project_title', array(
267 array('page_title', 'text_pattern_ops', 'btree', 0),
268 ),
269 'CREATE INDEX "page_project_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 4)' ),
270 array( 'checkIndex', 'page_talk_title', array(
271 array('page_title', 'text_pattern_ops', 'btree', 0),
272 ),
273 'CREATE INDEX "page_talk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 1)' ),
274 array( 'checkIndex', 'page_user_title', array(
275 array('page_title', 'text_pattern_ops', 'btree', 0),
276 ),
277 'CREATE INDEX "page_user_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 2)' ),
278 array( 'checkIndex', 'page_utalk_title', array(
279 array('page_title', 'text_pattern_ops', 'btree', 0),
280 ),
281 'CREATE INDEX "page_utalk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 3)' ),
282 array( 'checkIndex', 'ts2_page_text', array(
283 array('textvector', 'tsvector_ops', 'gist', 0),
284 ),
285 'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ),
286 array( 'checkIndex', 'ts2_page_title', array(
287 array('titlevector', 'tsvector_ops', 'gist', 0),
288 ),
289 'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ),
290
291 array( 'checkOiNameConstraint' ),
292 array( 'checkPageDeletedTrigger' ),
293 array( 'checkRevUserFkey' ),
294 array( 'dropIndex', 'ipblocks', 'ipb_address'),
295 array( 'checkIndex', 'ipb_address_unique', array(
296 array('ipb_address', 'text_ops', 'btree', 0),
297 array('ipb_user', 'int4_ops', 'btree', 0),
298 array('ipb_auto', 'int2_ops', 'btree', 0),
299 array('ipb_anon_only', 'int2_ops', 'btree', 0),
300 ),
301 'CREATE UNIQUE INDEX ipb_address_unique ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ),
302
303 array( 'checkIwlPrefix' ),
304
305 # All FK columns should be deferred
306 array( 'changeFkeyDeferrable', 'archive', 'ar_user', 'mwuser(user_id) ON DELETE SET NULL' ),
307 array( 'changeFkeyDeferrable', 'categorylinks', 'cl_from', 'page(page_id) ON DELETE CASCADE' ),
308 array( 'changeFkeyDeferrable', 'externallinks', 'el_from', 'page(page_id) ON DELETE CASCADE' ),
309 array( 'changeFkeyDeferrable', 'filearchive', 'fa_deleted_user', 'mwuser(user_id) ON DELETE SET NULL' ),
310 array( 'changeFkeyDeferrable', 'filearchive', 'fa_user', 'mwuser(user_id) ON DELETE SET NULL' ),
311 array( 'changeFkeyDeferrable', 'image', 'img_user', 'mwuser(user_id) ON DELETE SET NULL' ),
312 array( 'changeFkeyDeferrable', 'imagelinks', 'il_from', 'page(page_id) ON DELETE CASCADE' ),
313 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_by', 'mwuser(user_id) ON DELETE CASCADE' ),
314 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_user', 'mwuser(user_id) ON DELETE SET NULL' ),
315 array( 'changeFkeyDeferrable', 'ipblocks', 'ipb_parent_block_id', 'ipblocks(ipb_id) ON DELETE SET NULL' ),
316 array( 'changeFkeyDeferrable', 'langlinks', 'll_from', 'page(page_id) ON DELETE CASCADE' ),
317 array( 'changeFkeyDeferrable', 'logging', 'log_user', 'mwuser(user_id) ON DELETE SET NULL' ),
318 array( 'changeFkeyDeferrable', 'oldimage', 'oi_name', 'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ),
319 array( 'changeFkeyDeferrable', 'oldimage', 'oi_user', 'mwuser(user_id) ON DELETE SET NULL' ),
320 array( 'changeFkeyDeferrable', 'pagelinks', 'pl_from', 'page(page_id) ON DELETE CASCADE' ),
321 array( 'changeFkeyDeferrable', 'page_props', 'pp_page', 'page (page_id) ON DELETE CASCADE' ),
322 array( 'changeFkeyDeferrable', 'page_restrictions', 'pr_page', 'page(page_id) ON DELETE CASCADE' ),
323 array( 'changeFkeyDeferrable', 'protected_titles', 'pt_user', 'mwuser(user_id) ON DELETE SET NULL' ),
324 array( 'changeFkeyDeferrable', 'recentchanges', 'rc_cur_id', 'page(page_id) ON DELETE SET NULL' ),
325 array( 'changeFkeyDeferrable', 'recentchanges', 'rc_user', 'mwuser(user_id) ON DELETE SET NULL' ),
326 array( 'changeFkeyDeferrable', 'redirect', 'rd_from', 'page(page_id) ON DELETE CASCADE' ),
327 array( 'changeFkeyDeferrable', 'revision', 'rev_page', 'page (page_id) ON DELETE CASCADE' ),
328 array( 'changeFkeyDeferrable', 'revision', 'rev_user', 'mwuser(user_id) ON DELETE RESTRICT' ),
329 array( 'changeFkeyDeferrable', 'templatelinks', 'tl_from', 'page(page_id) ON DELETE CASCADE' ),
330 array( 'changeFkeyDeferrable', 'user_groups', 'ug_user', 'mwuser(user_id) ON DELETE CASCADE' ),
331 array( 'changeFkeyDeferrable', 'user_newtalk', 'user_id', 'mwuser(user_id) ON DELETE CASCADE' ),
332 array( 'changeFkeyDeferrable', 'user_properties', 'up_user', 'mwuser(user_id) ON DELETE CASCADE' ),
333 array( 'changeFkeyDeferrable', 'watchlist', 'wl_user', 'mwuser(user_id) ON DELETE CASCADE' ),
334
335 # r81574
336 array( 'addInterwikiType' ),
337 # end
338 array( 'tsearchFixes' ),
339 );
340 }
341
342 protected function getOldGlobalUpdates() {
343 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
344
345 $updates = array();
346
347 # Add missing extension tables
348 foreach ( $wgExtNewTables as $tableRecord ) {
349 $updates[] = array(
350 'addTable', $tableRecord[0], $tableRecord[1], true
351 );
352 }
353
354 # Add missing extension fields
355 foreach ( $wgExtPGNewFields as $fieldRecord ) {
356 $updates[] = array(
357 'addPgField', $fieldRecord[0], $fieldRecord[1],
358 $fieldRecord[2]
359 );
360 }
361
362 # Change altered columns
363 foreach ( $wgExtPGAlteredFields as $fieldRecord ) {
364 $updates[] = array(
365 'changeField', $fieldRecord[0], $fieldRecord[1],
366 $fieldRecord[2]
367 );
368 }
369
370 # Add missing extension indexes
371 foreach ( $wgExtNewIndexes as $fieldRecord ) {
372 $updates[] = array(
373 'addPgExtIndex', $fieldRecord[0], $fieldRecord[1],
374 $fieldRecord[2]
375 );
376 }
377
378 return $updates;
379 }
380
381 protected function describeTable( $table ) {
382 $q = <<<END
383 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
384 WHERE pg_class.relnamespace = pg_namespace.oid
385 AND attrelid=pg_class.oid AND attnum > 0
386 AND relname=%s AND nspname=%s
387 END;
388 $res = $this->db->query( sprintf( $q,
389 $this->db->addQuotes( $table ),
390 $this->db->addQuotes( $this->db->getCoreSchema() ) ) );
391 if ( !$res ) {
392 return null;
393 }
394
395 $cols = array();
396 foreach ( $res as $r ) {
397 $cols[] = array(
398 "name" => $r[0],
399 "ord" => $r[1],
400 );
401 }
402 return $cols;
403 }
404
405 function describeIndex( $idx ) {
406 // first fetch the key (which is a list of columns ords) and
407 // the table the index applies to (an oid)
408 $q = <<<END
409 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
410 WHERE nspname=%s
411 AND pg_class.relnamespace = pg_namespace.oid
412 AND relname=%s
413 AND indexrelid=pg_class.oid
414 END;
415 $res = $this->db->query(
416 sprintf(
417 $q,
418 $this->db->addQuotes( $this->db->getCoreSchema() ),
419 $this->db->addQuotes( $idx )
420 )
421 );
422 if ( !$res ) {
423 return null;
424 }
425 if ( !( $r = $this->db->fetchRow( $res ) ) ) {
426 return null;
427 }
428
429 $indkey = $r[0];
430 $relid = intval( $r[1] );
431 $indkeys = explode( ' ', $indkey );
432
433 $colnames = array();
434 foreach ( $indkeys as $rid ) {
435 $query = <<<END
436 SELECT attname FROM pg_class, pg_attribute
437 WHERE attrelid=$relid
438 AND attnum=%d
439 AND attrelid=pg_class.oid
440 END;
441 $r2 = $this->db->query( sprintf( $query, $rid ) );
442 if ( !$r2 ) {
443 return null;
444 }
445 if ( !( $row2 = $this->db->fetchRow( $r2 ) ) ) {
446 return null;
447 }
448 $colnames[] = $row2[0];
449 }
450
451 return $colnames;
452 }
453
454 function fkeyDeltype( $fkey ) {
455 $q = <<<END
456 SELECT confdeltype FROM pg_constraint, pg_namespace
457 WHERE connamespace=pg_namespace.oid
458 AND nspname=%s
459 AND conname=%s;
460 END;
461 $r = $this->db->query(
462 sprintf(
463 $q,
464 $this->db->addQuotes( $this->db->getCoreSchema() ),
465 $this->db->addQuotes( $fkey )
466 )
467 );
468 if ( !( $row = $this->db->fetchRow( $r ) ) ) {
469 return null;
470 }
471 return $row[0];
472 }
473
474 function ruleDef( $table, $rule ) {
475 $q = <<<END
476 SELECT definition FROM pg_rules
477 WHERE schemaname = %s
478 AND tablename = %s
479 AND rulename = %s
480 END;
481 $r = $this->db->query(
482 sprintf(
483 $q,
484 $this->db->addQuotes( $this->db->getCoreSchema() ),
485 $this->db->addQuotes( $table ),
486 $this->db->addQuotes( $rule )
487 )
488 );
489 $row = $this->db->fetchRow( $r );
490 if ( !$row ) {
491 return null;
492 }
493 $d = $row[0];
494 return $d;
495 }
496
497 protected function addSequence( $table, $pkey, $ns ) {
498 if ( !$this->db->sequenceExists( $ns ) ) {
499 $this->output( "Creating sequence $ns\n" );
500 $this->db->query( "CREATE SEQUENCE $ns" );
501 if( $pkey !== false ) {
502 $this->setDefault( $table, $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
503 }
504 }
505 }
506
507 protected function renameSequence( $old, $new ) {
508 if ( $this->db->sequenceExists( $new ) ) {
509 $this->output( "...sequence $new already exists.\n" );
510 return;
511 }
512 if ( $this->db->sequenceExists( $old ) ) {
513 $this->output( "Renaming sequence $old to $new\n" );
514 $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" );
515 }
516 }
517
518 protected function renameTable( $old, $new, $patch = false ) {
519 if ( $this->db->tableExists( $old ) ) {
520 $this->output( "Renaming table $old to $new\n" );
521 $old = $this->db->realTableName( $old, "quoted" );
522 $new = $this->db->realTableName( $new, "quoted" );
523 $this->db->query( "ALTER TABLE $old RENAME TO $new" );
524 if( $patch !== false ) {
525 $this->applyPatch( $patch );
526 }
527 }
528 }
529
530 protected function renameIndex( $table, $old, $new ) {
531 if ( $this->db->indexExists( $table, $old ) ) {
532 $this->output( "Renaming index $old to $new\n" );
533 $this->db->query( "ALTER INDEX $old RENAME TO $new" );
534 }
535 }
536
537 protected function addPgField( $table, $field, $type ) {
538 $fi = $this->db->fieldInfo( $table, $field );
539 if ( !is_null( $fi ) ) {
540 $this->output( "...column '$table.$field' already exists\n" );
541 return;
542 } else {
543 $this->output( "Adding column '$table.$field'\n" );
544 $this->db->query( "ALTER TABLE $table ADD $field $type" );
545 }
546 }
547
548 protected function changeField( $table, $field, $newtype, $default ) {
549 $fi = $this->db->fieldInfo( $table, $field );
550 if ( is_null( $fi ) ) {
551 $this->output( "...ERROR: expected column $table.$field to exist\n" );
552 exit( 1 );
553 }
554
555 if ( $fi->type() === $newtype )
556 $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
557 else {
558 $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
559 $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
560 if ( strlen( $default ) ) {
561 $res = array();
562 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
563 $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
564 $this->db->query( $sqldef );
565 $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
566 }
567 $sql .= " USING $default";
568 }
569 $this->db->query( $sql );
570 }
571 }
572
573 protected function setDefault( $table, $field, $default ) {
574
575 $info = $this->db->fieldInfo( $table, $field );
576 if ( $info->defaultValue() !== $default ) {
577 $this->output( "Changing '$table.$field' default value\n" );
578 $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
579 }
580 }
581
582 protected function changeNullableField( $table, $field, $null) {
583 $fi = $this->db->fieldInfo( $table, $field );
584 if ( is_null( $fi ) ) {
585 $this->output( "...ERROR: expected column $table.$field to exist\n" );
586 exit( 1 );
587 }
588 if ( $fi->isNullable() ) {
589 # # It's NULL - does it need to be NOT NULL?
590 if ( 'NOT NULL' === $null ) {
591 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
592 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" );
593 } else {
594 $this->output( "...column '$table.$field' is already set as NULL\n" );
595 }
596 } else {
597 # # It's NOT NULL - does it need to be NULL?
598 if ( 'NULL' === $null ) {
599 $this->output( "Changing '$table.$field' to allow NULLs\n" );
600 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" );
601 }
602 else {
603 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
604 }
605 }
606 }
607
608 public function addPgIndex( $table, $index, $type ) {
609 if ( $this->db->indexExists( $table, $index ) ) {
610 $this->output( "...index '$index' on table '$table' already exists\n" );
611 } else {
612 $this->output( "Creating index '$index' on table '$table' $type\n" );
613 $this->db->query( "CREATE INDEX $index ON $table $type" );
614 }
615 }
616
617 public function addPgExtIndex( $table, $index, $type ) {
618 if ( $this->db->indexExists( $table, $index ) ) {
619 $this->output( "...index '$index' on table '$table' already exists\n" );
620 } else {
621 if ( preg_match( '/^\(/', $type ) ) {
622 $this->output( "Creating index '$index' on table '$table'\n" );
623 $this->db->query( "CREATE INDEX $index ON $table $type" );
624 } else {
625 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
626 }
627 }
628 }
629
630 protected function changeFkeyDeferrable( $table, $field, $clause ) {
631 $fi = $this->db->fieldInfo( $table, $field );
632 if ( is_null( $fi ) ) {
633 $this->output( "WARNING! Column '$table.$field' does not exist but it should! Please report this.\n" );
634 return;
635 }
636 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
637 return;
638 }
639 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
640 $conname = $fi->conname();
641 if ( $fi->conname() ) {
642 $conclause = "CONSTRAINT \"$conname\"";
643 $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
644 $this->db->query( $command );
645 } else {
646 $this->output( "Column '$table.$field' does not have a foreign key constraint, will be added\n" );
647 $conclause = "";
648 }
649 $command = "ALTER TABLE $table ADD $conclause FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
650 $this->db->query( $command );
651 }
652
653 protected function convertArchive2() {
654 if ( $this->db->tableExists( "archive2" ) ) {
655 if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
656 $this->output( "Dropping rule 'archive_insert'\n" );
657 $this->db->query( 'DROP RULE archive_insert ON archive' );
658 }
659 if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) {
660 $this->output( "Dropping rule 'archive_delete'\n" );
661 $this->db->query( 'DROP RULE archive_delete ON archive' );
662 }
663 $this->applyPatch( 'patch-remove-archive2.sql', false, "Converting 'archive2' back to normal archive table" );
664 } else {
665 $this->output( "...obsolete table 'archive2' does not exist\n" );
666 }
667 }
668
669 protected function checkOiDeleted() {
670 if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
671 $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" );
672 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
673 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
674 $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
675 } else {
676 $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" );
677 }
678 }
679
680 protected function checkOiNameConstraint() {
681 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
682 $this->output( "...table 'oldimage' has correct cascading delete/update foreign key to image\n" );
683 } else {
684 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) {
685 $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
686 }
687 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
688 $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
689 }
690 $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" );
691 $this->db->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
692 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" );
693 }
694 }
695
696 protected function checkPageDeletedTrigger() {
697 if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
698 $this->applyPatch( 'patch-page_deleted.sql', false, "Adding function and trigger 'page_deleted' to table 'page'" );
699 } else {
700 $this->output( "...table 'page' has 'page_deleted' trigger\n" );
701 }
702 }
703
704 protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
705 if ( $this->db->indexExists( $table, $index ) ) {
706 $this->output( "Dropping obsolete index '$index'\n" );
707 $this->db->query( "DROP INDEX \"". $index ."\"" );
708 }
709 }
710
711 protected function checkIndex( $index, $should_be, $good_def ) {
712 $pu = $this->db->indexAttributes( $index );
713 if ( !empty( $pu ) && $pu != $should_be ) {
714 $this->output( "Dropping obsolete version of index '$index'\n" );
715 $this->db->query( "DROP INDEX \"". $index ."\"" );
716 $pu = array();
717 } else {
718 $this->output( "...no need to drop index '$index'\n" );
719 }
720
721 if ( empty( $pu ) ) {
722 $this->output( "Creating index '$index'\n" );
723 $this->db->query( $good_def );
724 } else {
725 $this->output( "...index '$index' exists\n" );
726 }
727 }
728
729 protected function checkRevUserFkey() {
730 if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
731 $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
732 } else {
733 $this->applyPatch( 'patch-revision_rev_user_fkey.sql', false, "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT" );
734 }
735 }
736
737 protected function checkIwlPrefix() {
738 if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
739 $this->applyPatch( 'patch-rename-iwl_prefix.sql', false, "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'" );
740 }
741 }
742
743 protected function addInterwikiType() {
744 $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
745 }
746
747 protected function tsearchFixes() {
748 # Tweak the page_title tsearch2 trigger to filter out slashes
749 # This is create or replace, so harmless to call if not needed
750 $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
751
752 # If the server is 8.3 or higher, rewrite the tsearch2 triggers
753 # in case they have the old 'default' versions
754 # Gather version numbers in case we need them
755 if ( $this->db->getServerVersion() >= 8.3 ) {
756 $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
757 }
758 }
759 }