Follow-up r81619: Add new message key to maintenance file
[lhc/web/wiklou.git] / maintenance / updateCollation.php
index 60578ce..2985add 100644 (file)
@@ -1,5 +1,24 @@
 <?php
 /**
+ * Script will find all rows in the categorylinks table whose collation is
+ * out-of-date (cl_collation != $wgCategoryCollation) and repopulate cl_sortkey
+ * using the page title and cl_sortkey_prefix.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Maintenance
  * @author Aryeh Gregor (Simetrical)
@@ -15,52 +34,69 @@ class UpdateCollation extends Maintenance {
        public function __construct() {
                parent::__construct();
 
-               global $wgCollationVersion;
+               global $wgCategoryCollation;
                $this->mDescription = <<<TEXT
 This script will find all rows in the categorylinks table whose collation is
-out-of-date (cl_collation != $wgCollationVersion) and repopulate cl_sortkey
+out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
 using the page title and cl_sortkey_prefix.  If everything's collation is
 up-to-date, it will do nothing.
 TEXT;
 
-               #$this->addOption( 'force', 'Run on all rows, even if the collation is supposed to be up-to-date.' );
+               $this->addOption( 'force', 'Run on all rows, even if the collation is ' . 
+                       'supposed to be up-to-date.' );
        }
-       
+
        public function execute() {
-               global $wgCollationVersion, $wgContLang;
+               global $wgCategoryCollation;
 
                $dbw = wfGetDB( DB_MASTER );
-               $count = $dbw->estimateRowCount(
-                       'categorylinks',
-                       array( 'cl_from', 'cl_to', 'cl_sortkey_prefix' ),
-                       'cl_collation != ' . $dbw->addQuotes( $wgCollationVersion ),
-                       __METHOD__
-               );
+               $force = $this->getOption( 'force' );
+
+               $options = array( 'LIMIT' => self::BATCH_SIZE );
 
-               $this->output( "Fixing around $count rows (estimate might be wrong).\n" );
+               if ( $force ) {
+                       $collationConds = array();
+                       $options['ORDER BY'] = 'cl_from, cl_to';
+               } else {
+                       $collationConds = array( 0 => 
+                               'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ) );
+
+                       $count = $dbw->selectField(
+                               'categorylinks',
+                               'COUNT(*)',
+                               $collationConds,
+                               __METHOD__
+                       );
+
+                       if ( $count == 0 ) {
+                               $this->output( "Collations up-to-date.\n" );
+                               return;
+                       }
+                       $this->output( "Fixing collation for $count rows.\n" );
+               }
 
                $count = 0;
+               $row = false;
+               $batchConds = array();
                do {
                        $res = $dbw->select(
                                array( 'categorylinks', 'page' ),
                                array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
                                        'cl_sortkey', 'page_namespace', 'page_title'
                                ),
-                               array(
-                                       'cl_collation != ' . $dbw->addQuotes( $wgCollationVersion ),
-                                       'cl_from = page_id'
-                               ),
+                               array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
                                __METHOD__,
-                               array( 'LIMIT' => self::BATCH_SIZE )
+                               $options
                        );
 
                        $dbw->begin();
                        foreach ( $res as $row ) {
                                $title = Title::newFromRow( $row );
-                               if ( $row->cl_collation == 0 ) {
+                               if ( !$row->cl_collation ) {
                                        # This is an old-style row, so the sortkey needs to be
                                        # converted.
-                                       if ( $row->cl_sortkey == $title->getCategorySortkey() ) {
+                                       if ( $row->cl_sortkey == $title->getText()
+                                       || $row->cl_sortkey == $title->getPrefixedText() ) {
                                                $prefix = '';
                                        } else {
                                                # Custom sortkey, use it as a prefix
@@ -81,11 +117,12 @@ TEXT;
                                $dbw->update(
                                        'categorylinks',
                                        array(
-                                               'cl_sortkey' => $wgContLang->convertToSortkey(
+                                               'cl_sortkey' => Collation::singleton()->getSortKey(
                                                        $title->getCategorySortkey( $prefix ) ),
                                                'cl_sortkey_prefix' => $prefix,
-                                               'cl_collation' => $wgCollationVersion,
+                                               'cl_collation' => $wgCategoryCollation,
                                                'cl_type' => $type,
+                                               'cl_timestamp = cl_timestamp',
                                        ),
                                        array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
                                        __METHOD__
@@ -93,6 +130,14 @@ TEXT;
                        }
                        $dbw->commit();
 
+                       if ( $force && $row ) {
+                               $encFrom = $dbw->addQuotes( $row->cl_from );
+                               $encTo = $dbw->addQuotes( $row->cl_to );
+                               $batchConds = array( 
+                                       "(cl_from = $encFrom AND cl_to > $encTo) " .
+                                       " OR cl_from > $encFrom" );
+                       }
+
                        $count += $res->numRows();
                        $this->output( "$count done.\n" );
                } while ( $res->numRows() == self::BATCH_SIZE );
@@ -100,4 +145,4 @@ TEXT;
 }
 
 $maintClass = "UpdateCollation";
-require_once( DO_MAINTENANCE );
+require_once( RUN_MAINTENANCE_IF_MAIN );