r48925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48924‎ | r48925 | r48926 >
Date:17:53, 27 March 2009
Author:aaron
Status:ok
Tags:
Comment:
Minor cleanup & refactoring of revisiondelete
Modified paths:
  • /trunk/phase3/includes/specials/SpecialRevisiondelete.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialRevisiondelete.php
@@ -11,18 +11,15 @@
1212
1313 public function __construct() {
1414 parent::__construct( 'Revisiondelete', 'deleterevision' );
15 - $this->includable( false );
 15+ $this->includable( false ); // paranoia
1616 }
1717
1818 public function execute( $par ) {
1919 global $wgOut, $wgUser, $wgRequest;
20 - if( wfReadOnly() ) {
21 - $wgOut->readOnlyPage();
22 - return;
23 - }
2420 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
25 - $wgOut->permissionRequired( 'deleterevision' );
26 - return;
 21+ return $wgOut->permissionRequired( 'deleterevision' );
 22+ } else if( wfReadOnly() ) {
 23+ return $wgOut->readOnlyPage();
2724 }
2825 $this->skin =& $wgUser->getSkin();
2926 $this->setHeaders();
@@ -40,15 +37,14 @@
4138 # For reviewing deleted files...
4239 $this->file = $wgRequest->getVal( 'file' );
4340 # Only one target set at a time please!
44 - $i = (bool)$this->file + (bool)$this->oldids + (bool)$this->logids
 41+ $types = (bool)$this->file + (bool)$this->oldids + (bool)$this->logids
4542 + (bool)$this->artimestamps + (bool)$this->fileids + (bool)$this->oldimgs;
4643 # No targets?
47 - if( $i == 0 ) {
48 - $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
 44+ if( $types == 0 ) {
 45+ $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
4946 return;
50 - }
5147 # Too many targets?
52 - if( $i !== 1 ) {
 48+ } else if( $types > 1 ) {
5349 $wgOut->showErrorPage( 'revdelete-toomanytargets-title', 'revdelete-toomanytargets-text' );
5450 return;
5551 }
@@ -62,45 +58,18 @@
6359 }
6460 # We need a target page!
6561 if( is_null($this->page) ) {
66 - $wgOut->addWikiMsg( 'undelete-header' );
67 - return;
 62+ return $wgOut->addWikiMsg( 'undelete-header' );
6863 }
69 - # Logs must have a type given
70 - if( $this->logids && !strpos($this->page->getDBKey(),'/') ) {
71 - $wgOut->showErrorPage( 'revdelete-nologtype-title', 'revdelete-nologtype-text' );
72 - return;
73 - }
7464 # For reviewing deleted files...show it now if allowed
7565 if( $this->file ) {
76 - $oimage = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->page, $this->file );
77 - $oimage->load();
78 - // Check if user is allowed to see this file
79 - if( !$oimage->userCan(File::DELETED_FILE) ) {
80 - $wgOut->permissionRequired( 'suppressrevision' );
81 - } else {
82 - $this->showFile( $this->file );
83 - }
84 - return;
 66+ return $this->tryShowFile( $this->file );
8567 }
86 - # Give a link to the logs/hist for this page
87 - if( !is_null($this->page) && $this->page->getNamespace() > -1 ) {
88 - $links = array();
89 -
90 - $logtitle = SpecialPage::getTitleFor( 'Log' );
91 - $links[] = $this->skin->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
92 - wfArrayToCGI( array( 'page' => $this->page->getPrefixedUrl() ) ) );
93 - # Give a link to the page history
94 - $links[] = $this->skin->makeKnownLinkObj( $this->page, wfMsgHtml( 'pagehist' ),
95 - wfArrayToCGI( array( 'action' => 'history' ) ) );
96 - # Link to deleted edits
97 - if( $wgUser->isAllowed('undelete') ) {
98 - $undelete = SpecialPage::getTitleFor( 'Undelete' );
99 - $links[] = $this->skin->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
100 - wfArrayToCGI( array( 'target' => $this->page->getPrefixedDBkey() ) ) );
101 - }
102 - # Logs themselves don't have histories or archived revisions
103 - $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
 68+ # Logs must have a type given
 69+ if( $this->logids && !strpos($this->page->getDBKey(),'/') ) {
 70+ return $wgOut->showErrorPage( 'revdelete-nologtype-title', 'revdelete-nologtype-text' );
10471 }
 72+ # Give a link to the logs/hist for this page
 73+ $this->showConvenienceLinks();
