Index: trunk/extensions/CodeReview/CodeRevisionAuthorLink.php |
— | — | @@ -3,49 +3,49 @@ |
4 | 4 | // Special:Code/MediaWiki/author/johndoe/link |
5 | 5 | |
6 | 6 | class CodeRevisionAuthorLink extends CodeRevisionAuthorView { |
| 7 | + function __construct( $repoName, $author ) { |
| 8 | + global $wgRequest; |
| 9 | + parent::__construct( $repoName, $author ); |
| 10 | + $this->mTarget = $wgRequest->getVal( 'linktouser' ); |
| 11 | + } |
| 12 | + |
7 | 13 | function getTitle() { |
8 | | - $repo = $this->mRepo->getName(); |
9 | | - $auth = $this->mAuthor; |
10 | | - return SpecialPage::getTitleFor( 'Code', "$repo/author/$auth/link"); |
| 14 | + $repo = $this->mRepo->getName(); |
| 15 | + $auth = $this->mAuthor; |
| 16 | + return SpecialPage::getTitleFor( 'Code', "$repo/author/$auth/link"); |
11 | 17 | } |
12 | 18 | |
13 | 19 | function execute() { |
14 | 20 | global $wgOut, $wgRequest, $wgUser; |
15 | | - |
16 | 21 | if ( !$wgUser->isAllowed( 'codereview-link-user' ) ) { |
17 | 22 | $wgOut->permissionRequired( 'codereview-link-user' ); |
18 | 23 | return; |
19 | 24 | } |
20 | | - |
21 | | - $target = $wgRequest->getVal( 'linktouser' ); |
22 | | - |
23 | | - if ( $target && $wgRequest->getVal( 'newname' ) ) { |
24 | | - $this->linkTo( $target ); |
25 | | - return; |
26 | | - } else if ( $wgRequest->getVal( 'unlink' ) ) { |
27 | | - $this->unlink(); |
28 | | - return; |
| 25 | + if ( $wgRequest->wasPosted() ) { |
| 26 | + $this->doSubmit(); |
| 27 | + } else { |
| 28 | + $this->doForm(); |
29 | 29 | } |
30 | | - |
| 30 | + } |
| 31 | + |
| 32 | + function doForm() { |
| 33 | + global $wgOut; |
| 34 | + $form = Xml::openElement( 'form', array( 'method' => 'post', |
| 35 | + 'action' => $this->getTitle()->getLocalUrl(), |
| 36 | + 'name' => 'uluser', 'id' => 'mw-codeauthor-form1' ) ); |
| 37 | + $form .= Xml::openElement( 'fieldset' ); |
31 | 38 | |
32 | | - $form = Xml::openElement( 'form', |
33 | | - array( 'method' => 'get', |
34 | | - 'action' => $this->getTitle()->getLocalUrl(), |
35 | | - 'name' => 'uluser', |
36 | | - 'id' => 'mw-codeauthor-form1' |
37 | | - ) |
38 | | - ) . Xml::openElement( 'fieldset' ); |
39 | | - |
40 | 39 | $additional = ''; |
41 | | - if ( !$this->mUser ) |
42 | | - $form .= Xml::element( 'legend', array(), wfMsg( 'code-author-dolink' ) ); |
43 | | - else { |
| 40 | + // Is there already a user linked to this author? |
| 41 | + if ( $this->mUser ) { |
44 | 42 | $form .= Xml::element( 'legend', array(), wfMsg( 'code-author-alterlink' ) ); |
45 | 43 | $additional = Xml::openElement( 'fieldset' ) . |
46 | 44 | Xml::element( 'legend', array(), wfMsg( 'code-author-orunlink' ) ) . |
47 | 45 | Xml::submitButton( wfMsg( 'code-author-unlink' ), array( 'name' => 'unlink' ) ) . |
48 | 46 | Xml::closeElement( 'fieldset' ); |
49 | | - } |
| 47 | + } else { |
| 48 | + $form .= Xml::element( 'legend', array(), wfMsg( 'code-author-dolink' ) ); |
| 49 | + } |
50 | 50 | |
51 | 51 | $form .= Xml::inputLabel( wfMsg( 'code-author-name' ), 'linktouser', 'username', 30, '') . ' ' . |
52 | 52 | Xml::submitButton( wfMsg( 'ok' ), array( 'name' => 'newname') ) . |
— | — | @@ -56,77 +56,36 @@ |
57 | 57 | $wgOut->addHtml( $this->linkStatus() . $form ); |
58 | 58 | } |
59 | 59 | |
60 | | - /* |
61 | | - * Link the author to the wikiuser $name |
62 | | - */ |
63 | | - function linkTo ( $name ) { |
64 | | - global $wgOut, $wgUser; |
65 | | - |
66 | | - if( $name == '' ) { |
67 | | - $wgOut->addWikiMsg( 'nouserspecified' ); |
68 | | - return; |
69 | | - } |
70 | | - $user = User::newFromName( $name ); |
71 | | - |
72 | | - if( !$user || $user->isAnon() ) { |
73 | | - $wgOut->addWikiMsg( 'nosuchusershort', $name ); |
74 | | - return; |
75 | | - } |
76 | | - $dbw = wfGetDB( DB_MASTER ); |
77 | | - if ( !$this->mUser ) |
78 | | - $dbw->insert( |
79 | | - 'code_authors', |
80 | | - array( |
81 | | - 'ca_repo_id' => $this->mRepo->getId(), |
82 | | - 'ca_author' => $this->mAuthor, |
83 | | - 'ca_user_text' => $user->getName() |
84 | | - ), |
85 | | - __METHOD__ |
| 60 | + function doSubmit() { |
| 61 | + global $wgOut, $wgRequest; |
| 62 | + // Link an author to a wiki user |
| 63 | + if ( strlen($this->mTarget) && $wgRequest->getCheck( 'newname' ) ) { |
| 64 | + $user = User::newFromName( $this->mTarget, false ); |
| 65 | + if( !$user || !$user->getId() ) { |
| 66 | + $wgOut->addWikiMsg( 'nosuchusershort', $name ); |
| 67 | + return; |
| 68 | + } |
| 69 | + $this->mRepo->linkTo( $this->mAuthor, $user ); |
| 70 | + $userlink = $this->mSkin->userLink( $user->getId(), $user->getName() ); |
| 71 | + parent::$userLinks[$this->mAuthor] = $user; |
| 72 | + $wgOut->addHtml( |
| 73 | + '<div class="successbox">' . |
| 74 | + wfMsgHtml( 'code-author-success', $this->authorLink( $this->mAuthor ), $userlink) . |
| 75 | + '</div>' |
86 | 76 | ); |
87 | | - else |
88 | | - $dbw->update( |
89 | | - 'code_authors', |
90 | | - array( 'ca_user_text' => $user->getName()), |
91 | | - array( |
92 | | - 'ca_repo_id' => $this->mRepo->getId(), |
93 | | - 'ca_author' => $this->mAuthor, |
94 | | - ), |
95 | | - __METHOD__ |
96 | | - ); |
97 | | - |
98 | | - $userlink = $this->mSkin->userLink( $user->getId(), $user->getName() ); |
99 | | - |
100 | | - parent::$userLinks[$this->mAuthor] = $user; |
101 | | - |
102 | | - $wgOut->addHtml( |
103 | | - '<div class="successbox">' . |
104 | | - wfMsgHtml( 'code-author-success', $this->authorLink( $this->mAuthor ), $userlink) . |
105 | | - '</div>' |
106 | | - ); |
107 | | - } |
108 | | - |
109 | | - function unlink() { |
110 | | - global $wgOut; |
111 | | - if ( !$this->mUser ) { |
112 | | - $wgOut->addHtml( wfMsg( 'code-author-orphan' ) ); |
113 | | - return; |
| 77 | + // Unlink an author to a wiki users |
| 78 | + } else if ( $wgRequest->getVal( 'unlink' ) ) { |
| 79 | + if ( !$this->mUser ) { |
| 80 | + $wgOut->addHtml( wfMsg( 'code-author-orphan' ) ); |
| 81 | + return; |
| 82 | + } |
| 83 | + $this->mRepo->unlink( $this->mAuthor ); |
| 84 | + parent::$userLinks[$this->mAuthor] = false; |
| 85 | + $wgOut->addHtml( |
| 86 | + '<div class="successbox">' . |
| 87 | + wfMsgHtml( 'code-author-unlinksuccess', $this->authorLink( $this->mAuthor ) ) . |
| 88 | + '</div>' |
| 89 | + ); |
114 | 90 | } |
115 | | - $dbw = wfGetDB( DB_MASTER ); |
116 | | - $dbw->delete( |
117 | | - 'code_authors', |
118 | | - array( |
119 | | - 'ca_repo_id' => $this->mRepo->getId(), |
120 | | - 'ca_author' => $this->mAuthor, |
121 | | - ), |
122 | | - __METHOD__ |
123 | | - ); |
124 | | - |
125 | | - parent::$userLinks[$this->mAuthor] = false; |
126 | | - |
127 | | - $wgOut->addHtml( |
128 | | - '<div class="successbox">' . |
129 | | - wfMsgHtml( 'code-author-unlinksuccess', $this->authorLink( $this->mAuthor ) ) . |
130 | | - '</div>' |
131 | | - ); |
132 | 91 | } |
133 | 92 | } |
Index: trunk/extensions/CodeReview/CodeRepository.php |
— | — | @@ -194,4 +194,60 @@ |
195 | 195 | } |
196 | 196 | return false; |
197 | 197 | } |
| 198 | + |
| 199 | + /* |
| 200 | + * Link the $author to the wikiuser $user |
| 201 | + * @param string $author |
| 202 | + * @param User $user |
| 203 | + * @return bool success |
| 204 | + */ |
| 205 | + function linkTo( $author, User $user ) { |
| 206 | + // We must link to an existing user |
| 207 | + if( !$user->getId() ) { |
| 208 | + return false; |
| 209 | + } |
| 210 | + $dbw = wfGetDB( DB_MASTER ); |
| 211 | + // Insert in the auther -> user link row. |
| 212 | + // Skip existing rows. |
| 213 | + $dbw->insert( 'code_authors', |
| 214 | + array( |
| 215 | + 'ca_repo_id' => $this->getId(), |
| 216 | + 'ca_author' => $author, |
| 217 | + 'ca_user_text' => $user->getName() |
| 218 | + ), |
| 219 | + __METHOD__, |
| 220 | + array( 'IGNORE' ) |
| 221 | + ); |
| 222 | + // If the last query already found a row, then update it. |
| 223 | + if( !$dbw->affectedRows() ) { |
| 224 | + $dbw->update( |
| 225 | + 'code_authors', |
| 226 | + array( 'ca_user_text' => $user->getName() ), |
| 227 | + array( |
| 228 | + 'ca_repo_id' => $this->getId(), |
| 229 | + 'ca_author' => $author, |
| 230 | + ), |
| 231 | + __METHOD__ |
| 232 | + ); |
| 233 | + } |
| 234 | + return ( $dbw->affectedRows() > 0 ); |
| 235 | + } |
| 236 | + |
| 237 | + /* |
| 238 | + * Link the $author to the wikiuser $user |
| 239 | + * @param string $author |
| 240 | + * @return bool success |
| 241 | + */ |
| 242 | + function unlink( $author ) { |
| 243 | + $dbw = wfGetDB( DB_MASTER ); |
| 244 | + $dbw->delete( |
| 245 | + 'code_authors', |
| 246 | + array( |
| 247 | + 'ca_repo_id' => $this->getId(), |
| 248 | + 'ca_author' => $author, |
| 249 | + ), |
| 250 | + __METHOD__ |
| 251 | + ); |
| 252 | + return ( $dbw->affectedRows() > 0 ); |
| 253 | + } |
198 | 254 | } |