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
21 * @ingroup Maintenance
24 require_once( __DIR__
. '/Maintenance.php' );
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->setBatchSize( 100 );
40 public function execute() {
41 if( $this->hasOption( 'namespace' ) ) {
42 $this->purgeNamespace();
46 $this->output( "Done!\n" );
49 /** Purge URL coming from stdin */
50 private function purgeList() {
51 $stdin = $this->getStdin();
54 while ( !feof( $stdin ) ) {
55 $page = trim( fgets( $stdin ) );
56 if ( preg_match( '%^https?://%', $page ) ) {
58 } elseif ( $page !== '' ) {
59 $title = Title
::newFromText( $page );
61 $url = $title->getInternalUrl();
62 $this->output( "$url\n" );
64 if ( $this->getOption( 'purge' ) ) {
65 $title->invalidateCache();
68 $this->output( "(Invalid title '$page')\n" );
72 $this->sendPurgeRequest( $urls );
75 /** Purge a namespace given by --namespace */
76 private function purgeNamespace() {
77 $dbr = wfGetDB( DB_SLAVE
);
78 $ns = $dbr->addQuotes( $this->getOption( 'namespace') );
80 $result = $dbr->select(
82 array( 'page_namespace', 'page_title' ),
83 array( "page_namespace = $ns" ),
85 array( 'ORDER BY' => 'page_id' )
89 $end = $dbr->numRows( $result );
90 $this->output( "Will purge $end pages from namespace $ns\n" );
93 $end +
= $this->mBatchSize
- 1;
95 $blockEnd = $start +
$this->mBatchSize
- 1;
97 while( $blockEnd <= $end ) {
98 # Select pages we will purge:
99 $result = $dbr->select(
101 array( 'page_namespace', 'page_title' ),
102 array( "page_namespace = $ns" ),
105 'ORDER BY' => 'page_id',
106 'LIMIT' => $this->mBatchSize
,
107 'OFFSET' => $blockStart,
110 # Initialize/reset URLs to be purged
112 foreach( $result as $row ) {
113 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
114 $url = $title->getInternalUrl();
118 $this->sendPurgeRequest( $urls );
120 $blockStart +
= $this->mBatchSize
;
121 $blockEnd +
= $this->mBatchSize
;
126 * Helper to purge an array of $urls
127 * @param $urls array List of URLS to purge from squids
129 private function sendPurgeRequest( $urls ) {
130 $this->output( "Purging " . count( $urls ). " urls\n" );
131 $u = new SquidUpdate( $urls );
137 $maintClass = "PurgeList";
138 require_once( RUN_MAINTENANCE_IF_MAIN
);