mediawiki.action.edit.editWarning: Reuse jQuery collections
[lhc/web/wiklou.git] / maintenance / populateCategory.php
1 <?php
2 /**
3 * Populate the category table.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Simetrical
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Mainteance script to populate the category table.
29 *
30 * @ingroup Maintenance
31 */
32 class PopulateCategory extends Maintenance {
33
34 const REPORTING_INTERVAL = 1000;
35
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = <<<TEXT
39 This script will populate the category table, added in MediaWiki 1.13. It will
40 print out progress indicators every 1000 categories it adds to the table. The
41 script is perfectly safe to run on large, live wikis, and running it multiple
42 times is harmless. You may want to use the throttling options if it's causing
43 too much load; they will not affect correctness.
44
45 If the script is stopped and later resumed, you can use the --begin option with
46 the last printed progress indicator to pick up where you left off. This is
47 safe, because any newly-added categories before this cutoff will have been
48 added after the software update and so will be populated anyway.
49
50 When the script has finished, it will make a note of this in the database, and
51 will not run again without the --force option.
52 TEXT;
53 # '
54 $this->addOption(
55 'begin',
56 'Only do categories whose names are alphabetically after the provided name',
57 false,
58 true
59 );
60 $this->addOption(
61 'max-slave-lag',
62 'If slave lag exceeds this many seconds, wait until it drops before continuing. Default: 10',
63 false,
64 true
65 );
66 $this->addOption(
67 'throttle',
68 'Wait this many milliseconds after each category. Default: 0',
69 false,
70 true
71 );
72 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
73 }
74
75 public function execute() {
76 $begin = $this->getOption( 'begin', '' );
77 $maxSlaveLag = $this->getOption( 'max-slave-lag', 10 );
78 $throttle = $this->getOption( 'throttle', 0 );
79 $force = $this->getOption( 'force', false );
80 $this->doPopulateCategory( $begin, $maxSlaveLag, $throttle, $force );
81 }
82
83 private function doPopulateCategory( $begin, $maxlag, $throttle, $force ) {
84 $dbw = wfGetDB( DB_MASTER );
85
86 if ( !$force ) {
87 $row = $dbw->selectRow(
88 'updatelog',
89 '1',
90 array( 'ul_key' => 'populate category' ),
91 __METHOD__
92 );
93 if ( $row ) {
94 $this->output( "Category table already populated. Use php " .
95 "maintenance/populateCategory.php\n--force from the command line " .
96 "to override.\n" );
97
98 return true;
99 }
100 }
101
102 $throttle = intval( $throttle );
103 if ( $begin !== '' ) {
104 $where = 'cl_to > ' . $dbw->addQuotes( $begin );
105 } else {
106 $where = null;
107 }
108 $i = 0;
109
110 while ( true ) {
111 # Find which category to update
112 $row = $dbw->selectRow(
113 'categorylinks',
114 'cl_to',
115 $where,
116 __METHOD__,
117 array(
118 'ORDER BY' => 'cl_to'
119 )
120 );
121 if ( !$row ) {
122 # Done, hopefully.
123 break;
124 }
125 $name = $row->cl_to;
126 $where = 'cl_to > ' . $dbw->addQuotes( $name );
127
128 # Use the row to update the category count
129 $cat = Category::newFromName( $name );
130 if ( !is_object( $cat ) ) {
131 $this->output( "The category named $name is not valid?!\n" );
132 } else {
133 $cat->refreshCounts();
134 }
135
136 ++$i;
137 if ( !( $i % self::REPORTING_INTERVAL ) ) {
138 $this->output( "$name\n" );
139 wfWaitForSlaves();
140 }
141 usleep( $throttle * 1000 );
142 }
143
144 if ( $dbw->insert(
145 'updatelog',
146 array( 'ul_key' => 'populate category' ),
147 __METHOD__,
148 'IGNORE'
149 ) ) {
150 $this->output( "Category population complete.\n" );
151
152 return true;
153 } else {
154 $this->output( "Could not insert category population row.\n" );
155
156 return false;
157 }
158 }
159 }
160
161 $maintClass = "PopulateCategory";
162 require_once RUN_MAINTENANCE_IF_MAIN;