phpcs: More require/include is not a function
[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->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 );
41 }
42
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();
50 }
51 $this->output( "Done!\n" );
52 }
53
54 /** Purge URL coming from stdin */
55 private function doPurge() {
56 $stdin = $this->getStdin();
57 $urls = array();
58
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();
71 }
72 } else {
73 $this->output( "(Invalid title '$page')\n" );
74 }
75 }
76 }
77 $this->output( "Purging " . count( $urls ) . " urls\n" );
78 $this->sendPurgeRequest( $urls );
79 }
80
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 );
89 }
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'
98
99 )
100 );
101 if ( !$res->numRows() ) {
102 break;
103 }
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;
110 }
111 $this->sendPurgeRequest( $urls );
112 }
113 }
114
115 /**
116 * Helper to purge an array of $urls
117 * @param $urls array List of URLS to purge from squids
118 */
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" );
125 }
126 $u = new SquidUpdate( array( $url ) );
127 $u->doUpdate();
128 usleep( $delay * 1e6 );
129 }
130 } else {
131 if ( $this->hasOption( 'verbose' ) ) {
132 $this->output( implode( "\n", $urls ) . "\n" );
133 }
134 $u = new SquidUpdate( $urls );
135 $u->doUpdate();
136 }
137 }
138
139 }
140
141 $maintClass = "PurgeList";
142 require_once RUN_MAINTENANCE_IF_MAIN;