* (bug 26498) allow LinksUpdate with API
[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 $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
72 if( $params['forcelinkupdate'] ) {
73 if ( !$wgUser->pingLimiter() ) {
74 global $wgParser, $wgEnableParserCache;
75 $popts = new ParserOptions();
76 $p_result = $wgParser->parse( $article->getContent(), $title, $popts );
77
78 # Update the links tables
79 $u = new LinksUpdate( $title, $p_result );
80 $u->doUpdate();
81
82 $r['linkupdate'] = '';
83
84 if ( $wgEnableParserCache ) {
85 $pcache = ParserCache::singleton();
86 $pcache->save( $p_result, $article, $popts );
87 }
88 }
89 }
90
91 $result[] = $r;
92 }
93 $this->getResult()->setIndexedTagName( $result, 'page' );
94 $this->getResult()->addValue( null, $this->getModuleName(), $result );
95 }
96
97 public function isWriteMode() {
98 return true;
99 }
100
101 public function getAllowedParams() {
102 return array(
103 'titles' => array(
104 ApiBase::PARAM_ISMULTI => true,
105 ApiBase::PARAM_REQUIRED => true
106 ),
107 'forcelinkupdate' => false,
108 );
109 }
110
111 public function getParamDescription() {
112 return array(
113 'titles' => 'A list of titles',
114 'forcelinkupdate' => 'Update the links tables',
115 );
116 }
117
118 public function getDescription() {
119 return array( 'Purge the cache for the given titles.',
120 'This module requires a POST request if the user is not logged in.'
121 );
122 }
123
124 public function getPossibleErrors() {
125 return array_merge( parent::getPossibleErrors(), array(
126 array( 'cantpurge' ),
127 ) );
128 }
129
130 protected function getExamples() {
131 return array(
132 'api.php?action=purge&titles=Main_Page|API'
133 );
134 }
135
136 public function getVersion() {
137 return __CLASS__ . ': $Id$';
138 }
139 }