5c83d34fa459076eb42b8c1f20e3e1401dbc83b2
[lhc/web/wiklou.git] / includes / api / ApiPurge.php
1 <?php
2
3 /**
4 * Created on Sep 2, 2008
5 *
6 * API for MediaWiki 1.14+
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 require_once( 'ApiBase.php' );
28 }
29
30 /**
31 * API interface for page purging
32 * @ingroup API
33 */
34 class ApiPurge extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 /**
41 * Purges the cache of a page
42 */
43 public function execute() {
44 global $wgUser;
45 $params = $this->extractRequestParams();
46 if ( !$wgUser->isAllowed( 'purge' ) ) {
47 $this->dieUsageMsg( array( 'cantpurge' ) );
48 }
49 if ( !isset( $params['titles'] ) ) {
50 $this->dieUsageMsg( array( 'missingparam', 'titles' ) );
51 }
52 $result = array();
53 foreach ( $params['titles'] as $t ) {
54 $r = array();
55 $title = Title::newFromText( $t );
56 if ( !$title instanceof Title ) {
57 $r['title'] = $t;
58 $r['invalid'] = '';
59 $result[] = $r;
60 continue;
61 }
62 ApiQueryBase::addTitleInfo( $r, $title );
63 if ( !$title->exists() ) {
64 $r['missing'] = '';
65 $result[] = $r;
66 continue;
67 }
68 $article = Mediawiki::articleFromTitle( $title );
69 $article->doPurge(); // Directly purge and skip the UI part of purge().
70 $r['purged'] = '';
71 $result[] = $r;
72 }
73 $this->getResult()->setIndexedTagName( $result, 'page' );
74 $this->getResult()->addValue( null, $this->getModuleName(), $result );
75 }
76
77 public function mustBePosted() {
78 global $wgUser;
79 return $wgUser->isAnon();
80 }
81
82 public function isWriteMode() {
83 return true;
84 }
85
86 public function getAllowedParams() {
87 return array(
88 'titles' => array(
89 ApiBase::PARAM_ISMULTI => true
90 )
91 );
92 }
93
94 public function getParamDescription() {
95 return array(
96 'titles' => 'A list of titles',
97 );
98 }
99
100 public function getDescription() {
101 return array(
102 'Purge the cache for the given titles.'
103 );
104 }
105
106 public function getPossibleErrors() {
107 return array_merge( parent::getPossibleErrors(), array(
108 array( 'cantpurge' ),
109 array( 'missingparam', 'titles' ),
110 ) );
111 }
112
113 protected function getExamples() {
114 return array(
115 'api.php?action=purge&titles=Main_Page|API'
116 );
117 }
118
119 public function getVersion() {
120 return __CLASS__ . ': $Id$';
121 }
122 }