MediaWiki  1.30.0
purgeList.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class PurgeList extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Send purge requests for listed pages to squid' );
35  $this->addOption( 'purge', 'Whether to update page_touched.', false, false );
36  $this->addOption( 'namespace', 'Namespace number', false, true );
37  $this->addOption( 'all', 'Purge all pages', false, false );
38  $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
39  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
40  $this->setBatchSize( 100 );
41  }
42 
43  public function execute() {
44  if ( $this->hasOption( 'all' ) ) {
45  $this->purgeNamespace( false );
46  } elseif ( $this->hasOption( 'namespace' ) ) {
47  $this->purgeNamespace( intval( $this->getOption( 'namespace' ) ) );
48  } else {
49  $this->doPurge();
50  }
51  $this->output( "Done!\n" );
52  }
53 
57  private function doPurge() {
58  $stdin = $this->getStdin();
59  $urls = [];
60 
61  while ( !feof( $stdin ) ) {
62  $page = trim( fgets( $stdin ) );
63  if ( preg_match( '%^https?://%', $page ) ) {
64  $urls[] = $page;
65  } elseif ( $page !== '' ) {
66  $title = Title::newFromText( $page );
67  if ( $title ) {
68  $url = $title->getInternalURL();
69  $this->output( "$url\n" );
70  $urls[] = $url;
71  if ( $this->getOption( 'purge' ) ) {
72  $title->invalidateCache();
73  }
74  } else {
75  $this->output( "(Invalid title '$page')\n" );
76  }
77  }
78  }
79  $this->output( "Purging " . count( $urls ) . " urls\n" );
80  $this->sendPurgeRequest( $urls );
81  }
82 
88  private function purgeNamespace( $namespace = false ) {
89  $dbr = $this->getDB( DB_REPLICA );
90  $startId = 0;
91  if ( $namespace === false ) {
92  $conds = [];
93  } else {
94  $conds = [ 'page_namespace' => $namespace ];
95  }
96  while ( true ) {
97  $res = $dbr->select( 'page',
98  [ 'page_id', 'page_namespace', 'page_title' ],
99  $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
100  __METHOD__,
101  [
102  'LIMIT' => $this->mBatchSize,
103  'ORDER BY' => 'page_id'
104 
105  ]
106  );
107  if ( !$res->numRows() ) {
108  break;
109  }
110  $urls = [];
111  foreach ( $res as $row ) {
112  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
113  $url = $title->getInternalURL();
114  $urls[] = $url;
115  $startId = $row->page_id;
116  }
117  $this->sendPurgeRequest( $urls );
118  }
119  }
120 
125  private function sendPurgeRequest( $urls ) {
126  if ( $this->hasOption( 'delay' ) ) {
127  $delay = floatval( $this->getOption( 'delay' ) );
128  foreach ( $urls as $url ) {
129  if ( $this->hasOption( 'verbose' ) ) {
130  $this->output( $url . "\n" );
131  }
132  $u = new CdnCacheUpdate( [ $url ] );
133  $u->doUpdate();
134  usleep( $delay * 1e6 );
135  }
136  } else {
137  if ( $this->hasOption( 'verbose' ) ) {
138  $this->output( implode( "\n", $urls ) . "\n" );
139  }
140  $u = new CdnCacheUpdate( $urls );
141  $u->doUpdate();
142  }
143  }
144 }
145 
146 $maintClass = "PurgeList";
147 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:346
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: purgeList.php:146
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
PurgeList
Maintenance script that sends purge requests for listed pages to squid.
Definition: purgeList.php:31
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
PurgeList\execute
execute()
Do the actual work.
Definition: purgeList.php:43
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
PurgeList\doPurge
doPurge()
Purge URL coming from stdin.
Definition: purgeList.php:57
PurgeList\__construct
__construct()
Default constructor.
Definition: purgeList.php:32
CdnCacheUpdate
Handles purging appropriate CDN URLs given a title (or titles)
Definition: CdnCacheUpdate.php:31
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
PurgeList\purgeNamespace
purgeNamespace( $namespace=false)
Purge a namespace or all pages.
Definition: purgeList.php:88
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
PurgeList\sendPurgeRequest
sendPurgeRequest( $urls)
Helper to purge an array of $urls.
Definition: purgeList.php:125
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314