ApiDelete.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Jun 30, 2007
  6. *
  7. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  8. *
  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.
  13. *
  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.
  18. *
  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
  23. *
  24. * @file
  25. */
  26. /**
  27. * API module that facilitates deleting pages. The API equivalent of action=delete.
  28. * Requires API write mode to be enabled.
  29. *
  30. * @ingroup API
  31. */
  32. class ApiDelete extends ApiBase {
  33. /**
  34. * Extracts the title and reason from the request parameters and invokes
  35. * the local delete() function with these as arguments. It does not make use of
  36. * the delete function specified by Article.php. If the deletion succeeds, the
  37. * details of the article deleted and the reason for deletion are added to the
  38. * result object.
  39. */
  40. public function execute() {
  41. $this->useTransactionalTimeLimit();
  42. $params = $this->extractRequestParams();
  43. $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
  44. $titleObj = $pageObj->getTitle();
  45. if ( !$pageObj->exists() &&
  46. !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
  47. ) {
  48. $this->dieWithError( 'apierror-missingtitle' );
  49. }
  50. $reason = $params['reason'];
  51. $user = $this->getUser();
  52. // Check that the user is allowed to carry out the deletion
  53. $this->checkTitleUserPermissions( $titleObj, 'delete' );
  54. // If change tagging was requested, check that the user is allowed to tag,
  55. // and the tags are valid
  56. if ( count( $params['tags'] ) ) {
  57. $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  58. if ( !$tagStatus->isOK() ) {
  59. $this->dieStatus( $tagStatus );
  60. }
  61. }
  62. if ( $titleObj->getNamespace() == NS_FILE ) {
  63. $status = self::deleteFile(
  64. $pageObj,
  65. $user,
  66. $params['oldimage'],
  67. $reason,
  68. false,
  69. $params['tags']
  70. );
  71. } else {
  72. $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
  73. }
  74. if ( !$status->isGood() ) {
  75. $this->dieStatus( $status );
  76. }
  77. // Deprecated parameters
  78. if ( $params['watch'] ) {
  79. $watch = 'watch';
  80. } elseif ( $params['unwatch'] ) {
  81. $watch = 'unwatch';
  82. } else {
  83. $watch = $params['watchlist'];
  84. }
  85. $this->setWatch( $watch, $titleObj, 'watchdeletion' );
  86. $r = [
  87. 'title' => $titleObj->getPrefixedText(),
  88. 'reason' => $reason,
  89. 'logid' => $status->value
  90. ];
  91. $this->getResult()->addValue( null, $this->getModuleName(), $r );
  92. }
  93. /**
  94. * We have our own delete() function, since Article.php's implementation is split in two phases
  95. *
  96. * @param Page|WikiPage $page Page or WikiPage object to work on
  97. * @param User $user User doing the action
  98. * @param string|null &$reason Reason for the deletion. Autogenerated if null
  99. * @param array $tags Tags to tag the deletion with
  100. * @return Status
  101. */
  102. protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
  103. $title = $page->getTitle();
  104. // Auto-generate a summary, if necessary
  105. if ( is_null( $reason ) ) {
  106. // Need to pass a throwaway variable because generateReason expects
  107. // a reference
  108. $hasHistory = false;
  109. $reason = $page->getAutoDeleteReason( $hasHistory );
  110. if ( $reason === false ) {
  111. return Status::newFatal( 'cannotdelete', $title->getPrefixedText() );
  112. }
  113. }
  114. $error = '';
  115. // Luckily, Article.php provides a reusable delete function that does the hard work for us
  116. return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
  117. }
  118. /**
  119. * @param File $file
  120. * @return bool
  121. */
  122. protected static function canDeleteFile( File $file ) {
  123. return $file->exists() && $file->isLocal() && !$file->getRedirected();
  124. }
  125. /**
  126. * @param Page $page Object to work on
  127. * @param User $user User doing the action
  128. * @param string $oldimage Archive name
  129. * @param string &$reason Reason for the deletion. Autogenerated if null.
  130. * @param bool $suppress Whether to mark all deleted versions as restricted
  131. * @param array $tags Tags to tag the deletion with
  132. * @return Status
  133. */
  134. protected static function deleteFile( Page $page, User $user, $oldimage,
  135. &$reason = null, $suppress = false, $tags = []
  136. ) {
  137. $title = $page->getTitle();
  138. $file = $page->getFile();
  139. if ( !self::canDeleteFile( $file ) ) {
  140. return self::delete( $page, $user, $reason, $tags );
  141. }
  142. if ( $oldimage ) {
  143. if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
  144. return Status::newFatal( 'invalidoldimage' );
  145. }
  146. $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
  147. if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
  148. return Status::newFatal( 'nodeleteablefile' );
  149. }
  150. }
  151. if ( is_null( $reason ) ) { // Log and RC don't like null reasons
  152. $reason = '';
  153. }
  154. return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
  155. }
  156. public function mustBePosted() {
  157. return true;
  158. }
  159. public function isWriteMode() {
  160. return true;
  161. }
  162. public function getAllowedParams() {
  163. return [
  164. 'title' => null,
  165. 'pageid' => [
  166. ApiBase::PARAM_TYPE => 'integer'
  167. ],
  168. 'reason' => null,
  169. 'tags' => [
  170. ApiBase::PARAM_TYPE => 'tags',
  171. ApiBase::PARAM_ISMULTI => true,
  172. ],
  173. 'watch' => [
  174. ApiBase::PARAM_DFLT => false,
  175. ApiBase::PARAM_DEPRECATED => true,
  176. ],
  177. 'watchlist' => [
  178. ApiBase::PARAM_DFLT => 'preferences',
  179. ApiBase::PARAM_TYPE => [
  180. 'watch',
  181. 'unwatch',
  182. 'preferences',
  183. 'nochange'
  184. ],
  185. ],
  186. 'unwatch' => [
  187. ApiBase::PARAM_DFLT => false,
  188. ApiBase::PARAM_DEPRECATED => true,
  189. ],
  190. 'oldimage' => null,
  191. ];
  192. }
  193. public function needsToken() {
  194. return 'csrf';
  195. }
  196. protected function getExamplesMessages() {
  197. return [
  198. 'action=delete&title=Main%20Page&token=123ABC'
  199. => 'apihelp-delete-example-simple',
  200. 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
  201. => 'apihelp-delete-example-reason',
  202. ];
  203. }
  204. public function getHelpUrls() {
  205. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
  206. }
  207. }