Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiPurge.php
blobec551370df31f314fa1ed89c108c8c8333e4bdec
1 <?php
3 /**
4 * API for MediaWiki 1.14+
6 * Created on Sep 2, 2008
8 * Copyright © 2008 Chad Horohoe
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
28 /**
29 * API interface for page purging
30 * @ingroup API
32 class ApiPurge extends ApiBase {
33 private $mPageSet;
35 /**
36 * Purges the cache of a page
38 public function execute() {
39 $params = $this->extractRequestParams();
41 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
43 $forceLinkUpdate = $params['forcelinkupdate'];
44 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
45 $pageSet = $this->getPageSet();
46 $pageSet->execute();
48 $result = $pageSet->getInvalidTitlesAndRevisions();
50 foreach ( $pageSet->getGoodTitles() as $title ) {
51 $r = array();
52 ApiQueryBase::addTitleInfo( $r, $title );
53 $page = WikiPage::factory( $title );
54 $page->doPurge(); // Directly purge and skip the UI part of purge().
55 $r['purged'] = '';
57 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
58 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
59 $popts = $page->makeParserOptions( 'canonical' );
61 # Parse content; note that HTML generation is only needed if we want to cache the result.
62 $content = $page->getContent( Revision::RAW );
63 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
64 $p_result = $content->getParserOutput(
65 $title,
66 $page->getLatest(),
67 $popts,
68 $enableParserCache
71 # Update the links tables
72 $updates = $content->getSecondaryDataUpdates(
73 $title, null, $forceRecursiveLinkUpdate, $p_result );
74 DataUpdate::runUpdates( $updates );
76 $r['linkupdate'] = '';
78 if ( $enableParserCache ) {
79 $pcache = ParserCache::singleton();
80 $pcache->save( $p_result, $page, $popts );
82 } else {
83 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
84 $this->setWarning( $error['info'] );
85 $forceLinkUpdate = false;
89 $result[] = $r;
91 $apiResult = $this->getResult();
92 $apiResult->setIndexedTagName( $result, 'page' );
93 $apiResult->addValue( null, $this->getModuleName(), $result );
95 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
96 if ( $values ) {
97 $apiResult->addValue( null, 'normalized', $values );
99 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
100 if ( $values ) {
101 $apiResult->addValue( null, 'converted', $values );
103 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
104 if ( $values ) {
105 $apiResult->addValue( null, 'redirects', $values );
108 $apiResult->endContinuation();
112 * Get a cached instance of an ApiPageSet object
113 * @return ApiPageSet
115 private function getPageSet() {
116 if ( !isset( $this->mPageSet ) ) {
117 $this->mPageSet = new ApiPageSet( $this );
120 return $this->mPageSet;
123 public function isWriteMode() {
124 return true;
127 public function mustBePosted() {
128 // Anonymous users are not allowed a non-POST request
129 return !$this->getUser()->isAllowed( 'purge' );
132 public function getAllowedParams( $flags = 0 ) {
133 $result = array(
134 'forcelinkupdate' => false,
135 'forcerecursivelinkupdate' => false,
136 'continue' => array(
137 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
140 if ( $flags ) {
141 $result += $this->getPageSet()->getFinalParams( $flags );
144 return $result;
147 protected function getExamplesMessages() {
148 return array(
149 'action=purge&titles=Main_Page|API'
150 => 'apihelp-purge-example-simple',
151 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
152 => 'apihelp-purge-example-generator',
156 public function getHelpUrls() {
157 return 'https://www.mediawiki.org/wiki/API:Purge';