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 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
51 if ( isset( $params['title'] ) ) {
52 $titleObj = Title
::newFromText( $params['title'] );
54 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
56 } elseif ( isset( $params['pageid'] ) ) {
57 $titleObj = Title
::newFromID( $params['pageid'] );
59 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
62 if ( !$titleObj->exists() ) {
63 $this->dieUsageMsg( 'notanarticle' );
66 $reason = ( isset( $params['reason'] ) ?
$params['reason'] : null );
67 $pageObj = WikiPage
::factory( $titleObj );
68 $user = $this->getUser();
70 if ( $titleObj->getNamespace() == NS_FILE
) {
71 $retval = self
::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
73 $retval = self
::delete( $pageObj, $user, $params['token'], $reason );
76 if ( count( $retval ) ) {
77 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
80 // Deprecated parameters
81 if ( $params['watch'] ) {
83 } elseif ( $params['unwatch'] ) {
86 $watch = $params['watchlist'];
88 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
90 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
91 $this->getResult()->addValue( null, $this->getModuleName(), $r );
96 * @param $user User doing the action
97 * @param $token String
100 private static function getPermissionsError( $title, $user, $token ) {
102 return $title->getUserPermissionsErrors( 'delete', $user );
106 * We have our own delete() function, since Article.php's implementation is split in two phases
108 * @param $page WikiPage object to work on
109 * @param $user User doing the action
110 * @param $token String: delete token (same as edit token)
111 * @param $reason String: reason for the deletion. Autogenerated if NULL
112 * @return Title::getUserPermissionsErrors()-like array
114 public static function delete( Page
$page, User
$user, $token, &$reason = null ) {
115 $title = $page->getTitle();
116 $errors = self
::getPermissionsError( $title, $user, $token );
117 if ( count( $errors ) ) {
121 // Auto-generate a summary, if necessary
122 if ( is_null( $reason ) ) {
123 // Need to pass a throwaway variable because generateReason expects
126 $reason = $page->getAutoDeleteReason( $hasHistory );
127 if ( $reason === false ) {
128 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
133 // Luckily, Article.php provides a reusable delete function that does the hard work for us
134 if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
137 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
142 * @param $page WikiPage object to work on
143 * @param $user User doing the action
147 * @param $suppress bool
148 * @return array|Title
150 public static function deleteFile( Page
$page, User
$user, $token, $oldimage, &$reason = null, $suppress = false ) {
151 $title = $page->getTitle();
152 $errors = self
::getPermissionsError( $title, $user, $token );
153 if ( count( $errors ) ) {
157 $file = $page->getFile();
158 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
159 return self
::delete( $page, $user, $token, $reason );
163 if ( !FileDeleteForm
::isValidOldSpec( $oldimage ) ) {
164 return array( array( 'invalidoldimage' ) );
166 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
167 if ( !$oldfile->exists() ||
!$oldfile->isLocal() ||
$oldfile->getRedirected() ) {
168 return array( array( 'nodeleteablefile' ) );
174 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177 $status = FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress );
178 if ( !$status->isGood() ) {
179 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
185 public function mustBePosted() {
189 public function isWriteMode() {
193 public function getAllowedParams() {
197 ApiBase
::PARAM_TYPE
=> 'integer'
202 ApiBase
::PARAM_DFLT
=> false,
203 ApiBase
::PARAM_DEPRECATED
=> true,
205 'watchlist' => array(
206 ApiBase
::PARAM_DFLT
=> 'preferences',
207 ApiBase
::PARAM_TYPE
=> array(
215 ApiBase
::PARAM_DFLT
=> false,
216 ApiBase
::PARAM_DEPRECATED
=> true,
222 public function getParamDescription() {
223 $p = $this->getModulePrefix();
225 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
226 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
227 'token' => 'A delete token previously retrieved through prop=info',
228 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
229 'watch' => 'Add the page to your watchlist',
230 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
231 'unwatch' => 'Remove the page from your watchlist',
232 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
236 public function getDescription() {
237 return 'Delete a page';
240 public function getPossibleErrors() {
241 return array_merge( parent
::getPossibleErrors(),
242 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
244 array( 'invalidtitle', 'title' ),
245 array( 'nosuchpageid', 'pageid' ),
246 array( 'notanarticle' ),
247 array( 'hookaborted', 'error' ),
248 array( 'delete-toobig', 'limit' ),
249 array( 'cannotdelete', 'title' ),
250 array( 'invalidoldimage' ),
251 array( 'nodeleteablefile' ),
256 public function needsToken() {
260 public function getTokenSalt() {
264 public function getExamples() {
266 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
267 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
271 public function getHelpUrls() {
272 return 'https://www.mediawiki.org/wiki/API:Delete';
275 public function getVersion() {
276 return __CLASS__
. ': $Id$';