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