66315575c5c90bad3f4a20d4bd49423150a96b9a
[lhc/web/wiklou.git] / maintenance / updateCollation.php
1 <?php
2 /**
3 * Script will find all rows in the categorylinks table whose collation is
4 * out-of-date (cl_collation != $wgCategoryCollation) and repopulate cl_sortkey
5 * using the page title and cl_sortkey_prefix.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 * @author Aryeh Gregor (Simetrical)
25 */
26
27 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
28
29 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
30
31 class UpdateCollation extends Maintenance {
32 const BATCH_SIZE = 1000;
33
34 public function __construct() {
35 parent::__construct();
36
37 global $wgCategoryCollation;
38 $this->mDescription = <<<TEXT
39 This script will find all rows in the categorylinks table whose collation is
40 out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
41 using the page title and cl_sortkey_prefix. If everything's collation is
42 up-to-date, it will do nothing.
43 TEXT;
44
45 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
46 'supposed to be up-to-date.' );
47 }
48
49 public function syncDBs() {
50 $lb = wfGetLB();
51 $dbw = $lb->getConnection( DB_MASTER );
52 $pos = $dbw->getMasterPos();
53 $lb->waitForAll( $pos );
54 }
55
56 public function execute() {
57 global $wgCategoryCollation, $wgMiserMode;
58
59 $dbw = wfGetDB( DB_MASTER );
60 $force = $this->getOption( 'force' );
61
62 $options = array( 'LIMIT' => self::BATCH_SIZE );
63
64 if ( $force ) {
65 $options['ORDER BY'] = 'cl_from, cl_to';
66 } else {
67 $collationConds = array( 0 =>
68 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ) );
69
70 if ( !$wgMiserMode ) {
71 $count = $dbw->selectField(
72 'categorylinks',
73 'COUNT(*)',
74 $collationConds,
75 __METHOD__
76 );
77
78 if ( $count == 0 ) {
79 $this->output( "Collations up-to-date.\n" );
80 return;
81 }
82 $this->output( "Fixing collation for $count rows.\n" );
83 }
84 }
85
86 $count = 0;
87 $row = false;
88 $batchConds = array();
89 do {
90 $this->output( 'Processing next ' . self::BATCH_SIZE . ' rows... ');
91 $res = $dbw->select(
92 array( 'categorylinks', 'page' ),
93 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
94 'cl_sortkey', 'page_namespace', 'page_title'
95 ),
96 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
97 __METHOD__,
98 $options
99 );
100
101 $dbw->begin();
102 foreach ( $res as $row ) {
103 $title = Title::newFromRow( $row );
104 if ( !$row->cl_collation ) {
105 # This is an old-style row, so the sortkey needs to be
106 # converted.
107 if ( $row->cl_sortkey == $title->getText()
108 || $row->cl_sortkey == $title->getPrefixedText() ) {
109 $prefix = '';
110 } else {
111 # Custom sortkey, use it as a prefix
112 $prefix = $row->cl_sortkey;
113 }
114 } else {
115 $prefix = $row->cl_sortkey_prefix;
116 }
117 # cl_type will be wrong for lots of pages if cl_collation is 0,
118 # so let's update it while we're here.
119 if ( $title->getNamespace() == NS_CATEGORY ) {
120 $type = 'subcat';
121 } elseif ( $title->getNamespace() == NS_FILE ) {
122 $type = 'file';
123 } else {
124 $type = 'page';
125 }
126 $dbw->update(
127 'categorylinks',
128 array(
129 'cl_sortkey' => Collation::singleton()->getSortKey(
130 $title->getCategorySortkey( $prefix ) ),
131 'cl_sortkey_prefix' => $prefix,
132 'cl_collation' => $wgCategoryCollation,
133 'cl_type' => $type,
134 'cl_timestamp = cl_timestamp',
135 ),
136 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
137 __METHOD__
138 );
139 }
140 $dbw->commit();
141
142 if ( $force && $row ) {
143 $encFrom = $dbw->addQuotes( $row->cl_from );
144 $encTo = $dbw->addQuotes( $row->cl_to );
145 $batchConds = array(
146 "(cl_from = $encFrom AND cl_to > $encTo) " .
147 " OR cl_from > $encFrom" );
148 }
149
150 $count += $res->numRows();
151 $this->output( "$count done.\n" );
152
153 $this->syncDBs();
154 } while ( $res->numRows() == self::BATCH_SIZE );
155 }
156 }
157
158 $maintClass = "UpdateCollation";
159 require_once( RUN_MAINTENANCE_IF_MAIN );