Two new parser tests related to bug 6200
[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 the page title and cl_sortkey_prefix. If everything's collation is
23 up-to-date, it will do 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_sortkey_prefix' ),
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 array( 'categorylinks', 'page' ),
46 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
47 'cl_sortkey', 'page_namespace', 'page_title'
48 ),
49 array(
50 'cl_collation != ' . $dbw->addQuotes( $wgCollationVersion ),
51 'cl_from = page_id'
52 ),
53 __METHOD__,
54 array( 'LIMIT' => self::BATCH_SIZE )
55 );
56
57 $dbw->begin();
58 foreach ( $res as $row ) {
59 $title = Title::newFromRow( $row );
60 if ( $row->cl_collation == 0 ) {
61 # This is an old-style row, so the sortkey needs to be
62 # converted.
63 if ( $row->cl_sortkey == $title->getCategorySortkey() ) {
64 $prefix = '';
65 } else {
66 # Custom sortkey, use it as a prefix
67 $prefix = $row->cl_sortkey;
68 }
69 } else {
70 $prefix = $row->cl_sortkey_prefix;
71 }
72 # cl_type will be wrong for lots of pages if cl_collation is 0,
73 # so let's update it while we're here.
74 if ( $title->getNamespace() == NS_CATEGORY ) {
75 $type = 'subcat';
76 } elseif ( $title->getNamespace() == NS_FILE ) {
77 $type = 'file';
78 } else {
79 $type = 'page';
80 }
81 $dbw->update(
82 'categorylinks',
83 array(
84 'cl_sortkey' => $wgContLang->convertToSortkey(
85 $title->getCategorySortkey( $prefix ) ),
86 'cl_sortkey_prefix' => $prefix,
87 'cl_collation' => $wgCollationVersion,
88 'cl_type' => $type,
89 ),
90 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
91 __METHOD__
92 );
93 }
94 $dbw->commit();
95
96 $count += $res->numRows();
97 $this->output( "$count done.\n" );
98 } while ( $res->numRows() == self::BATCH_SIZE );
99 }
100 }
101
102 $maintClass = "UpdateCollation";
103 require_once( DO_MAINTENANCE );