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;
34 public function __construct() {
35 parent
::__construct();
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.
45 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
46 'supposed to be up-to-date.' );
49 public function syncDBs() {
51 // bug 27975 - Don't try to wait for slaves if there are none
52 // Prevents permission error when getting master position
53 if ( $lb->getServerCount() > 1 ) {
54 $dbw = $lb->getConnection( DB_MASTER
);
55 $pos = $dbw->getMasterPos();
56 $lb->waitForAll( $pos );
60 public function execute() {
61 global $wgCategoryCollation, $wgMiserMode;
63 $dbw = wfGetDB( DB_MASTER
);
64 $force = $this->getOption( 'force' );
66 $options = array( 'LIMIT' => self
::BATCH_SIZE
);
69 $options['ORDER BY'] = 'cl_from, cl_to';
70 $collationConds = array();
72 $collationConds = array( 0 =>
73 '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" );
92 $batchConds = array();
94 $this->output( 'Processing next ' . self
::BATCH_SIZE
. ' rows... ');
96 array( 'categorylinks', 'page' ),
97 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
98 'cl_sortkey', 'page_namespace', 'page_title'
100 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
106 foreach ( $res as $row ) {
107 $title = Title
::newFromRow( $row );
108 if ( !$row->cl_collation
) {
109 # This is an old-style row, so the sortkey needs to be
111 if ( $row->cl_sortkey
== $title->getText()
112 ||
$row->cl_sortkey
== $title->getPrefixedText() ) {
115 # Custom sortkey, use it as a prefix
116 $prefix = $row->cl_sortkey
;
119 $prefix = $row->cl_sortkey_prefix
;
121 # cl_type will be wrong for lots of pages if cl_collation is 0,
122 # so let's update it while we're here.
123 if ( $title->getNamespace() == NS_CATEGORY
) {
125 } elseif ( $title->getNamespace() == NS_FILE
) {
133 'cl_sortkey' => Collation
::singleton()->getSortKey(
134 $title->getCategorySortkey( $prefix ) ),
135 'cl_sortkey_prefix' => $prefix,
136 'cl_collation' => $wgCategoryCollation,
138 'cl_timestamp = cl_timestamp',
140 array( 'cl_from' => $row->cl_from
, 'cl_to' => $row->cl_to
),
146 if ( $force && $row ) {
147 $encFrom = $dbw->addQuotes( $row->cl_from
);
148 $encTo = $dbw->addQuotes( $row->cl_to
);
150 "(cl_from = $encFrom AND cl_to > $encTo) " .
151 " OR cl_from > $encFrom" );
154 $count +
= $res->numRows();
155 $this->output( "$count done.\n" );
158 } while ( $res->numRows() == self
::BATCH_SIZE
);
162 $maintClass = "UpdateCollation";
163 require_once( RUN_MAINTENANCE_IF_MAIN
);