Follow-up r81619: Add new message key to maintenance file
[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 execute() {
50 global $wgCategoryCollation;
51
52 $dbw = wfGetDB( DB_MASTER );
53 $force = $this->getOption( 'force' );
54
55 $options = array( 'LIMIT' => self::BATCH_SIZE );
56
57 if ( $force ) {
58 $collationConds = array();
59 $options['ORDER BY'] = 'cl_from, cl_to';
60 } else {
61 $collationConds = array( 0 =>
62 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ) );
63
64 $count = $dbw->selectField(
65 'categorylinks',
66 'COUNT(*)',
67 $collationConds,
68 __METHOD__
69 );
70
71 if ( $count == 0 ) {
72 $this->output( "Collations up-to-date.\n" );
73 return;
74 }
75 $this->output( "Fixing collation for $count rows.\n" );
76 }
77
78 $count = 0;
79 $row = false;
80 $batchConds = array();
81 do {
82 $res = $dbw->select(
83 array( 'categorylinks', 'page' ),
84 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
85 'cl_sortkey', 'page_namespace', 'page_title'
86 ),
87 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
88 __METHOD__,
89 $options
90 );
91
92 $dbw->begin();
93 foreach ( $res as $row ) {
94 $title = Title::newFromRow( $row );
95 if ( !$row->cl_collation ) {
96 # This is an old-style row, so the sortkey needs to be
97 # converted.
98 if ( $row->cl_sortkey == $title->getText()
99 || $row->cl_sortkey == $title->getPrefixedText() ) {
100 $prefix = '';
101 } else {
102 # Custom sortkey, use it as a prefix
103 $prefix = $row->cl_sortkey;
104 }
105 } else {
106 $prefix = $row->cl_sortkey_prefix;
107 }
108 # cl_type will be wrong for lots of pages if cl_collation is 0,
109 # so let's update it while we're here.
110 if ( $title->getNamespace() == NS_CATEGORY ) {
111 $type = 'subcat';
112 } elseif ( $title->getNamespace() == NS_FILE ) {
113 $type = 'file';
114 } else {
115 $type = 'page';
116 }
117 $dbw->update(
118 'categorylinks',
119 array(
120 'cl_sortkey' => Collation::singleton()->getSortKey(
121 $title->getCategorySortkey( $prefix ) ),
122 'cl_sortkey_prefix' => $prefix,
123 'cl_collation' => $wgCategoryCollation,
124 'cl_type' => $type,
125 'cl_timestamp = cl_timestamp',
126 ),
127 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
128 __METHOD__
129 );
130 }
131 $dbw->commit();
132
133 if ( $force && $row ) {
134 $encFrom = $dbw->addQuotes( $row->cl_from );
135 $encTo = $dbw->addQuotes( $row->cl_to );
136 $batchConds = array(
137 "(cl_from = $encFrom AND cl_to > $encTo) " .
138 " OR cl_from > $encFrom" );
139 }
140
141 $count += $res->numRows();
142 $this->output( "$count done.\n" );
143 } while ( $res->numRows() == self::BATCH_SIZE );
144 }
145 }
146
147 $maintClass = "UpdateCollation";
148 require_once( RUN_MAINTENANCE_IF_MAIN );