PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/FileDeleteForm.php

https://github.com/daevid/MWFork
PHP | 335 lines | 256 code | 25 blank | 54 comment | 36 complexity | 2be3cbf1246e63c2ad8405938a428ef5 MD5 | raw file
  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 object 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. /**
  80. * Really delete the file
  81. *
  82. * @param $title Title object
  83. * @param $file File object
  84. * @param $oldimage String: archive name
  85. * @param $reason String: reason of the deletion
  86. * @param $suppress Boolean: whether to mark all deleted versions as restricted
  87. */
  88. public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
  89. global $wgUser;
  90. $article = null;
  91. $status = Status::newFatal( 'error' );
  92. if( $oldimage ) {
  93. $status = $file->deleteOld( $oldimage, $reason, $suppress );
  94. if( $status->ok ) {
  95. // Need to do a log item
  96. $log = new LogPage( 'delete' );
  97. $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
  98. if( trim( $reason ) != '' ) {
  99. $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
  100. }
  101. $log->addEntry( 'delete', $title, $logComment );
  102. }
  103. } else {
  104. $id = $title->getArticleID( Title::GAID_FOR_UPDATE );
  105. $article = new Article( $title );
  106. $dbw = wfGetDB( DB_MASTER );
  107. try {
  108. // delete the associated article first
  109. if( $article->doDeleteArticle( $reason, $suppress, $id, false ) ) {
  110. global $wgRequest;
  111. if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
  112. WatchAction::doWatch( $title, $wgUser );
  113. } elseif ( $title->userIsWatching() ) {
  114. WatchAction::doUnwatch( $title, $wgUser );
  115. }
  116. $status = $file->delete( $reason, $suppress );
  117. if( $status->ok ) {
  118. $dbw->commit();
  119. } else {
  120. $dbw->rollback();
  121. }
  122. }
  123. } catch ( MWException $e ) {
  124. // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
  125. $dbw->rollback();
  126. throw $e;
  127. }
  128. }
  129. if( $status->isGood() )
  130. wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
  131. return $status;
  132. }
  133. /**
  134. * Show the confirmation form
  135. */
  136. private function showForm() {
  137. global $wgOut, $wgUser, $wgRequest;
  138. if( $wgUser->isAllowed( 'suppressrevision' ) ) {
  139. $suppress = "<tr id=\"wpDeleteSuppressRow\">
  140. <td></td>
  141. <td class='mw-input'><strong>" .
  142. Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
  143. 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
  144. "</strong></td>
  145. </tr>";
  146. } else {
  147. $suppress = '';
  148. }
  149. $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
  150. $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
  151. 'id' => 'mw-img-deleteconfirm' ) ) .
  152. Xml::openElement( 'fieldset' ) .
  153. Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
  154. Html::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
  155. $this->prepareMessage( 'filedelete-intro' ) .
  156. Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
  157. "<tr>
  158. <td class='mw-label'>" .
  159. Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
  160. "</td>
  161. <td class='mw-input'>" .
  162. Xml::listDropDown( 'wpDeleteReasonList',
  163. wfMsgForContent( 'filedelete-reason-dropdown' ),
  164. wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
  165. "</td>
  166. </tr>
  167. <tr>
  168. <td class='mw-label'>" .
  169. Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
  170. "</td>
  171. <td class='mw-input'>" .
  172. Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
  173. array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
  174. "</td>
  175. </tr>
  176. {$suppress}";
  177. if( $wgUser->isLoggedIn() ) {
  178. $form .= "
  179. <tr>
  180. <td></td>
  181. <td class='mw-input'>" .
  182. Xml::checkLabel( wfMsg( 'watchthis' ),
  183. 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
  184. "</td>
  185. </tr>";
  186. }
  187. $form .= "
  188. <tr>
  189. <td></td>
  190. <td class='mw-submit'>" .
  191. Xml::submitButton( wfMsg( 'filedelete-submit' ),
  192. array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
  193. "</td>
  194. </tr>" .
  195. Xml::closeElement( 'table' ) .
  196. Xml::closeElement( 'fieldset' ) .
  197. Xml::closeElement( 'form' );
  198. if ( $wgUser->isAllowed( 'editinterface' ) ) {
  199. $skin = $wgUser->getSkin();
  200. $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
  201. $link = $skin->link(
  202. $title,
  203. wfMsgHtml( 'filedelete-edit-reasonlist' ),
  204. array(),
  205. array( 'action' => 'edit' )
  206. );
  207. $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
  208. }
  209. $wgOut->addHTML( $form );
  210. }
  211. /**
  212. * Show deletion log fragments pertaining to the current file
  213. */
  214. private function showLogEntries() {
  215. global $wgOut;
  216. $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
  217. LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
  218. }
  219. /**
  220. * Prepare a message referring to the file being deleted,
  221. * showing an appropriate message depending upon whether
  222. * it's a current file or an old version
  223. *
  224. * @param $message String: message base
  225. * @return String
  226. */
  227. private function prepareMessage( $message ) {
  228. global $wgLang;
  229. if( $this->oldimage ) {
  230. return wfMsgExt(
  231. "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
  232. 'parse',
  233. wfEscapeWikiText( $this->title->getText() ),
  234. $wgLang->date( $this->getTimestamp(), true ),
  235. $wgLang->time( $this->getTimestamp(), true ),
  236. wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
  237. } else {
  238. return wfMsgExt(
  239. $message,
  240. 'parse',
  241. wfEscapeWikiText( $this->title->getText() )
  242. );
  243. }
  244. }
  245. /**
  246. * Set headers, titles and other bits
  247. */
  248. private function setHeaders() {
  249. global $wgOut, $wgUser;
  250. $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
  251. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  252. $wgOut->setSubtitle( wfMsg(
  253. 'filedelete-backlink',
  254. $wgUser->getSkin()->link(
  255. $this->title,
  256. null,
  257. array(),
  258. array(),
  259. array( 'known', 'noclasses' )
  260. )
  261. ) );
  262. }
  263. /**
  264. * Is the provided `oldimage` value valid?
  265. *
  266. * @return bool
  267. */
  268. public static function isValidOldSpec($oldimage) {
  269. return strlen( $oldimage ) >= 16
  270. && strpos( $oldimage, '/' ) === false
  271. && strpos( $oldimage, '\\' ) === false;
  272. }
  273. /**
  274. * Could we delete the file specified? If an `oldimage`
  275. * value was provided, does it correspond to an
  276. * existing, local, old version of this file?
  277. *
  278. * @return bool
  279. */
  280. public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
  281. return $oldimage
  282. ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
  283. : $file && $file->exists() && $file->isLocal();
  284. }
  285. /**
  286. * Prepare the form action
  287. *
  288. * @return string
  289. */
  290. private function getAction() {
  291. $q = array();
  292. $q['action'] = 'delete';
  293. if( $this->oldimage )
  294. $q['oldimage'] = $this->oldimage;
  295. return $this->title->getLocalUrl( $q );
  296. }
  297. /**
  298. * Extract the timestamp of the old version
  299. *
  300. * @return string
  301. */
  302. private function getTimestamp() {
  303. return $this->oldfile->getTimestamp();
  304. }
  305. }