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 * Extracts the title and reason from the request parameters and invokes
36 * the local delete() function with these as arguments. It does not make use of
37 * the delete function specified by Article.php. If the deletion succeeds, the
38 * details of the article deleted and the reason for deletion are added to the
41 public function execute() {
42 $this->useTransactionalTimeLimit();
44 $params = $this->extractRequestParams();
46 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
47 $titleObj = $pageObj->getTitle();
48 if ( !$pageObj->exists() &&
49 !( $titleObj->getNamespace() == NS_FILE
&& self
::canDeleteFile( $pageObj->getFile() ) )
51 $this->dieWithError( 'apierror-missingtitle' );
54 $reason = $params['reason'];
55 $user = $this->getUser();
57 // Check that the user is allowed to carry out the deletion
58 $this->checkTitleUserPermissions( $titleObj, 'delete' );
60 // If change tagging was requested, check that the user is allowed to tag,
61 // and the tags are valid
62 if ( count( $params['tags'] ) ) {
63 $tagStatus = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
64 if ( !$tagStatus->isOK() ) {
65 $this->dieStatus( $tagStatus );
69 if ( $titleObj->getNamespace() == NS_FILE
) {
70 $status = self
::deleteFile(
79 $status = self
::delete( $pageObj, $user, $reason, $params['tags'] );
82 if ( !$status->isGood() ) {
83 $this->dieStatus( $status );
86 // Deprecated parameters
87 if ( $params['watch'] ) {
89 } elseif ( $params['unwatch'] ) {
92 $watch = $params['watchlist'];
94 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
97 'title' => $titleObj->getPrefixedText(),
99 'logid' => $status->value
101 $this->getResult()->addValue( null, $this->getModuleName(), $r );
105 * We have our own delete() function, since Article.php's implementation is split in two phases
107 * @param Page|WikiPage $page Page or WikiPage object to work on
108 * @param User $user User doing the action
109 * @param string|null $reason Reason for the deletion. Autogenerated if null
110 * @param array $tags Tags to tag the deletion with
113 protected static function delete( Page
$page, User
$user, &$reason = null, $tags = [] ) {
114 $title = $page->getTitle();
116 // Auto-generate a summary, if necessary
117 if ( is_null( $reason ) ) {
118 // Need to pass a throwaway variable because generateReason expects
121 $reason = $page->getAutoDeleteReason( $hasHistory );
122 if ( $reason === false ) {
123 return Status
::newFatal( 'cannotdelete', $title->getPrefixedText() );
129 // Luckily, Article.php provides a reusable delete function that does the hard work for us
130 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
137 protected static function canDeleteFile( File
$file ) {
138 return $file->exists() && $file->isLocal() && !$file->getRedirected();
142 * @param Page $page Object to work on
143 * @param User $user User doing the action
144 * @param string $oldimage Archive name
145 * @param string $reason Reason for the deletion. Autogenerated if null.
146 * @param bool $suppress Whether to mark all deleted versions as restricted
147 * @param array $tags Tags to tag the deletion with
150 protected static function deleteFile( Page
$page, User
$user, $oldimage,
151 &$reason = null, $suppress = false, $tags = []
153 $title = $page->getTitle();
155 $file = $page->getFile();
156 if ( !self
::canDeleteFile( $file ) ) {
157 return self
::delete( $page, $user, $reason, $tags );
161 if ( !FileDeleteForm
::isValidOldSpec( $oldimage ) ) {
162 return Status
::newFatal( 'invalidoldimage' );
164 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
165 if ( !$oldfile->exists() ||
!$oldfile->isLocal() ||
$oldfile->getRedirected() ) {
166 return Status
::newFatal( 'nodeleteablefile' );
170 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
174 return FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
177 public function mustBePosted() {
181 public function isWriteMode() {
185 public function getAllowedParams() {
189 ApiBase
::PARAM_TYPE
=> 'integer'
193 ApiBase
::PARAM_TYPE
=> 'tags',
194 ApiBase
::PARAM_ISMULTI
=> true,
197 ApiBase
::PARAM_DFLT
=> false,
198 ApiBase
::PARAM_DEPRECATED
=> true,
201 ApiBase
::PARAM_DFLT
=> 'preferences',
202 ApiBase
::PARAM_TYPE
=> [
210 ApiBase
::PARAM_DFLT
=> false,
211 ApiBase
::PARAM_DEPRECATED
=> true,
217 public function needsToken() {
221 protected function getExamplesMessages() {
223 'action=delete&title=Main%20Page&token=123ABC'
224 => 'apihelp-delete-example-simple',
225 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
226 => 'apihelp-delete-example-reason',
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';