Pasting lines typo in r80025
[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 supposed to be up-to-date.' );
46 }
47
48 public function execute() {
49 global $wgCategoryCollation, $wgContLang;
50
51 $dbw = wfGetDB( DB_MASTER );
52 $count = $dbw->selectField(
53 'categorylinks',
54 'COUNT(*)',
55 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ),
56 __METHOD__
57 );
58
59 if ( $count == 0 ) {
60 $this->output( "Collations up-to-date.\n" );
61 return;
62 }
63 $this->output( "Fixing collation for $count rows.\n" );
64
65 $count = 0;
66 do {
67 $res = $dbw->select(
68 array( 'categorylinks', 'page' ),
69 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
70 'cl_sortkey', 'page_namespace', 'page_title'
71 ),
72 array(
73 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ),
74 'cl_from = page_id'
75 ),
76 __METHOD__,
77 array( 'LIMIT' => self::BATCH_SIZE )
78 );
79
80 $dbw->begin();
81 foreach ( $res as $row ) {
82 $title = Title::newFromRow( $row );
83 if ( $row->cl_collation == 0 ) {
84 # This is an old-style row, so the sortkey needs to be
85 # converted.
86 if ( $row->cl_sortkey == $title->getText()
87 || $row->cl_sortkey == $title->getPrefixedText() ) {
88 $prefix = '';
89 } else {
90 # Custom sortkey, use it as a prefix
91 $prefix = $row->cl_sortkey;
92 }
93 } else {
94 $prefix = $row->cl_sortkey_prefix;
95 }
96 # cl_type will be wrong for lots of pages if cl_collation is 0,
97 # so let's update it while we're here.
98 if ( $title->getNamespace() == NS_CATEGORY ) {
99 $type = 'subcat';
100 } elseif ( $title->getNamespace() == NS_FILE ) {
101 $type = 'file';
102 } else {
103 $type = 'page';
104 }
105 $dbw->update(
106 'categorylinks',
107 array(
108 'cl_sortkey' => $wgContLang->convertToSortkey(
109 $title->getCategorySortkey( $prefix ) ),
110 'cl_sortkey_prefix' => $prefix,
111 'cl_collation' => $wgCategoryCollation,
112 'cl_type' => $type,
113 'cl_timestamp = cl_timestamp',
114 ),
115 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
116 __METHOD__
117 );
118 }
119 $dbw->commit();
120
121 $count += $res->numRows();
122 $this->output( "$count done.\n" );
123 } while ( $res->numRows() == self::BATCH_SIZE );
124 }
125 }
126
127 $maintClass = "UpdateCollation";
128 require_once( RUN_MAINTENANCE_IF_MAIN );