Call Linker methods statically
[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 if ( !defined( 'MEDIAWIKI' ) ) {
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * API interface for page purging
34 * @ingroup API
35 */
36 class ApiPurge extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 /**
43 * Purges the cache of a page
44 */
45 public function execute() {
46 global $wgUser;
47 $params = $this->extractRequestParams();
48 if ( !$wgUser->isAllowed( 'purge' ) && !$this->getMain()->isInternalMode() &&
49 !$this->getMain()->getRequest()->wasPosted() ) {
50 $this->dieUsageMsg( array( 'mustbeposted', $this->getModuleName() ) );
51 }
52
53 $forceLinkUpdate = $params['forcelinkupdate'];
54
55 $result = array();
56 foreach ( $params['titles'] as $t ) {
57 $r = array();
58 $title = Title::newFromText( $t );
59 if ( !$title instanceof Title ) {
60 $r['title'] = $t;
61 $r['invalid'] = '';
62 $result[] = $r;
63 continue;
64 }
65 ApiQueryBase::addTitleInfo( $r, $title );
66 if ( !$title->exists() ) {
67 $r['missing'] = '';
68 $result[] = $r;
69 continue;
70 }
71 $context = $this->createContext();
72 $context->setTitle( $title );
73 $article = Article::newFromTitle( $title, $context );
74 $article->doPurge(); // Directly purge and skip the UI part of purge().
75 $r['purged'] = '';
76
77 if( $forceLinkUpdate ) {
78 if ( !$wgUser->pingLimiter() ) {
79 global $wgParser, $wgEnableParserCache;
80 $popts = new ParserOptions();
81 $p_result = $wgParser->parse( $article->getContent(), $title, $popts );
82
83 # Update the links tables
84 $u = new LinksUpdate( $title, $p_result );
85 $u->doUpdate();
86
87 $r['linkupdate'] = '';
88
89 if ( $wgEnableParserCache ) {
90 $pcache = ParserCache::singleton();
91 $pcache->save( $p_result, $article, $popts );
92 }
93 } else {
94 $this->setWarning( $this->parseMsg( array( 'actionthrottledtext' ) ) );
95 $forceLinkUpdate = false;
96 }
97 }
98
99 $result[] = $r;
100 }
101 $apiResult = $this->getResult();
102 $apiResult->setIndexedTagName( $result, 'page' );
103 $apiResult->addValue( null, $this->getModuleName(), $result );
104 }
105
106 public function isWriteMode() {
107 return true;
108 }
109
110 public function getAllowedParams() {
111 return array(
112 'titles' => array(
113 ApiBase::PARAM_ISMULTI => true,
114 ApiBase::PARAM_REQUIRED => true
115 ),
116 'forcelinkupdate' => false,
117 );
118 }
119
120 public function getParamDescription() {
121 return array(
122 'titles' => 'A list of titles',
123 'forcelinkupdate' => 'Update the links tables',
124 );
125 }
126
127 public function getDescription() {
128 return array( 'Purge the cache for the given titles.',
129 'Requires a POST request if the user is not logged in.'
130 );
131 }
132
133 public function getPossibleErrors() {
134 return array_merge( parent::getPossibleErrors(), array(
135 array( 'cantpurge' ),
136 ) );
137 }
138
139 public function getExamples() {
140 return array(
141 'api.php?action=purge&titles=Main_Page|API'
142 );
143 }
144
145 public function getHelpUrls() {
146 return 'http://www.mediawiki.org/wiki/API:Purge';
147 }
148
149 public function getVersion() {
150 return __CLASS__ . ': $Id$';
151 }
152 }