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.
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.
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.
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
23 * @ingroup Maintenance
24 * @author Aryeh Gregor (Simetrical)
27 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
29 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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
35 public function __construct() {
36 parent
::__construct();
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.
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 );
55 public function execute() {
56 global $wgCategoryCollation, $wgMiserMode;
58 $dbw = $this->getDB( DB_MASTER
);
59 $force = $this->getOption( 'force' );
61 $options = array( 'LIMIT' => self
::BATCH_SIZE
, 'STRAIGHT_JOIN' );
64 $options['ORDER BY'] = 'cl_from, cl_to';
65 $collationConds = array();
67 if ( $this->hasOption( 'previous-collation' ) ) {
68 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
70 $collationConds = array( 0 =>
71 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation )
75 if ( !$wgMiserMode ) {
76 $count = $dbw->selectField(
84 $this->output( "Collations up-to-date.\n" );
87 $this->output( "Fixing collation for $count rows.\n" );
93 $batchConds = array();
95 $this->output( "Selecting next " . self
::BATCH_SIZE
. " rows..." );
97 array( 'categorylinks', 'page' ),
98 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
99 'cl_sortkey', 'page_namespace', 'page_title'
101 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
105 $this->output( " processing..." );
107 $dbw->begin( __METHOD__
);
108 foreach ( $res as $row ) {
109 $title = Title
::newFromRow( $row );
110 if ( !$row->cl_collation
) {
111 # This is an old-style row, so the sortkey needs to be
113 if ( $row->cl_sortkey
== $title->getText()
114 ||
$row->cl_sortkey
== $title->getPrefixedText() ) {
117 # Custom sortkey, use it as a prefix
118 $prefix = $row->cl_sortkey
;
121 $prefix = $row->cl_sortkey_prefix
;
123 # cl_type will be wrong for lots of pages if cl_collation is 0,
124 # so let's update it while we're here.
125 if ( $title->getNamespace() == NS_CATEGORY
) {
127 } elseif ( $title->getNamespace() == NS_FILE
) {
135 'cl_sortkey' => Collation
::singleton()->getSortKey(
136 $title->getCategorySortkey( $prefix ) ),
137 'cl_sortkey_prefix' => $prefix,
138 'cl_collation' => $wgCategoryCollation,
140 'cl_timestamp = cl_timestamp',
142 array( 'cl_from' => $row->cl_from
, 'cl_to' => $row->cl_to
),
146 $dbw->commit( __METHOD__
);
148 if ( $force && $row ) {
149 $encFrom = $dbw->addQuotes( $row->cl_from
);
150 $encTo = $dbw->addQuotes( $row->cl_to
);
152 "(cl_from = $encFrom AND cl_to > $encTo) " .
153 " OR cl_from > $encFrom" );
156 $count +
= $res->numRows();
157 $this->output( "$count done.\n" );
159 if ( ++
$batchCount % self
::SYNC_INTERVAL
== 0 ) {
160 $this->output( "Waiting for slaves ... " );
162 $this->output( "done\n" );
164 } while ( $res->numRows() == self
::BATCH_SIZE
);
168 $maintClass = "UpdateCollation";
169 require_once( RUN_MAINTENANCE_IF_MAIN
);