merged master
[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
34 public function __construct( $main, $action ) {
35 parent::__construct( $main, $action );
36 }
37
38 /**
39 * Purges the cache of a page
40 */
41 public function execute() {
42 $user = $this->getUser();
43 $params = $this->extractRequestParams();
44 if ( !$user->isAllowed( 'purge' ) && !$this->getMain()->isInternalMode() &&
45 !$this->getRequest()->wasPosted() ) {
46 $this->dieUsageMsg( array( 'mustbeposted', $this->getModuleName() ) );
47 }
48
49 $forceLinkUpdate = $params['forcelinkupdate'];
50 $pageSet = new ApiPageSet( $this );
51 $pageSet->execute();
52
53 $result = array();
54 foreach( $pageSet->getInvalidTitles() as $title ) {
55 $r = array();
56 $r['title'] = $title;
57 $r['invalid'] = '';
58 $result[] = $r;
59 }
60 foreach( $pageSet->getMissingPageIDs() as $p ) {
61 $page = array();
62 $page['pageid'] = $p;
63 $page['missing'] = '';
64 $result[] = $page;
65 }
66 foreach( $pageSet->getMissingRevisionIDs() as $r ) {
67 $rev = array();
68 $rev['revid'] = $r;
69 $rev['missing'] = '';
70 $result[] = $rev;
71 }
72
73 foreach ( $pageSet->getTitles() as $title ) {
74 $r = array();
75
76 ApiQueryBase::addTitleInfo( $r, $title );
77 if ( !$title->exists() ) {
78 $r['missing'] = '';
79 $result[] = $r;
80 continue;
81 }
82
83 $page = WikiPage::factory( $title );
84 $page->doPurge(); // Directly purge and skip the UI part of purge().
85 $r['purged'] = '';
86
87 if( $forceLinkUpdate ) {
88 if ( !$user->pingLimiter() ) {
89 global $wgEnableParserCache;
90
91 $popts = $page->makeParserOptions( 'canonical' );
92 $popts->setTidy( true );
93
94 # Parse content; note that HTML generation is only needed if we want to cache the result.
95 $content = $page->getContent( Revision::RAW );
96 $p_result = $content->getParserOutput( $title, $page->getLatest(), $popts, $wgEnableParserCache );
97
98 # Update the links tables
99 $updates = $content->getContentHandler()->getSecondaryDataUpdates( $content, $title, null, true, $p_result );
100 DataUpdate::runUpdates( $updates );
101
102 $r['linkupdate'] = '';
103
104 if ( $wgEnableParserCache ) {
105 $pcache = ParserCache::singleton();
106 $pcache->save( $p_result, $page, $popts );
107 }
108 } else {
109 $error = $this->parseMsg( array( 'actionthrottledtext' ) );
110 $this->setWarning( $error['info'] );
111 $forceLinkUpdate = false;
112 }
113 }
114
115 $result[] = $r;
116 }
117 $apiResult = $this->getResult();
118 $apiResult->setIndexedTagName( $result, 'page' );
119 $apiResult->addValue( null, $this->getModuleName(), $result );
120 }
121
122 public function isWriteMode() {
123 return true;
124 }
125
126 public function getAllowedParams() {
127 $psModule = new ApiPageSet( $this );
128 return $psModule->getAllowedParams() + array(
129 'forcelinkupdate' => false,
130 );
131 }
132
133 public function getParamDescription() {
134 $psModule = new ApiPageSet( $this );
135 return $psModule->getParamDescription() + array(
136 'forcelinkupdate' => 'Update the links tables',
137 );
138 }
139
140 public function getResultProperties() {
141 return array(
142 ApiBase::PROP_LIST => true,
143 '' => array(
144 'ns' => array(
145 ApiBase::PROP_TYPE => 'namespace',
146 ApiBase::PROP_NULLABLE => true
147 ),
148 'title' => array(
149 ApiBase::PROP_TYPE => 'string',
150 ApiBase::PROP_NULLABLE => true
151 ),
152 'pageid' => array(
153 ApiBase::PROP_TYPE => 'integer',
154 ApiBase::PROP_NULLABLE => true
155 ),
156 'revid' => array(
157 ApiBase::PROP_TYPE => 'integer',
158 ApiBase::PROP_NULLABLE => true
159 ),
160 'invalid' => 'boolean',
161 'missing' => 'boolean',
162 'purged' => 'boolean',
163 'linkupdate' => 'boolean'
164 )
165 );
166 }
167
168 public function getDescription() {
169 return array( 'Purge the cache for the given titles.',
170 'Requires a POST request if the user is not logged in.'
171 );
172 }
173
174 public function getPossibleErrors() {
175 $psModule = new ApiPageSet( $this );
176 return array_merge(
177 parent::getPossibleErrors(),
178 array( array( 'cantpurge' ), ),
179 $psModule->getPossibleErrors()
180 );
181 }
182
183 public function getExamples() {
184 return array(
185 'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
186 );
187 }
188
189 public function getHelpUrls() {
190 return 'https://www.mediawiki.org/wiki/API:Purge';
191 }
192
193 public function getVersion() {
194 return __CLASS__ . ': $Id$';
195 }
196 }