MediaWiki  master
purgeList.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class PurgeList extends Maintenance {
33  private $namespaceId;
35  private $allNamespaces;
37  private $doDbTouch;
39  private $delay;
40 
41  public function __construct() {
42  parent::__construct();
43  $this->addDescription( "Send purge requests for listed pages to CDN.\n"
44  . "By default this expects a list of URLs or page names from STDIN. "
45  . "To query the database for input, use --namespace or --all-namespaces instead."
46  );
47  $this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
48  $this->addOption( 'all-namespace', 'Purge pages in all namespaces', false, false );
49  $this->addOption( 'db-touch', 'Update the page.page_touched database field', false, false );
50  $this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
51  $this->addOption( 'verbose', 'Show more output', false, false, 'v' );
52  $this->setBatchSize( 100 );
53  }
54 
55  public function execute() {
56  $this->namespaceId = $this->getOption( 'namespace' );
57  $this->allNamespaces = $this->hasOption( 'all-namespaces' );
58  $this->doDbTouch = $this->hasOption( 'db-touch' );
59  $this->delay = floatval( $this->getOption( 'delay', '0' ) );
60 
61  $conf = $this->getConfig();
62  if ( ( $this->namespaceId !== null || $this->allNamespaces )
63  && $this->doDbTouch
64  && $conf->get( 'MiserMode' )
65  ) {
66  $this->fatalError( 'Prevented mass db-invalidation (MiserMode is enabled).' );
67  }
68 
69  if ( $this->allNamespaces ) {
70  $this->purgeNamespace( false );
71  } elseif ( $this->namespaceId !== null ) {
72  $this->purgeNamespace( intval( $this->namespaceId ) );
73  } else {
74  $this->doPurge();
75  }
76  $this->output( "Done!\n" );
77  }
78 
82  private function doPurge() {
83  $stdin = $this->getStdin();
84  $urls = [];
85 
86  while ( !feof( $stdin ) ) {
87  $page = trim( fgets( $stdin ) );
88  if ( preg_match( '%^https?://%', $page ) ) {
89  $urls[] = $page;
90  } elseif ( $page !== '' ) {
91  $title = Title::newFromText( $page );
92  if ( $title ) {
93  $newUrls = $title->getCdnUrls();
94 
95  foreach ( $newUrls as $url ) {
96  $this->output( "$url\n" );
97  }
98 
99  $urls = array_merge( $urls, $newUrls );
100 
101  if ( $this->doDbTouch ) {
102  $title->invalidateCache();
103  }
104  } else {
105  $this->output( "(Invalid title '$page')\n" );
106  }
107  }
108  }
109  $this->output( "Purging " . count( $urls ) . " urls\n" );
110  $this->sendPurgeRequest( $urls );
111  }
112 
118  private function purgeNamespace( $namespace = false ) {
119  $dbr = $this->getDB( DB_REPLICA );
120  $startId = 0;
121  if ( $namespace === false ) {
122  $conds = [];
123  } else {
124  $conds = [ 'page_namespace' => $namespace ];
125  }
126  while ( true ) {
127  $res = $dbr->select( 'page',
128  [ 'page_id', 'page_namespace', 'page_title' ],
129  $conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
130  __METHOD__,
131  [
132  'LIMIT' => $this->getBatchSize(),
133  'ORDER BY' => 'page_id'
134 
135  ]
136  );
137  if ( !$res->numRows() ) {
138  break;
139  }
140  $urls = [];
141  foreach ( $res as $row ) {
142  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
143  $urls = array_merge( $urls, $title->getCdnUrls() );
144  $startId = $row->page_id;
145  }
146  $this->sendPurgeRequest( $urls );
147  }
148  }
149 
154  private function sendPurgeRequest( $urls ) {
155  if ( $this->delay > 0 ) {
156  foreach ( $urls as $url ) {
157  if ( $this->hasOption( 'verbose' ) ) {
158  $this->output( $url . "\n" );
159  }
160  $u = new CdnCacheUpdate( [ $url ] );
161  $u->doUpdate();
162  usleep( $this->delay * 1e6 );
163  }
164  } else {
165  if ( $this->hasOption( 'verbose' ) ) {
166  $this->output( implode( "\n", $urls ) . "\n" );
167  }
168  $u = new CdnCacheUpdate( $urls );
169  $u->doUpdate();
170  }
171  }
172 }
173 
174 $maintClass = PurgeList::class;
175 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Definition: purgeList.php:174
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
purgeNamespace( $namespace=false)
Purge a namespace or all pages.
Definition: purgeList.php:118
getOption( $name, $default=null)
Get an option, or return the default.
sendPurgeRequest( $urls)
Helper to purge an array of $urls.
Definition: purgeList.php:154
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
Handles purging the appropriate CDN objects given a list of URLs or Title instances.
setBatchSize( $s=0)
Set the batch size.
hasOption( $name)
Checks to see if a particular option exists.
Maintenance script that sends purge requests for listed pages to CDN.
Definition: purgeList.php:31
bool $doDbTouch
Definition: purgeList.php:37
__construct()
Definition: purgeList.php:41
addDescription( $text)
Set the description text.
string null $namespaceId
Definition: purgeList.php:33
output( $out, $channel=null)
Throw some output to the user.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:584
getBatchSize()
Returns batch size.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
bool $allNamespaces
Definition: purgeList.php:35
getStdin( $len=null)
Return input from stdin.
const DB_REPLICA
Definition: defines.php:25
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
doPurge()
Purge URL coming from stdin.
Definition: purgeList.php:82
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
float $delay
Definition: purgeList.php:39
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317