r62708 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62707‎ | r62708 | r62709 >
Date:15:26, 19 February 2010
Author:ashley
Status:deferred
Tags:
Comment:
ChangeAuthor: more braces, spacing tweaks, else if -> elseif
Modified paths:
  • /trunk/extensions/ChangeAuthor/ChangeAuthor.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ChangeAuthor/ChangeAuthor.body.php
@@ -30,7 +30,7 @@
3131 /**
3232 * Constructor
3333 */
34 - public function __construct(){
 34+ public function __construct() {
3535 global $wgUser;
3636 wfLoadExtensionMessages( 'ChangeAuthor' );
3737 parent::__construct( 'ChangeAuthor'/* class */, 'changeauthor'/* restriction */ );
@@ -43,43 +43,45 @@
4444 *
4545 * @param $par Mixed: parameter passed to the page or null
4646 */
47 - public function execute( $par ){
 47+ public function execute( $par ) {
4848 global $wgRequest, $wgOut, $wgContLang, $wgUser;
 49+
4950 $this->setHeaders();
5051
5152 // Check permissions
52 - if( !$this->userCanExecute( $wgUser ) ){
 53+ if( !$this->userCanExecute( $wgUser ) ) {
5354 $this->displayRestrictionError();
5455 return;
5556 }
5657
5758 $wgOut->setPageTitle( wfMsg( 'changeauthor-title' ) );
5859
59 - if( !is_null( $par ) ){
 60+ if( !is_null( $par ) ) {
6061 $obj = $this->parseTitleOrRevID( $par );
61 - if( $obj instanceof Title ){
62 - if( $obj->exists() )
 62+ if( $obj instanceof Title ) {
 63+ if( $obj->exists() ) {
6364 $wgOut->addHTML( $this->buildRevisionList( $obj ) );
64 - else
 65+ } else {
6566 $wgOut->addHTML( $this->buildInitialForm( wfMsg( 'changeauthor-nosuchtitle', $obj->getPrefixedText() ) ) );
 67+ }
6668 return;
67 - } else if( $obj instanceof Revision ){
 69+ } elseif( $obj instanceof Revision ) {
6870 $wgOut->addHTML( $this->buildOneRevForm( $obj ) );
6971 return;
7072 }
7173 }
7274
7375 $action = $wgRequest->getVal( 'action' );
74 - if( $wgRequest->wasPosted() && $action == 'change' ){
 76+ if( $wgRequest->wasPosted() && $action == 'change' ) {
7577 $arr = $this->parseChangeRequest();
76 - if( !is_array( $arr ) ){
 78+ if( !is_array( $arr ) ) {
7779 $targetPage = $wgRequest->getVal( 'targetpage' );
78 - if( !is_null( $targetPage ) ){
 80+ if( !is_null( $targetPage ) ) {
7981 $wgOut->addHTML( $this->buildRevisionList( Title::newFromURL( $targetPage ), $arr ) );
8082 return;
8183 }
8284 $targetRev = $wgRequest->getVal( 'targetrev' );
83 - if( !is_null( $targetRev ) ){
 85+ if( !is_null( $targetRev ) ) {
8486 $wgOut->addHTML( $this->buildOneRevForm( Revision::newFromId( $targetRev ), $arr ) );
8587 return;
8688 }
@@ -90,14 +92,15 @@
9193 }
9294 return;
9395 }
94 - if( $wgRequest->wasPosted() && $action == 'list' ){
 96+ if( $wgRequest->wasPosted() && $action == 'list' ) {
9597 $obj = $this->parseTitleOrRevID( $wgRequest->getVal( 'pagename-revid' ) );
96 - if( $obj instanceof Title ){
97 - if( $obj->exists() )
 98+ if( $obj instanceof Title ) {
 99+ if( $obj->exists() ) {
98100 $wgOut->addHTML( $this->buildRevisionList( $obj ) );
99 - else
 101+ } else {
100102 $wgOut->addHTML( $this->buildInitialForm( wfMsg( 'changeauthor-nosuchtitle', $obj->getPrefixedText() ) ) );
101 - } else if( $obj instanceof Revision ){
 103+ }
 104+ } elseif( $obj instanceof Revision ) {
102105 $wgOut->addHTML( $this->buildOneRevForm( $obj ) );
103106 }
104107 return;
@@ -110,12 +113,14 @@
111114 * @param $str Mixed: revision ID or an article name
112115 * @return Title or Revision object, or NULL
113116 */
114 - private function parseTitleOrRevID( $str ){
 117+ private function parseTitleOrRevID( $str ) {
115118 $retval = false;
116 - if( is_numeric( $str ) )
 119+ if( is_numeric( $str ) ) {
117120 $retval = Revision::newFromID( $str );
118 - if( !$retval )
 121+ }
 122+ if( !$retval ) {
119123 $retval = Title::newFromURL( $str );
 124+ }
120125 return $retval;
121126 }
122127
@@ -124,7 +129,7 @@
125130 * @param $errMsg String: Error message
126131 * @return HTML
127132 */
128 - private function buildInitialForm( $errMsg = '' ){
 133+ private function buildInitialForm( $errMsg = '' ) {
129134 global $wgScript;
130135 $retval = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $wgScript ) );
131136 $retval .= Xml::hidden( 'title', $this->selfTitle->getPrefixedDBkey() );
@@ -134,7 +139,7 @@
135140 $retval .= Xml::inputLabel( wfMsg( 'changeauthor-pagename-or-revid' ),
136141 'pagename-revid', 'pagename-revid' );
137142 $retval .= Xml::submitButton( wfMsg( 'changeauthor-pagenameform-go' ) );
138 - if( $errMsg != '' ){
 143+ if( $errMsg != '' ) {
139144 $retval .= Xml::openElement( 'p' ) . Xml::openElement( 'b' );
140145 $retval .= Xml::element( 'font', array( 'color' => 'red' ), $errMsg );
141146 $retval .= Xml::closeElement( 'b' ) . Xml::closeElement( 'p' );
@@ -154,43 +159,48 @@
155160 * @param $isLast Boolean: set to true if $rev is the last revision
156161 * @return HTML
157162 */
158 - private function buildRevisionLine( $rev, $title, $isFirst = false, $isLast = false ){
 163+ private function buildRevisionLine( $rev, $title, $isFirst = false, $isLast = false ) {
159164 // Build curlink
160 - if( $isFirst )
 165+ if( $isFirst ) {
161166 $curLink = wfMsgExt( 'cur', 'escape' );
162 - else
 167+ } else {
163168 $curLink = $this->skin->makeKnownLinkObj( $title,
164169 wfMsgExt( 'cur', 'escape' ),
165170 "oldid={$rev->getId()}&diff=cur" );
 171+ }
166172
167 - if( $isLast )
 173+ if( $isLast ) {
168174 $lastLink = wfMsgExt( 'last', 'escape' );
169 - else
 175+ } else {
170176 $lastLink = $this->skin->makeKnownLinkObj( $title,
171177 wfMsgExt( 'last', 'escape' ),
172178 "oldid=prev&diff={$rev->getId()}" );
 179+ }
173180
174181 // Build oldid link
175182 global $wgLang;
176183 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $rev->getTimestamp() ), true );
177 - if( $rev->userCan( Revision::DELETED_TEXT ) )
 184+ if( $rev->userCan( Revision::DELETED_TEXT ) ) {
178185 $link = $this->skin->makeKnownLinkObj( $title, $date, "oldid={$rev->getId()}" );
179 - else
 186+ } else {
180187 $link = $date;
 188+ }
181189
182190 // Build user textbox
183191 global $wgRequest;
184192 $userBox = Xml::input( "user-new-{$rev->getId()}", 50, $wgRequest->getVal( "user-{$rev->getId()}", $rev->getUserText() ) );
185193 $userText = Xml::hidden( "user-old-{$rev->getId()}", $rev->getUserText() ) . $rev->getUserText();
186194
187 - if( !is_null( $size = $rev->getSize() ) ){
188 - if( $size == 0 )
 195+ if( !is_null( $size = $rev->getSize() ) ) {
 196+ if( $size == 0 ) {
189197 $stxt = wfMsgHtml( 'historyempty' );
190 - else
 198+ } else {
191199 $stxt = wfMsgHtml( 'historysize', $wgLang->formatNum( $size ) );
192 - } else
 200+ }
 201+ } else {
193202 $stxt = ''; // Stop PHP from whining about unset variables
194 - $comment = $this->skin->commentBlock( $rev->getComment(), $title );
 203+ }
 204+ $comment = $this->skin->commentBlock( $rev->getComment(), $title );
195205
196206 // Now put it all together
197207 return "<li>($curLink) ($lastLink) $link . . $userBox ($userText) $stxt $comment</li>\n";
@@ -202,7 +212,7 @@
203213 * @param $errMsg String: error message
204214 * @return HTML
205215 */
206 - private function buildRevisionList( $title, $errMsg = '' ){
 216+ private function buildRevisionList( $title, $errMsg = '' ) {
207217 global $wgScript;
208218 $dbr = wfGetDB( DB_SLAVE );
209219 $res = $dbr->select(
@@ -213,23 +223,28 @@
214224 array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => 50 )
215225 );
216226 $revs = array();
217 - while( ( $r = $dbr->fetchObject($res ) ) )
 227+ while( ( $r = $dbr->fetchObject( $res ) ) ) {
218228 $revs[] = new Revision( $r );
219 - if( empty( $revs ) )
 229+ }
 230+ if( empty( $revs ) ) {
220231 // That's *very* weird
221232 return wfMsg( 'changeauthor-weirderror' );
 233+ }
222234
223 - $retval = Xml::openElement('form', array( 'method' => 'post', 'action' => $wgScript ) );
 235+ $retval = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $wgScript ) );
224236 $retval .= Xml::hidden( 'title', $this->selfTitle->getPrefixedDBkey() );
225237 $retval .= Xml::hidden( 'action', 'change' );
226238 $retval .= Xml::hidden( 'targetpage', $title->getPrefixedDBkey() );
227239 $retval .= Xml::openElement( 'fieldset' );
228 - $retval .= Xml::element( 'p', array(), wfMsg('changeauthor-explanation-multi'));
 240+ $retval .= Xml::element( 'p', array(), wfMsg( 'changeauthor-explanation-multi' ) );
229241 $retval .= Xml::inputLabel( wfMsg( 'changeauthor-comment'), 'comment', 'comment', 50);
230242 $retval .= Xml::submitButton(
231 - wfMsgExt( 'changeauthor-changeauthors-multi', array( 'parsemag', 'escape' ),
232 - count( $revs ) ) );
233 - if( $errMsg != '' ){
 243+ wfMsgExt( 'changeauthor-changeauthors-multi',
 244+ array( 'parsemag', 'escape' ),
 245+ count( $revs )
 246+ )
 247+ );
 248+ if( $errMsg != '' ) {
234249 $retval .= Xml::openElement( 'p' ) . Xml::openElement( 'b' );
235250 $retval .= Xml::element( 'font', array( 'color' => 'red' ), $errMsg );
236251 $retval .= Xml::closeElement( 'b' ) . Xml::closeElement( 'p' );
@@ -237,12 +252,13 @@
238253 $retval .= Xml::element( 'h2', array(), $title->getPrefixedText() );
239254 $retval .= Xml::openElement( 'ul' );
240255 $count = count( $revs );
241 - foreach( $revs as $i => $rev )
242 - $retval .= $this->buildRevisionLine( $rev, $title, ($i == 0), ($i == $count - 1) );
 256+ foreach( $revs as $i => $rev ) {
 257+ $retval .= $this->buildRevisionLine( $rev, $title, ( $i == 0 ), ( $i == $count - 1 ) );
 258+ }
243259 $retval .= Xml::closeElement( 'ul' );
244260 $retval .= Xml::closeElement( 'fieldset' );
245261 $retval .= Xml::closeElement( 'form' );
246 - return $retval;
 262+ return $retval;
247263 }
248264
249265 /**
@@ -251,7 +267,7 @@
252268 * @param $errMsg String: error message
253269 * @return HTML
254270 */
255 - private function buildOneRevForm( $rev, $errMsg = '' ){
 271+ private function buildOneRevForm( $rev, $errMsg = '' ) {
256272 global $wgScript;
257273 $retval = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $wgScript ) );
258274 $retval .= Xml::hidden( 'title', $this->selfTitle->getPrefixedDBkey() );
@@ -261,7 +277,7 @@
262278 $retval .= Xml::element( 'p', array(), wfMsg( 'changeauthor-explanation-single' ) );
263279 $retval .= Xml::inputLabel( wfMsg( 'changeauthor-comment' ), 'comment', 'comment' );
264280 $retval .= Xml::submitButton( wfMsg( 'changeauthor-changeauthors-single' ) );
265 - if( $errMsg != '' ){
 281+ if( $errMsg != '' ) {
266282 $retval .= Xml::openElement( 'p' ) . Xml::openElement( 'b' );
267283 $retval .= Xml::element( 'font', array( 'color' => 'red' ), $errMsg );
268284 $retval .= Xml::closeElement( 'b' ) . Xml::closeElement( 'p' );
@@ -279,27 +295,33 @@
280296 * Extracts an array needed by changeRevAuthors() from $wgRequest
281297 * @return array
282298 */
283 - private function parseChangeRequest(){
 299+ private function parseChangeRequest() {
284300 global $wgRequest;
285301 $vals = $wgRequest->getValues();
286302 $retval = array();
287 - foreach( $vals as $name => $val ){
288 - if( substr( $name, 0, 9 ) != 'user-new-' )
 303+ foreach( $vals as $name => $val ) {
 304+ if( substr( $name, 0, 9 ) != 'user-new-' ) {
289305 continue;
 306+ }
290307 $revid = substr( $name, 9 );
291 - if( !is_numeric( $revid ) )
 308+ if( !is_numeric( $revid ) ) {
292309 continue;
 310+ }
293311
294312 $new = User::newFromName( $val, false );
295 - if( !$new ) // Can this even happen?
 313+ if( !$new ) { // Can this even happen?
296314 return wfMsg( 'changeauthor-invalid-username', $val );
297 - if( $new->getId() == 0 && $val != 'MediaWiki default' && !User::isIP( $new->getName() ) )
 315+ }
 316+ if( $new->getId() == 0 && $val != 'MediaWiki default' && !User::isIP( $new->getName() ) ) {
298317 return wfMsg( 'changeauthor-nosuchuser', $val );
 318+ }
299319 $old = User::newFromName( $wgRequest->getVal( "user-old-$revid" ), false );
300 - if( !$old->getName() )
 320+ if( !$old->getName() ) {
301321 return wfMsg( 'changeauthor-invalidform' );
302 - if( $old->getName() != $new->getName() )
 322+ }
 323+ if( $old->getName() != $new->getName() ) {
303324 $retval[$revid] = array( $old, $new );
 325+ }
304326 }
305327 return $retval;
306328 }
@@ -309,13 +331,14 @@
310332 * @param $authors Array: key=revid value=array(User from, User to)
311333 * @param $comment Mixed: log comment
312334 */
313 - private function changeRevAuthors( $authors, $comment ){
 335+ private function changeRevAuthors( $authors, $comment ) {
314336 $dbw = wfGetDB( DB_MASTER );
315337 $dbw->begin();
316338 $editcounts = array(); // Array to keep track of EC mutations; key=userid, value=mutation
317339 $log = new LogPage( 'changeauth' );
318 - foreach( $authors as $id => $users ){
319 - $dbw->update( 'revision',
 340+ foreach( $authors as $id => $users ) {
 341+ $dbw->update(
 342+ 'revision',
320343 /* SET */array(
321344 'rev_user' => $users[1]->getId(),
322345 'rev_user_text' => $users[1]->getName()
@@ -324,22 +347,33 @@
325348 __METHOD__
326349 );
327350 $rev = Revision::newFromId( $id );
328 - $log->addEntry( 'changeauth', $rev->getTitle(), $comment, array(
329 - wfMsg( 'changeauthor-rev', $id ), $users[0]->getName(), $users[1]->getName() ) );
 351+ $log->addEntry(
 352+ 'changeauth',
 353+ $rev->getTitle(),
 354+ $comment,
 355+ array(
 356+ wfMsg( 'changeauthor-rev', $id ),
 357+ $users[0]->getName(),
 358+ $users[1]->getName()
 359+ )
 360+ );
330361 $editcounts[$users[1]->getId()]++;
331362 $editcounts[$users[0]->getId()]--;
332363 }
333 - foreach( $editcounts as $userid => $mutation ){
334 - if( $mutation == 0 || $userid == 0 )
 364+ foreach( $editcounts as $userid => $mutation ) {
 365+ if( $mutation == 0 || $userid == 0 ) {
335366 continue;
336 - if( $mutation > 0 )
 367+ }
 368+ if( $mutation > 0 ) {
337369 $mutation = "+$mutation";
338 - $dbw->update( 'user',
 370+ }
 371+ $dbw->update(
 372+ 'user',
339373 array( "user_editcount=user_editcount$mutation" ),
340374 array( 'user_id' => $userid ),
341375 __METHOD__
342376 );
343 - if( $dbw->affectedRows() == 0 ){
 377+ if( $dbw->affectedRows() == 0 ) {
344378 // Let's have mercy on those who don't have a proper DB server
345379 // (but not enough to spare their master)
346380 $count = $dbw->selectField(
@@ -348,7 +382,8 @@
349383 array( 'rev_user' => $userid ),
350384 __METHOD__
351385 );
352 - $dbw->update( 'user',
 386+ $dbw->update(
 387+ 'user',
353388 array( 'user_editcount' => $count ),
354389 array( 'user_id' => $userid ),
355390 __METHOD__

Status & tagging log