Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[lhc/web/wiklou.git] / maintenance / populateCategory.php
1 <?php
2 /**
3 * Script to populate 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( dirname( __FILE__ ) . '/Maintenance.php' );
26
27
28 class PopulateCategory extends Maintenance {
29
30 const REPORTING_INTERVAL = 1000;
31
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = <<<TEXT
35 This script will populate the category table, added in MediaWiki 1.13. It will
36 print out progress indicators every 1000 categories it adds to the table. The
37 script is perfectly safe to run on large, live wikis, and running it multiple
38 times is harmless. You may want to use the throttling options if it's causing
39 too much load; they will not affect correctness.
40
41 If the script is stopped and later resumed, you can use the --begin option with
42 the last printed progress indicator to pick up where you left off. This is
43 safe, because any newly-added categories before this cutoff will have been
44 added after the software update and so will be populated anyway.
45
46 When the script has finished, it will make a note of this in the database, and
47 will not run again without the --force option.
48 TEXT;
49 # '
50 $this->addOption( 'begin', 'Only do categories whose names are alphabetically after the provided name', false, true );
51 $this->addOption( 'max-slave-lag', 'If slave lag exceeds this many seconds, wait until it drops before continuing. Default: 10', false, true );
52 $this->addOption( 'throttle', 'Wait this many milliseconds after each category. Default: 0', false, true );
53 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
54 }
55
56 public function execute() {
57 $begin = $this->getOption( 'begin', '' );
58 $maxSlaveLag = $this->getOption( 'max-slave-lag', 10 );
59 $throttle = $this->getOption( 'throttle', 0 );
60 $force = $this->getOption( 'force', false );
61 $this->doPopulateCategory( $begin, $maxSlaveLag, $throttle, $force );
62 }
63
64 private function doPopulateCategory( $begin, $maxlag, $throttle, $force ) {
65 $dbw = wfGetDB( DB_MASTER );
66
67 if ( !$force ) {
68 $row = $dbw->selectRow(
69 'updatelog',
70 '1',
71 array( 'ul_key' => 'populate category' ),
72 __METHOD__
73 );
74 if ( $row ) {
75 $this->output( "Category table already populated. Use php " .
76 "maintenance/populateCategory.php\n--force from the command line " .
77 "to override.\n" );
78 return true;
79 }
80 }
81
82 $throttle = intval( $throttle );
83 if ( $begin !== '' ) {
84 $where = 'cl_to > ' . $dbw->addQuotes( $begin );
85 } else {
86 $where = null;
87 }
88 $i = 0;
89
90 while ( true ) {
91 # Find which category to update
92 $row = $dbw->selectRow(
93 'categorylinks',
94 'cl_to',
95 $where,
96 __METHOD__,
97 array(
98 'ORDER BY' => 'cl_to'
99 )
100 );
101 if ( !$row ) {
102 # Done, hopefully.
103 break;
104 }
105 $name = $row->cl_to;
106 $where = 'cl_to > ' . $dbw->addQuotes( $name );
107
108 # Use the row to update the category count
109 $cat = Category::newFromName( $name );
110 if ( !is_object( $cat ) ) {
111 $this->output( "The category named $name is not valid?!\n" );
112 } else {
113 $cat->refreshCounts();
114 }
115
116 ++$i;
117 if ( !( $i % self::REPORTING_INTERVAL ) ) {
118 $this->output( "$name\n" );
119 wfWaitForSlaves();
120 }
121 usleep( $throttle * 1000 );
122 }
123
124 if ( $dbw->insert(
125 'updatelog',
126 array( 'ul_key' => 'populate category' ),
127 __METHOD__,
128 'IGNORE'
129 )
130 ) {
131 $this->output( "Category population complete.\n" );
132 return true;
133 } else {
134 $this->output( "Could not insert category population row.\n" );
135 return false;
136 }
137 }
138 }
139
140 $maintClass = "PopulateCategory";
141 require_once( RUN_MAINTENANCE_IF_MAIN );