<?php
-/*
- * Created on Sep 2, 2008
- *
+/**
* API for MediaWiki 1.14+
*
- * Copyright (C) 2008 Chad Horohoe
+ * Created on Sep 2, 2008
+ *
+ * Copyright © 2008 Chad Horohoe
*
* 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
*
* 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.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
*/
-
-if (!defined('MEDIAWIKI')) {
- require_once ('ApiBase.php');
-}
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
/**
* API interface for page purging
* @ingroup API
*/
class ApiPurge extends ApiBase {
-
- public function __construct($main, $action) {
- parent :: __construct($main, $action);
- }
+ private $mPageSet;
/**
* Purges the cache of a page
*/
public function execute() {
- global $wgUser;
$params = $this->extractRequestParams();
- if(!$wgUser->isAllowed('purge'))
- $this->dieUsageMsg(array('cantpurge'));
- if(!isset($params['titles']))
- $this->dieUsageMsg(array('missingparam', 'titles'));
- $result = array();
- foreach($params['titles'] as $t) {
- $r = array();
- $title = Title::newFromText($t);
- if(!$title instanceof Title)
- {
- $r['title'] = $t;
- $r['invalid'] = '';
- $result[] = $r;
- continue;
+
+ $continuationManager = new ApiContinuationManager( $this, [], [] );
+ $this->setContinuationManager( $continuationManager );
+
+ $forceLinkUpdate = $params['forcelinkupdate'];
+ $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
+ $pageSet = $this->getPageSet();
+ $pageSet->execute();
+
+ $result = $pageSet->getInvalidTitlesAndRevisions();
+ $user = $this->getUser();
+
+ foreach ( $pageSet->getGoodTitles() as $title ) {
+ $r = [];
+ ApiQueryBase::addTitleInfo( $r, $title );
+ $page = WikiPage::factory( $title );
+ if ( !$user->pingLimiter( 'purge' ) ) {
+ // Directly purge and skip the UI part of purge()
+ $page->doPurge();
+ $r['purged'] = true;
+ } else {
+ $this->addWarning( 'apierror-ratelimited' );
}
- ApiQueryBase::addTitleInfo($r, $title);
- if(!$title->exists())
- {
- $r['missing'] = '';
- $result[] = $r;
- continue;
+
+ if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
+ if ( !$user->pingLimiter( 'linkpurge' ) ) {
+ $popts = $page->makeParserOptions( 'canonical' );
+
+ # Parse content; note that HTML generation is only needed if we want to cache the result.
+ $content = $page->getContent( Revision::RAW );
+ if ( $content ) {
+ $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
+ $p_result = $content->getParserOutput(
+ $title,
+ $page->getLatest(),
+ $popts,
+ $enableParserCache
+ );
+
+ # Logging to better see expensive usage patterns
+ if ( $forceRecursiveLinkUpdate ) {
+ LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info(
+ "Recursive link purge enqueued for {title}",
+ [
+ 'user' => $this->getUser()->getName(),
+ 'title' => $title->getPrefixedText()
+ ]
+ );
+ }
+
+ # Update the links tables
+ $updates = $content->getSecondaryDataUpdates(
+ $title, null, $forceRecursiveLinkUpdate, $p_result );
+ foreach ( $updates as $update ) {
+ DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND );
+ }
+
+ $r['linkupdate'] = true;
+
+ if ( $enableParserCache ) {
+ $pcache = MediaWikiServices::getInstance()->getParserCache();
+ $pcache->save( $p_result, $page, $popts );
+ }
+ }
+ } else {
+ $this->addWarning( 'apierror-ratelimited' );
+ $forceLinkUpdate = false;
+ }
}
- $article = new Article($title);
- $article->doPurge(); // Directly purge and skip the UI part of purge().
- $r['purged'] = '';
+
$result[] = $r;
}
- $this->getResult()->setIndexedTagName($result, 'page');
- $this->getResult()->addValue(null, $this->getModuleName(), $result);
+ $apiResult = $this->getResult();
+ ApiResult::setIndexedTagName( $result, 'page' );
+ $apiResult->addValue( null, $this->getModuleName(), $result );
+
+ $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
+ if ( $values ) {
+ $apiResult->addValue( null, 'normalized', $values );
+ }
+ $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
+ if ( $values ) {
+ $apiResult->addValue( null, 'converted', $values );
+ }
+ $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
+ if ( $values ) {
+ $apiResult->addValue( null, 'redirects', $values );
+ }
+
+ $this->setContinuationManager( null );
+ $continuationManager->setContinuationIntoResult( $apiResult );
}
- public function mustBePosted() {
- global $wgUser;
- return $wgUser->isAnon();
+ /**
+ * Get a cached instance of an ApiPageSet object
+ * @return ApiPageSet
+ */
+ private function getPageSet() {
+ if ( !isset( $this->mPageSet ) ) {
+ $this->mPageSet = new ApiPageSet( $this );
+ }
+
+ return $this->mPageSet;
}
public function isWriteMode() {
return true;
}
- public function getAllowedParams() {
- return array (
- 'titles' => array(
- ApiBase :: PARAM_ISMULTI => true
- )
- );
+ public function mustBePosted() {
+ return true;
}
- public function getParamDescription() {
- return array (
- 'titles' => 'A list of titles',
- );
- }
+ public function getAllowedParams( $flags = 0 ) {
+ $result = [
+ 'forcelinkupdate' => false,
+ 'forcerecursivelinkupdate' => false,
+ 'continue' => [
+ ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
+ ],
+ ];
+ if ( $flags ) {
+ $result += $this->getPageSet()->getFinalParams( $flags );
+ }
- public function getDescription() {
- return array (
- 'Purge the cache for the given titles.'
- );
+ return $result;
}
- protected function getExamples() {
- return array(
- 'api.php?action=purge&titles=Main_Page|API'
- );
+ protected function getExamplesMessages() {
+ return [
+ 'action=purge&titles=Main_Page|API'
+ => 'apihelp-purge-example-simple',
+ 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
+ => 'apihelp-purge-example-generator',
+ ];
}
- public function getVersion() {
- return __CLASS__ . ': $Id: ApiPurge.php 69579 2010-07-20 02:49:55Z tstarling $';
+ public function getHelpUrls() {
+ return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge';
}
}