Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeRevisionCommitter
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 2
1482
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
72
 revisionUpdate
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
930
1<?php
2
3namespace MediaWiki\Extension\CodeReview\UI;
4
5use MediaWiki\Permissions\Authority;
6use RequestContext;
7use SpecialPage;
8
9class CodeRevisionCommitter extends CodeRevisionView {
10    public function execute() {
11        global $wgOut;
12
13        $performer = $this->performer;
14        $context = RequestContext::getMain();
15        $request = $context->getRequest();
16        $userToken = $context->getCsrfTokenSet();
17        if ( !$userToken->matchToken( $request->getVal( 'wpEditToken' ) ) ) {
18            $wgOut->addHTML( '<strong>' . wfMessage( 'sessionfailure' )->escaped() . '</strong>' );
19            parent::execute();
20            return;
21        }
22        if ( !$this->mRev ) {
23            parent::execute();
24            return;
25        }
26
27        $commentId = $this->revisionUpdate(
28            $this->mStatus,
29            $this->mAddTags,
30            $this->mRemoveTags,
31            $this->mSignoffFlags,
32            $this->mStrikeSignoffs,
33            $this->mAddReferences,
34            $this->mRemoveReferences,
35            $this->text,
36            $request->getIntOrNull( 'wpParent' ),
37            $this->mAddReferenced,
38            $this->mRemoveReferenced,
39            $performer
40        );
41
42        $redirTarget = null;
43
44        // For comments, take us back to the rev page focused on the new comment
45        if ( $commentId !== 0 && !$this->jumpToNext ) {
46            $redirTarget = $this->commentLink( $commentId );
47        }
48
49        // Return to rev page
50        if ( !$redirTarget ) {
51            // Was "next" (or "save & next") clicked?
52            if ( $this->jumpToNext ) {
53                $next = $this->mRev->getNextUnresolved( $this->mPath );
54                if ( $next ) {
55                    $redirTarget = SpecialPage::getTitleFor( 'Code', $this->mRepo->getName() . '/' . $next );
56                } else {
57                    $redirTarget = SpecialPage::getTitleFor( 'Code', $this->mRepo->getName() );
58                }
59            } else {
60                # $redirTarget already set for comments
61                $redirTarget = $this->revLink();
62            }
63        }
64        $wgOut->redirect( $redirTarget->getFullUrl( [ 'path' => $this->mPath ] ) );
65    }
66
67    /**
68     * Does the revision database update
69     *
70     * @param string $status Status to set the revision to
71     * @param array $addTags Tags to add to the revision
72     * @param array $removeTags Tags to remove from the Revision
73     * @param array $addSignoffs Sign-off flags to add
74     * @param array $strikeSignoffs Sign-off IDs to strike
75     * @param array $addReferences Revision IDs to add reference from
76     * @param array $removeReferences Revision IDs to remove references from
77     * @param string $commentText Comment to add to the revision
78     * @param null|int $parent What the parent comment is (if a subcomment)
79     * @param array $addReferenced
80     * @param array $removeReferenced
81     * @param Authority $performer
82     * @return int Comment ID if added, else 0
83     */
84    public function revisionUpdate( $status, $addTags, $removeTags, $addSignoffs, $strikeSignoffs,
85        $addReferences, $removeReferences, $commentText,
86        $parent, $addReferenced, $removeReferenced,
87        Authority $performer
88    ) {
89        if ( !$this->mRev ) {
90            return false;
91        }
92
93        $dbw = wfGetDB( DB_PRIMARY );
94        $dbw->startAtomic( __METHOD__ );
95
96        // Change the status if allowed
97        $statusChanged = false;
98        if ( $this->mRev->isValidStatus( $status ) &&
99            $this->validPost( 'codereview-set-status', $performer )
100        ) {
101            $statusChanged = $this->mRev->setStatus( $status, $performer );
102        }
103        $validAddTags = $validRemoveTags = [];
104        if ( count( $addTags ) && $this->validPost( 'codereview-add-tag', $performer ) ) {
105            $validAddTags = $addTags;
106        }
107        if ( count( $removeTags ) && $this->validPost( 'codereview-remove-tag', $performer ) ) {
108            $validRemoveTags = $removeTags;
109        }
110        // If allowed to change any tags, then do so
111        if ( count( $validAddTags ) || count( $validRemoveTags ) ) {
112            $this->mRev->changeTags( $validAddTags, $validRemoveTags, $performer );
113        }
114        // Add any signoffs
115        if ( count( $addSignoffs ) && $this->validPost( 'codereview-signoff', $performer ) ) {
116            $this->mRev->addSignoff( $performer, $addSignoffs );
117        }
118        // Strike any signoffs
119        if ( count( $strikeSignoffs ) && $this->validPost( 'codereview-signoff', $performer ) ) {
120            $this->mRev->strikeSignoffs( $performer, $strikeSignoffs );
121        }
122        // Add reference if requested
123        if ( count( $addReferences ) && $this->validPost( 'codereview-associate', $performer ) ) {
124            $this->mRev->addReferencesFrom( $addReferences );
125        }
126        // Remove references if requested
127        if ( count( $removeReferences ) &&
128            $this->validPost( 'codereview-associate', $performer )
129        ) {
130            $this->mRev->removeReferencesFrom( $removeReferences );
131        }
132        // Add reference if requested
133        if ( count( $addReferenced ) && $this->validPost( 'codereview-associate', $performer ) ) {
134            $this->mRev->addReferencesTo( $addReferenced );
135        }
136        // Remove references if requested
137        if ( count( $removeReferenced ) && $this->validPost( 'codereview-associate', $performer ) ) {
138            $this->mRev->removeReferencesTo( $removeReferenced );
139        }
140
141        // Add any comments
142        $commentAdded = false;
143        $commentId = 0;
144        if ( strlen( $commentText ) && $this->validPost( 'codereview-post-comment', $performer ) ) {
145            // $isPreview = $wgRequest->getCheck( 'wpPreview' );
146            $commentId = $this->mRev->saveComment( $commentText, $performer, $parent );
147
148            $commentAdded = ( $commentId !== 0 );
149        }
150
151        $dbw->endAtomic( __METHOD__ );
152
153        if ( $statusChanged || $commentAdded ) {
154            $url = $this->mRev->getCanonicalUrl( $commentId );
155            if ( $statusChanged && $commentAdded ) {
156                $this->mRev->emailNotifyUsersOfChanges(
157                    $performer,
158                    'codereview-email-subj4',
159                    'codereview-email-body4',
160                    $performer->getUser()->getName(),
161                    $this->mRev->getIdStringUnique(),
162                    $this->mRev->getOldStatus(),
163                    $this->mRev->getStatus(),
164                    $url,
165                    $this->text,
166                    $this->mRev->getMessage()
167                );
168            } elseif ( $statusChanged ) {
169                $this->mRev->emailNotifyUsersOfChanges(
170                    $performer,
171                    'codereview-email-subj3',
172                    'codereview-email-body3',
173                    $performer->getUser()->getName(),
174                    $this->mRev->getIdStringUnique(),
175                    $this->mRev->getOldStatus(),
176                    $this->mRev->getStatus(),
177                    $url,
178                    $this->mRev->getMessage()
179                );
180            } elseif ( $commentAdded ) {
181                $this->mRev->emailNotifyUsersOfChanges(
182                    $performer,
183                    'codereview-email-subj',
184                    'codereview-email-body',
185                    $performer->getUser()->getName(),
186                    $url,
187                    $this->mRev->getIdStringUnique(),
188                    $this->text,
189                    $this->mRev->getMessage()
190                );
191            }
192        }
193
194        return $commentId;
195    }
196}