MediaWiki  master
populateCategory.php
Go to the documentation of this file.
1 <?php
25 require_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
42 This script will populate the category table, added in MediaWiki 1.13. It will
43 print out progress indicators every 1000 categories it adds to the table. The
44 script is perfectly safe to run on large, live wikis, and running it multiple
45 times is harmless. You may want to use the throttling options if it's causing
46 too much load; they will not affect correctness.
47 
48 If the script is stopped and later resumed, you can use the --begin option with
49 the last printed progress indicator to pick up where you left off. This is
50 safe, because any newly-added categories before this cutoff will have been
51 added after the software update and so will be populated anyway.
52 
53 When the script has finished, it will make a note of this in the database, and
54 will not run again without the --force option.
55 TEXT
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;
152 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
PopulateCategory
Maintenance script to populate the category table.
Definition: populateCategory.php:34
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
REPORTING_INTERVAL
const REPORTING_INTERVAL
Definition: moveToExternal.php:26
MediaWiki
A helper class for throttling authentication attempts.
PopulateCategory\REPORTING_INTERVAL
const REPORTING_INTERVAL
Definition: populateCategory.php:36
$maintClass
$maintClass
Definition: populateCategory.php:151
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateCategory\execute
execute()
Do the actual work.
Definition: populateCategory.php:73
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
PopulateCategory\__construct
__construct()
Default constructor.
Definition: populateCategory.php:38
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Category\newFromName
static newFromName( $name)
Factory function.
Definition: Category.php:133
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266