r56326 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56325‎ | r56326 | r56327 >
Date:19:34, 14 September 2009
Author:aaron
Status:ok (Comments)
Tags:
Comment:
* Rate-limit review
* Improve failure messages
* Use status codes/other minor cleanups
Modified paths:
  • /trunk/extensions/ReaderFeedback/ReaderFeedback.php (modified) (history)
  • /trunk/extensions/ReaderFeedback/language/ReaderFeedback.i18n.php (modified) (history)
  • /trunk/extensions/ReaderFeedback/specialpages/ReaderFeedback_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ReaderFeedback/specialpages/ReaderFeedback_body.php
@@ -6,6 +6,10 @@
77
88 class ReaderFeedbackPage extends UnlistedSpecialPage
99 {
 10+ const REVIEW_ERROR = 0;
 11+ const REVIEW_OK = 1;
 12+ const REVIEW_DUP = 2;
 13+
1014 // Initialize to handle incomplete AJAX input
1115 var $page = null;
1216 var $oldid = 0;
@@ -14,7 +18,7 @@
1519 var $commentary = '';
1620
1721 public function __construct() {
18 - UnlistedSpecialPage::UnlistedSpecialPage( 'ReaderFeedback', 'feedback' );
 22+ parent::__construct( 'ReaderFeedback', 'feedback' );
1923 wfLoadExtensionMessages( 'ReaderFeedback' );
2024 }
2125
@@ -23,16 +27,13 @@
2428 $confirm = $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
2529 if( $wgUser->isAllowed( 'feedback' ) ) {
2630 if( $wgUser->isBlocked( !$confirm ) ) {
27 - $wgOut->blockedPage();
28 - return;
 31+ return $wgOut->blockedPage();
2932 }
3033 } else {
31 - $wgOut->permissionRequired( 'feedback' );
32 - return;
 34+ return $wgOut->permissionRequired( 'feedback' );
3335 }
3436 if( wfReadOnly() ) {
35 - $wgOut->readOnlyPage();
36 - return;
 37+ return $wgOut->readOnlyPage();
3738 }
3839 $this->setHeaders();
3940 # Our target page
@@ -73,11 +74,11 @@
7475 if( $confirm && !$wgRequest->getVal( 'commentary' ) ) {
7576 $ok = $this->submit();
7677 } else {
77 - $ok = false;
 78+ $ok = self::REVIEW_ERROR;
7879 }
7980 # Go to graphs!
8081 global $wgMiserMode;
81 - if( $ok && !$wgMiserMode ) {
 82+ if( $ok == self::REVIEW_OK && !$wgMiserMode ) {
8283 $ratingTitle = SpecialPage::getTitleFor( 'RatingHistory' );
8384 $wgOut->redirect( $ratingTitle->getLocalUrl('target='.$this->page->getPrefixedUrl() ) );
8485 # Already voted or graph is set to be skipped...
@@ -169,16 +170,24 @@
170171
171172 $dbw = wfGetDB( DB_MASTER );
172173 $dbw->begin();
173 - $ok = ( $bot || $form->submit() ); // don't submit for mindless drones
174 - $dbw->commit();
175 - if( $ok ) {
176 - return '<suc#>'.wfMsgExt( 'readerfeedback-success', array('parseinline'),
177 - $form->page->getPrefixedText(), $graphLink, $talk->getFullUrl( 'action=edit&section=new' ) ) .
178 - '<h4>'.wfMsgHtml('ratinghistory-table')."</h4>\n$tallyTable";
 174+ if( $bot ) {
 175+ $ok = self::REVIEW_ERROR; // don't submit for mindless drones
179176 } else {
180 - return '<err#>'.wfMsgExt( 'readerfeedback-voted', array('parseinline'),
181 - $form->page->getPrefixedText(), $graphLink, $talk->getFullUrl( 'action=edit&section=new' ) );
 177+ $ok = $form->submit();
182178 }
 179+ $dbw->commit();
 180+ switch( $ok ) {
 181+ case self::REVIEW_OK:
 182+ return '<suc#>'.wfMsgExt( 'readerfeedback-success', array('parseinline'),
 183+ $form->page->getPrefixedText(), $graphLink, $talk->getFullUrl( 'action=edit&section=new' ) ) .
 184+ '<h4>'.wfMsgHtml('ratinghistory-table')."</h4>\n$tallyTable";
 185+ case self::REVIEW_DUP:
 186+ return '<err#>'.wfMsgExt( 'readerfeedback-voted', array('parseinline'),
 187+ $form->page->getPrefixedText(), $graphLink, $talk->getFullUrl( 'action=edit&section=new' ) );
 188+ default:
 189+ return '<err#>'.wfMsgExt( 'readerfeedback-error', array('parseinline'),
 190+ $form->page->getPrefixedText(), $graphLink, $talk->getFullUrl( 'action=edit&section=new' ) );
 191+ }
183192 }
184193
185194 protected static function isValid( $int ) {
@@ -256,21 +265,25 @@
257266 $now = wfTimestampNow();
258267 $date = str_pad( substr( $now, 0, 8 ), 14, '0' );
259268 if( count($this->dims) == 0 )
260 - return false;
 269+ return self::REVIEW_ERROR;
261270 $ratings = $this->flattenRatings( $this->dims );
262271 # Make sure revision is valid!
263272 $rev = Revision::newFromId( $this->oldid );
264273 if( !$rev || !$rev->getTitle()->equals( $this->page ) ) {
265 - return false; // opps!
 274+ return self::REVIEW_ERROR; // opps!
266275 }
267276 $ip = wfGetIP();
268277 if( !$wgUser->getId() && !$ip ) {
269 - return false; // we need to keep track somehow
 278+ return self::REVIEW_ERROR; // we need to keep track somehow
270279 }
271280 $article = new Article( $this->page );
 281+ # Check if the user is spamming reviews...
 282+ if( $wgUser->pingLimiter( 'feedback' ) || $wgUser->pingLimiter() ) {
 283+ return self::REVIEW_ERROR;
 284+ }
272285 # Check if user already voted before...
273286 if( self::userAlreadyVoted( $this->page, $this->oldid ) ) {
274 - return false;
 287+ return self::REVIEW_DUP;
275288 }
276289 # Update review records to limit double voting!
277290 $insertRow = array(
@@ -328,6 +341,6 @@
329342 if( $wgUser->getId() ) {
330343 $this->page->invalidateCache();
331344 }
332 - return true;
 345+ return self::REVIEW_OK;
333346 }
334347 }
Index: trunk/extensions/ReaderFeedback/language/ReaderFeedback.i18n.php
@@ -34,6 +34,7 @@
3535 'readerfeedback-main' => 'Only content pages can be rated.',
3636 'readerfeedback-success' => '\'\'\'Thank you for reviewing this page!\'\'\' ([$3 Comments or questions?]).',
3737 'readerfeedback-voted' => '\'\'\'It appears that you already rated this page\'\'\' ([$3 Comments or questions?]).',
 38+ 'readerfeedback-error' => '\'\'\'An error has occurred while rating this page\'\'\' ([$3 Comments or questions?]).',
3839 'readerfeedback-submitting' => 'Submitting …',
3940 'readerfeedback-finished' => 'Thank you!',
4041 'readerfeedback-tagfilter' => 'Tag:',
Index: trunk/extensions/ReaderFeedback/ReaderFeedback.php
@@ -58,6 +58,14 @@
5959 $wgFeedbackAge = 7 * 24 * 3600;
6060 # How long before stats page is updated?
6161 $wgFeedbackStatsAge = 2 * 3600; // 2 hours
 62+# Limit people from spamming the system
 63+# (uses count => seconds tuples)
 64+$wgRateLimits['feedback'] = array(
 65+ 'newbie' => array( 5, 60 ), // for each recent (autoconfirmed) account; overrides 'user'
 66+ 'user' => null, // for each logged-in user
 67+ 'ip' => array( 5, 60 ), // for each anon and recent account
 68+ 'subnet' => null, // ... with final octet removed
 69+);
6270
6371 # URL location for readerfeedback.css and readerfeedback.js
6472 # Use a literal $wgScriptPath as a placeholder for the runtime value of $wgScriptPath

Follow-up revisions

RevisionCommit summaryAuthorDate
r59620Reverted return style changes from r56326 (just used to reduce code lines)aaron08:03, 1 December 2009

Comments

#Comment by Tim Starling (talk | contribs)   05:58, 1 December 2009
-			$wgOut->readOnlyPage();
-			return;
+			return $wgOut->readOnlyPage();

The first version was correct. Don't pretend to use the return value of a function that doesn't return anything, it's confusing.

Status & tagging log