MediaWiki  1.34.0
initImageData.php
Go to the documentation of this file.
1 <?php
2 
3 $IP = getenv( 'MW_INSTALL_PATH' );
4 if ( $IP === false ) {
5  $IP = __DIR__ . '/../../..';
6 }
7 require_once "$IP/maintenance/Maintenance.php";
8 
10 
15 class InitImageData extends Maintenance {
16  public function __construct() {
17  parent::__construct();
18  $this->addDescription( 'Initializes PageImages data' );
19  $this->addOption( 'namespaces',
20  'Comma-separated list of namespace(s) to refresh', false, true );
21  $this->addOption( 'earlier-than',
22  'Run only on pages touched earlier than this timestamp', false, true );
23  $this->addOption( 'later-than',
24  'Run only on pages touched later than this timestamp', false, true );
25  $this->addOption( 'start', 'Starting page ID', false, true );
26  $this->addOption( 'queue-pressure', 'Maximum number of jobs to enqueue at a time. ' .
27  'If not provided or 0 will be run in-process.', false, true );
28  $this->addOption( 'quiet', "Don't report on job queue pressure" );
29  $this->setBatchSize( 100 );
30 
31  $this->requireExtension( 'PageImages' );
32  }
33 
38  public function execute() {
39  global $wgPageImagesNamespaces;
40 
41  $lastId = $this->getOption( 'start', 0 );
42  $isQuiet = $this->getOption( 'quiet', false );
43  $queue = null;
44  $maxPressure = $this->getOption( 'queue-pressure', 0 );
45  if ( $maxPressure > 0 ) {
47  }
48 
49  do {
50  $tables = [ 'page', 'imagelinks' ];
51  $conds = [
52  'page_id > ' . (int)$lastId,
53  'il_from IS NOT NULL',
54  'page_is_redirect' => 0,
55  ];
56  $fields = [ 'page_id' ];
57  $joinConds = [ 'imagelinks' => [
58  'LEFT JOIN', 'page_id = il_from',
59  ] ];
60 
61  $dbr = wfGetDB( DB_REPLICA );
62  if ( $this->hasOption( 'namespaces' ) ) {
63  $ns = explode( ',', $this->getOption( 'namespaces' ) );
64  $conds['page_namespace'] = $ns;
65  } else {
66  $conds['page_namespace'] = $wgPageImagesNamespaces;
67  }
68  if ( $this->hasOption( 'earlier-than' ) ) {
69  $conds[] = 'page_touched < '
70  . $dbr->addQuotes( $this->getOption( 'earlier-than' ) );
71  }
72  if ( $this->hasOption( 'later-than' ) ) {
73  $conds[] = 'page_touched > '
74  . $dbr->addQuotes( $this->getOption( 'later-than' ) );
75  }
76  $res = $dbr->select( $tables, $fields, $conds, __METHOD__,
77  [ 'LIMIT' => $this->mBatchSize, 'ORDER_BY' => 'page_id', 'GROUP BY' => 'page_id' ],
78  $joinConds
79  );
80  $pageIds = [];
81  foreach ( $res as $row ) {
82  $pageIds[] = $row->page_id;
83  }
84  $job = new InitImageDataJob( Title::newMainPage(), [ 'page_ids' => $pageIds ] );
85  if ( $queue === null ) {
86  $job->run();
87  } else {
88  $queue->push( $job );
89  $this->waitForMaxPressure( $queue, $maxPressure, $isQuiet );
90  }
91  $lastId = end( $pageIds );
92  $this->output( "$lastId\n" );
93  } while ( $res->numRows() );
94  $this->output( "done\n" );
95  }
96 
103  private function waitForMaxPressure( JobQueueGroup $queue, $maxPressure, $isQuiet ) {
104  $group = $queue->get( 'InitImageDataJob' );
105  $i = 0;
106  do {
107  sleep( 1 );
108  $queued = $group->getSize();
109  $running = $group->getAcquiredCount();
110  $abandoned = $group->getAbandonedCount();
111 
112  if ( !$isQuiet && ++$i % 10 === 0 ) {
113  $now = date( 'Y-m-d H:i:s T' );
114  $this->output( "[$now] Queued: $queued Running: $running " .
115  "Abandoned: $abandoned Max: $maxPressure\n" );
116  }
117  } while ( $queued + $running - $abandoned >= $maxPressure );
118  }
119 }
120 
121 $maintClass = InitImageData::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$IP
$IP
Definition: initImageData.php:3
InitImageData\__construct
__construct()
Default constructor.
Definition: initImageData.php:16
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
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:649
$res
$res
Definition: testCompression.php:52
$dbr
$dbr
Definition: testCompression.php:50
PageImages\Job\InitImageDataJob
Definition: InitImageDataJob.php:11
$maintClass
$maintClass
Definition: initImageData.php:121
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
Maintenance\requireExtension
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
Definition: Maintenance.php:638
$queue
$queue
Definition: mergeMessageFileList.php:157
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
InitImageData\execute
execute()
Do the actual work of filling out page images.
Definition: initImageData.php:38
InitImageData\waitForMaxPressure
waitForMaxPressure(JobQueueGroup $queue, $maxPressure, $isQuiet)
Definition: initImageData.php:103
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
InitImageData
Definition: initImageData.php:15
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394
JobQueueGroup
Class to handle enqueueing of background jobs.
Definition: JobQueueGroup.php:30