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