FileDeleteForm.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * File deletion user interface
  4. *
  5. * @ingroup Media
  6. * @author Rob Church <robchur@gmail.com>
  7. */
  8. class FileDeleteForm {
  9. private $title = null;
  10. private $file = null;
  11. private $oldfile = null;
  12. private $oldimage = '';
  13. /**
  14. * Constructor
  15. *
  16. * @param File $file File we're deleting
  17. */
  18. public function __construct( $file ) {
  19. $this->title = $file->getTitle();
  20. $this->file = $file;
  21. }
  22. /**
  23. * Fulfil the request; shows the form or deletes the file,
  24. * pending authentication, confirmation, etc.
  25. */
  26. public function execute() {
  27. global $wgOut, $wgRequest, $wgUser;
  28. $this->setHeaders();
  29. if( wfReadOnly() ) {
  30. $wgOut->readOnlyPage();
  31. return;
  32. }
  33. $permission_errors = $this->title->getUserPermissionsErrors('delete', $wgUser);
  34. if (count($permission_errors)>0) {
  35. $wgOut->showPermissionsErrorPage( $permission_errors );
  36. return;
  37. }
  38. $this->oldimage = $wgRequest->getText( 'oldimage', false );
  39. $token = $wgRequest->getText( 'wpEditToken' );
  40. # Flag to hide all contents of the archived revisions
  41. $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
  42. if( $this->oldimage && !self::isValidOldSpec($this->oldimage) ) {
  43. $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage ) );
  44. return;
  45. }
  46. if( $this->oldimage )
  47. $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
  48. if( !self::haveDeletableFile($this->file, $this->oldfile, $this->oldimage) ) {
  49. $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
  50. $wgOut->addReturnTo( $this->title );
  51. return;
  52. }
  53. // Perform the deletion if appropriate
  54. if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
  55. $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
  56. $this->DeleteReason = $wgRequest->getText( 'wpReason' );
  57. $reason = $this->DeleteReasonList;
  58. if ( $reason != 'other' && $this->DeleteReason != '') {
  59. // Entry from drop down menu + additional comment
  60. $reason .= wfMsgForContent( 'colon-separator' ) . $this->DeleteReason;
  61. } elseif ( $reason == 'other' ) {
  62. $reason = $this->DeleteReason;
  63. }
  64. $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress );
  65. if( !$status->isGood() )
  66. $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
  67. if( $status->ok ) {
  68. $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
  69. $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
  70. // Return to the main page if we just deleted all versions of the
  71. // file, otherwise go back to the description page
  72. $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
  73. }
  74. return;
  75. }
  76. $this->showForm();
  77. $this->showLogEntries();
  78. }
  79. public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
  80. $article = null;
  81. if( $oldimage ) {
  82. $status = $file->deleteOld( $oldimage, $reason, $suppress );
  83. if( $status->ok ) {
  84. // Need to do a log item
  85. $log = new LogPage( 'delete' );
  86. $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
  87. if( trim( $reason ) != '' )
  88. $logComment .= ": {$reason}";
  89. $log->addEntry( 'delete', $title, $logComment );
  90. }
  91. } else {
  92. $status = $file->delete( $reason, $suppress );
  93. if( $status->ok ) {
  94. $id = $title->getArticleID( GAID_FOR_UPDATE );
  95. // Need to delete the associated article
  96. $article = new Article( $title );
  97. $error = '';
  98. if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason, &$error)) ) {
  99. if( $article->doDeleteArticle( $reason, $suppress, $id ) ) {
  100. global $wgRequest;
  101. if( $wgRequest->getCheck( 'wpWatch' ) ) {
  102. $article->doWatch();
  103. } elseif( $title->userIsWatching() ) {
  104. $article->doUnwatch();
  105. }
  106. wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
  107. }
  108. }
  109. }
  110. }
  111. if( $status->isGood() )
  112. wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
  113. return $status;
  114. }
  115. /**
  116. * Show the confirmation form
  117. */
  118. private function showForm() {
  119. global $wgOut, $wgUser, $wgRequest;
  120. if( $wgUser->isAllowed( 'suppressrevision' ) ) {
  121. $suppress = "<tr id=\"wpDeleteSuppressRow\">
  122. <td></td>
  123. <td class='mw-input'>" .
  124. Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
  125. 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
  126. "</td>
  127. </tr>";
  128. } else {
  129. $suppress = '';
  130. }
  131. $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
  132. $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
  133. 'id' => 'mw-img-deleteconfirm' ) ) .
  134. Xml::openElement( 'fieldset' ) .
  135. Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
  136. Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
  137. $this->prepareMessage( 'filedelete-intro' ) .
  138. Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
  139. "<tr>
  140. <td class='mw-label'>" .
  141. Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
  142. "</td>
  143. <td class='mw-input'>" .
  144. Xml::listDropDown( 'wpDeleteReasonList',
  145. wfMsgForContent( 'filedelete-reason-dropdown' ),
  146. wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
  147. "</td>
  148. </tr>
  149. <tr>
  150. <td class='mw-label'>" .
  151. Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
  152. "</td>
  153. <td class='mw-input'>" .
  154. Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
  155. array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
  156. "</td>
  157. </tr>
  158. {$suppress}
  159. <tr>
  160. <td></td>
  161. <td class='mw-input'>" .
  162. Xml::checkLabel( wfMsg( 'watchthis' ),
  163. 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
  164. "</td>
  165. </tr>
  166. <tr>
  167. <td></td>
  168. <td class='mw-submit'>" .
  169. Xml::submitButton( wfMsg( 'filedelete-submit' ),
  170. array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
  171. "</td>
  172. </tr>" .
  173. Xml::closeElement( 'table' ) .
  174. Xml::closeElement( 'fieldset' ) .
  175. Xml::closeElement( 'form' );
  176. if ( $wgUser->isAllowed( 'editinterface' ) ) {
  177. $skin = $wgUser->getSkin();
  178. $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
  179. $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
  180. }
  181. $wgOut->addHTML( $form );
  182. }
  183. /**
  184. * Show deletion log fragments pertaining to the current file
  185. */
  186. private function showLogEntries() {
  187. global $wgOut;
  188. $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
  189. LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
  190. }
  191. /**
  192. * Prepare a message referring to the file being deleted,
  193. * showing an appropriate message depending upon whether
  194. * it's a current file or an old version
  195. *
  196. * @param string $message Message base
  197. * @return string
  198. */
  199. private function prepareMessage( $message ) {
  200. global $wgLang;
  201. if( $this->oldimage ) {
  202. $url = $this->file->getArchiveUrl( $this->oldimage );
  203. return wfMsgExt(
  204. "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
  205. 'parse',
  206. $this->title->getText(),
  207. $wgLang->date( $this->getTimestamp(), true ),
  208. $wgLang->time( $this->getTimestamp(), true ),
  209. wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
  210. } else {
  211. return wfMsgExt(
  212. $message,
  213. 'parse',
  214. $this->title->getText()
  215. );
  216. }
  217. }
  218. /**
  219. * Set headers, titles and other bits
  220. */
  221. private function setHeaders() {
  222. global $wgOut, $wgUser;
  223. $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
  224. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  225. $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
  226. }
  227. /**
  228. * Is the provided `oldimage` value valid?
  229. *
  230. * @return bool
  231. */
  232. public static function isValidOldSpec($oldimage) {
  233. return strlen( $oldimage ) >= 16
  234. && strpos( $oldimage, '/' ) === false
  235. && strpos( $oldimage, '\\' ) === false;
  236. }
  237. /**
  238. * Could we delete the file specified? If an `oldimage`
  239. * value was provided, does it correspond to an
  240. * existing, local, old version of this file?
  241. *
  242. * @return bool
  243. */
  244. public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
  245. return $oldimage
  246. ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
  247. : $file && $file->exists() && $file->isLocal();
  248. }
  249. /**
  250. * Prepare the form action
  251. *
  252. * @return string
  253. */
  254. private function getAction() {
  255. $q = array();
  256. $q[] = 'action=delete';
  257. if( $this->oldimage )
  258. $q[] = 'oldimage=' . urlencode( $this->oldimage );
  259. return $this->title->getLocalUrl( implode( '&', $q ) );
  260. }
  261. /**
  262. * Extract the timestamp of the old version
  263. *
  264. * @return string
  265. */
  266. private function getTimestamp() {
  267. return $this->oldfile->getTimestamp();
  268. }
  269. }