10574 # Lock the operation and the form context
10675 $this->secureOperation();
10776 # Either submit or create our form
@@ -124,6 +93,28 @@
12594 }
12695 }
12796
 97+ private function showConvenienceLinks() {
 98+ global $wgOut, $wgUser;
 99+ # Give a link to the logs/hist for this page
 100+ if( !is_null($this->page) && $this->page->getNamespace() > -1 ) {
 101+ $links = array();
 102+ $logtitle = SpecialPage::getTitleFor( 'Log' );
 103+ $links[] = $this->skin->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
 104+ wfArrayToCGI( array( 'page' => $this->page->getPrefixedUrl() ) ) );
 105+ # Give a link to the page history
 106+ $links[] = $this->skin->makeKnownLinkObj( $this->page, wfMsgHtml( 'pagehist' ),
 107+ wfArrayToCGI( array( 'action' => 'history' ) ) );
 108+ # Link to deleted edits
 109+ if( $wgUser->isAllowed('undelete') ) {
 110+ $undelete = SpecialPage::getTitleFor( 'Undelete' );
 111+ $links[] = $this->skin->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
 112+ wfArrayToCGI( array( 'target' => $this->page->getPrefixedDBkey() ) ) );
 113+ }
 114+ # Logs themselves don't have histories or archived revisions
 115+ $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
 116+ }
 117+ }
 118+
