allow localization of elements via data-msg-text and data-msg-html
[lhc/web/wiklou.git] / maintenance / updateCollation.php
index 6631557..2363665 100644 (file)
 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
 class UpdateCollation extends Maintenance {
-       const BATCH_SIZE = 1000;
+       const BATCH_SIZE = 50; // Number of rows to process in one batch
+       const SYNC_INTERVAL = 20; // Wait for slaves after this many batches
+
+       var $sizeHistogram = array();
 
        public function __construct() {
                parent::__construct();
@@ -42,30 +45,50 @@ 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 ' . 
+               $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
                        'supposed to be up-to-date.' );
-       }
-       
-       public function syncDBs() {
-               $lb = wfGetLB();
-               $dbw = $lb->getConnection( DB_MASTER );
-               $pos = $dbw->getMasterPos();
-               $lb->waitForAll( $pos );
+               $this->addOption( 'previous-collation', 'Set the previous value of ' .
+                       '$wgCategoryCollation here to speed up this script, especially if your ' .
+                       'categorylinks table is large. This will only update rows with that ' .
+                       'collation, though, so it may miss out-of-date rows with a different, ' .
+                       'even older collation.', false, true );
+               $this->addOption( 'target-collation', 'Set this to the new collation type to ' .
+                       'use instead of $wgCategoryCollation. Usually you should not use this, ' . 
+                       'you should just update $wgCategoryCollation in LocalSettings.php.', 
+                       false, true );
+               $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
+                       'compile statistics.' );
+               $this->addOption( 'verbose-stats', 'Show more statistics.' );
        }
 
        public function execute() {
                global $wgCategoryCollation, $wgMiserMode;
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $this->getDB( DB_MASTER );
                $force = $this->getOption( 'force' );
+               $dryRun = $this->getOption( 'dry-run' );
+               $verboseStats = $this->getOption( 'verbose-stats' );
+               if ( $this->hasOption( 'target-collation' ) ) {
+                       $collationName = $this->getOption( 'target-collation' );
+                       $collation = Collation::factory( $collationName );
+               } else {
+                       $collationName = $wgCategoryCollation;
+                       $collation = Collation::singleton();
+               }
 
-               $options = array( 'LIMIT' => self::BATCH_SIZE );
+               $options = array( 'LIMIT' => self::BATCH_SIZE, 'STRAIGHT_JOIN' );
 
-               if ( $force ) {
+               if ( $force || $dryRun ) {
                        $options['ORDER BY'] = 'cl_from, cl_to';
+                       $collationConds = array();
                } else {
-                       $collationConds = array( 0 => 
-                               'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ) );
+                       if ( $this->hasOption( 'previous-collation' ) ) {
+                               $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
+                       } else {
+                               $collationConds = array( 0 =>
+                                       'cl_collation != ' . $dbw->addQuotes( $collationName )
+                               );
+                       }
 
                        if ( !$wgMiserMode ) {
                                $count = $dbw->selectField(
@@ -74,20 +97,26 @@ TEXT;
                                        $collationConds,
                                        __METHOD__
                                );
-
-                               if ( $count == 0 ) {
-                                       $this->output( "Collations up-to-date.\n" );
-                                       return;
-                               }
-                               $this->output( "Fixing collation for $count rows.\n" );
+                       } else {
+                               $count = $dbw->estimateRowCount(
+                                       'categorylinks',
+                                       '*',
+                                       $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;
+               $batchCount = 0;
                $batchConds = array();
                do {
-                       $this->output( 'Processing next ' . self::BATCH_SIZE . ' rows... ');
+                       $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
                        $res = $dbw->select(
                                array( 'categorylinks', 'page' ),
                                array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
@@ -97,15 +126,18 @@ TEXT;
                                __METHOD__,
                                $options
                        );
+                       $this->output( " processing..." );
 
-                       $dbw->begin();
+                       if ( !$dryRun ) {
+                               $dbw->begin( __METHOD__ );
+                       }
                        foreach ( $res as $row ) {
                                $title = Title::newFromRow( $row );
                                if ( !$row->cl_collation ) {
                                        # This is an old-style row, so the sortkey needs to be
                                        # converted.
                                        if ( $row->cl_sortkey == $title->getText()
-                                       || $row->cl_sortkey == $title->getPrefixedText() ) {
+                                               || $row->cl_sortkey == $title->getPrefixedText() ) {
                                                $prefix = '';
                                        } else {
                                                # Custom sortkey, use it as a prefix
@@ -123,35 +155,119 @@ TEXT;
                                } else {
                                        $type = 'page';
                                }
-                               $dbw->update(
-                                       'categorylinks',
-                                       array(
-                                               'cl_sortkey' => Collation::singleton()->getSortKey(
-                                                       $title->getCategorySortkey( $prefix ) ),
-                                               'cl_sortkey_prefix' => $prefix,
-                                               'cl_collation' => $wgCategoryCollation,
-                                               'cl_type' => $type,
-                                               'cl_timestamp = cl_timestamp',
-                                       ),
-                                       array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
-                                       __METHOD__
-                               );
+                               $newSortKey = $collation->getSortKey(
+                                       $title->getCategorySortkey( $prefix ) );
+                               if ( $verboseStats ) {
+                                       $this->updateSortKeySizeHistogram( $newSortKey );
+                               }
+
+                               if ( !$dryRun ) {
+                                       $dbw->update(
+                                               'categorylinks',
+                                               array(
+                                                       'cl_sortkey' => $newSortKey,
+                                                       'cl_sortkey_prefix' => $prefix,
+                                                       'cl_collation' => $collationName,
+                                                       'cl_type' => $type,
+                                                       'cl_timestamp = cl_timestamp',
+                                               ),
+                                               array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
+                                               __METHOD__
+                                       );
+                               }
+                       }
+                       if ( !$dryRun ) {
+                               $dbw->commit( __METHOD__ );
                        }
-                       $dbw->commit();
 
-                       if ( $force && $row ) {
+                       if ( ( $force || $dryRun ) && $row ) {
                                $encFrom = $dbw->addQuotes( $row->cl_from );
                                $encTo = $dbw->addQuotes( $row->cl_to );
-                               $batchConds = array( 
+                               $batchConds = array(
                                        "(cl_from = $encFrom AND cl_to > $encTo) " .
                                        " OR cl_from > $encFrom" );
                        }
 
                        $count += $res->numRows();
                        $this->output( "$count done.\n" );
-                       
-                       $this->syncDBs();
+
+                       if ( !$dryRun && ++$batchCount % self::SYNC_INTERVAL == 0 ) {
+                               $this->output( "Waiting for slaves ... " );
+                               wfWaitForSlaves();
+                               $this->output( "done\n" );
+                       }
                } while ( $res->numRows() == self::BATCH_SIZE );
+
+               $this->output( "$count rows processed\n" );
+
+               if ( $verboseStats ) {
+                       $this->output( "\n" );
+                       $this->showSortKeySizeHistogram();
+               }
+       }
+
+       function updateSortKeySizeHistogram( $key ) {
+               $length = strlen( $key );
+               if ( !isset( $this->sizeHistogram[$length] ) ) {
+                       $this->sizeHistogram[$length] = 0;
+               }
+               $this->sizeHistogram[$length]++;
+       }
+
+       function showSortKeySizeHistogram() {
+               $maxLength = max( array_keys( $this->sizeHistogram ) );
+               if ( $maxLength == 0 ) {
+                       return;
+               }
+               $numBins = 20;
+               $coarseHistogram = array_fill( 0, $numBins, 0 );
+               $coarseBoundaries = array();
+               $boundary = 0;
+               for ( $i = 0; $i < $numBins - 1; $i++ ) {
+                       $boundary += $maxLength / $numBins;
+                       $coarseBoundaries[$i] = round( $boundary );
+               }
+               $coarseBoundaries[$numBins - 1] = $maxLength + 1;
+               $raw = '';
+               for ( $i = 0; $i <= $maxLength; $i++ ) {
+                       if ( $raw !== '' ) {
+                               $raw .= ', ';
+                       }
+                       if ( !isset( $this->sizeHistogram[$i] ) ) {
+                               $val = 0;
+                       } else {
+                               $val = $this->sizeHistogram[$i];
+                       }
+                       for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
+                               if ( $coarseBoundaries[$coarseIndex] > $i ) {
+                                       $coarseHistogram[$coarseIndex] += $val;
+                                       break;
+                               }
+                       }
+                       if ( $coarseIndex == $numBins - 1 ) {
+                               $coarseHistogram[$coarseIndex] += $val;
+                       }
+                       $raw .= $val;
+               }
+
+               $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
+
+               $maxBinVal = max( $coarseHistogram );
+               $scale = 60 / $maxBinVal;
+               $prevBoundary = 0;
+               for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
+                       if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
+                               $val = 0;
+                       } else {
+                               $val = $coarseHistogram[$coarseIndex];
+                       }
+                       $boundary = $coarseBoundaries[$coarseIndex];
+                       $this->output( sprintf( "%-10s %-10d |%s\n",
+                               $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
+                               $val,
+                               str_repeat( '*', $scale * $val ) ) );
+                       $prevBoundary = $boundary;
+               }
        }
 }