5 * Created on Jun 30, 2007
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
33 class ApiDelete
extends ApiBase
{
35 public function __construct( $main, $action ) {
36 parent
::__construct( $main, $action );
40 * Extracts the title, token, and reason from the request parameters and invokes
41 * the local delete() function with these as arguments. It does not make use of
42 * the delete function specified by Article.php. If the deletion succeeds, the
43 * details of the article deleted and the reason for deletion are added to the
46 public function execute() {
47 $params = $this->extractRequestParams();
49 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
50 if ( !$pageObj->exists() ) {
51 $this->dieUsageMsg( 'notanarticle' );
54 $titleObj = $pageObj->getTitle();
55 $reason = ( isset( $params['reason'] ) ?
$params['reason'] : null );
56 $user = $this->getUser();
58 if ( $titleObj->getNamespace() == NS_FILE
) {
59 $retval = self
::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
61 $retval = self
::delete( $pageObj, $user, $params['token'], $reason );
64 if ( count( $retval ) ) {
65 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
68 // Deprecated parameters
69 if ( $params['watch'] ) {
71 } elseif ( $params['unwatch'] ) {
74 $watch = $params['watchlist'];
76 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
78 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
79 $this->getResult()->addValue( null, $this->getModuleName(), $r );
84 * @param $user User doing the action
85 * @param $token String
88 private static function getPermissionsError( $title, $user, $token ) {
90 return $title->getUserPermissionsErrors( 'delete', $user );
94 * We have our own delete() function, since Article.php's implementation is split in two phases
96 * @param $page WikiPage object to work on
97 * @param $user User doing the action
98 * @param $token String: delete token (same as edit token)
99 * @param $reason String: reason for the deletion. Autogenerated if NULL
100 * @return Title::getUserPermissionsErrors()-like array
102 public static function delete( Page
$page, User
$user, $token, &$reason = null ) {
103 $title = $page->getTitle();
104 $errors = self
::getPermissionsError( $title, $user, $token );
105 if ( count( $errors ) ) {
109 // Auto-generate a summary, if necessary
110 if ( is_null( $reason ) ) {
111 // Need to pass a throwaway variable because generateReason expects
114 $reason = $page->getAutoDeleteReason( $hasHistory );
115 if ( $reason === false ) {
116 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
121 // Luckily, Article.php provides a reusable delete function that does the hard work for us
122 if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
125 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
130 * @param $page WikiPage object to work on
131 * @param $user User doing the action
135 * @param $suppress bool
136 * @return array|Title
138 public static function deleteFile( Page
$page, User
$user, $token, $oldimage, &$reason = null, $suppress = false ) {
139 $title = $page->getTitle();
140 $errors = self
::getPermissionsError( $title, $user, $token );
141 if ( count( $errors ) ) {
145 $file = $page->getFile();
146 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
147 return self
::delete( $page, $user, $token, $reason );
151 if ( !FileDeleteForm
::isValidOldSpec( $oldimage ) ) {
152 return array( array( 'invalidoldimage' ) );
154 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
155 if ( !$oldfile->exists() ||
!$oldfile->isLocal() ||
$oldfile->getRedirected() ) {
156 return array( array( 'nodeleteablefile' ) );
162 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
165 $status = FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress );
166 if ( !$status->isGood() ) {
167 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
173 public function mustBePosted() {
177 public function isWriteMode() {
181 public function getAllowedParams() {
185 ApiBase
::PARAM_TYPE
=> 'integer'
190 ApiBase
::PARAM_DFLT
=> false,
191 ApiBase
::PARAM_DEPRECATED
=> true,
193 'watchlist' => array(
194 ApiBase
::PARAM_DFLT
=> 'preferences',
195 ApiBase
::PARAM_TYPE
=> array(
203 ApiBase
::PARAM_DFLT
=> false,
204 ApiBase
::PARAM_DEPRECATED
=> true,
210 public function getParamDescription() {
211 $p = $this->getModulePrefix();
213 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
214 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
215 'token' => 'A delete token previously retrieved through prop=info',
216 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
217 'watch' => 'Add the page to your watchlist',
218 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
219 'unwatch' => 'Remove the page from your watchlist',
220 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
224 public function getDescription() {
225 return 'Delete a page';
228 public function getPossibleErrors() {
229 return array_merge( parent
::getPossibleErrors(),
230 $this->getTitleOrPageIdErrorMessage(),
232 array( 'notanarticle' ),
233 array( 'hookaborted', 'error' ),
234 array( 'delete-toobig', 'limit' ),
235 array( 'cannotdelete', 'title' ),
236 array( 'invalidoldimage' ),
237 array( 'nodeleteablefile' ),
242 public function needsToken() {
246 public function getTokenSalt() {
250 public function getExamples() {
252 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
253 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Delete';
261 public function getVersion() {
262 return __CLASS__
. ': $Id$';