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" );
31 * API module that facilitates deleting pages. The API eqivalent of action=delete.
32 * Requires API write mode to be enabled.
36 class ApiDelete
extends ApiBase
{
38 public function __construct( $main, $action ) {
39 parent
:: __construct( $main, $action );
43 * Extracts the title, token, and reason from the request parameters and invokes
44 * the local delete() function with these as arguments. It does not make use of
45 * the delete function specified by Article.php. If the deletion succeeds, the
46 * details of the article deleted and the reason for deletion are added to the
49 public function execute() {
51 $params = $this->extractRequestParams();
53 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
55 if ( isset( $params['title'] ) )
57 $titleObj = Title
::newFromText( $params['title'] );
59 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
61 else if ( isset( $params['pageid'] ) )
63 $titleObj = Title
::newFromID( $params['pageid'] );
65 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
67 if ( !$titleObj->exists() )
68 $this->dieUsageMsg( array( 'notanarticle' ) );
70 $reason = ( isset( $params['reason'] ) ?
$params['reason'] : null );
71 if ( $titleObj->getNamespace() == NS_FILE
) {
72 $retval = self
::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
73 if ( count( $retval ) )
74 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
76 $articleObj = new Article( $titleObj );
77 $retval = self
::delete( $articleObj, $params['token'], $reason );
79 if ( count( $retval ) )
80 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
82 if ( $params['watch'] ||
$wgUser->getOption( 'watchdeletion' ) )
83 $articleObj->doWatch();
84 else if ( $params['unwatch'] )
85 $articleObj->doUnwatch();
88 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
89 $this->getResult()->addValue( null, $this->getModuleName(), $r );
92 private static function getPermissionsError( &$title, $token ) {
96 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
97 if ( count( $errors ) > 0 ) return $errors;
100 if ( !$wgUser->matchEditToken( $token ) )
101 return array( array( 'sessionfailure' ) );
106 * We have our own delete() function, since Article.php's implementation is split in two phases
108 * @param Article $article - Article object to work on
109 * @param string $token - Delete token (same as edit token)
110 * @param string $reason - Reason for the deletion. Autogenerated if NULL
111 * @return Title::getUserPermissionsErrors()-like array
113 public static function delete( &$article, $token, &$reason = null )
116 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
117 global $wgDeleteRevisionsLimit;
118 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
120 $title = $article->getTitle();
121 $errors = self
::getPermissionsError( $title, $token );
122 if ( count( $errors ) ) return $errors;
124 // Auto-generate a summary, if necessary
125 if ( is_null( $reason ) )
127 // Need to pass a throwaway variable because generateReason expects
130 $reason = $article->generateReason( $hasHistory );
131 if ( $reason === false )
132 return array( array( 'cannotdelete' ) );
136 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) )
137 $this->dieUsageMsg( array( 'hookaborted', $error ) );
139 // Luckily, Article.php provides a reusable delete function that does the hard work for us
140 if ( $article->doDeleteArticle( $reason ) ) {
141 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
144 return array( array( 'cannotdelete', $article->mTitle
->getPrefixedText() ) );
147 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false )
149 $errors = self
::getPermissionsError( $title, $token );
150 if ( count( $errors ) ) return $errors;
152 if ( $oldimage && !FileDeleteForm
::isValidOldSpec( $oldimage ) )
153 return array( array( 'invalidoldimage' ) );
155 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
159 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
161 if ( !FileDeleteForm
::haveDeletableFile( $file, $oldfile, $oldimage ) )
162 return self
::delete( new Article( $title ), $token, $reason );
163 if ( is_null( $reason ) ) // Log and RC don't like null reasons
165 $status = FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress );
167 if ( !$status->isGood() )
168 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
173 public function mustBePosted() {
177 public function isWriteMode() {
181 public function getAllowedParams() {
185 ApiBase
::PARAM_TYPE
=> 'integer'
195 public function getParamDescription() {
197 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
198 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
199 'token' => 'A delete token previously retrieved through prop=info',
200 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
201 'watch' => 'Add the page to your watchlist',
202 'unwatch' => 'Remove the page from your watchlist',
203 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
207 public function getDescription() {
213 public function getPossibleErrors() {
214 return array_merge( parent
::getPossibleErrors(), array(
215 array( 'invalidtitle', 'title' ),
216 array( 'nosuchpageid', 'pageid' ),
217 array( 'notanarticle' ),
218 array( 'hookaborted', 'error' ),
222 public function requiresToken() {
226 protected function getExamples() {
228 'api.php?action=delete&title=Main%20Page&token=123ABC',
229 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
233 public function getVersion() {
234 return __CLASS__
. ': $Id$';