reverts r103894
[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
51 $result = array();
52 foreach ( $params['titles'] as $t ) {
53 $r = array();
54 $title = Title::newFromText( $t );
55 if ( !$title instanceof Title ) {
56 $r['title'] = $t;
57 $r['invalid'] = '';
58 $result[] = $r;
59 continue;
60 }
61 ApiQueryBase::addTitleInfo( $r, $title );
62 if ( !$title->exists() ) {
63 $r['missing'] = '';
64 $result[] = $r;
65 continue;
66 }
67
68 $page = WikiPage::factory( $title );
69 $page->doPurge(); // Directly purge and skip the UI part of purge().
70 $r['purged'] = '';
71
72 if( $forceLinkUpdate ) {
73 if ( !$user->pingLimiter() ) {
74 global $wgParser, $wgEnableParserCache;
75
76 $popts = ParserOptions::newFromContext( $this->getContext() );
77 $p_result = $wgParser->parse( $page->getRawText(), $title, $popts );
78
79 # Update the links tables
80 $u = new LinksUpdate( $title, $p_result );
81 $u->doUpdate();
82
83 $r['linkupdate'] = '';
84
85 if ( $wgEnableParserCache ) {
86 $pcache = ParserCache::singleton();
87 $pcache->save( $p_result, $page, $popts );
88 }
89 } else {
90 $this->setWarning( $this->parseMsg( array( 'actionthrottledtext' ) ) );
91 $forceLinkUpdate = false;
92 }
93 }
94
95 $result[] = $r;
96 }
97 $apiResult = $this->getResult();
98 $apiResult->setIndexedTagName( $result, 'page' );
99 $apiResult->addValue( null, $this->getModuleName(), $result );
100 }
101
102 public function isWriteMode() {
103 return true;
104 }
105
106 public function getAllowedParams() {
107 return array(
108 'titles' => array(
109 ApiBase::PARAM_ISMULTI => true,
110 ApiBase::PARAM_REQUIRED => true
111 ),
112 'forcelinkupdate' => false,
113 );
114 }
115
116 public function getParamDescription() {
117 return array(
118 'titles' => 'A list of titles',
119 'forcelinkupdate' => 'Update the links tables',
120 );
121 }
122
123 public function getDescription() {
124 return array( 'Purge the cache for the given titles.',
125 'Requires a POST request if the user is not logged in.'
126 );
127 }
128
129 public function getPossibleErrors() {
130 return array_merge( parent::getPossibleErrors(), array(
131 array( 'cantpurge' ),
132 ) );
133 }
134
135 public function getExamples() {
136 return array(
137 'api.php?action=purge&titles=Main_Page|API'
138 );
139 }
140
141 public function getHelpUrls() {
142 return 'http://www.mediawiki.org/wiki/API:Purge';
143 }
144
145 public function getVersion() {
146 return __CLASS__ . ': $Id$';
147 }
148 }