(bug 37179) Allow undelete revision rows l10n
[lhc/web/wiklou.git] / maintenance / purgeList.php
1 <?php
2 /**
3 * Send purge requests for listed pages to squid
4 *
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.
9 *
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.
14 *
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
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class PurgeList extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Send purge requests for listed pages to squid";
29 $this->addOption( 'purge', 'Whether to update page_touched.' , false, false );
30 $this->addOption( 'namespace', 'Namespace number', false, true );
31 $this->setBatchSize( 100 );
32 }
33
34 public function execute() {
35 if( $this->hasOption( 'namespace' ) ) {
36 $this->purgeNamespace();
37 } else {
38 $this->purgeList();
39 }
40 $this->output( "Done!\n" );
41 }
42
43 /** Purge URL coming from stdin */
44 private function purgeList() {
45 $stdin = $this->getStdin();
46 $urls = array();
47
48 while ( !feof( $stdin ) ) {
49 $page = trim( fgets( $stdin ) );
50 if ( preg_match( '%^https?://%', $page ) ) {
51 $urls[] = $page;
52 } elseif ( $page !== '' ) {
53 $title = Title::newFromText( $page );
54 if ( $title ) {
55 $url = $title->getInternalUrl();
56 $this->output( "$url\n" );
57 $urls[] = $url;
58 if ( $this->getOption( 'purge' ) ) {
59 $title->invalidateCache();
60 }
61 } else {
62 $this->output( "(Invalid title '$page')\n" );
63 }
64 }
65 }
66 $this->sendPurgeRequest( $urls );
67 }
68
69 /** Purge a namespace given by --namespace */
70 private function purgeNamespace() {
71 $dbr = wfGetDB( DB_SLAVE );
72 $ns = $dbr->addQuotes( $this->getOption( 'namespace') );
73
74 $result = $dbr->select(
75 array( 'page' ),
76 array( 'page_namespace', 'page_title' ),
77 array( "page_namespace = $ns" ),
78 __METHOD__,
79 array( 'ORDER BY' => 'page_id' )
80 );
81
82 $start = 0;
83 $end = $dbr->numRows( $result );
84 $this->output( "Will purge $end pages from namespace $ns\n" );
85
86 # Do remaining chunk
87 $end += $this->mBatchSize - 1;
88 $blockStart = $start;
89 $blockEnd = $start + $this->mBatchSize - 1;
90
91 while( $blockEnd <= $end ) {
92 # Select pages we will purge:
93 $result = $dbr->select(
94 array( 'page' ),
95 array( 'page_namespace', 'page_title' ),
96 array( "page_namespace = $ns" ),
97 __METHOD__,
98 array( # conditions
99 'ORDER BY' => 'page_id',
100 'LIMIT' => $this->mBatchSize,
101 'OFFSET' => $blockStart,
102 )
103 );
104 # Initialize/reset URLs to be purged
105 $urls = array();
106 foreach( $result as $row ) {
107 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
108 $url = $title->getInternalUrl();
109 $urls[] = $url;
110 }
111
112 $this->sendPurgeRequest( $urls );
113
114 $blockStart += $this->mBatchSize;
115 $blockEnd += $this->mBatchSize;
116 }
117 }
118
119 /**
120 * Helper to purge an array of $urls
121 * @param $urls array List of URLS to purge from squids
122 */
123 private function sendPurgeRequest( $urls ) {
124 $this->output( "Purging " . count( $urls ). " urls\n" );
125 $u = new SquidUpdate( $urls );
126 $u->doUpdate();
127 }
128
129 }
130
131 $maintClass = "PurgeList";
132 require_once( RUN_MAINTENANCE_IF_MAIN );