2006-10-31 16:58:39 +00:00
|
|
|
<?php
|
2006-10-31 17:00:28 +00:00
|
|
|
/**
|
2017-11-01 20:55:24 +00:00
|
|
|
* Send purge requests for listed pages to CDN
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-04 18:31:42 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2006-10-31 17:00:28 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2019-03-15 00:23:26 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-08-02 19:35:17 +00:00
|
|
|
|
2012-08-04 18:31:42 +00:00
|
|
|
/**
|
2017-11-01 20:55:24 +00:00
|
|
|
* Maintenance script that sends purge requests for listed pages to CDN.
|
2012-08-04 18:31:42 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class PurgeList extends Maintenance {
|
2019-09-18 21:42:20 +00:00
|
|
|
/** @var string|null */
|
|
|
|
|
private $namespaceId;
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $allNamespaces;
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $doDbTouch;
|
2022-03-08 23:17:44 +00:00
|
|
|
/** @var int */
|
2019-09-18 21:42:20 +00:00
|
|
|
private $delay;
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2019-09-18 21:42:20 +00:00
|
|
|
$this->addDescription( "Send purge requests for listed pages to CDN.\n"
|
|
|
|
|
. "By default this expects a list of URLs or page names from STDIN. "
|
|
|
|
|
. "To query the database for input, use --namespace or --all-namespaces instead."
|
|
|
|
|
);
|
|
|
|
|
$this->addOption( 'namespace', 'Purge pages with this namespace number', false, true );
|
2020-09-27 18:54:17 +00:00
|
|
|
$this->addOption( 'all-namespaces', 'Purge pages in all namespaces', false, false );
|
2022-06-03 21:31:00 +00:00
|
|
|
$this->addOption( 'db-touch',
|
|
|
|
|
"Update the page.page_touched database field.\n"
|
|
|
|
|
. "This is only considered when purging by title, not when purging by namespace or URL.",
|
|
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
);
|
2013-01-07 04:00:37 +00:00
|
|
|
$this->addOption( 'delay', 'Number of seconds to delay between each purge', false, true );
|
|
|
|
|
$this->addOption( 'verbose', 'Show more output', false, false, 'v' );
|
2011-08-14 08:51:36 +00:00
|
|
|
$this->setBatchSize( 100 );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2006-10-31 16:58:39 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2019-09-18 21:42:20 +00:00
|
|
|
$this->namespaceId = $this->getOption( 'namespace' );
|
|
|
|
|
$this->allNamespaces = $this->hasOption( 'all-namespaces' );
|
|
|
|
|
$this->doDbTouch = $this->hasOption( 'db-touch' );
|
2022-03-08 23:17:44 +00:00
|
|
|
$this->delay = intval( $this->getOption( 'delay', '0' ) );
|
2019-09-18 21:42:20 +00:00
|
|
|
|
|
|
|
|
if ( $this->allNamespaces ) {
|
2013-01-07 04:00:37 +00:00
|
|
|
$this->purgeNamespace( false );
|
2019-09-18 21:42:20 +00:00
|
|
|
} elseif ( $this->namespaceId !== null ) {
|
|
|
|
|
$this->purgeNamespace( intval( $this->namespaceId ) );
|
2011-08-14 08:51:36 +00:00
|
|
|
} else {
|
2013-01-30 15:05:17 +00:00
|
|
|
$this->doPurge();
|
2011-08-14 08:51:36 +00:00
|
|
|
}
|
|
|
|
|
$this->output( "Done!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 20:13:54 +00:00
|
|
|
/**
|
|
|
|
|
* Purge URL coming from stdin
|
|
|
|
|
*/
|
2013-01-30 15:05:17 +00:00
|
|
|
private function doPurge() {
|
2009-08-02 19:35:17 +00:00
|
|
|
$stdin = $this->getStdin();
|
2016-02-17 09:09:32 +00:00
|
|
|
$urls = [];
|
2023-08-31 09:21:12 +00:00
|
|
|
$htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
|
2006-10-31 16:58:39 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( !feof( $stdin ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$page = trim( fgets( $stdin ) );
|
2012-02-14 20:20:56 +00:00
|
|
|
if ( preg_match( '%^https?://%', $page ) ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$urls[] = $page;
|
2010-05-22 16:50:39 +00:00
|
|
|
} elseif ( $page !== '' ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$title = Title::newFromText( $page );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $title ) {
|
2020-11-03 10:55:14 +00:00
|
|
|
$newUrls = $htmlCacheUpdater->getUrls( $title );
|
2019-09-18 20:36:26 +00:00
|
|
|
|
|
|
|
|
foreach ( $newUrls as $url ) {
|
|
|
|
|
$this->output( "$url\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$urls = array_merge( $urls, $newUrls );
|
|
|
|
|
|
2019-09-18 21:42:20 +00:00
|
|
|
if ( $this->doDbTouch ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$title->invalidateCache();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "(Invalid title '$page')\n" );
|
|
|
|
|
}
|
2007-01-17 00:54:54 +00:00
|
|
|
}
|
2006-10-31 16:58:39 +00:00
|
|
|
}
|
2013-04-13 11:36:24 +00:00
|
|
|
$this->output( "Purging " . count( $urls ) . " urls\n" );
|
2011-08-14 08:51:36 +00:00
|
|
|
$this->sendPurgeRequest( $urls );
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 20:13:54 +00:00
|
|
|
/**
|
|
|
|
|
* Purge a namespace or all pages
|
2014-04-17 20:48:32 +00:00
|
|
|
*
|
|
|
|
|
* @param int|bool $namespace
|
2014-03-15 20:13:54 +00:00
|
|
|
*/
|
2013-01-07 04:00:37 +00:00
|
|
|
private function purgeNamespace( $namespace = false ) {
|
2022-06-03 21:31:00 +00:00
|
|
|
if ( $this->doDbTouch ) {
|
|
|
|
|
// NOTE: If support for this is added in the future,
|
|
|
|
|
// it MUST NOT be allowed when $wgMiserMode is enabled.
|
|
|
|
|
// Change this to a check and error about instead! (T263957)
|
|
|
|
|
$this->fatalError( 'The --db-touch option is not supported when purging by namespace.' );
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
2023-08-31 09:21:12 +00:00
|
|
|
$htmlCacheUpdater = $this->getServiceContainer()->getHtmlCacheUpdater();
|
2013-01-07 04:00:37 +00:00
|
|
|
$startId = 0;
|
|
|
|
|
if ( $namespace === false ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [];
|
2013-01-07 04:00:37 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [ 'page_namespace' => $namespace ];
|
2013-01-07 04:00:37 +00:00
|
|
|
}
|
|
|
|
|
while ( true ) {
|
2023-07-18 22:56:37 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'page_id', 'page_namespace', 'page_title' ] )
|
|
|
|
|
->from( 'page' )
|
|
|
|
|
->where( $conds )
|
2024-01-17 17:48:40 +00:00
|
|
|
->andWhere( $dbr->expr( 'page_id', '>', $startId ) )
|
2023-07-18 22:56:37 +00:00
|
|
|
->orderBy( 'page_id' )
|
|
|
|
|
->limit( $this->getBatchSize() )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2013-01-07 04:00:37 +00:00
|
|
|
if ( !$res->numRows() ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$urls = [];
|
2013-01-07 04:00:37 +00:00
|
|
|
foreach ( $res as $row ) {
|
2011-08-15 10:23:48 +00:00
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
2020-11-03 10:55:14 +00:00
|
|
|
$urls = array_merge( $urls, $htmlCacheUpdater->getUrls( $title ) );
|
2013-01-07 04:00:37 +00:00
|
|
|
$startId = $row->page_id;
|
2011-08-15 10:23:48 +00:00
|
|
|
}
|
|
|
|
|
$this->sendPurgeRequest( $urls );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-14 08:51:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to purge an array of $urls
|
2017-11-01 20:55:24 +00:00
|
|
|
* @param array $urls List of URLS to purge from CDNs
|
2011-08-14 08:51:36 +00:00
|
|
|
*/
|
2011-08-15 10:23:48 +00:00
|
|
|
private function sendPurgeRequest( $urls ) {
|
2023-08-31 09:21:12 +00:00
|
|
|
$hcu = $this->getServiceContainer()->getHtmlCacheUpdater();
|
2019-09-18 21:42:20 +00:00
|
|
|
if ( $this->delay > 0 ) {
|
2013-01-07 04:00:37 +00:00
|
|
|
foreach ( $urls as $url ) {
|
|
|
|
|
if ( $this->hasOption( 'verbose' ) ) {
|
|
|
|
|
$this->output( $url . "\n" );
|
|
|
|
|
}
|
2019-03-15 00:23:26 +00:00
|
|
|
$hcu->purgeUrls( $url, $hcu::PURGE_NAIVE );
|
2022-03-08 23:17:44 +00:00
|
|
|
sleep( $this->delay );
|
2013-01-07 04:00:37 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ( $this->hasOption( 'verbose' ) ) {
|
2013-04-18 18:48:44 +00:00
|
|
|
$this->output( implode( "\n", $urls ) . "\n" );
|
2013-01-07 04:00:37 +00:00
|
|
|
}
|
2019-03-15 00:23:26 +00:00
|
|
|
$hcu->purgeUrls( $urls, $hcu::PURGE_NAIVE );
|
2013-01-07 04:00:37 +00:00
|
|
|
}
|
2011-08-15 10:23:48 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = PurgeList::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|