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