0 AND relname=%s AND nspname=%s END; $res = $this->db->query( sprintf( $q, $this->db->addQuotes( $table ), $this->db->addQuotes( $wgDBmwschema ) ) ); if ( !$res ) { return null; } $cols = array(); foreach ( $res as $r ) { $cols[] = array( "name" => $r[0], "ord" => $r[1], ); } return $cols; } function describeIndex( $idx ) { global $wgDBmwschema; // first fetch the key (which is a list of columns ords) and // the table the index applies to (an oid) $q = <<db->query( sprintf( $q, $this->db->addQuotes( $wgDBmwschema ), $this->db->addQuotes( $idx ) ) ); if ( !$res ) { return null; } if ( !( $r = $this->db->fetchRow( $res ) ) ) { return null; } $indkey = $r[0]; $relid = intval( $r[1] ); $indkeys = explode( ' ', $indkey ); $colnames = array(); foreach ( $indkeys as $rid ) { $query = <<db->query( sprintf( $query, $rid ) ); if ( !$r2 ) { return null; } if ( !( $row2 = $this->db->fetchRow( $r2 ) ) ) { return null; } $colnames[] = $row2[0]; } return $colnames; } function fkeyDeltype( $fkey ) { global $wgDBmwschema; $q = <<db->query( sprintf( $q, $this->db->addQuotes( $wgDBmwschema ), $this->db->addQuotes( $fkey ) ) ); if ( !( $row = $this->db->fetchRow( $r ) ) ) { return null; } return $row[0]; } function ruleDef( $table, $rule ) { global $wgDBmwschema; $q = <<db->query( sprintf( $q, $this->db->addQuotes( $wgDBmwschema ), $this->db->addQuotes( $table ), $this->db->addQuotes( $rule ) ) ); $row = $this->db->fetchRow( $r ); if ( !$row ) { return null; } $d = $row[0]; return $d; } protected function addSequence( $ns ) { if ( !$this->db->sequenceExists( $ns ) ) { $this->output( "Creating sequence $ns\n" ); $this->db->query( "CREATE SEQUENCE $ns" ); } } protected function renameSequence( $old, $new ) { if ( $this->db->sequenceExists( $old ) ) { $this->output( "Renaming sequence $old to $new\n" ); $this->db->query( "ALTER SEQUENCE $old RENAME TO $new" ); } } protected function renameTable( $old, $new ) { if ( $this->db->tableExists( $old ) ) { $this->output( "Renaming table $old to $new\n" ); $old = $this->db->addQuotes( $old ); $this->db->query( "ALTER TABLE $old RENAME TO $new" ); } } protected function addPgField( $table, $field, $type ) { $fi = $this->db->fieldInfo( $table, $field ); if ( !is_null( $fi ) ) { $this->output( "... column '$table.$field' already exists\n" ); return; } else { $this->output( "Adding column '$table.$field'\n" ); $this->db->query( "ALTER TABLE $table ADD $field $type" ); } } protected function changeField( $table, $field, $newtype, $default ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "... error: expected column $table.$field to exist\n" ); exit( 1 ); } if ( $fi->type() === $newtype ) $this->output( "... column '$table.$field' is already of type '$newtype'\n" ); else { $this->output( "Changing column type of '$table.$field' from '{$fi->type()}' to '$newtype'\n" ); $sql = "ALTER TABLE $table ALTER $field TYPE $newtype"; if ( strlen( $default ) ) { $res = array(); if ( preg_match( '/DEFAULT (.+)/', $default, $res ) ) { $sqldef = "ALTER TABLE $table ALTER $field SET DEFAULT $res[1]"; $this->db->query( $sqldef ); $default = preg_replace( '/\s*DEFAULT .+/', '', $default ); } $sql .= " USING $default"; } $sql .= ";\nCOMMIT;\n"; $this->db->query( $sql ); } } protected function changeNullableField( $table, $field, $null ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "... error: expected column $table.$field to exist\n" ); exit( 1 ); } if ( $fi->isNullable() ) { # # It's NULL - does it need to be NOT NULL? if ( 'NOT NULL' === $null ) { $this->output( "Changing '$table.$field' to not allow NULLs\n" ); $this->db->query( "ALTER TABLE $table ALTER $field SET NOT NULL" ); } else { $this->output( "... column '$table.$field' is already set as NULL\n" ); } } else { # # It's NOT NULL - does it need to be NULL? if ( 'NULL' === $null ) { $this->output( "Changing '$table.$field' to allow NULLs\n" ); $this->db->query( "ALTER TABLE $table ALTER $field DROP NOT NULL" ); } else { $this->output( "... column '$table.$field' is already set as NOT NULL\n" ); } } } public function addPgIndex( $table, $index, $type ) { if ( $this->db->indexExists( $table, $index ) ) { $this->output( "... index '$index' on table '$table' already exists\n" ); } else { $this->output( "Creating index '$index' on table '$table' $type\n" ); $this->db->query( "CREATE INDEX $index ON $table $type" ); } } public function addPgExtIndex( $table, $index, $type ) { if ( $this->db->indexExists( $table, $index ) ) { $this->output( "... index '$index' on table '$table' already exists\n" ); } else { $this->output( "Creating index '$index' on table '$table'\n" ); if ( preg_match( '/^\(/', $type ) ) { $this->db->query( "CREATE INDEX $index ON $table $type" ); } else { $this->applyPatch( $type, true ); } } } protected function changeFkeyDeferrable( $table, $field, $clause ) { $fi = $this->db->fieldInfo( $table, $field ); if ( is_null( $fi ) ) { $this->output( "WARNING! Column '$table.$field' does not exist but it should! Please report this.\n" ); return; } if ( $fi->is_deferred() && $fi->is_deferrable() ) { return; } $this->output( "Altering column '$table.$field' to be DEFERRABLE INITIALLY DEFERRED\n" ); $conname = $fi->conname(); $command = "ALTER TABLE $table DROP CONSTRAINT $conname"; $this->db->query( $command ); $command = "ALTER TABLE $table ADD CONSTRAINT $conname FOREIGN KEY ($field) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED"; $this->db->query( $command ); } protected function convertArchive2() { if ( $this->db->tableExists( "archive2" ) ) { $this->output( "Converting 'archive2' back to normal archive table\n" ); if ( $this->db->ruleExists( 'archive', 'archive_insert' ) ) { $this->output( "Dropping rule 'archive_insert'\n" ); $this->db->query( 'DROP RULE archive_insert ON archive' ); } if ( $this->db->ruleExists( 'archive', 'archive_delete' ) ) { $this->output( "Dropping rule 'archive_delete'\n" ); $this->db->query( 'DROP RULE archive_delete ON archive' ); } $this->applyPatch( 'patch-remove-archive2.sql' ); } else { $this->output( "... obsolete table 'archive2' does not exist\n" ); } } protected function checkOiDeleted() { if ( $this->db->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) { $this->output( "Changing 'oldimage.oi_deleted' to type 'smallint'\n" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" ); $this->db->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" ); } else { $this->output( "... column 'oldimage.oi_deleted' is already of type 'smallint'\n" ); } } protected function checkOiNameConstraint() { if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) { $this->output( "... table 'oldimage' has correct cascading delete/update foreign key to image\n" ); } else { if ( $this->db->hasConstraint( "oldimage_oi_name_fkey" ) ) { $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" ); } if ( $this->db->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) { $this->db->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" ); } $this->output( "Making foreign key on table 'oldimage' (to image) a cascade delete/update\n" ); $this->db->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " . "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" ); } } protected function checkPageDeletedTrigger() { if ( !$this->db->triggerExists( 'page', 'page_deleted' ) ) { $this->output( "Adding function and trigger 'page_deleted' to table 'page'\n" ); $this->applyPatch( 'patch-page_deleted.sql' ); } else { $this->output( "... table 'page' has 'page_deleted' trigger\n" ); } } protected function checkRcCurIdNullable(){ $fi = $this->db->fieldInfo( 'recentchanges', 'rc_cur_id' ); if ( !$fi->isNullable() ) { $this->output( "Removing NOT NULL constraint from 'recentchanges.rc_cur_id'\n" ); $this->applyPatch( 'patch-rc_cur_id-not-null.sql' ); } else { $this->output( "... column 'recentchanges.rc_cur_id' has a NOT NULL constraint\n" ); } } protected function checkPagelinkUniqueIndex() { $pu = $this->describeIndex( 'pagelink_unique' ); if ( !is_null( $pu ) && ( $pu[0] != 'pl_from' || $pu[1] != 'pl_namespace' || $pu[2] != 'pl_title' ) ) { $this->output( "Dropping obsolete version of index 'pagelink_unique index'\n" ); $this->db->query( 'DROP INDEX pagelink_unique' ); $pu = null; } else { $this->output( "... obsolete version of index 'pagelink_unique index' does not exist\n" ); } if ( is_null( $pu ) ) { $this->output( "Creating index 'pagelink_unique index'\n" ); $this->db->query( 'CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)' ); } else { $this->output( "... index 'pagelink_unique_index' already exists\n" ); } } protected function checkRevUserFkey() { if ( $this->fkeyDeltype( 'revision_rev_user_fkey' ) == 'r' ) { $this->output( "... constraint 'revision_rev_user_fkey' is ON DELETE RESTRICT\n" ); } else { $this->output( "Changing constraint 'revision_rev_user_fkey' to ON DELETE RESTRICT\n" ); $this->applyPatch( 'patch-revision_rev_user_fkey.sql' ); } } protected function checkIpbAdress() { if ( $this->db->indexExists( 'ipblocks', 'ipb_address' ) ) { $this->output( "Removing deprecated index 'ipb_address'...\n" ); $this->db->query( 'DROP INDEX ipb_address' ); } if ( $this->db->indexExists( 'ipblocks', 'ipb_address_unique' ) ) { $this->output( "... have ipb_address_unique\n" ); } else { $this->output( "Adding ipb_address_unique index\n" ); $this->applyPatch( 'patch-ipb_address_unique.sql' ); } } protected function checkIwlPrefix() { if ( $this->db->indexExists( 'iwlinks', 'iwl_prefix' ) ) { $this->output( "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'...\n" ); $this->applyPatch( 'patch-rename-iwl_prefix.sql' ); } } protected function tsearchFixes() { # Tweak the page_title tsearch2 trigger to filter out slashes # This is create or replace, so harmless to call if not needed $this->applyPatch( 'patch-ts2pagetitle.sql' ); # If the server is 8.3 or higher, rewrite the tsearch2 triggers # in case they have the old 'default' versions # Gather version numbers in case we need them if ( $this->db->getServerVersion() >= 8.3 ) { $this->applyPatch( 'patch-tsearch2funcs.sql' ); } } }