]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiDelete.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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                 $this->getMain()->requestWriteMode();
53                 $params = $this->extractRequestParams();
54
55                 $this->requireOnlyOneParameter($params, 'title', 'pageid');
56                 if(!isset($params['token']))
57                         $this->dieUsageMsg(array('missingparam', 'token'));
58
59                 if(isset($params['title']))
60                 {
61                         $titleObj = Title::newFromText($params['title']);
62                         if(!$titleObj)
63                                 $this->dieUsageMsg(array('invalidtitle', $params['title']));
64                 }
65                 else if(isset($params['pageid']))
66                 {
67                         $titleObj = Title::newFromID($params['pageid']);
68                         if(!$titleObj)
69                                 $this->dieUsageMsg(array('nosuchpageid', $params['pageid']));
70                 }
71                 if(!$titleObj->exists())
72                         $this->dieUsageMsg(array('notanarticle'));
73
74                 $reason = (isset($params['reason']) ? $params['reason'] : NULL);
75                 if ($titleObj->getNamespace() == NS_FILE) {
76                         $retval = self::deleteFile($params['token'], $titleObj, $params['oldimage'], $reason, false);
77                         if(count($retval))
78                                 // We don't care about multiple errors, just report one of them
79                                 $this->dieUsageMsg(current($retval));
80                 } else {
81                         $articleObj = new Article($titleObj);
82                         $retval = self::delete($articleObj, $params['token'], $reason);
83                         
84                         if(count($retval))
85                                 // We don't care about multiple errors, just report one of them
86                                 $this->dieUsageMsg(current($retval));
87                         
88                         if($params['watch'] || $wgUser->getOption('watchdeletion'))
89                                 $articleObj->doWatch();
90                         else if($params['unwatch'])
91                                 $articleObj->doUnwatch();
92                 }
93
94                 $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
95                 $this->getResult()->addValue(null, $this->getModuleName(), $r);
96         }
97
98         private static function getPermissionsError(&$title, $token) {
99                 global $wgUser;
100                 
101                 // Check permissions
102                 $errors = $title->getUserPermissionsErrors('delete', $wgUser);
103                 if (count($errors) > 0) return $errors;
104                 
105                 // Check token
106                 if(!$wgUser->matchEditToken($token))
107                         return array(array('sessionfailure'));
108                 return array();
109         }
110
111         /**
112          * We have our own delete() function, since Article.php's implementation is split in two phases
113          *
114          * @param Article $article - Article object to work on
115          * @param string $token - Delete token (same as edit token)
116          * @param string $reason - Reason for the deletion. Autogenerated if NULL
117          * @return Title::getUserPermissionsErrors()-like array
118          */
119         public static function delete(&$article, $token, &$reason = NULL)
120         {
121                 global $wgUser;
122                 $title = $article->getTitle();
123                 $errors = self::getPermissionsError($title, $token);
124                 if (count($errors)) return $errors;
125
126                 // Auto-generate a summary, if necessary
127                 if(is_null($reason))
128                 {
129                         # Need to pass a throwaway variable because generateReason expects
130                         # a reference
131                         $hasHistory = false;
132                         $reason = $article->generateReason($hasHistory);
133                         if($reason === false)
134                                 return array(array('cannotdelete'));
135                 }
136                 
137                 if (!wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)))
138                         $this->dieUsageMsg(array('hookaborted'));
139
140                 // Luckily, Article.php provides a reusable delete function that does the hard work for us
141                 if($article->doDeleteArticle($reason)) {
142                         wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $article->getId()));
143                         return array();
144                 }
145                 return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
146         }
147
148         public static function deleteFile($token, &$title, $oldimage, &$reason = NULL, $suppress = false)
149         {
150                 $errors = self::getPermissionsError($title, $token);
151                 if (count($errors)) return $errors;
152
153                 if( $oldimage && !FileDeleteForm::isValidOldSpec($oldimage) )
154                         return array(array('invalidoldimage'));
155
156                 $file = wfFindFile($title, false, FileRepo::FIND_IGNORE_REDIRECT);
157                 $oldfile = false;
158                 
159                 if( $oldimage )
160                         $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
161                         
162                 if( !FileDeleteForm::haveDeletableFile($file, $oldfile, $oldimage) )
163                         return array(array('nofile'));
164                 if (is_null($reason)) # Log and RC don't like null reasons
165                         $reason = '';
166                 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
167                                 
168                 if( !$status->isGood() )
169                         return array(array('cannotdelete', $title->getPrefixedText()));
170                         
171                 return array();
172         }
173         
174         public function mustBePosted() { return true; }
175
176         public function getAllowedParams() {
177                 return array (
178                         'title' => null,
179                         'pageid' => array(
180                                 ApiBase::PARAM_TYPE => 'integer'
181                         ),
182                         'token' => null,
183                         'reason' => null,
184                         'watch' => false,
185                         'unwatch' => false,
186                         'oldimage' => null
187                 );
188         }
189
190         public function getParamDescription() {
191                 return array (
192                         'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
193                         'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
194                         'token' => 'A delete token previously retrieved through prop=info',
195                         'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
196                         'watch' => 'Add the page to your watchlist',
197                         'unwatch' => 'Remove the page from your watchlist',
198                         'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
199                 );
200         }
201
202         public function getDescription() {
203                 return array(
204                         'Delete a page.'
205                 );
206         }
207
208         protected function getExamples() {
209                 return array (
210                         'api.php?action=delete&title=Main%20Page&token=123ABC',
211                         'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
212                 );
213         }
214
215         public function getVersion() {
216                 return __CLASS__ . ': $Id: ApiDelete.php 44541 2008-12-13 21:07:18Z mrzman $';
217         }
218 }