r42130 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r42129‎ | r42130 | r42131 >
Date:02:11, 16 October 2008
Author:aaron
Status:old (Comments)
Tags:
Comment:
Clean up author link code
Modified paths:
  • /trunk/extensions/CodeReview/CodeRepository.php (modified) (history)
  • /trunk/extensions/CodeReview/CodeRevisionAuthorLink.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/CodeRevisionAuthorLink.php
@@ -3,49 +3,49 @@
44 // Special:Code/MediaWiki/author/johndoe/link
55
66 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+
713 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");
1117 }
1218
1319 function execute() {
1420 global $wgOut, $wgRequest, $wgUser;
15 -
1621 if ( !$wgUser->isAllowed( 'codereview-link-user' ) ) {
1722 $wgOut->permissionRequired( 'codereview-link-user' );
1823 return;
1924 }
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();
2929 }
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' );
3138
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 -
4039 $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 ) {
4442 $form .= Xml::element( 'legend', array(), wfMsg( 'code-author-alterlink' ) );
4543 $additional = Xml::openElement( 'fieldset' ) .
4644 Xml::element( 'legend', array(), wfMsg( 'code-author-orunlink' ) ) .
4745 Xml::submitButton( wfMsg( 'code-author-unlink' ), array( 'name' => 'unlink' ) ) .
4846 Xml::closeElement( 'fieldset' );
49 - }
 47+ } else {
 48+ $form .= Xml::element( 'legend', array(), wfMsg( 'code-author-dolink' ) );
 49+ }
5050
5151 $form .= Xml::inputLabel( wfMsg( 'code-author-name' ), 'linktouser', 'username', 30, '') . ' ' .
5252 Xml::submitButton( wfMsg( 'ok' ), array( 'name' => 'newname') ) .
@@ -56,77 +56,36 @@
5757 $wgOut->addHtml( $this->linkStatus() . $form );
5858 }
5959
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>'
8676 );
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+ );
11490 }
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 - );
13291 }
13392 }
Index: trunk/extensions/CodeReview/CodeRepository.php
@@ -194,4 +194,60 @@
195195 }
196196 return false;
197197 }
 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+ }
198254 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r42233Cleanup r42130 and later revs:...brion23:22, 19 October 2008

Comments

#Comment by NicDumZ (talk | contribs)   06:16, 17 October 2008

that fixes r41641 :)

#Comment by Brion VIBBER (talk | contribs)   23:13, 19 October 2008

Should rename the functions to be clear they are about users...

#Comment by Brion VIBBER (talk | contribs)   23:22, 19 October 2008

Fixed in r42233

Status & tagging log