Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeRevisionAuthorLink
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 doForm
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 doSubmit
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3namespace MediaWiki\Extension\CodeReview\UI;
4
5use Html;
6use Linker;
7use PermissionsError;
8use SpecialPage;
9use User;
10use Xml;
11
12/**
13 * Special:Code/MediaWiki/author/johndoe/link
14 */
15class CodeRevisionAuthorLink extends CodeRevisionAuthorView {
16    /** @var User */
17    private $user;
18
19    public function __construct( $repo, $author, User $user ) {
20        global $wgRequest;
21        parent::__construct( $repo, $author );
22        $this->mTarget = $wgRequest->getVal( 'linktouser' );
23        $this->user = $user;
24    }
25
26    public function getTitle() {
27        $repo = $this->mRepo->getName();
28        $auth = $this->mAuthor;
29        return SpecialPage::getTitleFor( 'Code', "$repo/author/$auth/link" );
30    }
31
32    public function execute() {
33        global $wgRequest;
34
35        if ( !$this->user->isAllowed( 'codereview-link-user' ) ) {
36            throw new PermissionsError( 'codereview-link-user' );
37        }
38
39        if ( $wgRequest->wasPosted() ) {
40            $this->doSubmit();
41        } else {
42            $this->doForm();
43        }
44    }
45
46    private function doForm() {
47        global $wgOut;
48
49        $form = Xml::openElement( 'form', [ 'method' => 'post',
50            'action' => $this->getTitle()->getLocalURL(),
51            'name' => 'uluser', 'id' => 'mw-codeauthor-form1' ] );
52
53        $form .= Html::hidden( 'linktoken', $this->user->getEditToken( 'link' ) );
54        $form .= Xml::openElement( 'fieldset' );
55
56        $additional = '';
57        // Is there already a user linked to this author?
58        if ( $this->mUser ) {
59            $form .= Xml::element( 'legend', [], wfMessage( 'code-author-alterlink' )->text() );
60            $additional = Xml::openElement( 'fieldset' ) .
61                Xml::element( 'legend', [], wfMessage( 'code-author-orunlink' )->text() ) .
62                Xml::submitButton( wfMessage( 'code-author-unlink' )->text(), [ 'name' => 'unlink' ] ) .
63                Xml::closeElement( 'fieldset' );
64        } else {
65            $form .= Xml::element( 'legend', [], wfMessage( 'code-author-dolink' )->text() );
66        }
67
68        $form .= Xml::inputLabel( wfMessage( 'code-author-name' )->text(),
69            'linktouser', 'username', 30, '', [ 'class' => 'mw-autocomplete-user' ] ) . ' ' .
70                Xml::submitButton( wfMessage( 'ok' )->text(), [ 'name' => 'newname' ] ) .
71                Xml::closeElement( 'fieldset' ) .
72                $additional .
73                Xml::closeElement( 'form' ) . "\n";
74
75        $wgOut->addModules( 'mediawiki.userSuggest' );
76        $wgOut->addHTML( $this->linkStatus() . $form );
77    }
78
79    private function doSubmit() {
80        global $wgOut, $wgRequest;
81        // Link an author to a wiki user
82
83        if ( !$this->user->matchEditToken( $wgRequest->getVal( 'linktoken' ), 'link' ) ) {
84            $wgOut->addWikiMsg( 'code-author-badtoken' );
85            return;
86        }
87
88        if ( strlen( $this->mTarget ) && $wgRequest->getCheck( 'newname' ) ) {
89            $user = User::newFromName( $this->mTarget, false );
90            if ( !$user || !$user->getId() ) {
91                $wgOut->addWikiMsg( 'nosuchusershort', $this->mTarget );
92                return;
93            }
94            $this->mRepo->linkUser( $this->mAuthor, $user );
95            $userlink = Linker::userLink( $user->getId(), $user->getName() );
96            $wgOut->addHTML(
97                '<div class="successbox">' .
98                    wfMessage( 'code-author-success' )
99                        ->rawParams( $this->authorLink( $this->mAuthor ), $userlink )->escaped() .
100                '</div>'
101            );
102        // Unlink an author to a wiki users
103        } elseif ( $wgRequest->getVal( 'unlink' ) ) {
104            if ( !$this->mUser ) {
105                $wgOut->addHTML( wfMessage( 'code-author-orphan' )
106                    ->rawParams( $this->authorLink( $this->mAuthor ) )->escaped()
107                );
108                return;
109            }
110            $this->mRepo->unlinkUser( $this->mAuthor );
111            $wgOut->addHTML( '<div class="successbox">' .
112                    wfMessage( 'code-author-unlinksuccess' )
113                        ->rawParams( $this->authorLink( $this->mAuthor ) )
114                        ->escaped() .
115                '</div>'
116            );
117        }
118    }
119}