| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Title\Title; |
| 25 |
|
| 26 |
|
| 27 |
require_once __DIR__ . '/Maintenance.php'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class PurgeList extends Maintenance { |
| 36 |
|
| 37 |
private $namespaceId; |
| 38 |
|
| 39 |
private $allNamespaces; |
| 40 |
|
| 41 |
private $doDbTouch; |
| 42 |
|
| 43 |
private $delay; |
| 44 |
|
| 45 |
public function __construct() { |
| 46 |
parent::__construct(); |
| 47 |
$this->addDescription( "Send purge requests for listed pages to CDN.\n" |
| 48 |
. "By default this expects a list of URLs or page names from STDIN. " |
| 49 |
. "To query the database for input, use --namespace or --all-namespaces instead." |
| 50 |
); |
| 51 |
$this->addOption( 'namespace', 'Purge pages with this namespace number', false, true ); |
| 52 |
$this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false ); |
| 53 |
$this->addOption( 'db-touch', |
| 54 |
"Update the page.page_touched database field.\n" |
| 55 |
. "This is only considered when purging by title, not when purging by namespace or URL.", |
| 56 |
false, |
| 57 |
false |
| 58 |
); |
| 59 |
$this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true ); |
| 60 |
$this->addOption( 'verbose', 'Show more output', false, false, 'v' ); |
| 61 |
$this->setBatchSize( 100 ); |
| 62 |
} |
| 63 |
|
| 64 |
public function execute() { |
| 65 |
$this->namespaceId = $this->getOption( 'namespace' ); |
| 66 |
$this->allNamespaces = $this->hasOption( 'all-namespaces' ); |
| 67 |
$this->doDbTouch = $this->hasOption( 'db-touch' ); |
| 68 |
$this->delay = intval( $this->getOption( 'delay', '0' ) ); |
| 69 |
|
| 70 |
if ( $this->allNamespaces ) { |
| 71 |
$this->purgeNamespace( false ); |
| 72 |
} elseif ( $this->namespaceId !== null ) { |
| 73 |
$this->purgeNamespace( intval( $this->namespaceId ) ); |
| 74 |
} else { |
| 75 |
$this->doPurge(); |
| 76 |
} |
| 77 |
$this->output( "Done!\n" ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private function doPurge() { |
| 84 |
$stdin = $this->getStdin(); |
| 85 |
$urls = []; |
| 86 |
$htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater(); |
| 87 |
|
| 88 |
while ( !feof( $stdin ) ) { |
| 89 |
$page = trim( fgets( $stdin ) ); |
| 90 |
if ( preg_match( '%^https?://%', $page ) ) { |
| 91 |
$urls[] = $page; |
| 92 |
} elseif ( $page !== '' ) { |
| 93 |
$title = Title::newFromText( $page ); |
| 94 |
if ( $title ) { |
| 95 |
$newUrls = $htmlCacheUpdater->getUrls( $title ); |
| 96 |
|
| 97 |
foreach ( $newUrls as $url ) { |
| 98 |
$this->output( "$url\n" ); |
| 99 |
} |
| 100 |
|
| 101 |
$urls = array_merge( $urls, $newUrls ); |
| 102 |
|
| 103 |
if ( $this->doDbTouch ) { |
| 104 |
$title->invalidateCache(); |
| 105 |
} |
| 106 |
} else { |
| 107 |
$this->output( "(Invalid title '$page')\n" ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
$this->output( "Purging " . count( $urls ) . " urls\n" ); |
| 112 |
$this->sendPurgeRequest( $urls ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private function purgeNamespace( $namespace = false ) { |
| 121 |
if ( $this->doDbTouch ) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
$this->fatalError( 'The --db-touch option is not supported when purging by namespace.' ); |
| 126 |
} |
| 127 |
|
| 128 |
$dbr = $this->getReplicaDB(); |
| 129 |
$htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater(); |
| 130 |
$startId = 0; |
| 131 |
if ( $namespace === false ) { |
| 132 |
$conds = []; |
| 133 |
} else { |
| 134 |
$conds = [ 'page_namespace' => $namespace ]; |
| 135 |
} |
| 136 |
while ( true ) { |
| 137 |
$res = $dbr->newSelectQueryBuilder() |
| 138 |
->select( [ 'page_id', 'page_namespace', 'page_title' ] ) |
| 139 |
->from( 'page' ) |
| 140 |
->where( $conds ) |
| 141 |
->andWhere( $dbr->expr( 'page_id', '>', $startId ) ) |
| 142 |
->orderBy( 'page_id' ) |
| 143 |
->limit( $this->getBatchSize() ) |
| 144 |
->caller( __METHOD__ )->fetchResultSet(); |
| 145 |
if ( !$res->numRows() ) { |
| 146 |
break; |
| 147 |
} |
| 148 |
$urls = []; |
| 149 |
foreach ( $res as $row ) { |
| 150 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 151 |
$urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) ); |
| 152 |
$startId = $row->page_id; |
| 153 |
} |
| 154 |
$this->sendPurgeRequest( $urls ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function sendPurgeRequest( $urls ) { |
| 163 |
$hcu = $this->getServiceContainer()->getHtmlCacheUpdater(); |
| 164 |
if ( $this->delay > 0 ) { |
| 165 |
foreach ( $urls as $url ) { |
| 166 |
if ( $this->hasOption( 'verbose' ) ) { |
| 167 |
$this->output( $url . "\n" ); |
| 168 |
} |
| 169 |
$hcu->purgeUrls( $url, $hcu::PURGE_NAIVE ); |
| 170 |
sleep( $this->delay ); |
| 171 |
} |
| 172 |
} else { |
| 173 |
if ( $this->hasOption( 'verbose' ) ) { |
| 174 |
$this->output( implode( "\n", $urls ) . "\n" ); |
| 175 |
} |
| 176 |
$hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE ); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
$maintClass = PurgeList::class; |
| 183 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 184 |
|