ApiDelete.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API module that facilitates deleting pages. The API equivalent of action=delete.
  24. * Requires API write mode to be enabled.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiDelete extends ApiBase {
  29. /**
  30. * Extracts the title and reason from the request parameters and invokes
  31. * the local delete() function with these as arguments. It does not make use of
  32. * the delete function specified by Article.php. If the deletion succeeds, the
  33. * details of the article deleted and the reason for deletion are added to the
  34. * result object.
  35. */
  36. public function execute() {
  37. $this->useTransactionalTimeLimit();
  38. $params = $this->extractRequestParams();
  39. $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
  40. $titleObj = $pageObj->getTitle();
  41. if ( !$pageObj->exists() &&
  42. // @phan-suppress-next-line PhanUndeclaredMethod
  43. !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
  44. ) {
  45. $this->dieWithError( 'apierror-missingtitle' );
  46. }
  47. $reason = $params['reason'];
  48. $user = $this->getUser();
  49. // Check that the user is allowed to carry out the deletion
  50. $this->checkTitleUserPermissions( $titleObj, 'delete' );
  51. // If change tagging was requested, check that the user is allowed to tag,
  52. // and the tags are valid
  53. if ( $params['tags'] ) {
  54. $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  55. if ( !$tagStatus->isOK() ) {
  56. $this->dieStatus( $tagStatus );
  57. }
  58. }
  59. if ( $titleObj->getNamespace() == NS_FILE ) {
  60. $status = self::deleteFile(
  61. $pageObj,
  62. $user,
  63. $params['oldimage'],
  64. $reason,
  65. false,
  66. $params['tags']
  67. );
  68. } else {
  69. $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
  70. }
  71. if ( !$status->isOK() ) {
  72. $this->dieStatus( $status );
  73. }
  74. $this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
  75. // Deprecated parameters
  76. if ( $params['watch'] ) {
  77. $watch = 'watch';
  78. } elseif ( $params['unwatch'] ) {
  79. $watch = 'unwatch';
  80. } else {
  81. $watch = $params['watchlist'];
  82. }
  83. $this->setWatch( $watch, $titleObj, 'watchdeletion' );
  84. $r = [
  85. 'title' => $titleObj->getPrefixedText(),
  86. 'reason' => $reason,
  87. ];
  88. if ( $status->hasMessage( 'delete-scheduled' ) ) {
  89. $r['scheduled'] = true;
  90. }
  91. if ( $status->value !== null ) {
  92. // Scheduled deletions don't currently have a log entry available at this point
  93. $r['logid'] = $status->value;
  94. }
  95. $this->getResult()->addValue( null, $this->getModuleName(), $r );
  96. }
  97. /**
  98. * We have our own delete() function, since Article.php's implementation is split in two phases
  99. *
  100. * @param Page|WikiPage $page Page or WikiPage object to work on
  101. * @param User $user User doing the action
  102. * @param string|null &$reason Reason for the deletion. Autogenerated if null
  103. * @param array $tags Tags to tag the deletion with
  104. * @return Status
  105. */
  106. protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
  107. $title = $page->getTitle();
  108. // Auto-generate a summary, if necessary
  109. if ( is_null( $reason ) ) {
  110. // Need to pass a throwaway variable because generateReason expects
  111. // a reference
  112. $hasHistory = false;
  113. $reason = $page->getAutoDeleteReason( $hasHistory );
  114. if ( $reason === false ) {
  115. // Should be reachable only if the page has no revisions
  116. return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
  117. }
  118. }
  119. $error = '';
  120. // Luckily, Article.php provides a reusable delete function that does the hard work for us
  121. return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
  122. }
  123. /**
  124. * @param File $file
  125. * @return bool
  126. */
  127. protected static function canDeleteFile( File $file ) {
  128. return $file->exists() && $file->isLocal() && !$file->getRedirected();
  129. }
  130. /**
  131. * @param Page $page Object to work on
  132. * @param User $user User doing the action
  133. * @param string $oldimage Archive name
  134. * @param string|null &$reason Reason for the deletion. Autogenerated if null.
  135. * @param bool $suppress Whether to mark all deleted versions as restricted
  136. * @param array $tags Tags to tag the deletion with
  137. * @return Status
  138. */
  139. protected static function deleteFile( Page $page, User $user, $oldimage,
  140. &$reason = null, $suppress = false, $tags = []
  141. ) {
  142. $title = $page->getTitle();
  143. // @phan-suppress-next-line PhanUndeclaredMethod There's no right typehint for it
  144. $file = $page->getFile();
  145. if ( !self::canDeleteFile( $file ) ) {
  146. return self::delete( $page, $user, $reason, $tags );
  147. }
  148. if ( $oldimage ) {
  149. if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
  150. return Status::newFatal( 'invalidoldimage' );
  151. }
  152. $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
  153. if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
  154. return Status::newFatal( 'nodeleteablefile' );
  155. }
  156. }
  157. if ( is_null( $reason ) ) { // Log and RC don't like null reasons
  158. $reason = '';
  159. }
  160. return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
  161. }
  162. public function mustBePosted() {
  163. return true;
  164. }
  165. public function isWriteMode() {
  166. return true;
  167. }
  168. public function getAllowedParams() {
  169. return [
  170. 'title' => null,
  171. 'pageid' => [
  172. ApiBase::PARAM_TYPE => 'integer'
  173. ],
  174. 'reason' => null,
  175. 'tags' => [
  176. ApiBase::PARAM_TYPE => 'tags',
  177. ApiBase::PARAM_ISMULTI => true,
  178. ],
  179. 'watch' => [
  180. ApiBase::PARAM_DFLT => false,
  181. ApiBase::PARAM_DEPRECATED => true,
  182. ],
  183. 'watchlist' => [
  184. ApiBase::PARAM_DFLT => 'preferences',
  185. ApiBase::PARAM_TYPE => [
  186. 'watch',
  187. 'unwatch',
  188. 'preferences',
  189. 'nochange'
  190. ],
  191. ],
  192. 'unwatch' => [
  193. ApiBase::PARAM_DFLT => false,
  194. ApiBase::PARAM_DEPRECATED => true,
  195. ],
  196. 'oldimage' => null,
  197. ];
  198. }
  199. public function needsToken() {
  200. return 'csrf';
  201. }
  202. protected function getExamplesMessages() {
  203. return [
  204. 'action=delete&title=Main%20Page&token=123ABC'
  205. => 'apihelp-delete-example-simple',
  206. 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
  207. => 'apihelp-delete-example-reason',
  208. ];
  209. }
  210. public function getHelpUrls() {
  211. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
  212. }
  213. }