In case tables are created as MyISAM, use row hints for MySQL <5.0 to avoid 4GB limit
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
index 0144f90..c5c1ec5 100644 (file)
@@ -14,7 +14,7 @@
 #
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
 $options = array( 'fix', 'suffix', 'help' );
@@ -27,7 +27,7 @@ if(isset( $options['help'] ) ) {
 print <<<END
 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
     --help          : this help message
-    --fix           : attempt to automaticly fix errors
+    --fix           : attempt to automatically fix errors
     --suffix=<text> : dupes will be renamed with correct namespace with <text>
                       appended after the article name.
 
@@ -39,7 +39,7 @@ class NamespaceConflictChecker {
        function NamespaceConflictChecker( &$db ) {
                $this->db =& $db;
        }
-       
+
        function checkAll( $fix, $suffix = '' ) {
                global $wgContLang;
                $spaces = $wgContLang->getNamespaces();
@@ -49,21 +49,21 @@ class NamespaceConflictChecker {
                }
                return $ok;
        }
-       
+
        function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
                echo "Checking namespace $ns: \"$name\"\n";
                if( $name == '' ) {
                        echo "... skipping article namespace\n";
                        return true;
                }
-               
+
                $conflicts = $this->getConflicts( $ns, $name );
                $count = count( $conflicts );
                if( $count == 0 ) {
                        echo "... no conflicts detected!\n";
                        return true;
                }
-               
+
                echo "... $count conflicts detected:\n";
                $ok = true;
                foreach( $conflicts as $row ) {
@@ -76,13 +76,21 @@ class NamespaceConflictChecker {
                return $ok;
        }
        
+       /**
+        * @fixme: do this for reals
+        */
+       function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
+               echo "Checking prefix \"$prefix\" vs namespace $key\n";
+               return $this->checkNamespace( $key, $prefix, $fix, $suffix );
+       }
+
        function getConflicts( $ns, $name ) {
                $page  = $this->newSchema() ? 'page' : 'cur';
                $table = $this->db->tableName( $page );
-               
+
                $prefix     = $this->db->strencode( $name );
                $likeprefix = str_replace( '_', '\\_', $prefix);
-               
+
                $sql = "SELECT {$page}_id                                  AS id,
                               {$page}_title                               AS oldtitle,
                               $ns                                         AS namespace,
@@ -90,27 +98,27 @@ class NamespaceConflictChecker {
                          FROM {$table}
                         WHERE {$page}_namespace=0
                           AND {$page}_title LIKE '$likeprefix:%'";
-               
+
                $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
-               
+
                $set = array();
                while( $row = $this->db->fetchObject( $result ) ) {
                        $set[] = $row;
                }
                $this->db->freeResult( $result );
-               
+
                return $set;
        }
-       
+
        function reportConflict( $row, $suffix ) {
-               $newTitle = Title::makeTitle( $row->namespace, $row->title );
+               $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
                printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
                        $row->id,
                        $row->oldtitle,
-                       $row->namespace,
-                       $row->title,
+                       $newTitle->getNamespace(),
+                       $newTitle->getDbKey(),
                        $newTitle->getPrefixedText() );
-               
+
                $id = $newTitle->getArticleId();
                if( $id ) {
                        echo "...  *** cannot resolve automatically; page exists with ID $id ***\n";
@@ -119,11 +127,11 @@ class NamespaceConflictChecker {
                        return true;
                }
        }
-       
+
        function resolveConflict( $row, $resolvable, $suffix ) {
                if( !$resolvable ) {
                        $row->title .= $suffix;
-                       $title = Title::makeTitle( $row->namespace, $row->title );
+                       $title = Title::makeTitleSafe( $row->namespace, $row->title );
                        echo "...  *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
                }
                $tables = $this->newSchema()
@@ -134,14 +142,15 @@ class NamespaceConflictChecker {
                }
                return true;
        }
-       
+
        function resolveConflictOn( $row, $table ) {
                $fname = 'NamespaceConflictChecker::resolveConflictOn';
                echo "... resolving on $table... ";
+               $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
                $this->db->update( $table,
                        array(
-                               "{$table}_namespace" => $row->namespace,
-                               "{$table}_title"     => $row->title,
+                               "{$table}_namespace" => $newTitle->getNamespace(),
+                               "{$table}_title"     => $newTitle->getDbKey(),
                        ),
                        array(
                                "{$table}_namespace" => 0,
@@ -151,7 +160,7 @@ class NamespaceConflictChecker {
                echo "ok.\n";
                return true;
        }
-       
+
        function newSchema() {
                return class_exists( 'Revision' );
        }
@@ -164,9 +173,16 @@ $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
 
 $fix = isset( $options['fix'] );
 $suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
-$dbw =& wfGetDB( DB_MASTER );
+$prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
+$key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
+$dbw = wfGetDB( DB_MASTER );
 $duper = new NamespaceConflictChecker( $dbw );
-$retval = $duper->checkAll( $fix, $suffix );
+
+if( $prefix ) {
+       $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
+} else {
+       $retval = $duper->checkAll( $fix, $suffix );
+}
 
 if( $retval ) {
        echo "\nLooks good!\n";