Merge "content: Recognise .json as JsonContent in User and MediaWiki namespace"
[lhc/web/wiklou.git] / includes / api / ApiPurge.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.14+
5 *
6 * Created on Sep 2, 2008
7 *
8 * Copyright © 2008 Chad Horohoe
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27
28 /**
29 * API interface for page purging
30 * @ingroup API
31 */
32 class ApiPurge extends ApiBase {
33 private $mPageSet;
34
35 /**
36 * Purges the cache of a page
37 */
38 public function execute() {
39 $params = $this->extractRequestParams();
40
41 $continuationManager = new ApiContinuationManager( $this, array(), array() );
42 $this->setContinuationManager( $continuationManager );
43
44 $forceLinkUpdate = $params['forcelinkupdate'];
45 $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate'];
46 $pageSet = $this->getPageSet();
47 $pageSet->execute();
48
49 $result = $pageSet->getInvalidTitlesAndRevisions();
50
51 foreach ( $pageSet->getGoodTitles() as $title ) {
52 $r = array();
53 ApiQueryBase::addTitleInfo( $r, $title );
54 $page = WikiPage::factory( $title );
55 $page->doPurge(); // Directly purge and skip the UI part of purge().
56 $r['purged'] = true;
57
58 if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) {
59 if ( !$this->getUser()->pingLimiter( 'linkpurge' ) ) {
60 $popts = $page->makeParserOptions( 'canonical' );
61
62 # Parse content; note that HTML generation is only needed if we want to cache the result.
63 $content = $page->getContent( Revision::RAW );
64 $enableParserCache = $this->getConfig()->get( 'EnableParserCache' );
65 $p_result = $content->getParserOutput(
66 $title,
67 $page->getLatest(),
68 $popts,
69 $enableParserCache
70 );
71
72 # Update the links tables
73 $updates = $content->getSecondaryDataUpdates(
74 $title, null, $forceRecursiveLinkUpdate, $p_result );
75 DataUpdate::runUpdates( $updates );
76
77 $r['linkupdate'] = true;
78
79 if ( $enableParserCache ) {
80 $pcache = ParserCache::singleton();
81 $pcache->save( $p_result, $page, $popts );
82 }
83 } else {
84 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
85 $this->setWarning( $error['info'] );
86 $forceLinkUpdate = false;
87 }
88 }
89
90 $result[] = $r;
91 }
92 $apiResult = $this->getResult();
93 ApiResult::setIndexedTagName( $result, 'page' );
94 $apiResult->addValue( null, $this->getModuleName(), $result );
95
96 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
97 if ( $values ) {
98 $apiResult->addValue( null, 'normalized', $values );
99 }
100 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
101 if ( $values ) {
102 $apiResult->addValue( null, 'converted', $values );
103 }
104 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
105 if ( $values ) {
106 $apiResult->addValue( null, 'redirects', $values );
107 }
108
109 $this->setContinuationManager( null );
110 $continuationManager->setContinuationIntoResult( $apiResult );
111 }
112
113 /**
114 * Get a cached instance of an ApiPageSet object
115 * @return ApiPageSet
116 */
117 private function getPageSet() {
118 if ( !isset( $this->mPageSet ) ) {
119 $this->mPageSet = new ApiPageSet( $this );
120 }
121
122 return $this->mPageSet;
123 }
124
125 public function isWriteMode() {
126 return true;
127 }
128
129 public function mustBePosted() {
130 // Anonymous users are not allowed a non-POST request
131 return !$this->getUser()->isAllowed( 'purge' );
132 }
133
134 public function getAllowedParams( $flags = 0 ) {
135 $result = array(
136 'forcelinkupdate' => false,
137 'forcerecursivelinkupdate' => false,
138 'continue' => array(
139 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
140 ),
141 );
142 if ( $flags ) {
143 $result += $this->getPageSet()->getFinalParams( $flags );
144 }
145
146 return $result;
147 }
148
149 protected function getExamplesMessages() {
150 return array(
151 'action=purge&titles=Main_Page|API'
152 => 'apihelp-purge-example-simple',
153 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10'
154 => 'apihelp-purge-example-generator',
155 );
156 }
157
158 public function getHelpUrls() {
159 return 'https://www.mediawiki.org/wiki/API:Purge';
160 }
161 }