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