MediaWiki REL1_35
populateCategory.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
28
35
36 private const REPORTING_INTERVAL = 1000;
37
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription(
41 <<<TEXT
42This script will populate the category table, added in MediaWiki 1.13. It will
43print out progress indicators every 1000 categories it adds to the table. The
44script is perfectly safe to run on large, live wikis, and running it multiple
45times is harmless. You may want to use the throttling options if it's causing
46too much load; they will not affect correctness.
47
48If the script is stopped and later resumed, you can use the --begin option with
49the last printed progress indicator to pick up where you left off. This is
50safe, because any newly-added categories before this cutoff will have been
51added after the software update and so will be populated anyway.
52
53When the script has finished, it will make a note of this in the database, and
54will not run again without the --force option.
55TEXT
56 );
57
58 $this->addOption(
59 'begin',
60 'Only do categories whose names are alphabetically after the provided name',
61 false,
62 true
63 );
64 $this->addOption(
65 'throttle',
66 'Wait this many milliseconds after each category. Default: 0',
67 false,
68 true
69 );
70 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
71 }
72
73 public function execute() {
74 $begin = $this->getOption( 'begin', '' );
75 $throttle = $this->getOption( 'throttle', 0 );
76 $force = $this->hasOption( 'force' );
77
78 $dbw = $this->getDB( DB_MASTER );
79
80 if ( !$force ) {
81 $row = $dbw->selectRow(
82 'updatelog',
83 '1',
84 [ 'ul_key' => 'populate category' ],
85 __METHOD__
86 );
87 if ( $row ) {
88 $this->output( "Category table already populated. Use php " .
89 "maintenance/populateCategory.php\n--force from the command line " .
90 "to override.\n" );
91
92 return true;
93 }
94 }
95
96 $throttle = intval( $throttle );
97 if ( $begin !== '' ) {
98 $where = [ 'cl_to > ' . $dbw->addQuotes( $begin ) ];
99 } else {
100 $where = [ '1 = 1' ];
101 }
102 $i = 0;
103
104 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
105
106 while ( true ) {
107 # Find which category to update
108 $row = $dbw->selectRow(
109 'categorylinks',
110 'cl_to',
111 $where,
112 __METHOD__,
113 [
114 'ORDER BY' => 'cl_to'
115 ]
116 );
117 if ( !$row ) {
118 # Done, hopefully.
119 break;
120 }
121 $name = $row->cl_to;
122 $where = 'cl_to > ' . $dbw->addQuotes( $name );
123
124 # Use the row to update the category count
125 $cat = Category::newFromName( $name );
126 if ( !is_object( $cat ) ) {
127 $this->output( "The category named $name is not valid?!\n" );
128 } else {
129 $cat->refreshCounts();
130 }
131
132 ++$i;
133 if ( !( $i % self::REPORTING_INTERVAL ) ) {
134 $this->output( "$name\n" );
135 $lbFactory->waitForReplication();
136 }
137 usleep( $throttle * 1000 );
138 }
139
140 $dbw->insert(
141 'updatelog',
142 [ 'ul_key' => 'populate category' ],
143 __METHOD__,
144 [ 'IGNORE' ]
145 );
146
147 return true;
148 }
149}
150
151$maintClass = PopulateCategory::class;
152require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
getOption( $name, $default=null)
Get an option, or return the default.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script to populate the category table.
execute()
Do the actual work.
__construct()
Default constructor.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
A helper class for throttling authentication attempts.
const DB_MASTER
Definition defines.php:29