MediaWiki REL1_34
initImageData.php
Go to the documentation of this file.
1<?php
2
3$IP = getenv( 'MW_INSTALL_PATH' );
4if ( $IP === false ) {
5 $IP = __DIR__ . '/../../..';
6}
7require_once "$IP/maintenance/Maintenance.php";
8
10
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 ) {
46 $queue = JobQueueGroup::singleton();
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
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;
122require_once RUN_MAINTENANCE_IF_MAIN;
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
const RUN_MAINTENANCE_IF_MAIN
execute()
Do the actual work of filling out page images.
waitForMaxPressure(JobQueueGroup $queue, $maxPressure, $isQuiet)
__construct()
Default constructor.
Class to handle enqueueing of background jobs.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option exists.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Set the batch size.
$maintClass
const DB_REPLICA
Definition defines.php:25
if(count( $args)< 1) $job