stylize.php on API code
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2
3 /*
4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once ( "ApiBase.php" );
28 }
29
30
31 /**
32 * API module that facilitates deleting pages. The API eqivalent of action=delete.
33 * Requires API write mode to be enabled.
34 *
35 * @ingroup API
36 */
37 class ApiDelete extends ApiBase {
38
39 public function __construct( $main, $action ) {
40 parent :: __construct( $main, $action );
41 }
42
43 /**
44 * Extracts the title, token, and reason from the request parameters and invokes
45 * the local delete() function with these as arguments. It does not make use of
46 * the delete function specified by Article.php. If the deletion succeeds, the
47 * details of the article deleted and the reason for deletion are added to the
48 * result object.
49 */
50 public function execute() {
51 global $wgUser;
52 $params = $this->extractRequestParams();
53
54 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
55 if ( !isset( $params['token'] ) )
56 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
57
58 if ( isset( $params['title'] ) )
59 {
60 $titleObj = Title::newFromText( $params['title'] );
61 if ( !$titleObj )
62 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
63 }
64 else if ( isset( $params['pageid'] ) )
65 {
66 $titleObj = Title::newFromID( $params['pageid'] );
67 if ( !$titleObj )
68 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
69 }
70 if ( !$titleObj->exists() )
71 $this->dieUsageMsg( array( 'notanarticle' ) );
72
73 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
74 if ( $titleObj->getNamespace() == NS_FILE ) {
75 $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
76 if ( count( $retval ) )
77 // We don't care about multiple errors, just report one of them
78 $this->dieUsageMsg( reset( $retval ) );
79 } else {
80 $articleObj = new Article( $titleObj );
81 $retval = self::delete( $articleObj, $params['token'], $reason );
82
83 if ( count( $retval ) )
84 // We don't care about multiple errors, just report one of them
85 $this->dieUsageMsg( reset( $retval ) );
86
87 if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) )
88 $articleObj->doWatch();
89 else if ( $params['unwatch'] )
90 $articleObj->doUnwatch();
91 }
92
93 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
94 $this->getResult()->addValue( null, $this->getModuleName(), $r );
95 }
96
97 private static function getPermissionsError( &$title, $token ) {
98 global $wgUser;
99
100 // Check permissions
101 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
102 if ( count( $errors ) > 0 ) return $errors;
103
104 // Check token
105 if ( !$wgUser->matchEditToken( $token ) )
106 return array( array( 'sessionfailure' ) );
107 return array();
108 }
109
110 /**
111 * We have our own delete() function, since Article.php's implementation is split in two phases
112 *
113 * @param Article $article - Article object to work on
114 * @param string $token - Delete token (same as edit token)
115 * @param string $reason - Reason for the deletion. Autogenerated if NULL
116 * @return Title::getUserPermissionsErrors()-like array
117 */
118 public static function delete( &$article, $token, &$reason = null )
119 {
120 global $wgUser;
121 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
122 global $wgDeleteRevisionsLimit;
123 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
124 }
125 $title = $article->getTitle();
126 $errors = self::getPermissionsError( $title, $token );
127 if ( count( $errors ) ) return $errors;
128
129 // Auto-generate a summary, if necessary
130 if ( is_null( $reason ) )
131 {
132 # Need to pass a throwaway variable because generateReason expects
133 # a reference
134 $hasHistory = false;
135 $reason = $article->generateReason( $hasHistory );
136 if ( $reason === false )
137 return array( array( 'cannotdelete' ) );
138 }
139
140 $error = '';
141 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) )
142 $this->dieUsageMsg( array( 'hookaborted', $error ) );
143
144 // Luckily, Article.php provides a reusable delete function that does the hard work for us
145 if ( $article->doDeleteArticle( $reason ) ) {
146 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
147 return array();
148 }
149 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
150 }
151
152 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false )
153 {
154 $errors = self::getPermissionsError( $title, $token );
155 if ( count( $errors ) ) return $errors;
156
157 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) )
158 return array( array( 'invalidoldimage' ) );
159
160 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
161 $oldfile = false;
162
163 if ( $oldimage )
164 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
165
166 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) )
167 return self::delete( new Article( $title ), $token, $reason );
168 if ( is_null( $reason ) ) # Log and RC don't like null reasons
169 $reason = '';
170 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
171
172 if ( !$status->isGood() )
173 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
174
175 return array();
176 }
177
178 public function mustBePosted() { return true; }
179
180 public function isWriteMode() {
181 return true;
182 }
183
184 public function getAllowedParams() {
185 return array (
186 'title' => null,
187 'pageid' => array(
188 ApiBase::PARAM_TYPE => 'integer'
189 ),
190 'token' => null,
191 'reason' => null,
192 'watch' => false,
193 'unwatch' => false,
194 'oldimage' => null
195 );
196 }
197
198 public function getParamDescription() {
199 return array (
200 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
201 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
202 'token' => 'A delete token previously retrieved through prop=info',
203 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
204 'watch' => 'Add the page to your watchlist',
205 'unwatch' => 'Remove the page from your watchlist',
206 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
207 );
208 }
209
210 public function getDescription() {
211 return array(
212 'Delete a page.'
213 );
214 }
215
216 protected function getExamples() {
217 return array (
218 'api.php?action=delete&title=Main%20Page&token=123ABC',
219 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
220 );
221 }
222
223 public function getVersion() {
224 return __CLASS__ . ': $Id$';
225 }
226 }