r48232 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48231‎ | r48232 | r48233 >
Date:18:09, 9 March 2009
Author:aaron
Status:ok
Tags:
Comment:
* Refactored revisiondelete to use subclassing
* Removed $action usage
* Check edit token
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/SpecialPage.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialRevisiondelete.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/AutoLoader.php
@@ -483,7 +483,7 @@
484484 'PopularPagesPage' => 'includes/specials/SpecialPopularpages.php',
485485 'PreferencesForm' => 'includes/specials/SpecialPreferences.php',
486486 'RandomPage' => 'includes/specials/SpecialRandompage.php',
487 - 'RevisionDeleteForm' => 'includes/specials/SpecialRevisiondelete.php',
 487+ 'SpecialRevisionDelete' => 'includes/specials/SpecialRevisiondelete.php',
488488 'RevisionDeleter' => 'includes/specials/SpecialRevisiondelete.php',
489489 'ShortPagesPage' => 'includes/specials/SpecialShortpages.php',
490490 'SpecialAllpages' => 'includes/specials/SpecialAllpages.php',
Index: trunk/phase3/includes/specials/SpecialRevisiondelete.php
@@ -7,93 +7,73 @@
88 * @ingroup SpecialPage
99 */
1010
11 -function wfSpecialRevisiondelete( $par = null ) {
12 - global $wgOut, $wgRequest, $wgUser;
13 -
14 - if ( wfReadOnly() ) {
15 - $wgOut->readOnlyPage();
16 - return;
 11+class SpecialRevisionDelete extends SpecialPage {
 12+
 13+ public function __construct() {
 14+ parent::__construct( 'RevisionDelete', 'deleterevision' );
 15+ $this->includable( false );
1716 }
18 -
19 - # Handle our many different possible input types
20 - $target = $wgRequest->getText( 'target' );
21 - $oldid = $wgRequest->getArray( 'oldid' );
22 - $artimestamp = $wgRequest->getArray( 'artimestamp' );
23 - $logid = $wgRequest->getArray( 'logid' );
24 - $img = $wgRequest->getArray( 'oldimage' );
25 - $fileid = $wgRequest->getArray( 'fileid' );
26 - # For reviewing deleted files...
27 - $file = $wgRequest->getVal( 'file' );
28 - # If this is a revision, then we need a target page
29 - $page = Title::newFromUrl( $target );
30 - if( is_null($page) ) {
31 - $wgOut->addWikiMsg( 'undelete-header' );
32 - return;
33 - }
34 - # Only one target set at a time please!
35 - $i = (bool)$file + (bool)$oldid + (bool)$logid + (bool)$artimestamp + (bool)$fileid + (bool)$img;
36 - if( $i !== 1 ) {
37 - $wgOut->showErrorPage( 'revdelete-toomanytargets-title', 'revdelete-toomanytargets-text' );
38 - return;
39 - }
40 - # Logs must have a type given
41 - if( $logid && !strpos($page->getDBKey(),'/') ) {
42 - $wgOut->showErrorPage( 'revdelete-nologtype-title', 'revdelete-nologtype-text' );
43 - return;
44 - }
45 - # Either submit or create our form
46 - $form = new RevisionDeleteForm( $page, $oldid, $logid, $artimestamp, $fileid, $img, $file );
47 - if( $wgRequest->wasPosted() ) {
48 - $form->submit( $wgRequest );
49 - } else if( $oldid || $artimestamp ) {
50 - $form->showRevs();
51 - } else if( $fileid || $img ) {
52 - $form->showImages();
53 - } else if( $logid ) {
54 - $form->showLogItems();
55 - }
56 - # Show relevant lines from the deletion log. This will show even if said ID
57 - # does not exist...might be helpful
58 - $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
59 - LogEventsList::showLogExtract( $wgOut, 'delete', $page->getPrefixedText() );
60 - if( $wgUser->isAllowed( 'suppressionlog' ) ){
61 - $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
62 - LogEventsList::showLogExtract( $wgOut, 'suppress', $page->getPrefixedText() );
63 - }
64 -}
6517
66 -/**
67 - * Implements the GUI for Revision Deletion.
68 - * @ingroup SpecialPage
69 - */
70 -class RevisionDeleteForm {
71 - /**
72 - * @param Title $page
73 - * @param array $oldids
74 - * @param array $logids
75 - * @param array $artimestamps
76 - * @param array $fileids
77 - * @param array $img
78 - * @param string $file
79 - */
80 - function __construct( $page, $oldids, $logids, $artimestamps, $fileids, $img, $file ) {
81 - global $wgUser, $wgOut;
82 -
83 - $this->page = $page;
 18+ public function execute( $par ) {
 19+ global $wgOut, $wgUser, $wgRequest;
 20+ if( wfReadOnly() ) {
 21+ $wgOut->readOnlyPage();
 22+ return;
 23+ }
 24+ if( !$wgUser->isAllowed( 'deleterevision' ) ) {
 25+ $wgOut->permissionRequired( 'deleterevision' );
 26+ return;
 27+ }
 28+ $this->skin =& $wgUser->getSkin();
 29+ # Set title and such
 30+ $this->setHeaders();
 31+ $this->outputHeader();
 32+ $this->wasPosted = $wgRequest->wasPosted();
 33+ # Handle our many different possible input types
 34+ $this->target = $wgRequest->getText( 'target' );
 35+ $this->oldids = $wgRequest->getArray( 'oldid' );
 36+ $this->artimestamps = $wgRequest->getArray( 'artimestamp' );
 37+ $this->logids = $wgRequest->getArray( 'logid' );
 38+ $this->oldimgs = $wgRequest->getArray( 'oldimage' );
 39+ $this->fileids = $wgRequest->getArray( 'fileid' );
8440 # For reviewing deleted files...
85 - if( $file ) {
86 - $oimage = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $page, $file );
 41+ $this->file = $wgRequest->getVal( 'file' );
 42+ # If this is a revision, then we need a target page
 43+ $this->page = Title::newFromUrl( $this->target );
 44+ if( is_null($this->page) ) {
 45+ $wgOut->addWikiMsg( 'undelete-header' );
 46+ return;
 47+ }
 48+ # Only one target set at a time please!
 49+ $i = (bool)$this->file + (bool)$this->oldids + (bool)$this->logids
 50+ + (bool)$this->artimestamps + (bool)$this->fileids + (bool)$this->oldimgs;
 51+ if( $i !== 1 ) {
 52+ $wgOut->showErrorPage( 'revdelete-toomanytargets-title', 'revdelete-toomanytargets-text' );
 53+ return;
 54+ }
 55+ # Logs must have a type given
 56+ if( $this->logids && !strpos($this->page->getDBKey(),'/') ) {
 57+ $wgOut->showErrorPage( 'revdelete-nologtype-title', 'revdelete-nologtype-text' );
 58+ return;
 59+ }
 60+ # Check edit token on submission
 61+ if( $this->wasPosted && !$wgUser->matchEditToken( $wgRequest->getVal('wpEditToken') ) ) {
 62+ $wgOut->addWikiMsg( 'sessionfailure' );
 63+ return;
 64+ }
 65+ # For reviewing deleted files...show it now if allowed
 66+ if( $this->file ) {
 67+ $oimage = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->page, $this->file );
8768 $oimage->load();
8869 // Check if user is allowed to see this file
8970 if( !$oimage->userCan(File::DELETED_FILE) ) {
9071 $wgOut->permissionRequired( 'suppressrevision' );
9172 } else {
92 - $this->showFile( $file );
 73+ $this->showFile( $this->file );
9374 }
9475 return;
9576 }
96 - $this->skin = $wgUser->getSkin();
97 - # Give a link to the log for this page
 77+ # Give a link to the logs/hist for this page
9878 if( !is_null($this->page) && $this->page->getNamespace() > -1 ) {
9979 $links = array();
10080
@@ -112,34 +92,60 @@
11393 # Logs themselves don't have histories or archived revisions
11494 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
11595 }
 96+ # Lock the operation and the form context
 97+ $this->secureOperation();
 98+ # Either submit or create our form
 99+ if( $this->wasPosted ) {
 100+ $this->submit( $wgRequest );
 101+ } else if( $this->deleteKey == 'oldid' || $this->deleteKey == 'artimestamp' ) {
 102+ $this->showRevs();
 103+ } else if( $this->deleteKey == 'fileid' || $this->deleteKey == 'oldimage' ) {
 104+ $this->showImages();
 105+ } else if( $this->deleteKey == 'logid' ) {
 106+ $this->showLogItems();
 107+ }
 108+ # Show relevant lines from the deletion log. This will show even if said ID
 109+ # does not exist...might be helpful
 110+ $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
 111+ LogEventsList::showLogExtract( $wgOut, 'delete', $this->page->getPrefixedText() );
 112+ if( $wgUser->isAllowed( 'suppressionlog' ) ){
 113+ $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
 114+ LogEventsList::showLogExtract( $wgOut, 'suppress', $this->page->getPrefixedText() );
 115+ }
 116+ }
 117+
 118+ private function secureOperation() {
 119+ global $wgUser;
 120+ $this->deleteKey = '';
116121 // At this point, we should only have one of these
117 - if( $oldids ) {
118 - $this->revisions = $oldids;
 122+ if( $this->oldids ) {
 123+ $this->revisions = $this->oldids;
119124 $hide_content_name = array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT );
120 - $this->deleteKey='oldid';
121 - } else if( $artimestamps ) {
122 - $this->archrevs = $artimestamps;
 125+ $this->deleteKey = 'oldid';
 126+ } else if( $this->artimestamps ) {
 127+ $this->archrevs = $this->artimestamps;
123128 $hide_content_name = array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT );
124 - $this->deleteKey='artimestamp';
125 - } else if( $img ) {
126 - $this->ofiles = $img;
 129+ $this->deleteKey = 'artimestamp';
 130+ } else if( $this->oldimgs ) {
 131+ $this->ofiles = $this->oldimgs;
127132 $hide_content_name = array( 'revdelete-hide-image', 'wpHideImage', File::DELETED_FILE );
128 - $this->deleteKey='oldimage';
129 - } else if( $fileids ) {
130 - $this->afiles = $fileids;
 133+ $this->deleteKey = 'oldimage';
 134+ } else if( $this->fileids ) {
 135+ $this->afiles = $this->fileids;
131136 $hide_content_name = array( 'revdelete-hide-image', 'wpHideImage', File::DELETED_FILE );
132 - $this->deleteKey='fileid';
133 - } else if( $logids ) {
134 - $this->events = $logids;
 137+ $this->deleteKey = 'fileid';
 138+ } else if( $this->logids ) {
 139+ $this->events = $this->logids;
135140 $hide_content_name = array( 'revdelete-hide-name', 'wpHideName', LogPage::DELETED_ACTION );
136 - $this->deleteKey='logid';
 141+ $this->deleteKey = 'logid';
137142 }
138143 // Our checkbox messages depends one what we are doing,
139144 // e.g. we don't hide "text" for logs or images
140145 $this->checks = array(
141146 $hide_content_name,
142147 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
143 - array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER ) );
 148+ array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
 149+ );
144150 if( $wgUser->isAllowed('suppressrevision') ) {
145151 $this->checks[] = array( 'revdelete-hide-restricted', 'wpHideRestricted', Revision::DELETED_RESTRICTED );
146152 }
@@ -167,9 +173,8 @@
168174 /**
169175 * This lets a user set restrictions for live and archived revisions
170176 */
171 - function showRevs() {
172 - global $wgOut, $wgUser, $action;
173 -
 177+ private function showRevs() {
 178+ global $wgOut, $wgUser;
174179 $UserAllowed = true;
175180
176181 $count = ($this->deleteKey=='oldid') ?
@@ -204,7 +209,7 @@
205210 continue;
206211 } else if( !$revObjs[$revid]->userCan(Revision::DELETED_RESTRICTED) ) {
207212 // If a rev is hidden from sysops
208 - if( $action != 'submit') {
 213+ if( !$this->wasPosted ) {
209214 $wgOut->permissionRequired( 'suppressrevision' );
210215 return;
211216 }
@@ -246,7 +251,7 @@
247252 continue;
248253 } else if( !$revObjs[$timestamp]->userCan(Revision::DELETED_RESTRICTED) ) {
249254 // If a rev is hidden from sysops
250 - if( $action != 'submit') {
 255+ if( !$this->wasPosted ) {
251256 $wgOut->permissionRequired( 'suppressrevision' );
252257 return;
253258 }
@@ -309,15 +314,12 @@
310315 /**
311316 * This lets a user set restrictions for archived images
312317 */
313 - function showImages() {
314 - // What is $action doing here???
315 - global $wgOut, $wgUser, $action, $wgLang;
316 -
 318+ private function showImages() {
 319+ global $wgOut, $wgUser, $wgLang;
317320 $UserAllowed = true;
318321
319322 $count = ($this->deleteKey=='oldimage') ? count($this->ofiles) : count($this->afiles);
320 - $wgOut->addWikiMsg( 'revdelete-selected',
321 - $this->page->getPrefixedText(),
 323+ $wgOut->addWikiMsg( 'revdelete-selected', $this->page->getPrefixedText(),
322324 $wgLang->formatNum($count) );
323325
324326 $bitfields = 0;
@@ -349,7 +351,7 @@
350352 continue;
351353 } else if( !$filesObjs[$archivename]->userCan(File::DELETED_RESTRICTED) ) {
352354 // If a rev is hidden from sysops
353 - if( $action != 'submit' ) {
 355+ if( !$this->wasPosted ) {
354356 $wgOut->permissionRequired( 'suppressrevision' );
355357 return;
356358 }
@@ -380,7 +382,7 @@
381383 continue;
382384 } else if( !$filesObjs[$fileid]->userCan(File::DELETED_RESTRICTED) ) {
383385 // If a rev is hidden from sysops
384 - if( $action != 'submit' ) {
 386+ if( !$this->wasPosted ) {
385387 $wgOut->permissionRequired( 'suppressrevision' );
386388 return;
387389 }
@@ -443,10 +445,10 @@
444446 /**
445447 * This lets a user set restrictions for log items
446448 */
447 - function showLogItems() {
448 - global $wgOut, $wgUser, $action, $wgMessageCache, $wgLang;
 449+ private function showLogItems() {
 450+ global $wgOut, $wgUser, $wgMessageCache, $wgLang;
 451+ $UserAllowed = true;
449452
450 - $UserAllowed = true;
451453 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->events) ) );
452454
453455 $bitfields = 0;
@@ -475,7 +477,7 @@
476478 continue;
477479 } else if( !LogEventsList::userCan( $logRows[$logid],Revision::DELETED_RESTRICTED) ) {
478480 // If an event is hidden from sysops
479 - if( $action != 'submit') {
 481+ if( !$this->wasPosted ) {
480482 $wgOut->permissionRequired( 'suppressrevision' );
481483 return;
482484 }
@@ -731,7 +733,7 @@
732734 /**
733735 * @param WebRequest $request
734736 */
735 - function submit( $request ) {
 737+ private function submit( $request ) {
736738 global $wgUser, $wgOut;
737739
738740 $bitfield = $this->extractBitfield( $request );
Index: trunk/phase3/includes/SpecialPage.php
@@ -183,7 +183,7 @@
184184 'Mycontributions' => array( 'SpecialMycontributions' ),
185185 'Mypage' => array( 'SpecialMypage' ),
186186 'Mytalk' => array( 'SpecialMytalk' ),
187 - 'Revisiondelete' => array( 'UnlistedSpecialPage', 'Revisiondelete', 'deleterevision' ),
 187+ 'Revisiondelete' => 'SpecialRevisiondelete',
188188 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
189189 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
190190 );

Status & tagging log