Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / purgeList.php
blob2f89520189cd54543a59c29cc0152081109444cc
1 <?php
2 /**
3 * Send purge requests for listed pages to squid
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that sends purge requests for listed pages to squid.
29 * @ingroup Maintenance
31 class PurgeList extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "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 );
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();
51 $this->output( "Done!\n" );
54 /** Purge URL coming from stdin */
55 private function doPurge() {
56 $stdin = $this->getStdin();
57 $urls = array();
59 while ( !feof( $stdin ) ) {
60 $page = trim( fgets( $stdin ) );
61 if ( preg_match( '%^https?://%', $page ) ) {
62 $urls[] = $page;
63 } elseif ( $page !== '' ) {
64 $title = Title::newFromText( $page );
65 if ( $title ) {
66 $url = $title->getInternalURL();
67 $this->output( "$url\n" );
68 $urls[] = $url;
69 if ( $this->getOption( 'purge' ) ) {
70 $title->invalidateCache();
72 } else {
73 $this->output( "(Invalid title '$page')\n" );
77 $this->output( "Purging " . count( $urls ) . " urls\n" );
78 $this->sendPurgeRequest( $urls );
81 /** Purge a namespace or all pages */
82 private function purgeNamespace( $namespace = false ) {
83 $dbr = wfGetDB( DB_SLAVE );
84 $startId = 0;
85 if ( $namespace === false ) {
86 $conds = array();
87 } else {
88 $conds = array( 'page_namespace' => $namespace );
90 while ( true ) {
91 $res = $dbr->select( 'page',
92 array( 'page_id', 'page_namespace', 'page_title' ),
93 $conds + array( 'page_id > ' . $dbr->addQuotes( $startId ) ),
94 __METHOD__,
95 array(
96 'LIMIT' => $this->mBatchSize,
97 'ORDER BY' => 'page_id'
101 if ( !$res->numRows() ) {
102 break;
104 $urls = array();
105 foreach ( $res as $row ) {
106 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
107 $url = $title->getInternalURL();
108 $urls[] = $url;
109 $startId = $row->page_id;
111 $this->sendPurgeRequest( $urls );
116 * Helper to purge an array of $urls
117 * @param $urls array List of URLS to purge from squids
119 private function sendPurgeRequest( $urls ) {
120 if ( $this->hasOption( 'delay' ) ) {
121 $delay = floatval( $this->getOption( 'delay' ) );
122 foreach ( $urls as $url ) {
123 if ( $this->hasOption( 'verbose' ) ) {
124 $this->output( $url . "\n" );
126 $u = new SquidUpdate( array( $url ) );
127 $u->doUpdate();
128 usleep( $delay * 1e6 );
130 } else {
131 if ( $this->hasOption( 'verbose' ) ) {
132 $this->output( implode( "\n", $urls ) . "\n" );
134 $u = new SquidUpdate( $urls );
135 $u->doUpdate();
141 $maintClass = "PurgeList";
142 require_once RUN_MAINTENANCE_IF_MAIN;