X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=maintenance%2FnamespaceDupes.php;h=b883ea6857b20f2ba4aa7d4a13381014982f5ec5;hb=606872436bd27a669a6dfc23901923a87d0b6fa4;hp=5b010e84af100c4e01c64135d20f481097b71533;hpb=567f244e3611c9e020bc69446a77a10c21257f84;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php index 5b010e84af..b883ea6857 100644 --- a/maintenance/namespaceDupes.php +++ b/maintenance/namespaceDupes.php @@ -23,18 +23,17 @@ * @ingroup Maintenance */ -require_once( dirname(__FILE__) . '/Maintenance.php' ); +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); class NamespaceConflictChecker extends Maintenance { public function __construct() { parent::__construct(); $this->mDescription = ""; $this->addOption( 'fix', 'Attempt to automatically fix errors' ); - $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with\n" . - "\t\t Appended after the article name", false, true ); - $this->addOption( 'prefix', "Do an explicit check for the given title prefix\n" . - "\t\tappended after the article name", false, true ); - $this->addOption( 'wiki', 'Enter the wiki database to edit', false, true ); + $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " . + " appended after the article name", false, true ); + $this->addOption( 'prefix', "Do an explicit check for the given title prefix " . + "appended after the article name", false, true ); } public function execute() { @@ -48,13 +47,13 @@ class NamespaceConflictChecker extends Maintenance { $prefix = $this->getOption( 'prefix', '' ); $key = intval( $this->getOption( 'key', 0 ) ); - if( $prefix ) { + if ( $prefix ) { $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix ); } else { $retval = $this->checkAll( $fix, $suffix ); } - - if( $retval ) { + + if ( $retval ) { $this->output( "\nLooks good!\n" ); } else { $this->output( "\nOh noeees\n" ); @@ -63,44 +62,43 @@ class NamespaceConflictChecker extends Maintenance { /** * @todo Document - * @param $fix bool Whether or not to fix broken entries - * @param $suffix String Suffix to append to renamed articles + * @param $fix Boolean: whether or not to fix broken entries + * @param $suffix String: suffix to append to renamed articles */ private function checkAll( $fix, $suffix = '' ) { - global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames; - global $wgCapitalLinks; - + global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks; + $spaces = array(); - + // List interwikis first, so they'll be overridden // by any conflicting local namespaces. - foreach( $this->getInterwikiList() as $prefix ) { + foreach ( $this->getInterwikiList() as $prefix ) { $name = $wgContLang->ucfirst( $prefix ); $spaces[$name] = 0; } // Now pull in all canonical and alias namespaces... - foreach( $wgCanonicalNamespaceNames as $ns => $name ) { + foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) { // This includes $wgExtraNamespaces - if( $name !== '' ) { + if ( $name !== '' ) { $spaces[$name] = $ns; } } - foreach( $wgContLang->getNamespaces() as $ns => $name ) { - if( $name !== '' ) { + foreach ( $wgContLang->getNamespaces() as $ns => $name ) { + if ( $name !== '' ) { $spaces[$name] = $ns; } } - foreach( $wgNamespaceAliases as $name => $ns ) { + foreach ( $wgNamespaceAliases as $name => $ns ) { $spaces[$name] = $ns; } - foreach( $wgContLang->namespaceAliases as $name => $ns ) { + foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) { $spaces[$name] = $ns; } - + // We'll need to check for lowercase keys as well, // since we're doing case-sensitive searches in the db. - foreach( $spaces as $name => $ns ) { + foreach ( $spaces as $name => $ns ) { $moreNames = array(); $moreNames[] = $wgContLang->uc( $name ); $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) ); @@ -108,24 +106,24 @@ class NamespaceConflictChecker extends Maintenance { $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) ); $moreNames[] = $wgContLang->ucwordbreaks( $name ); $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) ); - if( !$wgCapitalLinks ) { - foreach( $moreNames as $altName ) { + if ( !$wgCapitalLinks ) { + foreach ( $moreNames as $altName ) { $moreNames[] = $wgContLang->lcfirst( $altName ); } $moreNames[] = $wgContLang->lcfirst( $name ); } - foreach( array_unique( $moreNames ) as $altName ) { - if( $altName !== $name ) { + foreach ( array_unique( $moreNames ) as $altName ) { + if ( $altName !== $name ) { $spaces[$altName] = $ns; } } } - + ksort( $spaces ); asort( $spaces ); - + $ok = true; - foreach( $spaces as $name => $ns ) { + foreach ( $spaces as $name => $ns ) { $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok; } return $ok; @@ -133,51 +131,44 @@ class NamespaceConflictChecker extends Maintenance { /** * Get the interwiki list - * @return array + * + * @todo Needs to respect interwiki cache! + * @return Array */ private function getInterwikiList() { $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) ); $prefixes = array(); - while( $row = $this->db->fetchObject( $result ) ) { + foreach ( $result as $row ) { $prefixes[] = $row->iw_prefix; } - $this->db->freeResult( $result ); return $prefixes; } /** * @todo Document - * @param $ns int A namespace id + * @param $ns Integer: a namespace id * @param $name String - * @param $fix bool Whether to fix broken entries - * @param $suffix String Suffix to append to renamed articles + * @param $fix Boolean: whether to fix broken entries + * @param $suffix String: suffix to append to renamed articles */ private function checkNamespace( $ns, $name, $fix, $suffix = '' ) { - if( $ns == 0 ) { - $header = "Checking interwiki prefix: \"$name\"\n"; - } else { - $header = "Checking namespace $ns: \"$name\"\n"; - } - $conflicts = $this->getConflicts( $ns, $name ); $count = count( $conflicts ); - if( $count == 0 ) { - $this->output( $header . "... no conflict detected!\n" ); + if ( $count == 0 ) { return true; } - $this->output( $header . "... $count conflicts detected:\n" ); $ok = true; - foreach( $conflicts as $row ) { + foreach ( $conflicts as $row ) { $resolvable = $this->reportConflict( $row, $suffix ); $ok = $ok && $resolvable; - if( $fix && ( $resolvable || $suffix != '' ) ) { + if ( $fix && ( $resolvable || $suffix != '' ) ) { $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok; } } return $ok; } - + /** * @todo: do this for reals */ @@ -189,39 +180,38 @@ class NamespaceConflictChecker extends Maintenance { /** * Find pages in mainspace that have a prefix of the new namespace * so we know titles that will need migrating - * @param $ns int Namespace id (id for new namespace?) - * @param $name String Prefix that is being made a namespace + * + * @param $ns Integer: namespace id (id for new namespace?) + * @param $name String: prefix that is being made a namespace */ private function getConflicts( $ns, $name ) { $page = 'page'; $table = $this->db->tableName( $page ); $prefix = $this->db->strencode( $name ); - $likeprefix = str_replace( '_', '\\_', $prefix); $encNamespace = $this->db->addQuotes( $ns ); $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)"; - if( $ns == 0 ) { + if ( $ns == 0 ) { // An interwiki; try an alternate encoding with '-' for ':' $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) ); } - + $sql = "SELECT {$page}_id AS id, - {$page}_title AS oldtitle, - $encNamespace AS namespace, - $titleSql AS title - FROM {$table} - WHERE {$page}_namespace=0 - AND {$page}_title LIKE '$likeprefix:%'"; + {$page}_title AS oldtitle, + $encNamespace + {$page}_namespace AS namespace, + $titleSql AS title, + {$page}_namespace AS oldnamespace + FROM {$table} + WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 ) + AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() ); $result = $this->db->query( $sql, __METHOD__ ); $set = array(); - while( $row = $this->db->fetchObject( $result ) ) { + foreach ( $result as $row ) { $set[] = $row; } - $this->db->freeResult( $result ); - return $set; } @@ -230,25 +220,27 @@ class NamespaceConflictChecker extends Maintenance { */ private function reportConflict( $row, $suffix ) { $newTitle = Title::makeTitleSafe( $row->namespace, $row->title ); - if( is_null($newTitle) || !$newTitle->canExist() ) { + if ( is_null( $newTitle ) || !$newTitle->canExist() ) { // Title is also an illegal title... // For the moment we'll let these slide to cleanupTitles or whoever. - $this->output( sprintf( "... %d (0,\"%s\")\n", + $this->output( sprintf( "... %d (%d,\"%s\")\n", $row->id, + $row->oldnamespace, $row->oldtitle ) ); $this->output( "... *** cannot resolve automatically; illegal title ***\n" ); return false; } - $this->output( sprintf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n", + $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n", $row->id, + $row->oldnamespace, $row->oldtitle, $newTitle->getNamespace(), $newTitle->getDBkey(), $newTitle->getPrefixedText() ) ); $id = $newTitle->getArticleId(); - if( $id ) { + if ( $id ) { $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" ); return false; } else { @@ -258,52 +250,54 @@ class NamespaceConflictChecker extends Maintenance { /** * Resolve any conflicts - * @param $row Row from the page table to fix - * @param $resolveable bool - * @param $suffix String Suffix to append to the fixed page + * + * @param $row Object: row from the page table to fix + * @param $resolvable Boolean + * @param $suffix String: suffix to append to the fixed page */ private function resolveConflict( $row, $resolvable, $suffix ) { - if( !$resolvable ) { + if ( !$resolvable ) { $this->output( "... *** old title {$row->title}\n" ); - while( true ) { + while ( true ) { $row->title .= $suffix; $this->output( "... *** new title {$row->title}\n" ); $title = Title::makeTitleSafe( $row->namespace, $row->title ); - if ( ! $title ) { + if ( !$title ) { $this->output( "... !!! invalid title\n" ); return false; } - if ( $id = $title->getArticleId() ) { + $id = $title->getArticleId(); + if ( $id ) { $this->output( "... *** page exists with ID $id ***\n" ); - } else { + } else { break; } } $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" ); } - $tables = array( 'page' ); - foreach( $tables as $table ) { - $this->resolveConflictOn( $row, $table ); - } + $this->resolveConflictOn( $row, 'page', 'page' ); return true; } /** * Resolve a given conflict - * @param $row Row from the old broken entry - * @param $table String Table to update + * + * @param $row Object: row from the old broken entry + * @param $table String: table to update + * @param $prefix String: prefix for column name, like page or ar */ - private function resolveConflictOn( $row, $table ) { + private function resolveConflictOn( $row, $table, $prefix ) { $this->output( "... resolving on $table... " ); $newTitle = Title::makeTitleSafe( $row->namespace, $row->title ); $this->db->update( $table, array( - "{$table}_namespace" => $newTitle->getNamespace(), - "{$table}_title" => $newTitle->getDBkey(), + "{$prefix}_namespace" => $newTitle->getNamespace(), + "{$prefix}_title" => $newTitle->getDBkey(), ), array( - "{$table}_namespace" => 0, - "{$table}_title" => $row->oldtitle, + // "{$prefix}_namespace" => 0, + // "{$prefix}_title" => $row->oldtitle, + "{$prefix}_id" => $row->id, ), __METHOD__ ); $this->output( "ok.\n" ); @@ -312,4 +306,4 @@ class NamespaceConflictChecker extends Maintenance { } $maintClass = "NamespaceConflictChecker"; -require_once( DO_MAINTENANCE ); +require_once( RUN_MAINTENANCE_IF_MAIN );