Completed
Branch master (1655eb)
by
unknown
22:24
created

MarkpatrolledAction::onSubmit()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 13
nop 1
dl 0
loc 40
rs 6.7272
c 0
b 0
f 0
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
23
/**
24
 * Mark a revision as patrolled on a page
25
 *
26
 * @ingroup Actions
27
 */
28
class MarkpatrolledAction extends FormAction {
29
30
	public function getName() {
31
		return 'markpatrolled';
32
	}
33
34
	protected function getDescription() {
35
		// Disable default header "subtitle"
36
		return '';
37
	}
38
39
	public function getRestriction() {
40
		return 'patrol';
41
	}
42
43
	protected function getRecentChange( $data = null ) {
44
		$rc = null;
45
		// Note: This works both on initial GET url and after submitting the form
46
		$rcId = $data ? intval( $data['rcid'] ) : $this->getRequest()->getInt( 'rcid' );
47
		if ( $rcId ) {
48
			$rc = RecentChange::newFromId( $rcId );
49
		}
50
		if ( !$rc ) {
51
			throw new ErrorPageError( 'markedaspatrollederror', 'markedaspatrollederrortext' );
52
		}
53
		return $rc;
54
	}
55
56
	protected function preText() {
57
		$rc = $this->getRecentChange();
58
		$title = $rc->getTitle();
59
60
		// Based on logentry-patrol-patrol (see PatrolLogFormatter)
61
		$revId = $rc->getAttribute( 'rc_this_oldid' );
62
		$query = [
63
			'curid' => $rc->getAttribute( 'rc_cur_id' ),
64
			'diff' => $revId,
65
			'oldid' => $rc->getAttribute( 'rc_last_oldid' )
66
		];
67
		$revlink = Linker::link( $title, htmlspecialchars( $revId ), [], $query );
68
		$pagelink = Linker::link( $title, htmlspecialchars( $title->getPrefixedText() ) );
69
70
		return $this->msg( 'confirm-markpatrolled-top' )->params(
71
			$title->getPrefixedText(),
72
			// Provide pre-rendered link as parser would render [[:$1]] as bold non-link
73
			Message::rawParam( $pagelink ),
74
			Message::rawParam( $revlink )
75
		)->parse();
76
	}
77
78
	protected function alterForm( HTMLForm $form ) {
79
		$form->addHiddenField( 'rcid', $this->getRequest()->getInt( 'rcid' ) );
80
		$form->setTokenSalt( 'patrol' );
81
		$form->setSubmitTextMsg( 'confirm-markpatrolled-button' );
82
	}
83
84
	/**
85
	 * @return bool|array True for success, false for didn't-try, array of errors on failure
86
	 */
87
	public function onSubmit( $data ) {
88
		$user = $this->getUser();
89
		$rc = $this->getRecentChange( $data );
90
		$errors = $rc->doMarkPatrolled( $user );
91
92
		if ( in_array( [ 'rcpatroldisabled' ], $errors ) ) {
93
			throw new ErrorPageError( 'rcpatroldisabled', 'rcpatroldisabledtext' );
94
		}
95
96
		// Guess where the user came from
97
		// TODO: Would be nice to see where the user actually came from
98
		if ( $rc->getAttribute( 'rc_type' ) == RC_NEW ) {
99
			$returnTo = 'Newpages';
100
		} elseif ( $rc->getAttribute( 'rc_log_type' ) == 'upload' ) {
101
			$returnTo = 'Newfiles';
102
		} else {
103
			$returnTo = 'Recentchanges';
104
		}
105
		$return = SpecialPage::getTitleFor( $returnTo );
106
107
		if ( in_array( [ 'markedaspatrollederror-noautopatrol' ], $errors ) ) {
108
			$this->getOutput()->setPageTitle( $this->msg( 'markedaspatrollederror' ) );
109
			$this->getOutput()->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
110
			$this->getOutput()->returnToMain( null, $return );
111
			return true;
112
		}
113
114
		if ( $errors ) {
115
			if ( !in_array( [ 'hookaborted' ], $errors ) ) {
116
				throw new PermissionsError( 'patrol', $errors );
117
			}
118
			// The hook itself has handled any output
119
			return $errors;
120
		}
121
122
		$this->getOutput()->setPageTitle( $this->msg( 'markedaspatrolled' ) );
123
		$this->getOutput()->addWikiMsg( 'markedaspatrolledtext', $rc->getTitle()->getPrefixedText() );
124
		$this->getOutput()->returnToMain( null, $return );
125
		return true;
126
	}
127
128
	public function onSuccess() {
129
		// Required by parent class. Redundant as our onSubmit handles output already.
130
	}
131
132
	public function doesWrites() {
133
		return true;
134
	}
135
}
136