3 * Populate the category table.
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.
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.
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
21 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script to populate the category table.
30 * @ingroup Maintenance
32 class PopulateCategory
extends Maintenance
{
34 const REPORTING_INTERVAL
= 1000;
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription(
40 This script will populate the category table, added in MediaWiki 1.13. It will
41 print out progress indicators every 1000 categories it adds to the table. The
42 script is perfectly safe to run on large, live wikis, and running it multiple
43 times is harmless. You may want to use the throttling options if it's causing
44 too much load; they will not affect correctness.
46 If the script is stopped and later resumed, you can use the --begin option with
47 the last printed progress indicator to pick up where you left off. This is
48 safe, because any newly-added categories before this cutoff will have been
49 added after the software update and so will be populated anyway.
51 When the script has finished, it will make a note of this in the database, and
52 will not run again without the --force option.
58 'Only do categories whose names are alphabetically after the provided name',
64 'Wait this many milliseconds after each category. Default: 0',
68 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
71 public function execute() {
72 $begin = $this->getOption( 'begin', '' );
73 $throttle = $this->getOption( 'throttle', 0 );
74 $force = $this->getOption( 'force', false );
76 $dbw = $this->getDB( DB_MASTER
);
79 $row = $dbw->selectRow(
82 [ 'ul_key' => 'populate category' ],
86 $this->output( "Category table already populated. Use php " .
87 "maintenance/populateCategory.php\n--force from the command line " .
94 $throttle = intval( $throttle );
95 if ( $begin !== '' ) {
96 $where = 'cl_to > ' . $dbw->addQuotes( $begin );
103 # Find which category to update
104 $row = $dbw->selectRow(
110 'ORDER BY' => 'cl_to'
118 $where = 'cl_to > ' . $dbw->addQuotes( $name );
120 # Use the row to update the category count
121 $cat = Category
::newFromName( $name );
122 if ( !is_object( $cat ) ) {
123 $this->output( "The category named $name is not valid?!\n" );
125 $cat->refreshCounts();
129 if ( !( $i % self
::REPORTING_INTERVAL
) ) {
130 $this->output( "$name\n" );
133 usleep( $throttle * 1000 );
138 [ 'ul_key' => 'populate category' ],
142 $this->output( "Category population complete.\n" );
146 $this->output( "Could not insert category population row.\n" );
153 $maintClass = "PopulateCategory";
154 require_once RUN_MAINTENANCE_IF_MAIN
;