Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / actions / MarkpatrolledAction.php
blob611e6837f5924d6b16a6addd62baf26cf868757b
1 <?php
2 /**
3 * Copyright © 2011 Alexandre Emsenhuber
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.
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.
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
19 * @file
20 * @ingroup Actions
23 use MediaWiki\MediaWikiServices;
25 /**
26 * Mark a revision as patrolled on a page
28 * @ingroup Actions
30 class MarkpatrolledAction extends FormAction {
32 public function getName() {
33 return 'markpatrolled';
36 protected function getDescription() {
37 // Disable default header "subtitle"
38 return '';
41 public function getRestriction() {
42 return 'patrol';
45 protected function getRecentChange( $data = null ) {
46 $rc = null;
47 // Note: This works both on initial GET url and after submitting the form
48 $rcId = $data ? intval( $data['rcid'] ) : $this->getRequest()->getInt( 'rcid' );
49 if ( $rcId ) {
50 $rc = RecentChange::newFromId( $rcId );
52 if ( !$rc ) {
53 throw new ErrorPageError( 'markedaspatrollederror', 'markedaspatrollederrortext' );
55 return $rc;
58 protected function preText() {
59 $rc = $this->getRecentChange();
60 $title = $rc->getTitle();
61 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
63 // Based on logentry-patrol-patrol (see PatrolLogFormatter)
64 $revId = $rc->getAttribute( 'rc_this_oldid' );
65 $query = [
66 'curid' => $rc->getAttribute( 'rc_cur_id' ),
67 'diff' => $revId,
68 'oldid' => $rc->getAttribute( 'rc_last_oldid' )
70 $revlink = $linkRenderer->makeLink( $title, $revId, [], $query );
71 $pagelink = $linkRenderer->makeLink( $title, $title->getPrefixedText() );
73 return $this->msg( 'confirm-markpatrolled-top' )->params(
74 $title->getPrefixedText(),
75 // Provide pre-rendered link as parser would render [[:$1]] as bold non-link
76 Message::rawParam( $pagelink ),
77 Message::rawParam( $revlink )
78 )->parse();
81 protected function alterForm( HTMLForm $form ) {
82 $form->addHiddenField( 'rcid', $this->getRequest()->getInt( 'rcid' ) );
83 $form->setTokenSalt( 'patrol' );
84 $form->setSubmitTextMsg( 'confirm-markpatrolled-button' );
87 /**
88 * @return bool|array True for success, false for didn't-try, array of errors on failure
90 public function onSubmit( $data ) {
91 $user = $this->getUser();
92 $rc = $this->getRecentChange( $data );
93 $errors = $rc->doMarkPatrolled( $user );
95 if ( in_array( [ 'rcpatroldisabled' ], $errors ) ) {
96 throw new ErrorPageError( 'rcpatroldisabled', 'rcpatroldisabledtext' );
99 // Guess where the user came from
100 // TODO: Would be nice to see where the user actually came from
101 if ( $rc->getAttribute( 'rc_type' ) == RC_NEW ) {
102 $returnTo = 'Newpages';
103 } elseif ( $rc->getAttribute( 'rc_log_type' ) == 'upload' ) {
104 $returnTo = 'Newfiles';
105 } else {
106 $returnTo = 'Recentchanges';
108 $return = SpecialPage::getTitleFor( $returnTo );
110 if ( in_array( [ 'markedaspatrollederror-noautopatrol' ], $errors ) ) {
111 $this->getOutput()->setPageTitle( $this->msg( 'markedaspatrollederror' ) );
112 $this->getOutput()->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
113 $this->getOutput()->returnToMain( null, $return );
114 return true;
117 if ( $errors ) {
118 if ( !in_array( [ 'hookaborted' ], $errors ) ) {
119 throw new PermissionsError( 'patrol', $errors );
121 // The hook itself has handled any output
122 return $errors;
125 $this->getOutput()->setPageTitle( $this->msg( 'markedaspatrolled' ) );
126 $this->getOutput()->addWikiMsg( 'markedaspatrolledtext', $rc->getTitle()->getPrefixedText() );
127 $this->getOutput()->returnToMain( null, $return );
128 return true;
131 public function onSuccess() {
132 // Required by parent class. Redundant as our onSubmit handles output already.
135 public function doesWrites() {
136 return true;