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