Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
MarkpatrolledAction
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 11
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRestriction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 usesOOUI
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRecentChange
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 preText
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
56
 onSuccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2011 Alexandre Emsenhuber
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Actions
8 */
9
10namespace MediaWiki\Actions;
11
12use MediaWiki\Context\IContextSource;
13use MediaWiki\Exception\ErrorPageError;
14use MediaWiki\Exception\PermissionsError;
15use MediaWiki\HTMLForm\HTMLForm;
16use MediaWiki\Linker\LinkRenderer;
17use MediaWiki\Message\Message;
18use MediaWiki\Page\Article;
19use MediaWiki\RecentChanges\PatrolManager;
20use MediaWiki\RecentChanges\RecentChange;
21use MediaWiki\RecentChanges\RecentChangeLookup;
22use MediaWiki\SpecialPage\SpecialPage;
23use StatusValue;
24
25/**
26 * Mark a revision as patrolled on a page
27 *
28 * @ingroup Actions
29 */
30class MarkpatrolledAction extends FormAction {
31
32    public function __construct(
33        Article $article,
34        IContextSource $context,
35        private readonly LinkRenderer $linkRenderer,
36        private readonly PatrolManager $patrolManager,
37        private readonly RecentChangeLookup $recentChangeLookup,
38    ) {
39        parent::__construct( $article, $context );
40    }
41
42    /** @inheritDoc */
43    public function getName() {
44        return 'markpatrolled';
45    }
46
47    /** @inheritDoc */
48    protected function getDescription() {
49        // Disable default header "subtitle"
50        return '';
51    }
52
53    /** @inheritDoc */
54    public function getRestriction() {
55        return 'patrol';
56    }
57
58    /** @inheritDoc */
59    protected function usesOOUI() {
60        return true;
61    }
62
63    /**
64     * @param array|null $data
65     * @return RecentChange
66     */
67    protected function getRecentChange( $data = null ) {
68        $rc = null;
69        // Note: This works both on initial GET url and after submitting the form
70        $rcId = $data ? intval( $data['rcid'] ) : $this->getRequest()->getInt( 'rcid' );
71        if ( $rcId ) {
72            $rc = $this->recentChangeLookup->getRecentChangeById( $rcId );
73        }
74        if ( !$rc ) {
75            throw new ErrorPageError( 'markedaspatrollederror', 'markedaspatrollederrortext' );
76        }
77        return $rc;
78    }
79
80    /** @inheritDoc */
81    protected function preText() {
82        $rc = $this->getRecentChange();
83        $title = $rc->getTitle();
84
85        // Based on logentry-patrol-patrol (see PatrolLogFormatter)
86        $revId = $rc->getAttribute( 'rc_this_oldid' );
87        $query = [
88            'curid' => $rc->getAttribute( 'rc_cur_id' ),
89            'diff' => $revId,
90            'oldid' => $rc->getAttribute( 'rc_last_oldid' )
91        ];
92        $revlink = $this->linkRenderer->makeLink( $title, $revId, [], $query );
93        $pagelink = $this->linkRenderer->makeLink( $title, $title->getPrefixedText() );
94
95        return $this->msg( 'confirm-markpatrolled-top' )->params(
96            $title->getPrefixedText(),
97            // Provide pre-rendered link as parser would render [[:$1]] as bold non-link
98            Message::rawParam( $pagelink ),
99            Message::rawParam( $revlink )
100        )->parse();
101    }
102
103    protected function alterForm( HTMLForm $form ) {
104        $form->addHiddenField( 'rcid', $this->getRequest()->getInt( 'rcid' ) );
105        $form->setTokenSalt( 'patrol' );
106        $form->setSubmitTextMsg( 'confirm-markpatrolled-button' );
107    }
108
109    /**
110     * @param array $data
111     * @return bool|StatusValue True for success, false for didn't-try, StatusValue on failure
112     */
113    public function onSubmit( $data ) {
114        $rc = $this->getRecentChange( $data );
115        $status = $this->patrolManager->markPatrolled( $rc, $this->getAuthority() );
116
117        if ( $status->hasMessage( 'rcpatroldisabled' ) ) {
118            throw new ErrorPageError( 'rcpatroldisabled', 'rcpatroldisabledtext' );
119        }
120
121        // Guess where the user came from
122        // TODO: Would be nice to see where the user actually came from
123        if ( $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_NEW ) {
124            $returnTo = 'Newpages';
125        } elseif ( $rc->getAttribute( 'rc_log_type' ) == 'upload' ) {
126            $returnTo = 'Newfiles';
127        } else {
128            $returnTo = 'Recentchanges';
129        }
130        $return = SpecialPage::getTitleFor( $returnTo );
131
132        if ( $status->hasMessage( 'markedaspatrollederror-noautopatrol' ) ) {
133            $this->getOutput()->setPageTitleMsg( $this->msg( 'markedaspatrollederror' ) );
134            $this->getOutput()->addWikiMsg( 'markedaspatrollederror-noautopatrol' );
135            $this->getOutput()->returnToMain( null, $return );
136            return true;
137        }
138
139        if ( !$status->isGood() ) {
140            if ( !$status->hasMessage( 'hookaborted' ) ) {
141                throw new PermissionsError( 'patrol', $status );
142            }
143            // The MarkPatrolled hook itself has handled any output
144            return $status;
145        }
146
147        $this->getOutput()->setPageTitleMsg( $this->msg( 'markedaspatrolled' ) );
148        $this->getOutput()->addWikiMsg( 'markedaspatrolledtext', $rc->getTitle()->getPrefixedText() );
149        $this->getOutput()->returnToMain( null, $return );
150        return true;
151    }
152
153    /** @inheritDoc */
154    public function onSuccess() {
155        // Required by parent class. Redundant as our onSubmit handles output already.
156    }
157
158    /** @inheritDoc */
159    public function doesWrites() {
160        return true;
161    }
162}
163
164/** @deprecated class alias since 1.44 */
165class_alias( MarkpatrolledAction::class, 'MarkpatrolledAction' );