Merge "Added result properties to action=paraminfo"
[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
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 $batchCount = 0;
93 $batchConds = array();
94 do {
95 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
96 $res = $dbw->select(
97 array( 'categorylinks', 'page' ),
98 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
99 'cl_sortkey', 'page_namespace', 'page_title'
100 ),
101 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
102 __METHOD__,
103 $options
104 );
105 $this->output( " processing..." );
106
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
112 # converted.
113 if ( $row->cl_sortkey == $title->getText()
114 || $row->cl_sortkey == $title->getPrefixedText() ) {
115 $prefix = '';
116 } else {
117 # Custom sortkey, use it as a prefix
118 $prefix = $row->cl_sortkey;
119 }
120 } else {
121 $prefix = $row->cl_sortkey_prefix;
122 }
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 ) {
126 $type = 'subcat';
127 } elseif ( $title->getNamespace() == NS_FILE ) {
128 $type = 'file';
129 } else {
130 $type = 'page';
131 }
132 $dbw->update(
133 'categorylinks',
134 array(
135 'cl_sortkey' => Collation::singleton()->getSortKey(
136 $title->getCategorySortkey( $prefix ) ),
137 'cl_sortkey_prefix' => $prefix,
138 'cl_collation' => $wgCategoryCollation,
139 'cl_type' => $type,
140 'cl_timestamp = cl_timestamp',
141 ),
142 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
143 __METHOD__
144 );
145 }
146 $dbw->commit( __METHOD__ );
147
148 if ( $force && $row ) {
149 $encFrom = $dbw->addQuotes( $row->cl_from );
150 $encTo = $dbw->addQuotes( $row->cl_to );
151 $batchConds = array(
152 "(cl_from = $encFrom AND cl_to > $encTo) " .
153 " OR cl_from > $encFrom" );
154 }
155
156 $count += $res->numRows();
157 $this->output( "$count done.\n" );
158
159 if ( ++$batchCount % self::SYNC_INTERVAL == 0 ) {
160 $this->output( "Waiting for slaves ... " );
161 wfWaitForSlaves();
162 $this->output( "done\n" );
163 }
164 } while ( $res->numRows() == self::BATCH_SIZE );
165 }
166 }
167
168 $maintClass = "UpdateCollation";
169 require_once( RUN_MAINTENANCE_IF_MAIN );