4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once ( "ApiBase.php" );
32 * API module that facilitates deleting pages. The API eqivalent of action=delete.
33 * Requires API write mode to be enabled.
37 class ApiDelete
extends ApiBase
{
39 public function __construct( $main, $action ) {
40 parent
:: __construct( $main, $action );
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
50 public function execute() {
52 $params = $this->extractRequestParams();
54 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
55 if ( !isset( $params['token'] ) )
56 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
58 if ( isset( $params['title'] ) )
60 $titleObj = Title
::newFromText( $params['title'] );
62 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
64 else if ( isset( $params['pageid'] ) )
66 $titleObj = Title
::newFromID( $params['pageid'] );
68 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
70 if ( !$titleObj->exists() )
71 $this->dieUsageMsg( array( 'notanarticle' ) );
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 ) );
80 $articleObj = new Article( $titleObj );
81 $retval = self
::delete( $articleObj, $params['token'], $reason );
83 if ( count( $retval ) )
84 // We don't care about multiple errors, just report one of them
85 $this->dieUsageMsg( reset( $retval ) );
87 if ( $params['watch'] ||
$wgUser->getOption( 'watchdeletion' ) )
88 $articleObj->doWatch();
89 else if ( $params['unwatch'] )
90 $articleObj->doUnwatch();
93 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
94 $this->getResult()->addValue( null, $this->getModuleName(), $r );
97 private static function getPermissionsError( &$title, $token ) {
101 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
102 if ( count( $errors ) > 0 ) return $errors;
105 if ( !$wgUser->matchEditToken( $token ) )
106 return array( array( 'sessionfailure' ) );
111 * We have our own delete() function, since Article.php's implementation is split in two phases
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
118 public static function delete( &$article, $token, &$reason = null )
121 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
122 global $wgDeleteRevisionsLimit;
123 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
125 $title = $article->getTitle();
126 $errors = self
::getPermissionsError( $title, $token );
127 if ( count( $errors ) ) return $errors;
129 // Auto-generate a summary, if necessary
130 if ( is_null( $reason ) )
132 # Need to pass a throwaway variable because generateReason expects
135 $reason = $article->generateReason( $hasHistory );
136 if ( $reason === false )
137 return array( array( 'cannotdelete' ) );
141 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) )
142 $this->dieUsageMsg( array( 'hookaborted', $error ) );
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() ) );
149 return array( array( 'cannotdelete', $article->mTitle
->getPrefixedText() ) );
152 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false )
154 $errors = self
::getPermissionsError( $title, $token );
155 if ( count( $errors ) ) return $errors;
157 if ( $oldimage && !FileDeleteForm
::isValidOldSpec( $oldimage ) )
158 return array( array( 'invalidoldimage' ) );
160 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
164 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
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
170 $status = FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress );
172 if ( !$status->isGood() )
173 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
178 public function mustBePosted() { return true; }
180 public function isWriteMode() {
184 public function getAllowedParams() {
188 ApiBase
::PARAM_TYPE
=> 'integer'
198 public function getParamDescription() {
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'
210 public function getDescription() {
216 protected function getExamples() {
218 'api.php?action=delete&title=Main%20Page&token=123ABC',
219 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
223 public function getVersion() {
224 return __CLASS__
. ': $Id$';