PurgeList::doPurge()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 6
nop 0
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 View Code Duplication
	public function __construct() {
33
		parent::__construct();
34
		$this->addDescription( '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
	/**
55
	 * Purge URL coming from stdin
56
	 */
57
	private function doPurge() {
58
		$stdin = $this->getStdin();
59
		$urls = [];
60
61
		while ( !feof( $stdin ) ) {
62
			$page = trim( fgets( $stdin ) );
63
			if ( preg_match( '%^https?://%', $page ) ) {
64
				$urls[] = $page;
65
			} elseif ( $page !== '' ) {
66
				$title = Title::newFromText( $page );
67
				if ( $title ) {
68
					$url = $title->getInternalURL();
69
					$this->output( "$url\n" );
70
					$urls[] = $url;
71
					if ( $this->getOption( 'purge' ) ) {
72
						$title->invalidateCache();
73
					}
74
				} else {
75
					$this->output( "(Invalid title '$page')\n" );
76
				}
77
			}
78
		}
79
		$this->output( "Purging " . count( $urls ) . " urls\n" );
80
		$this->sendPurgeRequest( $urls );
81
	}
82
83
	/**
84
	 * Purge a namespace or all pages
85
	 *
86
	 * @param int|bool $namespace
87
	 */
88
	private function purgeNamespace( $namespace = false ) {
89
		$dbr = $this->getDB( DB_REPLICA );
90
		$startId = 0;
91
		if ( $namespace === false ) {
92
			$conds = [];
93
		} else {
94
			$conds = [ 'page_namespace' => $namespace ];
95
		}
96
		while ( true ) {
97
			$res = $dbr->select( 'page',
98
				[ 'page_id', 'page_namespace', 'page_title' ],
99
				$conds + [ 'page_id > ' . $dbr->addQuotes( $startId ) ],
100
				__METHOD__,
101
				[
102
					'LIMIT' => $this->mBatchSize,
103
					'ORDER BY' => 'page_id'
104
105
				]
106
			);
107
			if ( !$res->numRows() ) {
108
				break;
109
			}
110
			$urls = [];
111 View Code Duplication
			foreach ( $res as $row ) {
112
				$title = Title::makeTitle( $row->page_namespace, $row->page_title );
113
				$url = $title->getInternalURL();
114
				$urls[] = $url;
115
				$startId = $row->page_id;
116
			}
117
			$this->sendPurgeRequest( $urls );
118
		}
119
	}
120
121
	/**
122
	 * Helper to purge an array of $urls
123
	 * @param array $urls List of URLS to purge from squids
124
	 */
125
	private function sendPurgeRequest( $urls ) {
126
		if ( $this->hasOption( 'delay' ) ) {
127
			$delay = floatval( $this->getOption( 'delay' ) );
128
			foreach ( $urls as $url ) {
129
				if ( $this->hasOption( 'verbose' ) ) {
130
					$this->output( $url . "\n" );
131
				}
132
				$u = new CdnCacheUpdate( [ $url ] );
133
				$u->doUpdate();
134
				usleep( $delay * 1e6 );
135
			}
136
		} else {
137
			if ( $this->hasOption( 'verbose' ) ) {
138
				$this->output( implode( "\n", $urls ) . "\n" );
139
			}
140
			$u = new CdnCacheUpdate( $urls );
141
			$u->doUpdate();
142
		}
143
	}
144
}
145
146
$maintClass = "PurgeList";
147
require_once RUN_MAINTENANCE_IF_MAIN;
148