Apply patch from Karsten Düsterloh in Bug #28103.
[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;
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 // 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 );
57 }
58 }
59
60 public function execute() {
61 global $wgCategoryCollation, $wgMiserMode;
62
63 $dbw = wfGetDB( DB_MASTER );
64 $force = $this->getOption( 'force' );
65
66 $options = array( 'LIMIT' => self::BATCH_SIZE );
67
68 if ( $force ) {
69 $options['ORDER BY'] = 'cl_from, cl_to';
70 $collationConds = array();
71 } else {
72 $collationConds = array( 0 =>
73 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ) );
74
75 if ( !$wgMiserMode ) {
76 $count = $dbw->selectField(
77 'categorylinks',
78 'COUNT(*)',
79 $collationConds,
80 __METHOD__
81 );
82
83 if ( $count == 0 ) {
84 $this->output( "Collations up-to-date.\n" );
85 return;
86 }
87 $this->output( "Fixing collation for $count rows.\n" );
88 }
89 }
90
91 $count = 0;
92 $batchConds = array();
93 do {
94 $this->output( 'Processing next ' . self::BATCH_SIZE . ' rows... ');
95 $res = $dbw->select(
96 array( 'categorylinks', 'page' ),
97 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
98 'cl_sortkey', 'page_namespace', 'page_title'
99 ),
100 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
101 __METHOD__,
102 $options
103 );
104
105 $dbw->begin();
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
110 # converted.
111 if ( $row->cl_sortkey == $title->getText()
112 || $row->cl_sortkey == $title->getPrefixedText() ) {
113 $prefix = '';
114 } else {
115 # Custom sortkey, use it as a prefix
116 $prefix = $row->cl_sortkey;
117 }
118 } else {
119 $prefix = $row->cl_sortkey_prefix;
120 }
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 ) {
124 $type = 'subcat';
125 } elseif ( $title->getNamespace() == NS_FILE ) {
126 $type = 'file';
127 } else {
128 $type = 'page';
129 }
130 $dbw->update(
131 'categorylinks',
132 array(
133 'cl_sortkey' => Collation::singleton()->getSortKey(
134 $title->getCategorySortkey( $prefix ) ),
135 'cl_sortkey_prefix' => $prefix,
136 'cl_collation' => $wgCategoryCollation,
137 'cl_type' => $type,
138 'cl_timestamp = cl_timestamp',
139 ),
140 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
141 __METHOD__
142 );
143 }
144 $dbw->commit();
145
146 if ( $force && $row ) {
147 $encFrom = $dbw->addQuotes( $row->cl_from );
148 $encTo = $dbw->addQuotes( $row->cl_to );
149 $batchConds = array(
150 "(cl_from = $encFrom AND cl_to > $encTo) " .
151 " OR cl_from > $encFrom" );
152 }
153
154 $count += $res->numRows();
155 $this->output( "$count done.\n" );
156
157 $this->syncDBs();
158 } while ( $res->numRows() == self::BATCH_SIZE );
159 }
160 }
161
162 $maintClass = "UpdateCollation";
163 require_once( RUN_MAINTENANCE_IF_MAIN );