Merge "adjust method comment for Ia19f1011"
[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 = 50; // Number of rows to process in one batch
33 const SYNC_INTERVAL = 20; // Wait for slaves after this many batches
34
35 public function __construct() {
36 parent::__construct();
37
38 global $wgCategoryCollation;
39 $this->mDescription = <<<TEXT
40 This script will find all rows in the categorylinks table whose collation is
41 out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
42 using the page title and cl_sortkey_prefix. If everything's collation is
43 up-to-date, it will do nothing.
44 TEXT;
45
46 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
47 'supposed to be up-to-date.' );
48 $this->addOption( 'previous-collation', 'Set the previous value of ' .
49 '$wgCategoryCollation here to speed up this script, especially if your ' .
50 'categorylinks table is large. This will only update rows with that ' .
51 'collation, though, so it may miss out-of-date rows with a different, ' .
52 'even older collation.', false, true );
53 }
54
55 public function execute() {
56 global $wgCategoryCollation, $wgMiserMode;
57
58 $dbw = $this->getDB( DB_MASTER );
59 $force = $this->getOption( 'force' );
60
61 $options = array( 'LIMIT' => self::BATCH_SIZE, 'STRAIGHT_JOIN' );
62
63 if ( $force ) {
64 $options['ORDER BY'] = 'cl_from, cl_to';
65 $collationConds = array();
66 } else {
67 if ( $this->hasOption( 'previous-collation' ) ) {
68 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
69 } else {
70 $collationConds = array( 0 =>
71 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation )
72 );
73 }
74
75 if ( !$wgMiserMode ) {
76 $count = $dbw->selectField(
77 'categorylinks',
78 'COUNT(*)',
79 $collationConds,
80 __METHOD__
81 );
82 } else {
83 $count = $dbw->estimateRowCount(
84 'categorylinks',
85 '',
86 $collationConds,
87 __METHOD__
88 );
89 }
90 if ( $count == 0 ) {
91 $this->output( "Collations up-to-date.\n" );
92 return;
93 }
94 $this->output( "Fixing collation for $count rows.\n" );
95 }
96
97 $count = 0;
98 $batchCount = 0;
99 $batchConds = array();
100 do {
101 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
102 $res = $dbw->select(
103 array( 'categorylinks', 'page' ),
104 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
105 'cl_sortkey', 'page_namespace', 'page_title'
106 ),
107 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
108 __METHOD__,
109 $options
110 );
111 $this->output( " processing..." );
112
113 $dbw->begin( __METHOD__ );
114 foreach ( $res as $row ) {
115 $title = Title::newFromRow( $row );
116 if ( !$row->cl_collation ) {
117 # This is an old-style row, so the sortkey needs to be
118 # converted.
119 if ( $row->cl_sortkey == $title->getText()
120 || $row->cl_sortkey == $title->getPrefixedText() ) {
121 $prefix = '';
122 } else {
123 # Custom sortkey, use it as a prefix
124 $prefix = $row->cl_sortkey;
125 }
126 } else {
127 $prefix = $row->cl_sortkey_prefix;
128 }
129 # cl_type will be wrong for lots of pages if cl_collation is 0,
130 # so let's update it while we're here.
131 if ( $title->getNamespace() == NS_CATEGORY ) {
132 $type = 'subcat';
133 } elseif ( $title->getNamespace() == NS_FILE ) {
134 $type = 'file';
135 } else {
136 $type = 'page';
137 }
138 $dbw->update(
139 'categorylinks',
140 array(
141 'cl_sortkey' => Collation::singleton()->getSortKey(
142 $title->getCategorySortkey( $prefix ) ),
143 'cl_sortkey_prefix' => $prefix,
144 'cl_collation' => $wgCategoryCollation,
145 'cl_type' => $type,
146 'cl_timestamp = cl_timestamp',
147 ),
148 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
149 __METHOD__
150 );
151 }
152 $dbw->commit( __METHOD__ );
153
154 if ( $force && $row ) {
155 $encFrom = $dbw->addQuotes( $row->cl_from );
156 $encTo = $dbw->addQuotes( $row->cl_to );
157 $batchConds = array(
158 "(cl_from = $encFrom AND cl_to > $encTo) " .
159 " OR cl_from > $encFrom" );
160 }
161
162 $count += $res->numRows();
163 $this->output( "$count done.\n" );
164
165 if ( ++$batchCount % self::SYNC_INTERVAL == 0 ) {
166 $this->output( "Waiting for slaves ... " );
167 wfWaitForSlaves();
168 $this->output( "done\n" );
169 }
170 } while ( $res->numRows() == self::BATCH_SIZE );
171 }
172 }
173
174 $maintClass = "UpdateCollation";
175 require_once( RUN_MAINTENANCE_IF_MAIN );