4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
7 * Copyright © 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() {
52 $params = $this->extractRequestParams();
54 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
56 if ( isset( $params['title'] ) ) {
57 $titleObj = Title
::newFromText( $params['title'] );
59 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
61 } elseif ( isset( $params['pageid'] ) ) {
62 $titleObj = Title
::newFromID( $params['pageid'] );
64 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
67 if ( !$titleObj->exists() ) {
68 $this->dieUsageMsg( array( 'notanarticle' ) );
71 $reason = ( isset( $params['reason'] ) ?
$params['reason'] : null );
72 if ( $titleObj->getNamespace() == NS_FILE
) {
73 $retval = self
::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
74 if ( count( $retval ) ) {
75 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
78 $articleObj = new Article( $titleObj );
79 $retval = self
::delete( $articleObj, $params['token'], $reason );
81 if ( count( $retval ) ) {
82 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
85 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) ||
$wgUser->getOption( 'watchdeletion' );
87 // Deprecated parameters
88 if ( $params['watch'] ||
$watch ) {
89 $articleObj->doWatch();
90 } elseif ( $params['unwatch'] ||
!$watch ) {
91 $articleObj->doUnwatch();
95 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
96 $this->getResult()->addValue( null, $this->getModuleName(), $r );
99 private static function getPermissionsError( &$title, $token ) {
103 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
104 if ( count( $errors ) > 0 ) {
112 * We have our own delete() function, since Article.php's implementation is split in two phases
114 * @param $article Article object to work on
115 * @param $token String: delete token (same as edit token)
116 * @param $reason String: reason for the deletion. Autogenerated if NULL
117 * @return Title::getUserPermissionsErrors()-like array
119 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 ) ) {
131 // Auto-generate a summary, if necessary
132 if ( is_null( $reason ) ) {
133 // Need to pass a throwaway variable because generateReason expects
136 $reason = $article->generateReason( $hasHistory );
137 if ( $reason === false ) {
138 return array( array( 'cannotdelete' ) );
143 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
144 $this->dieUsageMsg( array( 'hookaborted', $error ) );
147 // Luckily, Article.php provides a reusable delete function that does the hard work for us
148 if ( $article->doDeleteArticle( $reason ) ) {
149 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
152 return array( array( 'cannotdelete', $article->mTitle
->getPrefixedText() ) );
155 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
156 $errors = self
::getPermissionsError( $title, $token );
157 if ( count( $errors ) ) {
161 if ( $oldimage && !FileDeleteForm
::isValidOldSpec( $oldimage ) ) {
162 return array( array( 'invalidoldimage' ) );
165 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
169 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
172 if ( !FileDeleteForm
::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
173 return self
::delete( new Article( $title ), $token, $reason );
175 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
178 $status = FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress );
180 if ( !$status->isGood() ) {
181 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
187 public function mustBePosted() {
191 public function isWriteMode() {
195 public function getAllowedParams() {
199 ApiBase
::PARAM_TYPE
=> 'integer'
204 ApiBase
::PARAM_DFLT
=> false,
205 ApiBase
::PARAM_DEPRECATED
=> true,
207 'watchlist' => array(
208 ApiBase
::PARAM_DFLT
=> 'preferences',
209 ApiBase
::PARAM_TYPE
=> array(
221 public function getParamDescription() {
223 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
224 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
225 'token' => 'A delete token previously retrieved through prop=info',
226 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
227 'watch' => 'Add the page to your watchlist',
228 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
229 'unwatch' => 'Remove the page from your watchlist',
230 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
234 public function getDescription() {
240 public function getPossibleErrors() {
241 return array_merge( parent
::getPossibleErrors(), array(
242 array( 'invalidtitle', 'title' ),
243 array( 'nosuchpageid', 'pageid' ),
244 array( 'notanarticle' ),
245 array( 'hookaborted', 'error' ),
249 public function getTokenSalt() {
253 protected function getExamples() {
255 'api.php?action=delete&title=Main%20Page&token=123ABC',
256 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
260 public function getVersion() {
261 return __CLASS__
. ': $Id$';