Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / populateCategory.php
blob4c8cdaa18a59c011ed9279f37019d7b58f98466c
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
22 * @author Simetrical
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Mainteance 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->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.
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.
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( 'begin', 'Only do categories whose names are alphabetically after the provided name', false, true );
55 $this->addOption( 'max-slave-lag', 'If slave lag exceeds this many seconds, wait until it drops before continuing. Default: 10', false, true );
56 $this->addOption( 'throttle', 'Wait this many milliseconds after each category. Default: 0', false, true );
57 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
60 public function execute() {
61 $begin = $this->getOption( 'begin', '' );
62 $maxSlaveLag = $this->getOption( 'max-slave-lag', 10 );
63 $throttle = $this->getOption( 'throttle', 0 );
64 $force = $this->getOption( 'force', false );
65 $this->doPopulateCategory( $begin, $maxSlaveLag, $throttle, $force );
68 private function doPopulateCategory( $begin, $maxlag, $throttle, $force ) {
69 $dbw = wfGetDB( DB_MASTER );
71 if ( !$force ) {
72 $row = $dbw->selectRow(
73 'updatelog',
74 '1',
75 array( 'ul_key' => 'populate category' ),
76 __METHOD__
78 if ( $row ) {
79 $this->output( "Category table already populated. Use php " .
80 "maintenance/populateCategory.php\n--force from the command line " .
81 "to override.\n" );
82 return true;
86 $throttle = intval( $throttle );
87 if ( $begin !== '' ) {
88 $where = 'cl_to > ' . $dbw->addQuotes( $begin );
89 } else {
90 $where = null;
92 $i = 0;
94 while ( true ) {
95 # Find which category to update
96 $row = $dbw->selectRow(
97 'categorylinks',
98 'cl_to',
99 $where,
100 __METHOD__,
101 array(
102 'ORDER BY' => 'cl_to'
105 if ( !$row ) {
106 # Done, hopefully.
107 break;
109 $name = $row->cl_to;
110 $where = 'cl_to > ' . $dbw->addQuotes( $name );
112 # Use the row to update the category count
113 $cat = Category::newFromName( $name );
114 if ( !is_object( $cat ) ) {
115 $this->output( "The category named $name is not valid?!\n" );
116 } else {
117 $cat->refreshCounts();
120 ++$i;
121 if ( !( $i % self::REPORTING_INTERVAL ) ) {
122 $this->output( "$name\n" );
123 wfWaitForSlaves();
125 usleep( $throttle * 1000 );
128 if ( $dbw->insert(
129 'updatelog',
130 array( 'ul_key' => 'populate category' ),
131 __METHOD__,
132 'IGNORE'
135 $this->output( "Category population complete.\n" );
136 return true;
137 } else {
138 $this->output( "Could not insert category population row.\n" );
139 return false;
144 $maintClass = "PopulateCategory";
145 require_once RUN_MAINTENANCE_IF_MAIN;