Mattflaschen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53909


Change subject: Use single-parameter version of srand.
......................................................................

Use single-parameter version of srand.

Change-Id: I4a7cdb3b74f54e46a95846ababc147c227b31e09
---
M CategoryRoulette.php
1 file changed, 19 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GettingStarted 
refs/changes/09/53909/1

diff --git a/CategoryRoulette.php b/CategoryRoulette.php
index 70d0448..b484440 100644
--- a/CategoryRoulette.php
+++ b/CategoryRoulette.php
@@ -12,6 +12,7 @@
  *
  */
 class CategoryRoulette {
+       const MAX_ATTEMPTS = 20;
 
        /** @var Category **/
        public $category = null;
@@ -43,15 +44,24 @@
                        return array();
                }
 
-               try {
-                       $articleIDs = $redis->sRandMember( $key, $numWanted );
-               } catch ( RedisException $e ) {
-                       wfDebugLog( 'GettingStarted', 'Redis exception: ' . 
$e->getMessage() . "\n" );
-                       return array();
-               }
-
-               if ( !$articleIDs ) {
-                       return array();
+               $articleIDs = array();
+               $attempts = 0;
+               while ( count( $articleIDs ) < $numWanted ) {
+                       $attempts++;
+                       // Sanity check to prevent calling srand too many times
+                       if ( $attempts >= self::MAX_ATTEMPTS ) {
+                               wfDebugLog( 'GettingStarted', 'Returning early 
after ' . self::MAX_ATTEMPTS . ".\n" );
+                               return Title::newFromIDs( $articleIDs );
+                       }
+                       try {
+                               $randomID = $redis->sRandMember( $key );
+                               if ( !in_array( $randomID, $articleIDs, true ) 
) {
+                                       $articleIDs[] = $randomID;
+                               }
+                       } catch ( RedisException $e ) {
+                               wfDebugLog( 'GettingStarted', 'Redis exception: 
' . $e->getMessage() . ".  Returning early.\n" );
+                               return Title::newFromIDs( $articleIDs );
+                       }
                }
 
                return Title::newFromIDs( $articleIDs );

-- 
To view, visit https://gerrit.wikimedia.org/r/53909
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4a7cdb3b74f54e46a95846ababc147c227b31e09
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GettingStarted
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to