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