wiki.techinc.nl/includes/api/ApiPurge.php
Yuri Astrakhan 62216932c1 API PageSet allows generator for non-query modules
* PageSet can now be used in any action to process titles/pageids/revids
or any generator, redirects resolution, and converttitle functionality.
* action=purge proper usage of MustBePosted()
* Add supports for all pageset capabilities - generators, redirects, converttitles to
  action=purge and action=setnotificationtimestamp
* BREAKING CHANGE: ApiPageSet constructor now has two params instead of three, with only the
  first one keeping its meaning. ApiPageSet is now derived from ApiBase.
* BREAKING CHANGE: ApiQuery::newGenerator() and executeGeneratorModule() were deleted.

Change-Id: I7a3d7b6eb015d21ec1a9b9d9c6af9d97663f3f9a
2013-02-08 15:42:21 -05:00

220 lines
6.1 KiB
PHP

<?php
/**
* API for MediaWiki 1.14+
*
* 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
* 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
*
* @file
*/
/**
* API interface for page purging
* @ingroup API
*/
class ApiPurge extends ApiBase {
private $mPageSet;
/**
* Add all items from $values into the result
* @param $result array output
* @param $values array values to add
* @param $flag string the name of the boolean flag to mark this element
* @param $name string if given, name of the value
*/
private static function addValues( array &$result, $values, $flag = null, $name = null ) {
foreach ( $values as $val ) {
if( $val instanceof Title ) {
$v = array();
ApiQueryBase::addTitleInfo( $v, $val );
} elseif( $name !== null ) {
$v = array( $name => $val );
} else {
$v = $val;
}
if( $flag !== null ) {
$v[$flag] = '';
}
$result[] = $v;
}
}
/**
* Purges the cache of a page
*/
public function execute() {
$params = $this->extractRequestParams();
$forceLinkUpdate = $params['forcelinkupdate'];
$pageSet = $this->getPageSet();
$pageSet->execute();
$result = array();
self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
self::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
foreach ( $pageSet->getGoodTitles() as $title ) {
$r = array();
ApiQueryBase::addTitleInfo( $r, $title );
$page = WikiPage::factory( $title );
$page->doPurge(); // Directly purge and skip the UI part of purge().
$r['purged'] = '';
if ( $forceLinkUpdate ) {
if ( !$this->getUser()->pingLimiter() ) {
global $wgEnableParserCache;
$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 );
$p_result = $content->getParserOutput( $title, $page->getLatest(), $popts, $wgEnableParserCache );
# Update the links tables
$updates = $content->getSecondaryDataUpdates( $title, null, true, $p_result );
DataUpdate::runUpdates( $updates );
$r['linkupdate'] = '';
if ( $wgEnableParserCache ) {
$pcache = ParserCache::singleton();
$pcache->save( $p_result, $page, $popts );
}
} else {
$error = $this->parseMsg( array( 'actionthrottledtext' ) );
$this->setWarning( $error['info'] );
$forceLinkUpdate = false;
}
}
$result[] = $r;
}
$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 );
}
}
/**
* 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 mustBePosted() {
// Anonymous users are not allowed a non-POST request
return !$this->getUser()->isAllowed( 'purge' );
}
public function getAllowedParams( $flags = 0 ) {
$result = array( 'forcelinkupdate' => false );
if ( $flags ) {
$result += $this->getPageSet()->getFinalParams( $flags );
}
return $result;
}
public function getParamDescription() {
return $this->getPageSet()->getParamDescription()
+ array( 'forcelinkupdate' => 'Update the links tables' );
}
public function getResultProperties() {
return array(
ApiBase::PROP_LIST => true,
'' => array(
'ns' => array(
ApiBase::PROP_TYPE => 'namespace',
ApiBase::PROP_NULLABLE => true
),
'title' => array(
ApiBase::PROP_TYPE => 'string',
ApiBase::PROP_NULLABLE => true
),
'pageid' => array(
ApiBase::PROP_TYPE => 'integer',
ApiBase::PROP_NULLABLE => true
),
'revid' => array(
ApiBase::PROP_TYPE => 'integer',
ApiBase::PROP_NULLABLE => true
),
'invalid' => 'boolean',
'special' => 'boolean',
'missing' => 'boolean',
'purged' => 'boolean',
'linkupdate' => 'boolean',
'iw' => array(
ApiBase::PROP_TYPE => 'string',
ApiBase::PROP_NULLABLE => true
),
)
);
}
public function getDescription() {
return array( 'Purge the cache for the given titles.',
'Requires a POST request if the user is not logged in.'
);
}
public function getPossibleErrors() {
return array_merge(
parent::getPossibleErrors(),
$this->getPageSet()->getPossibleErrors()
);
}
public function getExamples() {
return array(
'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
);
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/API:Purge';
}
}