Merge "Special:Version: Link to tree instead of commit for git hashes"
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
index 5fc972c..cbc389b 100644 (file)
@@ -44,9 +44,9 @@ class NamespaceConflictChecker extends Maintenance {
                $this->mDescription = "";
                $this->addOption( 'fix', 'Attempt to automatically fix errors' );
                $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
-                                                                       "<text> appended after the article name", false, true );
+                       "<text> 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 );
+                       "appended after the article name", false, true );
        }
 
        public function execute() {
@@ -138,6 +138,7 @@ class NamespaceConflictChecker extends Maintenance {
                foreach ( $spaces as $name => $ns ) {
                        $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
                }
+
                return $ok;
        }
 
@@ -152,6 +153,7 @@ class NamespaceConflictChecker extends Maintenance {
                foreach ( $result as $row ) {
                        $prefixes[] = $row['iw_prefix'];
                }
+
                return $prefixes;
        }
 
@@ -178,19 +180,21 @@ class NamespaceConflictChecker extends Maintenance {
                                $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
                        }
                }
+
                return $ok;
        }
 
        /**
         * @todo Do this for real
-        * @param int $ns
-        * @param string $name
+        * @param int $key
+        * @param string $prefix
         * @param bool $fix
         * @param string $suffix
         * @return bool
         */
        private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
                $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
+
                return $this->checkNamespace( $key, $prefix, $fix, $suffix );
        }
 
@@ -204,34 +208,29 @@ class NamespaceConflictChecker extends Maintenance {
         * @return array
         */
        private function getConflicts( $ns, $name ) {
-               $page = 'page';
-               $table = $this->db->tableName( $page );
-
-               $prefix = $this->db->strencode( $name );
-               $encNamespace = $this->db->addQuotes( $ns );
-
-               $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
+               $titleSql = "TRIM(LEADING {$this->db->addQuotes( "$name:" )} FROM page_title)";
                if ( $ns == 0 ) {
                        // An interwiki; try an alternate encoding with '-' for ':'
-                       $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
+                       $titleSql = $this->db->buildConcat( array(
+                               $this->db->addQuotes( "$name-" ),
+                               $titleSql,
+                       ) );
                }
 
-               $sql = "SELECT {$page}_id    AS id,
-                                          {$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();
-               foreach ( $result as $row ) {
-                       $set[] = $row;
-               }
-               return $set;
+               return iterator_to_array( $this->db->select( 'page',
+                       array(
+                               'id' => 'page_id',
+                               'oldtitle' => 'page_title',
+                               'namespace' => $this->db->addQuotes( $ns ) . ' + page_namespace',
+                               'title' => $titleSql,
+                               'oldnamespace' => 'page_namespace',
+                       ),
+                       array(
+                               'page_namespace' => array( 0, 1 ),
+                               'page_title' . $this->db->buildLike( "$name:", $this->db->anyString() ),
+                       ),
+                       __METHOD__
+               ) );
        }
 
        /**
@@ -251,6 +250,7 @@ class NamespaceConflictChecker extends Maintenance {
                                $row->oldnamespace,
                                $row->oldtitle ) );
                        $this->output( "...  *** cannot resolve automatically; illegal title ***\n" );
+
                        return false;
                }
 
@@ -265,6 +265,7 @@ class NamespaceConflictChecker extends Maintenance {
                $id = $newTitle->getArticleID();
                if ( $id ) {
                        $this->output( "...  *** cannot resolve automatically; page exists with ID $id ***\n" );
+
                        return false;
                } else {
                        return true;
@@ -288,6 +289,7 @@ class NamespaceConflictChecker extends Maintenance {
                                $title = Title::makeTitleSafe( $row->namespace, $row->title );
                                if ( !$title ) {
                                        $this->output( "... !!! invalid title\n" );
+
                                        return false;
                                }
                                $id = $title->getArticleID();
@@ -300,6 +302,7 @@ class NamespaceConflictChecker extends Maintenance {
                        $this->output( "...  *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
                }
                $this->resolveConflictOn( $row, 'page', 'page' );
+
                return true;
        }
 
@@ -326,6 +329,7 @@ class NamespaceConflictChecker extends Maintenance {
                        ),
                        __METHOD__ );
                $this->output( "ok.\n" );
+
                return true;
        }
 }