MediaWiki  master
PostgresUpdater.php
Go to the documentation of this file.
00001 <?php
00031 class PostgresUpdater extends DatabaseUpdater {
00032 
00036         protected $db;
00037 
00043         protected function getCoreUpdateList() {
00044                 return array(
00045                         # rename tables 1.7.3
00046                         # r15791 Change reserved word table names "user" and "text"
00047                         array( 'renameTable', 'user', 'mwuser' ),
00048                         array( 'renameTable', 'text', 'pagecontent' ),
00049                         array( 'renameIndex', 'mwuser', 'user_pkey', 'mwuser_pkey'),
00050                         array( 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ),
00051                         array( 'renameIndex', 'pagecontent','text_pkey', 'pagecontent_pkey' ),
00052 
00053                         # renamed sequences
00054                         array( 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq'         ),
00055                         array( 'renameSequence', 'rev_rev_id_val',      'revision_rev_id_seq'         ),
00056                         array( 'renameSequence', 'text_old_id_val',     'text_old_id_seq'             ),
00057                         array( 'renameSequence', 'rc_rc_id_seq',        'recentchanges_rc_id_seq'     ),
00058                         array( 'renameSequence', 'log_log_id_seq',      'logging_log_id_seq'          ),
00059                         array( 'renameSequence', 'pr_id_val',           'page_restrictions_pr_id_seq' ),
00060                         array( 'renameSequence', 'us_id_seq',           'uploadstash_us_id_seq' ),
00061 
00062                         # since r58263
00063                         array( 'renameSequence', 'category_id_seq', 'category_cat_id_seq'),
00064 
00065                         # new sequences if not renamed above
00066                         array( 'addSequence', 'logging', false, 'logging_log_id_seq' ),
00067                         array( 'addSequence', 'page_restrictions', false, 'page_restrictions_pr_id_seq' ),
00068                         array( 'addSequence', 'filearchive', 'fa_id', 'filearchive_fa_id_seq' ),
00069 
00070                         # new tables
00071                         array( 'addTable', 'category',          'patch-category.sql' ),
00072                         array( 'addTable', 'page',              'patch-page.sql' ),
00073                         array( 'addTable', 'querycachetwo',     'patch-querycachetwo.sql' ),
00074                         array( 'addTable', 'page_props',        'patch-page_props.sql' ),
00075                         array( 'addTable', 'page_restrictions', 'patch-page_restrictions.sql' ),
00076                         array( 'addTable', 'profiling',         'patch-profiling.sql' ),
00077                         array( 'addTable', 'protected_titles',  'patch-protected_titles.sql' ),
00078                         array( 'addTable', 'redirect',          'patch-redirect.sql' ),
00079                         array( 'addTable', 'updatelog',         'patch-updatelog.sql' ),
00080                         array( 'addTable', 'change_tag',        'patch-change_tag.sql' ),
00081                         array( 'addTable', 'tag_summary',       'patch-tag_summary.sql' ),
00082                         array( 'addTable', 'valid_tag',         'patch-valid_tag.sql' ),
00083                         array( 'addTable', 'user_properties',   'patch-user_properties.sql' ),
00084                         array( 'addTable', 'log_search',        'patch-log_search.sql' ),
00085                         array( 'addTable', 'l10n_cache',        'patch-l10n_cache.sql' ),
00086                         array( 'addTable', 'iwlinks',           'patch-iwlinks.sql' ),
00087                         array( 'addTable', 'msg_resource',      'patch-msg_resource.sql' ),
00088                         array( 'addTable', 'msg_resource_links','patch-msg_resource_links.sql' ),
00089                         array( 'addTable', 'module_deps',       'patch-module_deps.sql' ),
00090                         array( 'addTable', 'uploadstash',       'patch-uploadstash.sql' ),
00091                         array( 'addTable', 'user_former_groups','patch-user_former_groups.sql' ),
00092                         array( 'addTable', 'external_user',     'patch-external_user.sql' ),
00093 
00094                         # Needed before new field
00095                         array( 'convertArchive2' ),
00096 
00097                         # new fields
00098                         array( 'addPgField', 'updatelog',     'ul_value',             'TEXT' ),
00099                         array( 'addPgField', 'archive',       'ar_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
00100                         array( 'addPgField', 'archive',       'ar_len',               'INTEGER' ),
00101                         array( 'addPgField', 'archive',       'ar_page_id',           'INTEGER' ),
00102                         array( 'addPgField', 'archive',       'ar_parent_id',         'INTEGER' ),
00103                         array( 'addPgField', 'archive',       'ar_content_model',     'TEXT' ),
00104                         array( 'addPgField', 'archive',       'ar_content_format',    'TEXT' ),
00105                         array( 'addPgField', 'categorylinks', 'cl_sortkey_prefix',    "TEXT NOT NULL DEFAULT ''"),
00106                         array( 'addPgField', 'categorylinks', 'cl_collation',         "TEXT NOT NULL DEFAULT 0"),
00107                         array( 'addPgField', 'categorylinks', 'cl_type',              "TEXT NOT NULL DEFAULT 'page'"),
00108                         array( 'addPgField', 'image',         'img_sha1',             "TEXT NOT NULL DEFAULT ''" ),
00109                         array( 'addPgField', 'ipblocks',      'ipb_allow_usertalk',   'SMALLINT NOT NULL DEFAULT 0' ),
00110                         array( 'addPgField', 'ipblocks',      'ipb_anon_only',        'SMALLINT NOT NULL DEFAULT 0' ),
00111                         array( 'addPgField', 'ipblocks',      'ipb_by_text',          "TEXT NOT NULL DEFAULT ''" ),
00112                         array( 'addPgField', 'ipblocks',      'ipb_block_email',      'SMALLINT NOT NULL DEFAULT 0' ),
00113                         array( 'addPgField', 'ipblocks',      'ipb_create_account',   'SMALLINT NOT NULL DEFAULT 1' ),
00114                         array( 'addPgField', 'ipblocks',      'ipb_deleted',          'SMALLINT NOT NULL DEFAULT 0' ),
00115                         array( 'addPgField', 'ipblocks',      'ipb_enable_autoblock', 'SMALLINT NOT NULL DEFAULT 1' ),
00116                         array( 'addPgField', 'ipblocks',      'ipb_parent_block_id',            'INTEGER DEFAULT NULL REFERENCES ipblocks(ipb_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED' ),
00117                         array( 'addPgField', 'filearchive',   'fa_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
00118                         array( 'addPgField', 'filearchive',   'fa_sha1',              "TEXT NOT NULL DEFAULT ''" ),
00119                         array( 'addPgField', 'logging',       'log_deleted',          'SMALLINT NOT NULL DEFAULT 0' ),
00120                         array( 'addPgField', 'logging',       'log_id',               "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ),
00121                         array( 'addPgField', 'logging',       'log_params',           'TEXT' ),
00122                         array( 'addPgField', 'mwuser',        'user_editcount',       'INTEGER' ),
00123                         array( 'addPgField', 'mwuser',        'user_newpass_time',    'TIMESTAMPTZ' ),
00124                         array( 'addPgField', 'oldimage',      'oi_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
00125                         array( 'addPgField', 'oldimage',      'oi_major_mime',        "TEXT NOT NULL DEFAULT 'unknown'" ),
00126                         array( 'addPgField', 'oldimage',      'oi_media_type',        'TEXT' ),
00127                         array( 'addPgField', 'oldimage',      'oi_metadata',          "BYTEA NOT NULL DEFAULT ''" ),
00128                         array( 'addPgField', 'oldimage',      'oi_minor_mime',        "TEXT NOT NULL DEFAULT 'unknown'" ),
00129                         array( 'addPgField', 'oldimage',      'oi_sha1',              "TEXT NOT NULL DEFAULT ''" ),
00130                         array( 'addPgField', 'page',          'page_content_model',   'TEXT' ),
00131                         array( 'addPgField', 'page_restrictions', 'pr_id',            "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_seq')" ),
00132                         array( 'addPgField', 'profiling',     'pf_memory',            'NUMERIC(18,10) NOT NULL DEFAULT 0' ),
00133                         array( 'addPgField', 'recentchanges', 'rc_deleted',           'SMALLINT NOT NULL DEFAULT 0' ),
00134                         array( 'addPgField', 'recentchanges', 'rc_log_action',        'TEXT' ),
00135                         array( 'addPgField', 'recentchanges', 'rc_log_type',          'TEXT' ),
00136                         array( 'addPgField', 'recentchanges', 'rc_logid',             'INTEGER NOT NULL DEFAULT 0' ),
00137                         array( 'addPgField', 'recentchanges', 'rc_new_len',           'INTEGER' ),
00138                         array( 'addPgField', 'recentchanges', 'rc_old_len',           'INTEGER' ),
00139                         array( 'addPgField', 'recentchanges', 'rc_params',            'TEXT' ),
00140                         array( 'addPgField', 'redirect',      'rd_interwiki',         'TEXT NULL' ),
00141                         array( 'addPgField', 'redirect',      'rd_fragment',          'TEXT NULL' ),
00142                         array( 'addPgField', 'revision',      'rev_deleted',          'SMALLINT NOT NULL DEFAULT 0' ),
00143                         array( 'addPgField', 'revision',      'rev_len',              'INTEGER' ),
00144                         array( 'addPgField', 'revision',      'rev_parent_id',        'INTEGER DEFAULT NULL' ),
00145                         array( 'addPgField', 'revision',      'rev_content_model',    'TEXT' ),
00146                         array( 'addPgField', 'revision',      'rev_content_format',   'TEXT' ),
00147                         array( 'addPgField', 'site_stats',    'ss_active_users',      "INTEGER DEFAULT '-1'" ),
00148                         array( 'addPgField', 'user_newtalk',  'user_last_timestamp',  'TIMESTAMPTZ' ),
00149                         array( 'addPgField', 'logging',       'log_user_text',        "TEXT NOT NULL DEFAULT ''" ),
00150                         array( 'addPgField', 'logging',       'log_page',             'INTEGER' ),
00151                         array( 'addPgField', 'interwiki',     'iw_api',               "TEXT NOT NULL DEFAULT ''"),
00152                         array( 'addPgField', 'interwiki',     'iw_wikiid',            "TEXT NOT NULL DEFAULT ''"),
00153                         array( 'addPgField', 'revision',      'rev_sha1',             "TEXT NOT NULL DEFAULT ''" ),
00154                         array( 'addPgField', 'archive',       'ar_sha1',              "TEXT NOT NULL DEFAULT ''" ),
00155                         array( 'addPgField', 'uploadstash',   'us_chunk_inx',         "INTEGER NULL" ),
00156                         array( 'addPgField', 'job',           'job_timestamp',        "TIMESTAMPTZ" ),
00157                         array( 'addPgField', 'job',           'job_random',           "INTEGER NOT NULL DEFAULT 0" ),
00158                         array( 'addPgField', 'job',           'job_token',            "TEXT NOT NULL DEFAULT ''" ),
00159                         array( 'addPgField', 'job',           'job_token_timestamp',  "TIMESTAMPTZ" ),
00160                         array( 'addPgField', 'job',           'job_sha1',             "TEXT NOT NULL DEFAULT ''" ),
00161 
00162                         # type changes
00163                         array( 'changeField', 'archive',       'ar_deleted',      'smallint', '' ),
00164                         array( 'changeField', 'archive',       'ar_minor_edit',   'smallint', 'ar_minor_edit::smallint DEFAULT 0' ),
00165                         array( 'changeField', 'filearchive',   'fa_deleted',      'smallint', '' ),
00166                         array( 'changeField', 'filearchive',   'fa_height',       'integer',  '' ),
00167                         array( 'changeField', 'filearchive',   'fa_metadata',     'bytea',    "decode(fa_metadata,'escape')" ),
00168                         array( 'changeField', 'filearchive',   'fa_size',         'integer',  '' ),
00169                         array( 'changeField', 'filearchive',   'fa_width',        'integer',  '' ),
00170                         array( 'changeField', 'filearchive',   'fa_storage_group', 'text',     '' ),
00171                         array( 'changeField', 'filearchive',   'fa_storage_key',  'text',     '' ),
00172                         array( 'changeField', 'image',         'img_metadata',    'bytea',    "decode(img_metadata,'escape')" ),
00173                         array( 'changeField', 'image',         'img_size',        'integer',  '' ),
00174                         array( 'changeField', 'image',         'img_width',       'integer',  '' ),
00175                         array( 'changeField', 'image',         'img_height',      'integer',  '' ),
00176                         array( 'changeField', 'interwiki',     'iw_local',        'smallint', 'iw_local::smallint' ),
00177                         array( 'changeField', 'interwiki',     'iw_trans',        'smallint', 'iw_trans::smallint DEFAULT 0' ),
00178                         array( 'changeField', 'ipblocks',      'ipb_auto',        'smallint', 'ipb_auto::smallint DEFAULT 0' ),
00179                         array( 'changeField', 'ipblocks',      'ipb_anon_only',   'smallint', "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
00180                         array( 'changeField', 'ipblocks',      'ipb_create_account', 'smallint', "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ),
00181                         array( 'changeField', 'ipblocks',      'ipb_enable_autoblock', 'smallint', "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ),
00182                         array( 'changeField', 'ipblocks',      'ipb_block_email', 'smallint', "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ),
00183                         array( 'changeField', 'ipblocks',      'ipb_address',     'text',     'ipb_address::text' ),
00184                         array( 'changeField', 'ipblocks',      'ipb_deleted',     'smallint', 'ipb_deleted::smallint DEFAULT 0' ),
00185                         array( 'changeField', 'mwuser',        'user_token',      'text',     '' ),
00186                         array( 'changeField', 'mwuser',        'user_email_token', 'text',     '' ),
00187                         array( 'changeField', 'objectcache',   'keyname',         'text',     '' ),
00188                         array( 'changeField', 'oldimage',      'oi_height',       'integer',  '' ),
00189                         array( 'changeField', 'oldimage',      'oi_metadata',     'bytea',    "decode(img_metadata,'escape')" ),
00190                         array( 'changeField', 'oldimage',      'oi_size',         'integer',  '' ),
00191                         array( 'changeField', 'oldimage',      'oi_width',        'integer',  '' ),
00192                         array( 'changeField', 'page',          'page_is_redirect', 'smallint', 'page_is_redirect::smallint DEFAULT 0' ),
00193                         array( 'changeField', 'page',          'page_is_new',     'smallint', 'page_is_new::smallint DEFAULT 0' ),
00194                         array( 'changeField', 'querycache',    'qc_value',        'integer',  '' ),
00195                         array( 'changeField', 'querycachetwo', 'qcc_value',       'integer',  '' ),
00196                         array( 'changeField', 'recentchanges', 'rc_bot',          'smallint', 'rc_bot::smallint DEFAULT 0' ),
00197                         array( 'changeField', 'recentchanges', 'rc_deleted',      'smallint', '' ),
00198                         array( 'changeField', 'recentchanges', 'rc_minor',        'smallint', 'rc_minor::smallint DEFAULT 0' ),
00199                         array( 'changeField', 'recentchanges', 'rc_new',          'smallint', 'rc_new::smallint DEFAULT 0' ),
00200                         array( 'changeField', 'recentchanges', 'rc_type',         'smallint', 'rc_type::smallint DEFAULT 0' ),
00201                         array( 'changeField', 'recentchanges', 'rc_patrolled',    'smallint', 'rc_patrolled::smallint DEFAULT 0' ),
00202                         array( 'changeField', 'revision',      'rev_deleted',     'smallint', 'rev_deleted::smallint DEFAULT 0' ),
00203                         array( 'changeField', 'revision',      'rev_minor_edit',  'smallint', 'rev_minor_edit::smallint DEFAULT 0' ),
00204                         array( 'changeField', 'templatelinks', 'tl_namespace',    'smallint', 'tl_namespace::smallint' ),
00205                         array( 'changeField', 'user_newtalk',  'user_ip',         'text',     'host(user_ip)' ),
00206                         array( 'changeField', 'uploadstash',   'us_image_bits',   'smallint', '' ),
00207 
00208                         # null changes
00209                         array( 'changeNullableField', 'oldimage', 'oi_bits',       'NULL' ),
00210                         array( 'changeNullableField', 'oldimage', 'oi_timestamp',  'NULL' ),
00211                         array( 'changeNullableField', 'oldimage', 'oi_major_mime', 'NULL' ),
00212                         array( 'changeNullableField', 'oldimage', 'oi_minor_mime', 'NULL' ),
00213                         array( 'changeNullableField', 'image', 'img_metadata', 'NOT NULL'),
00214                         array( 'changeNullableField', 'filearchive', 'fa_metadata', 'NOT NULL'),
00215                         array( 'changeNullableField', 'recentchanges', 'rc_cur_id', 'NULL' ),
00216 
00217                         array( 'checkOiDeleted' ),
00218 
00219                         # New indexes
00220                         array( 'addPgIndex', 'archive',       'archive_user_text',      '(ar_user_text)' ),
00221                         array( 'addPgIndex', 'image',         'img_sha1',               '(img_sha1)' ),
00222                         array( 'addPgIndex', 'ipblocks',      'ipb_parent_block_id',              '(ipb_parent_block_id)' ),
00223                         array( 'addPgIndex', 'oldimage',      'oi_sha1',                '(oi_sha1)' ),
00224                         array( 'addPgIndex', 'page',          'page_mediawiki_title',   '(page_title) WHERE page_namespace = 8' ),
00225                         array( 'addPgIndex', 'pagelinks',     'pagelinks_title',        '(pl_title)' ),
00226                         array( 'addPgIndex', 'revision',      'rev_text_id_idx',        '(rev_text_id)' ),
00227                         array( 'addPgIndex', 'recentchanges', 'rc_timestamp_bot',       '(rc_timestamp) WHERE rc_bot = 0' ),
00228                         array( 'addPgIndex', 'templatelinks', 'templatelinks_from',     '(tl_from)' ),
00229                         array( 'addPgIndex', 'watchlist',     'wl_user',                '(wl_user)' ),
00230                         array( 'addPgIndex', 'logging',       'logging_user_type_time', '(log_user, log_type, log_timestamp)' ),
00231                         array( 'addPgIndex', 'logging',       'logging_page_id_time',   '(log_page,log_timestamp)' ),
00232                         array( 'addPgIndex', 'iwlinks',       'iwl_prefix_title_from',  '(iwl_prefix, iwl_title, iwl_from)' ),
00233                         array( 'addPgIndex', 'job',           'job_timestamp_idx',      '(job_timestamp)' ),
00234                         array( 'addPgIndex', 'job',           'job_sha1',               '(job_sha1)' ),
00235                         array( 'addPgIndex', 'job',           'job_cmd_token',          '(job_cmd, job_token, job_random)' ),
00236                         array( 'addPgIndex', 'filearchive',   'fa_sha1',                '(fa_sha1)' ),
00237 
00238                         array( 'checkIndex', 'pagelink_unique', array(
00239                                 array('pl_from', 'int4_ops', 'btree', 0),
00240                                 array('pl_namespace', 'int2_ops', 'btree', 0),
00241                                 array('pl_title', 'text_ops', 'btree', 0),
00242                         ),
00243                         'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ),
00244                         array( 'checkIndex', 'cl_sortkey', array(
00245                                 array('cl_to', 'text_ops', 'btree', 0),
00246                                 array('cl_sortkey', 'text_ops', 'btree', 0),
00247                                 array('cl_from', 'int4_ops', 'btree', 0),
00248                         ),
00249                         'CREATE INDEX cl_sortkey ON "categorylinks" USING "btree" ("cl_to", "cl_sortkey", "cl_from")' ),
00250                         array( 'checkIndex', 'logging_times', array(
00251                                 array('log_timestamp', 'timestamptz_ops', 'btree', 0),
00252                         ),
00253                         'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ),
00254                         array( 'dropIndex', 'oldimage', 'oi_name' ),
00255                         array( 'checkIndex', 'oi_name_archive_name', array(
00256                                 array('oi_name', 'text_ops', 'btree', 0),
00257                                 array('oi_archive_name', 'text_ops', 'btree', 0),
00258                         ),
00259                         'CREATE INDEX "oi_name_archive_name" ON "oldimage" USING "btree" ("oi_name", "oi_archive_name")' ),
00260                         array( 'checkIndex', 'oi_name_timestamp', array(
00261                                 array('oi_name', 'text_ops', 'btree', 0),
00262                                 array('oi_timestamp', 'timestamptz_ops', 'btree', 0),
00263                         ),
00264                         'CREATE INDEX "oi_name_timestamp" ON "oldimage" USING "btree" ("oi_name", "oi_timestamp")' ),
00265                         array( 'checkIndex', 'page_main_title', array(
00266                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00267                         ),
00268                         'CREATE INDEX "page_main_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 0)' ),
00269                         array( 'checkIndex', 'page_mediawiki_title', array(
00270                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00271                         ),
00272                         'CREATE INDEX "page_mediawiki_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 8)' ),
00273                         array( 'checkIndex', 'page_project_title', array(
00274                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00275                         ),
00276                         'CREATE INDEX "page_project_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 4)' ),
00277                         array( 'checkIndex', 'page_talk_title', array(
00278                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00279                         ),
00280                         'CREATE INDEX "page_talk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 1)' ),
00281                         array( 'checkIndex', 'page_user_title', array(
00282                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00283                         ),
00284                         'CREATE INDEX "page_user_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 2)' ),
00285                         array( 'checkIndex', 'page_utalk_title', array(
00286                                 array('page_title', 'text_pattern_ops', 'btree', 0),
00287                         ),
00288                         'CREATE INDEX "page_utalk_title" ON "page" USING "btree" ("page_title" "text_pattern_ops") WHERE ("page_namespace" = 3)' ),
00289                         array( 'checkIndex', 'ts2_page_text', array(
00290                                 array('textvector', 'tsvector_ops', 'gist', 0),
00291                         ),
00292                         'CREATE INDEX "ts2_page_text" ON "pagecontent" USING "gist" ("textvector")' ),
00293                         array( 'checkIndex', 'ts2_page_title', array(
00294                                 array('titlevector', 'tsvector_ops', 'gist', 0),
00295                         ),
00296                         'CREATE INDEX "ts2_page_title" ON "page" USING "gist" ("titlevector")' ),
00297 
00298                         array( 'checkOiNameConstraint' ),
00299                         array( 'checkPageDeletedTrigger' ),
00300                         array( 'checkRevUserFkey' ),
00301                         array( 'dropIndex', 'ipblocks', 'ipb_address'),
00302                         array( 'checkIndex', 'ipb_address_unique', array(
00303                                 array('ipb_address', 'text_ops', 'btree', 0),
00304                                 array('ipb_user',    'int4_ops', 'btree', 0),
00305                                 array('ipb_auto',    'int2_ops', 'btree', 0),
00306                                 array('ipb_anon_only', 'int2_ops', 'btree', 0),
00307                         ),
00308                         'CREATE UNIQUE INDEX ipb_address_unique ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only)' ),
00309 
00310                         array( 'checkIwlPrefix' ),
00311 
00312                         # All FK columns should be deferred
00313                         array( 'changeFkeyDeferrable', 'archive',          'ar_user',         'mwuser(user_id) ON DELETE SET NULL' ),
00314                         array( 'changeFkeyDeferrable', 'categorylinks',    'cl_from',         'page(page_id) ON DELETE CASCADE' ),
00315                         array( 'changeFkeyDeferrable', 'externallinks',    'el_from',         'page(page_id) ON DELETE CASCADE' ),
00316                         array( 'changeFkeyDeferrable', 'filearchive',      'fa_deleted_user', 'mwuser(user_id) ON DELETE SET NULL' ),
00317                         array( 'changeFkeyDeferrable', 'filearchive',      'fa_user',         'mwuser(user_id) ON DELETE SET NULL' ),
00318                         array( 'changeFkeyDeferrable', 'image',            'img_user',        'mwuser(user_id) ON DELETE SET NULL' ),
00319                         array( 'changeFkeyDeferrable', 'imagelinks',       'il_from',         'page(page_id) ON DELETE CASCADE' ),
00320                         array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_by',          'mwuser(user_id) ON DELETE CASCADE' ),
00321                         array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_user',        'mwuser(user_id) ON DELETE SET NULL' ),
00322                         array( 'changeFkeyDeferrable', 'ipblocks',         'ipb_parent_block_id',       'ipblocks(ipb_id) ON DELETE SET NULL' ),
00323                         array( 'changeFkeyDeferrable', 'langlinks',        'll_from',         'page(page_id) ON DELETE CASCADE' ),
00324                         array( 'changeFkeyDeferrable', 'logging',          'log_user',        'mwuser(user_id) ON DELETE SET NULL' ),
00325                         array( 'changeFkeyDeferrable', 'oldimage',         'oi_name',         'image(img_name) ON DELETE CASCADE ON UPDATE CASCADE' ),
00326                         array( 'changeFkeyDeferrable', 'oldimage',         'oi_user',         'mwuser(user_id) ON DELETE SET NULL' ),
00327                         array( 'changeFkeyDeferrable', 'pagelinks',        'pl_from',         'page(page_id) ON DELETE CASCADE' ),
00328                         array( 'changeFkeyDeferrable', 'page_props',       'pp_page',         'page (page_id) ON DELETE CASCADE' ),
00329                         array( 'changeFkeyDeferrable', 'page_restrictions', 'pr_page',         'page(page_id) ON DELETE CASCADE' ),
00330                         array( 'changeFkeyDeferrable', 'protected_titles', 'pt_user',         'mwuser(user_id) ON DELETE SET NULL' ),
00331                         array( 'changeFkeyDeferrable', 'recentchanges',    'rc_cur_id',       'page(page_id) ON DELETE SET NULL' ),
00332                         array( 'changeFkeyDeferrable', 'recentchanges',    'rc_user',         'mwuser(user_id) ON DELETE SET NULL' ),
00333                         array( 'changeFkeyDeferrable', 'redirect',         'rd_from',         'page(page_id) ON DELETE CASCADE' ),
00334                         array( 'changeFkeyDeferrable', 'revision',         'rev_page',        'page (page_id) ON DELETE CASCADE' ),
00335                         array( 'changeFkeyDeferrable', 'revision',         'rev_user',        'mwuser(user_id) ON DELETE RESTRICT' ),
00336                         array( 'changeFkeyDeferrable', 'templatelinks',    'tl_from',         'page(page_id) ON DELETE CASCADE' ),
00337                         array( 'changeFkeyDeferrable', 'user_groups',      'ug_user',         'mwuser(user_id) ON DELETE CASCADE' ),
00338                         array( 'changeFkeyDeferrable', 'user_newtalk',     'user_id',         'mwuser(user_id) ON DELETE CASCADE' ),
00339                         array( 'changeFkeyDeferrable', 'user_properties',  'up_user',         'mwuser(user_id) ON DELETE CASCADE' ),
00340                         array( 'changeFkeyDeferrable', 'watchlist',        'wl_user',         'mwuser(user_id) ON DELETE CASCADE' ),
00341 
00342                         # r81574
00343                         array( 'addInterwikiType' ),
00344                         # end
00345                         array( 'tsearchFixes' ),
00346                 );
00347         }
00348 
00349         protected function getOldGlobalUpdates() {
00350                 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
00351 
00352                 $updates = array();
00353 
00354                 # Add missing extension tables
00355                 foreach ( $wgExtNewTables as $tableRecord ) {
00356                         $updates[] = array(
00357                                 'addTable', $tableRecord[0], $tableRecord[1], true
00358                         );
00359                 }
00360 
00361                 # Add missing extension fields
00362                 foreach ( $wgExtPGNewFields as $fieldRecord ) {
00363                         $updates[] = array(
00364                                         'addPgField', $fieldRecord[0], $fieldRecord[1],
00365                                         $fieldRecord[2]
00366                                 );
00367                 }
00368 
00369                 # Change altered columns
00370                 foreach ( $wgExtPGAlteredFields as $fieldRecord ) {
00371                         $updates[] = array(
00372                                         'changeField', $fieldRecord[0], $fieldRecord[1],
00373                                         $fieldRecord[2]
00374                                 );
00375                 }
00376 
00377                 # Add missing extension indexes
00378                 foreach ( $wgExtNewIndexes as $fieldRecord ) {
00379                         $updates[] = array(
00380                                         'addPgExtIndex', $fieldRecord[0], $fieldRecord[1],
00381                                         $fieldRecord[2]
00382                                 );
00383                 }
00384 
00385                 return $updates;
00386         }
00387 
00388         protected function describeTable( $table ) {
00389                 $q = <<<END
00390 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
00391         WHERE pg_class.relnamespace = pg_namespace.oid
00392           AND attrelid=pg_class.oid AND attnum > 0
00393           AND relname=%s AND nspname=%s
00394 END;
00395                 $res = $this->db->query( sprintf( $q,
00396                                 $this->db->addQuotes( $table ),
00397                                 $this->db->addQuotes( $this->db->getCoreSchema() ) ) );
00398                 if ( !$res ) {
00399                         return null;
00400                 }
00401 
00402                 $cols = array();
00403                 foreach ( $res as $r ) {
00404                         $cols[] = array(
00405                                         "name" => $r[0],
00406                                         "ord" => $r[1],
00407                                 );
00408                 }
00409                 return $cols;
00410         }
00411 
00412         function describeIndex( $idx ) {
00413                 // first fetch the key (which is a list of columns ords) and
00414                 // the table the index applies to (an oid)
00415                 $q = <<<END
00416 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
00417         WHERE nspname=%s
00418           AND pg_class.relnamespace = pg_namespace.oid
00419           AND relname=%s
00420           AND indexrelid=pg_class.oid
00421 END;
00422                 $res = $this->db->query(
00423                         sprintf(
00424                                 $q,
00425                                 $this->db->addQuotes( $this->db->getCoreSchema() ),
00426                                 $this->db->addQuotes( $idx )
00427                         )
00428                 );
00429                 if ( !$res ) {
00430                         return null;
00431                 }
00432                 if ( !( $r = $this->db->fetchRow( $res ) ) ) {
00433                         return null;
00434                 }
00435 
00436                 $indkey = $r[0];
00437                 $relid = intval( $r[1] );
00438                 $indkeys = explode( ' ', $indkey );
00439 
00440                 $colnames = array();
00441                 foreach ( $indkeys as $rid ) {
00442                         $query = <<<END
00443 SELECT attname FROM pg_class, pg_attribute
00444         WHERE attrelid=$relid
00445           AND attnum=%d
00446           AND attrelid=pg_class.oid
00447 END;
00448                         $r2 = $this->db->query( sprintf( $query, $rid ) );
00449                         if ( !$r2 ) {
00450                                 return null;
00451                         }
00452                         if ( !( $row2 = $this->db->fetchRow( $r2 ) ) ) {
00453                                 return null;
00454                         }
00455                         $colnames[] = $row2[0];
00456                 }
00457 
00458                 return $colnames;
00459         }
00460 
00461         function fkeyDeltype( $fkey ) {
00462                 $q = <<<END
00463 SELECT confdeltype FROM pg_constraint, pg_namespace
00464         WHERE connamespace=pg_namespace.oid
00465           AND nspname=%s
00466           AND conname=%s;
00467 END;
00468                 $r = $this->db->query(
00469                         sprintf(
00470                                 $q,
00471                                 $this->db->addQuotes( $this->db->getCoreSchema() ),
00472                                 $this->db->addQuotes( $fkey )
00473                         )
00474                 );
00475                 if ( !( $row = $this->db->fetchRow( $r ) ) ) {
00476                         return null;
00477                 }
00478                 return $row[0];
00479         }
00480 
00481         function ruleDef( $table, $rule ) {
00482         $q = <<<END
00483 SELECT definition FROM pg_rules
00484         WHERE schemaname = %s
00485           AND tablename = %s
00486           AND rulename = %s
00487 END;
00488                 $r = $this->db->query(
00489                         sprintf(
00490                                 $q,
00491                                 $this->db->addQuotes( $this->db->getCoreSchema() ),
00492                                 $this->db->addQuotes( $table ),
00493                                 $this->db->addQuotes( $rule )
00494                         )
00495                 );
00496                 $row = $this->db->fetchRow( $r );
00497                 if ( !$row ) {
00498                         return null;
00499                 }
00500                 $d = $row[0];
00501                 return $d;
00502         }
00503 
00504         protected function addSequence( $table, $pkey, $ns ) {
00505                 if ( !$this->db->sequenceExists( $ns ) ) {
00506                         $this->output( "Creating sequence $ns\n" );
00507                         $this->db->query( "CREATE SEQUENCE $ns" );
00508                         if( $pkey !== false ) {
00509                                 $this->setDefault( $table,  $pkey, '"nextval"(\'"' . $ns . '"\'::"regclass")' );
00510                         }
00511                 }
00512         }
00513 
00514         protected function renameSequence( $old, $new ) {
00515                 if ( $this->db->sequenceExists( $new ) ) {
00516                         $this->output( "...sequence $new already exists.\n" );
00517                         return;
00518                 }
00519                 if ( $this->db->sequenceExists( $old ) ) {
00520                         $this->output( "Renaming sequence $old to $new\n" );
00521                         $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" );
00522                 }
00523         }
00524 
00525         protected function renameTable( $old, $new, $patch = false ) {
00526                 if ( $this->db->tableExists( $old ) ) {
00527                         $this->output( "Renaming table $old to $new\n" );
00528                         $old = $this->db->realTableName( $old, "quoted" );
00529                         $new = $this->db->realTableName( $new, "quoted" );
00530                         $this->db->query( "ALTER TABLE $old RENAME TO $new" );
00531                         if( $patch !== false ) {
00532                                 $this->applyPatch( $patch );
00533                         }
00534                 }
00535         }
00536 
00537         protected function renameIndex( $table, $old, $new ) {
00538                 if ( $this->db->indexExists( $table, $old ) ) {
00539                         $this->output( "Renaming index $old to $new\n" );
00540                         $this->db->query( "ALTER INDEX $old RENAME TO $new" );
00541                 }
00542         }
00543 
00544         protected function addPgField( $table, $field, $type ) {
00545                 $fi = $this->db->fieldInfo( $table, $field );
00546                 if ( !is_null( $fi ) ) {
00547                         $this->output( "...column '$table.$field' already exists\n" );
00548                         return;
00549                 } else {
00550                         $this->output( "Adding column '$table.$field'\n" );
00551                         $this->db->query( "ALTER TABLE $table ADD $field $type" );
00552                 }
00553         }
00554 
00555         protected function changeField( $table, $field, $newtype, $default ) {
00556                 $fi = $this->db->fieldInfo( $table, $field );
00557                 if ( is_null( $fi ) ) {
00558                         $this->output( "...ERROR: expected column $table.$field to exist\n" );
00559                         exit( 1 );
00560                 }
00561 
00562                 if ( $fi->type() === $newtype )
00563                         $this->output( "...column '$table.$field' is already of type '$newtype'\n" );
00564                 else {
00565                         $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" );
00566                         $sql = "ALTER TABLE $table ALTER $field TYPE $newtype";
00567                         if ( strlen( $default ) ) {
00568                                 $res = array();
00569                                 if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) {
00570                                         $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]";
00571                                         $this->db->query( $sqldef );
00572                                         $default = preg_replace( '/\s*DEFAULT .+/', '', $default );
00573                                 }
00574                                 $sql .= " USING $default";
00575                         }
00576                         $this->db->query( $sql );
00577                 }
00578         }
00579 
00580         protected function setDefault( $table, $field, $default ) {
00581 
00582                 $info = $this->db->fieldInfo( $table, $field );
00583                 if ( $info->defaultValue() !== $default ) {
00584                         $this->output( "Changing '$table.$field' default value\n" );
00585                         $this->db->query( "ALTER TABLE $table ALTER $field SET DEFAULT " . $default );
00586                 }
00587         }
00588 
00589         protected function changeNullableField( $table, $field, $null) {
00590                 $fi = $this->db->fieldInfo( $table, $field );
00591                 if ( is_null( $fi ) ) {
00592                         $this->output( "...ERROR: expected column $table.$field to exist\n" );
00593                         exit( 1 );
00594                 }
00595                 if ( $fi->isNullable() ) {
00596                         # # It's NULL - does it need to be NOT NULL?
00597                         if ( 'NOT NULL' === $null ) {
00598                                 $this->output( "Changing '$table.$field' to not allow NULLs\n" );
00599                                 $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" );
00600                         } else {
00601                                 $this->output( "...column '$table.$field' is already set as NULL\n" );
00602                         }
00603                 } else {
00604                         # # It's NOT NULL - does it need to be NULL?
00605                         if ( 'NULL' === $null ) {
00606                                 $this->output( "Changing '$table.$field' to allow NULLs\n" );
00607                                 $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" );
00608                         }
00609                         else {
00610                                 $this->output( "...column '$table.$field' is already set as NOT NULL\n" );
00611                         }
00612                 }
00613         }
00614 
00615         public function addPgIndex( $table, $index, $type ) {
00616                 if ( $this->db->indexExists( $table, $index ) ) {
00617                         $this->output( "...index '$index' on table '$table' already exists\n" );
00618                 } else {
00619                         $this->output( "Creating index '$index' on table '$table' $type\n" );
00620                         $this->db->query( "CREATE INDEX $index ON $table $type" );
00621                 }
00622         }
00623 
00624         public function addPgExtIndex( $table, $index, $type ) {
00625                 if ( $this->db->indexExists( $table, $index ) ) {
00626                         $this->output( "...index '$index' on table '$table' already exists\n" );
00627                 } else {
00628                         if ( preg_match( '/^\(/', $type ) ) {
00629                                 $this->output( "Creating index '$index' on table '$table'\n" );
00630                                 $this->db->query( "CREATE INDEX $index ON $table $type" );
00631                         } else {
00632                                 $this->applyPatch( $type, true, "Creating index '$index' on table '$table'" );
00633                         }
00634                 }
00635         }
00636 
00637         protected function changeFkeyDeferrable( $table, $field, $clause ) {
00638                 $fi = $this->db->fieldInfo( $table, $field );
00639                 if ( is_null( $fi ) ) {
00640                         $this->output( "WARNING! Column '$table.$field' does not exist but it should! Please report this.\n" );
00641                         return;
00642                 }
00643                 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
00644                         return;
00645                 }
00646                 $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" );
00647                 $conname = $fi->conname();
00648                 if ( $fi->conname() ) {
00649                         $conclause = "CONSTRAINT \"$conname\"";
00650                         $command = "ALTER TABLE $table DROP CONSTRAINT $conname";
00651                         $this->db->query( $command );
00652                 } else {
00653                         $this->output( "Column '$table.$field' does not have a foreign key constraint, will be added\n" );
00654                         $conclause = "";
00655                 }
00656                 $command = "ALTER TABLE $table ADD $conclause FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
00657                 $this->db->query( $command );
00658         }
00659 
00660         protected function convertArchive2() {
00661                 if ( $this->db->tableExists( "archive2" ) ) {
00662                         if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) {
00663                                 $this->output( "Dropping rule 'archive_insert'\n" );
00664                                 $this->db->query( 'DROP RULE archive_insert ON archive' );
00665                         }
00666                         if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) {
00667                                 $this->output( "Dropping rule 'archive_delete'\n" );
00668                                 $this->db->query( 'DROP RULE archive_delete ON archive' );
00669                         }
00670                         $this->applyPatch( 'patch-remove-archive2.sql', false, "Converting 'archive2' back to normal archive table" );
00671                 } else {
00672                         $this->output( "...obsolete table 'archive2' does not exist\n" );
00673                 }
00674         }
00675 
00676         protected function checkOiDeleted() {
00677                 if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
00678                         $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" );
00679                         $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
00680                         $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
00681                         $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
00682                 } else {
00683                         $this->output( "...column 'oldimage.oi_deleted' is already of type 'smallint'\n" );
00684                 }
00685         }
00686 
00687         protected function checkOiNameConstraint() {
00688                 if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
00689                         $this->output( "...table 'oldimage' has correct cascading delete/update foreign key to image\n" );
00690                 } else {
00691                         if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) {
00692                                 $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
00693                         }
00694                         if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
00695                                 $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
00696                         }
00697                         $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" );
00698                         $this->db->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
00699                                 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" );
00700                 }
00701         }
00702 
00703         protected function checkPageDeletedTrigger() {
00704                 if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) {
00705                         $this->applyPatch( 'patch-page_deleted.sql', false, "Adding function and trigger 'page_deleted' to table 'page'" );
00706                 } else {
00707                         $this->output( "...table 'page' has 'page_deleted' trigger\n" );
00708                 }
00709         }
00710 
00711         protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
00712                 if ( $this->db->indexExists( $table, $index ) ) {
00713                         $this->output( "Dropping obsolete index '$index'\n" );
00714                         $this->db->query( "DROP INDEX \"". $index ."\"" );
00715                 }
00716         }
00717 
00718         protected function checkIndex( $index, $should_be, $good_def ) {
00719                 $pu = $this->db->indexAttributes( $index );
00720                 if ( !empty( $pu ) && $pu != $should_be ) {
00721                         $this->output( "Dropping obsolete version of index '$index'\n" );
00722                         $this->db->query( "DROP INDEX \"". $index ."\"" );
00723                         $pu = array();
00724                 } else {
00725                         $this->output( "...no need to drop index '$index'\n" );
00726                 }
00727 
00728                 if ( empty( $pu ) ) {
00729                         $this->output( "Creating index '$index'\n" );
00730                         $this->db->query( $good_def );
00731                 } else {
00732                         $this->output( "...index '$index' exists\n" );
00733                 }
00734         }
00735 
00736         protected function checkRevUserFkey() {
00737                 if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) {
00738                         $this->output( "...constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" );
00739                 } else {
00740                         $this->applyPatch( 'patch-revision_rev_user_fkey.sql', false, "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT" );
00741                 }
00742         }
00743 
00744         protected function checkIwlPrefix() {
00745                 if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) {
00746                         $this->applyPatch( 'patch-rename-iwl_prefix.sql', false, "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'" );
00747                 }
00748         }
00749 
00750         protected function addInterwikiType() {
00751                 $this->applyPatch( 'patch-add_interwiki.sql', false, "Refreshing add_interwiki()" );
00752         }
00753 
00754         protected function tsearchFixes() {
00755                 # Tweak the page_title tsearch2 trigger to filter out slashes
00756                 # This is create or replace, so harmless to call if not needed
00757                 $this->applyPatch( 'patch-ts2pagetitle.sql', false, "Refreshing ts2_page_title()" );
00758 
00759                 # If the server is 8.3 or higher, rewrite the tsearch2 triggers
00760                 # in case they have the old 'default' versions
00761                 # Gather version numbers in case we need them
00762                 if ( $this->db->getServerVersion() >= 8.3 ) {
00763                         $this->applyPatch( 'patch-tsearch2funcs.sql', false, "Rewriting tsearch2 triggers" );
00764                 }
00765         }
00766 }