MarkpatrolledAction.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © 2011 Alexandre Emsenhuber
  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
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *
  19. * @file
  20. * @ingroup Actions
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Mark a revision as patrolled on a page
  25. *
  26. * @ingroup Actions
  27. */
  28. class MarkpatrolledAction extends FormAction {
  29. public function getName() {
  30. return 'markpatrolled';
  31. }
  32. protected function getDescription() {
  33. // Disable default header "subtitle"
  34. return '';
  35. }
  36. public function getRestriction() {
  37. return 'patrol';
  38. }
  39. protected function usesOOUI() {
  40. return true;
  41. }
  42. protected function getRecentChange( $data = null ) {
  43. $rc = null;
  44. // Note: This works both on initial GET url and after submitting the form
  45. $rcId = $data ? intval( $data['rcid'] ) : $this->getRequest()->getInt( 'rcid' );
  46. if ( $rcId ) {
  47. $rc = RecentChange::newFromId( $rcId );
  48. }
  49. if ( !$rc ) {
  50. throw new ErrorPageError( 'markedaspatrollederror', 'markedaspatrollederrortext' );
  51. }
  52. return $rc;
  53. }
  54. protected function preText() {
  55. $rc = $this->getRecentChange();
  56. $title = $rc->getTitle();
  57. $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  58. // Based on logentry-patrol-patrol (see PatrolLogFormatter)
  59. $revId = $rc->getAttribute( 'rc_this_oldid' );
  60. $query = [
  61. 'curid' => $rc->getAttribute( 'rc_cur_id' ),
  62. 'diff' => $revId,
  63. 'oldid' => $rc->getAttribute( 'rc_last_oldid' )
  64. ];
  65. $revlink = $linkRenderer->makeLink( $title, $revId, [], $query );
  66. $pagelink = $linkRenderer->makeLink( $title, $title->getPrefixedText() );
  67. return $this->msg( 'confirm-markpatrolled-top' )->params(
  68. $title->getPrefixedText(),
  69. // Provide pre-rendered link as parser would render [[:$1]] as bold non-link
  70. Message::rawParam( $pagelink ),
  71. Message::rawParam( $revlink )
  72. )->parse();
  73. }
  74. protected function alterForm( HTMLForm $form ) {
  75. $form->addHiddenField( 'rcid', $this->getRequest()->getInt( 'rcid' ) );
  76. $form->setTokenSalt( 'patrol' );
  77. $form->setSubmitTextMsg( 'confirm-markpatrolled-button' );
  78. }
  79. /**
  80. * @param array $data
  81. * @return bool|array True for success, false for didn't-try, array of errors on failure
  82. */
  83. public function onSubmit( $data ) {
  84. $user = $this->getUser();
  85. $rc = $this->getRecentChange( $data );
  86. $errors = $rc->doMarkPatrolled( $user );
  87. if ( in_array( [ 'rcpatroldisabled' ], $errors ) ) {
  88. throw new ErrorPageError( 'rcpatroldisabled', 'rcpatroldisabledtext' );
  89. }
  90. // Guess where the user came from
  91. // TODO: Would be nice to see where the user actually came from
  92. if ( $rc->getAttribute( 'rc_type' ) == RC_NEW ) {
  93. $returnTo = 'Newpages';
  94. } elseif ( $rc->getAttribute( 'rc_log_type' ) == 'upload' ) {
  95. $returnTo = 'Newfiles';
  96. } else {
  97. $returnTo = 'Recentchanges';
  98. }
  99. $return = SpecialPage::getTitleFor( $returnTo );
  100. if ( in_array( [ 'markedaspatrollederror-noautopatrol' ], $errors ) ) {
  101. $this->getOutput()->setPageTitle( $this->msg( 'markedaspatrollederror' ) );
  102. $this->getOutput()->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
  103. $this->getOutput()->returnToMain( null, $return );
  104. return true;
  105. }
  106. if ( $errors ) {
  107. if ( !in_array( [ 'hookaborted' ], $errors ) ) {
  108. throw new PermissionsError( 'patrol', $errors );
  109. }
  110. // The hook itself has handled any output
  111. return $errors;
  112. }
  113. $this->getOutput()->setPageTitle( $this->msg( 'markedaspatrolled' ) );
  114. $this->getOutput()->addWikiMsg( 'markedaspatrolledtext', $rc->getTitle()->getPrefixedText() );
  115. $this->getOutput()->returnToMain( null, $return );
  116. return true;
  117. }
  118. public function onSuccess() {
  119. // Required by parent class. Redundant as our onSubmit handles output already.
  120. }
  121. public function doesWrites() {
  122. return true;
  123. }
  124. }