Add non-identity collation, with migration script
[lhc/web/wiklou.git] / maintenance / updateCollation.php
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 * @author Aryeh Gregor (Simetrical)
6 */
7
8 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
9
10 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
11
12 class UpdateCollation extends Maintenance {
13 const BATCH_SIZE = 1000;
14
15 public function __construct() {
16 parent::__construct();
17
18 global $wgCollationVersion;
19 $this->mDescription = <<<TEXT
20 This script will find all rows in the categorylinks table whose collation is
21 out-of-date (cl_collation < $wgCollationVersion) and repopulate cl_sortkey
22 using cl_raw_sortkey. If everything's collation is up-to-date, it will do
23 nothing.
24 TEXT;
25
26 #$this->addOption( 'force', 'Run on all rows, even if the collation is supposed to be up-to-date.' );
27 }
28
29 public function execute() {
30 global $wgCollationVersion, $wgContLang;
31
32 $dbw = wfGetDB( DB_MASTER );
33 $count = $dbw->estimateRowCount(
34 'categorylinks',
35 array( 'cl_from', 'cl_to', 'cl_raw_sortkey' ),
36 'cl_collation < ' . $dbw->addQuotes( $wgCollationVersion ),
37 __METHOD__
38 );
39
40 $this->output( "Fixing around $count rows (estimate might be wrong).\n" );
41
42 $count = 0;
43 do {
44 $res = $dbw->select(
45 'categorylinks',
46 array( 'cl_from', 'cl_to', 'cl_raw_sortkey' ),
47 'cl_collation < ' . $dbw->addQuotes( $wgCollationVersion ),
48 __METHOD__,
49 array( 'LIMIT' => self::BATCH_SIZE )
50 );
51
52 $dbw->begin();
53 foreach ( $res as $row ) {
54 # TODO: Handle the case where cl_raw_sortkey is null.
55 $dbw->update(
56 'categorylinks',
57 array(
58 'cl_sortkey' => $wgContLang->convertToSortkey( $row->cl_raw_sortkey ),
59 'cl_collation' => $wgCollationVersion
60 ),
61 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
62 __METHOD__
63 );
64 }
65 $dbw->commit();
66
67 $count += self::BATCH_SIZE;
68 $this->output( "$count done.\n" );
69 } while ( $res->numRows() >= self::BATCH_SIZE );
70 }
71 }
72
73 $maintClass = "UpdateCollation";
74 require_once( DO_MAINTENANCE );