128119 private function getLogQueryCond() {
129120 $ids = $safeIds = array();
130121 $action = 'revision';
@@ -203,20 +194,26 @@
204195 /**
205196 * Show a deleted file version requested by the visitor.
206197 */
207 - private function showFile( $key ) {
 198+ private function tryShowFile( $key ) {
208199 global $wgOut, $wgRequest;
209 - $wgOut->disable();
210 -
211 - # We mustn't allow the output to be Squid cached, otherwise
212 - # if an admin previews a deleted image, and it's cached, then
213 - # a user without appropriate permissions can toddle off and
214 - # nab the image, and Squid will serve it
215 - $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
216 - $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
217 - $wgRequest->response()->header( 'Pragma: no-cache' );
218 -
219 - $store = FileStore::get( 'deleted' );
220 - $store->stream( $key );
 200+ $oimage = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->page, $key );
 201+ $oimage->load();
 202+ // Check if user is allowed to see this file
 203+ if( !$oimage->userCan(File::DELETED_FILE) ) {
 204+ $wgOut->permissionRequired( 'suppressrevision' );
 205+ } else {
 206+ $wgOut->disable();
 207+ # We mustn't allow the output to be Squid cached, otherwise
 208+ # if an admin previews a deleted image, and it's cached, then
 209+ # a user without appropriate permissions can toddle off and
 210+ # nab the image, and Squid will serve it
 211+ $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
 212+ $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
 213+ $wgRequest->response()->header( 'Pragma: no-cache' );
 214+ # Stream the file to the client
 215+ $store = FileStore::get( 'deleted' );
 216+ $store->stream( $key );
 217+ }
221218 }
222219
223220 /**
@@ -226,7 +223,7 @@
227224 global $wgOut, $wgUser;
228225 $UserAllowed = true;
229226
230 - $count = ($this->deleteKey=='oldid') ?
 227+ $count = ($this->deleteKey == 'oldid') ?
231228 count($this->revisions) : count($this->archrevs);
232229 $wgOut->addWikiMsg( 'revdelete-selected', $this->page->getPrefixedText(), $count );
233230
@@ -238,7 +235,7 @@
239236
240237 $revisions = 0;
241238 // Live revisions...
242 - if( $this->deleteKey=='oldid' ) {
 239+ if( $this->deleteKey == 'oldid' ) {
243240 // Run through and pull all our data in one query
244241 foreach( $this->revisions as $revid ) {
245242 $where[] = intval($revid);
@@ -332,7 +329,7 @@
333330 Xml::hidden( 'target', $this->page->getPrefixedText() ),
334331 Xml::hidden( 'type', $this->deleteKey )
335332 );
336 - if( $this->deleteKey=='oldid' ) {
 333+ if( $this->deleteKey == 'oldid' ) {
337334 $hidden[] = Xml::hidden( 'oldid', implode(',',$this->oldids) );
338335 } else {
339336 $hidden[] = Xml::hidden( 'artimestamp', implode(',',$this->artimestamps) );
@@ -366,7 +363,7 @@
367364 global $wgOut, $wgUser, $wgLang;
368365 $UserAllowed = true;
369366
370 - $count = ($this->deleteKey=='oldimage') ? count($this->ofiles) : count($this->afiles);
 367+ $count = ($this->deleteKey == 'oldimage') ? count($this->ofiles) : count($this->afiles);
371368 $wgOut->addWikiMsg( 'revdelete-selected', $this->page->getPrefixedText(),
372369 $wgLang->formatNum($count) );
373370
@@ -377,7 +374,7 @@
378375 $dbr = wfGetDB( DB_MASTER );
379376 // Live old revisions...
380377 $revisions = 0;
381 - if( $this->deleteKey=='oldimage' ) {
 378+ if( $this->deleteKey == 'oldimage' ) {
382379 // Run through and pull all our data in one query
383380 foreach( $this->ofiles as $timestamp ) {
384381 $where[] = $timestamp.'!'.$this->page->getDBKey();
@@ -462,7 +459,7 @@
463460 Xml::hidden( 'target', $this->page->getPrefixedText() ),
464461 Xml::hidden( 'type', $this->deleteKey )
465462 );
466 - if( $this->deleteKey=='oldimage' ) {
 463+ if( $this->deleteKey == 'oldimage' ) {
467464 $hidden[] = Xml::hidden( 'oldimage', implode(',',$this->oldimgs) );
468465 } else {
469466 $hidden[] = Xml::hidden( 'fileid', implode(',',$this->fileids) );
@@ -612,7 +609,7 @@
613610 $date = $wgLang->timeanddate( $rev->getTimestamp() );
614611 $difflink = $del = '';
615612 // Live revisions
616 - if( $this->deleteKey=='oldid' ) {
 613+ if( $this->deleteKey == 'oldid' ) {
617614 $tokenParams = '&unhide=1&token='.urlencode( $wgUser->editToken( $rev->getId() ) );
618615 $revlink = $this->skin->makeLinkObj( $this->page, $date, 'oldid='.$rev->getId() . $tokenParams );
619616 $difflink = '(' . $this->skin->makeKnownLinkObj( $this->page, wfMsgHtml('diff'),
@@ -674,7 +671,8 @@
675672 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
676673 $data = htmlspecialchars( $data );
677674
678 - return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".$this->fileComment( $file )."$del</li>";
 675+ return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".
 676+ $this->fileComment( $file )."$del</li>";
679677 }
680678
681679 /**
@@ -688,7 +686,8 @@
689687 $date = $wgLang->timeanddate( $file->getTimestamp(), true );
690688
691689 $undelete = SpecialPage::getTitleFor( 'Undelete' );
692 - $pageLink = $this->skin->makeKnownLinkObj( $undelete, $date, "target=$target&file={$file->getKey()}" );
 690+ $pageLink = $this->skin->makeKnownLinkObj( $undelete, $date,
 691+ "target=$target&file={$file->getKey()}" );
693692
694693 $del = '';
695694 if( $file->isDeleted(File::DELETED_FILE) ) {
@@ -701,7 +700,8 @@
702701 ' (' . wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $file->getSize() ) ) . ')';
703702 $data = htmlspecialchars( $data );
704703
705 - return "<li> $pageLink ".$this->fileUserTools( $file )." $data ".$this->fileComment( $file )."$del</li>";
 704+ return "<li>$pageLink ".$this->fileUserTools( $file )." $data ".
 705+ $this->fileComment( $file )."$del</li>";
706706 }
707707
708708 /**
@@ -814,16 +814,16 @@
815815
816816 $wrap = '<span class="success">$1</span>';
817817
818 - if( $this->deleteKey=='logid' ) {
 818+ if( $this->deleteKey == 'logid' ) {
819819 $wgOut->wrapWikiMsg( $wrap, 'logdelete-success' );
820820 $this->showLogItems();
821 - } else if( $this->deleteKey=='oldid' || $this->deleteKey=='artimestamp' ) {
 821+ } else if( $this->deleteKey == 'oldid' || $this->deleteKey == 'artimestamp' ) {
822822 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
823823 $this->showRevs();
824 - } else if( $this->deleteKey=='fileid' ) {
 824+ } else if( $this->deleteKey == 'fileid' ) {
825825 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
826826 $this->showImages();
827 - } else if( $this->deleteKey=='oldimage' ) {
 827+ } else if( $this->deleteKey == 'oldimage' ) {
828828 $wgOut->wrapWikiMsg( $wrap, 'revdelete-success' );
829829 $this->showImages();
830830 }
@@ -1440,9 +1440,9 @@
14411441 * @param int $diff The xor of the old and new bitfields.
14421442 * @param array $arr The array to update.
14431443 */
1444 - function checkItem ( $desc, $field, $diff, $new, &$arr ) {
1445 - if ( $diff & $field ) {
1446 - $arr [ ( $new & $field ) ? 0 : 1 ][] = $desc;
 1444+ function checkItem( $desc, $field, $diff, $new, &$arr ) {
 1445+ if( $diff & $field ) {
 1446+ $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
14471447 }
14481448 }
14491449
@@ -1458,25 +1458,23 @@
14591459 * @param int $o The old bitfield.
14601460 * @return An array as described above.
14611461 */
1462 - function getChanges ( $n, $o ) {
 1462+ function getChanges( $n, $o ) {
14631463 $diff = $n ^ $o;
1464 - $ret = array ( 0 => array(), 1 => array(), 2 => array() );
1465 -
1466 - $this->checkItem ( wfMsgForContent ( 'revdelete-content' ),
1467 - Revision::DELETED_TEXT, $diff, $n, $ret );
1468 - $this->checkItem ( wfMsgForContent ( 'revdelete-summary' ),
1469 - Revision::DELETED_COMMENT, $diff, $n, $ret );
1470 - $this->checkItem ( wfMsgForContent ( 'revdelete-uname' ),
1471 - Revision::DELETED_USER, $diff, $n, $ret );
1472 -
 1464+ $ret = array( 0 => array(), 1 => array(), 2 => array() );
 1465+ // Build bitfield changes in language
 1466+ $this->checkItem( wfMsgForContent( 'revdelete-content' ),
 1467+ Revision::DELETED_TEXT, $diff, $n, $ret );
 1468+ $this->checkItem( wfMsgForContent( 'revdelete-summary' ),
 1469+ Revision::DELETED_COMMENT, $diff, $n, $ret );
 1470+ $this->checkItem( wfMsgForContent( 'revdelete-uname' ),
 1471+ Revision::DELETED_USER, $diff, $n, $ret );
14731472 // Restriction application to sysops
1474 - if ( $diff & Revision::DELETED_RESTRICTED ) {
1475 - if ( $n & Revision::DELETED_RESTRICTED )
1476 - $ret[2][] = wfMsgForContent ( 'revdelete-restricted' );
 1473+ if( $diff & Revision::DELETED_RESTRICTED ) {
 1474+ if( $n & Revision::DELETED_RESTRICTED )
 1475+ $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
14771476 else
1478 - $ret[2][] = wfMsgForContent ( 'revdelete-unrestricted' );
 1477+ $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
14791478 }
1480 -
14811479 return $ret;
14821480 }
14831481
@@ -1491,35 +1489,28 @@
14921490 * @param string $comment The comment associated with the change.
14931491 * @param bool $isForLog
14941492 */
1495 - function getLogMessage ( $count, $nbitfield, $obitfield, $comment, $isForLog = false ) {
 1493+ function getLogMessage( $count, $nbitfield, $obitfield, $comment, $isForLog = false ) {
14961494 global $wgContLang;
14971495
14981496 $s = '';
14991497 $changes = $this->getChanges( $nbitfield, $obitfield );
15001498
1501 - if ( count ( $changes[0] ) ) {
 1499+ if( count( $changes[0] ) ) {
15021500 $s .= wfMsgForContent ( 'revdelete-hid', implode ( ', ', $changes[0] ) );
15031501 }
1504 -
1505 - if ( count ( $changes[1] ) ) {
 1502+ if( count( $changes[1] ) ) {
15061503 if ($s) $s .= '; ';
1507 -
15081504 $s .= wfMsgForContent ( 'revdelete-unhid', implode ( ', ', $changes[1] ) );
15091505 }
1510 -
1511 - if ( count ( $changes[2] )) {
1512 - if ($s)
1513 - $s .= ' (' . $changes[2][0] . ')';
1514 - else
1515 - $s = $changes[2][0];
 1506+ if( count( $changes[2] ) ) {
 1507+ $s .= $s ? ' (' . $changes[2][0] . ')' : $changes[2][0];
15161508 }
15171509
15181510 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
15191511 $ret = wfMsgExt ( $msg, array( 'parsemag', 'content' ),
15201512 $s, $wgContLang->formatNum( $count ) );
15211513
1522 - if ( $comment )
1523 - $ret .= ": $comment";
 1514+ if( $comment ) $ret .= ": $comment";
15241515
15251516 return $ret;
15261517
@@ -1536,11 +1527,12 @@
15371528 * @param string $param, URL param
15381529 * @param Array $items
15391530 */
1540 - function updateLog( $title, $count, $nbitfield, $obitfield, $comment,
1541 - $target, $param, $items = array() )
 1531+ function updateLog( $title, $count, $nbitfield, $obitfield, $comment, $target,
 1532+ $param, $items = array() )
15421533 {
15431534 // Put things hidden from sysops in the oversight log
1544 - $logtype = ( ($nbitfield | $obitfield) & Revision::DELETED_RESTRICTED ) ? 'suppress' : 'delete';
 1535+ $logtype = ( ($nbitfield | $obitfield) & Revision::DELETED_RESTRICTED ) ?
 1536+ 'suppress' : 'delete';
15451537 $log = new LogPage( $logtype );
15461538
15471539 $reason = $this->getLogMessage( $count, $nbitfield, $obitfield, $comment, $param == 'logid' );

Status & tagging log