MediaWiki  1.34.0
populateCategory.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
33 
34  const REPORTING_INTERVAL = 1000;
35 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription(
39  <<<TEXT
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.
45 
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.
50 
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.
53 TEXT
54  );
55 
56  $this->addOption(
57  'begin',
58  'Only do categories whose names are alphabetically after the provided name',
59  false,
60  true
61  );
62  $this->addOption(
63  'throttle',
64  'Wait this many milliseconds after each category. Default: 0',
65  false,
66  true
67  );
68  $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
69  }
70 
71  public function execute() {
72  $begin = $this->getOption( 'begin', '' );
73  $throttle = $this->getOption( 'throttle', 0 );
74  $force = $this->hasOption( 'force' );
75 
76  $dbw = $this->getDB( DB_MASTER );
77 
78  if ( !$force ) {
79  $row = $dbw->selectRow(
80  'updatelog',
81  '1',
82  [ 'ul_key' => 'populate category' ],
83  __METHOD__
84  );
85  if ( $row ) {
86  $this->output( "Category table already populated. Use php " .
87  "maintenance/populateCategory.php\n--force from the command line " .
88  "to override.\n" );
89 
90  return true;
91  }
92  }
93 
94  $throttle = intval( $throttle );
95  if ( $begin !== '' ) {
96  $where = [ 'cl_to > ' . $dbw->addQuotes( $begin ) ];
97  } else {
98  $where = [ '1 = 1' ];
99  }
100  $i = 0;
101 
102  while ( true ) {
103  # Find which category to update
104  $row = $dbw->selectRow(
105  'categorylinks',
106  'cl_to',
107  $where,
108  __METHOD__,
109  [
110  'ORDER BY' => 'cl_to'
111  ]
112  );
113  if ( !$row ) {
114  # Done, hopefully.
115  break;
116  }
117  $name = $row->cl_to;
118  $where = 'cl_to > ' . $dbw->addQuotes( $name );
119 
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" );
124  } else {
125  $cat->refreshCounts();
126  }
127 
128  ++$i;
129  if ( !( $i % self::REPORTING_INTERVAL ) ) {
130  $this->output( "$name\n" );
131  wfWaitForSlaves();
132  }
133  usleep( $throttle * 1000 );
134  }
135 
136  $dbw->insert(
137  'updatelog',
138  [ 'ul_key' => 'populate category' ],
139  __METHOD__,
140  [ 'IGNORE' ]
141  );
142 
143  return true;
144  }
145 }
146 
147 $maintClass = PopulateCategory::class;
148 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
PopulateCategory
Maintenance script to populate the category table.
Definition: populateCategory.php:32
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
REPORTING_INTERVAL
const REPORTING_INTERVAL
Definition: moveToExternal.php:26
MediaWiki
This class serves as a utility class for this extension.
PopulateCategory\REPORTING_INTERVAL
const REPORTING_INTERVAL
Definition: populateCategory.php:34
$maintClass
$maintClass
Definition: populateCategory.php:133
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateCategory\execute
execute()
Do the actual work.
Definition: populateCategory.php:71
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
PopulateCategory\__construct
__construct()
Default constructor.
Definition: populateCategory.php:36
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Category\newFromName
static newFromName( $name)
Factory function.
Definition: Category.php:126
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288