https://www.mediawiki.org/wiki/Special:Code/MediaWiki/109666

Revision: 109666
Author:   raindrift
Date:     2012-01-21 00:11:12 +0000 (Sat, 21 Jan 2012)
Log Message:
-----------
added checkoutResult() for generating more informative user-facing errors

Modified Paths:
--------------
    trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php
    trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php

Modified: trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php
===================================================================
--- trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php 
2012-01-21 00:09:24 UTC (rev 109665)
+++ trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php 
2012-01-21 00:11:12 UTC (rev 109666)
@@ -43,6 +43,7 @@
                // TODO: create a registry of all valid resourceTypes that 
client app can add to.
                $this->resourceType = $resourceType;
                $this->setExpirationTime( $expirationTime );
+               $this->lastCheckout = array();
        }
 
        /**
@@ -71,6 +72,7 @@
                                $cached['expiration'] > time()
                        ) {
                                // this is already checked out.
+                               $this->checkoutResult( $cached );
                                return false;
                        }
                }
@@ -95,14 +97,15 @@
                        // "By default, MediaWiki opens a transaction at the 
first query, and commits
                        // it before the output is sent."
                        // set the cache key, since this is inside an implicit 
transaction.
-                       $wgMemc->set( $cacheKey, array(
-                                       'userId' => $userId,
-                                       'expiration' => $expiration,
-                               ),
-                               $this->expirationTime
+                       $toCache = array(
+                               'userId' => $userId,
+                               'expiration' => $expiration,                    
        
                        );
                        
+                       $wgMemc->set( $cacheKey, $toCache, 
$this->expirationTime );
+                       
                        $dbw->commit( __METHOD__ );
+                       $this->checkoutResult( $toCache );
                        return true;
                }
 
@@ -130,13 +133,19 @@
                        // invalid then anyway.
                        // inside this transaction, a row-level lock is 
established which ensures cache
                        // concurrency
-                       $wgMemc->set( $cacheKey, array(
-                                       'userId' => $row->cc_user,
-                                       'expiration' => wfTimestamp( TS_UNIX, 
$row->cc_expiration )
-                               ),
+                       $toCache = array(
+                               'userId' => $row->cc_user,
+                               'expiration' => wfTimestamp( TS_UNIX, 
$row->cc_expiration )
+                       );
+                       
+                       $wgMemc->set(
+                               $cacheKey,
+                               $toCache,
                                wfTimestamp( TS_UNIX, $row->cc_expiration ) - 
time()
                        );
+                       
                        $dbw->rollback( __METHOD__ );
+                       $this->checkoutResult( $toCache );
                        return false;
                }
 
@@ -153,14 +162,17 @@
                        __METHOD__
                );
 
+               $toCache = array( 'userId' => $userId, 'expiration' => 
$expiration );
+
                // cache the result.
                $wgMemc->set( 
                        $cacheKey,
-                       array( 'userId' => $userId, 'expiration' => $expiration 
),
+                       $toCache,
                        $this->expirationTime
                );
 
                $dbw->commit( __METHOD__ );
+               $this->checkoutResult = $toCache;
                return true;
        }
 
@@ -387,6 +399,20 @@
        }
 
        /**
+        * Return information about the owner of the record on which a checkout 
was last
+        * attempted.
+        * 
+        * @param $checkoutInfo array (optional) of checkout information to 
store
+        * @return array
+        */
+       public function checkoutResult( $checkoutInfo = null ) {
+               if( isset( $checkoutInfo ) ) { // true on empty array
+                       $this->lastCheckout = $checkoutInfo;
+               }
+               return $this->lastCheckout;
+       }
+
+       /**
         * Setter for user.
         *
         * @param $user user

Modified: 
trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php
===================================================================
--- 
trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php    
    2012-01-21 00:09:24 UTC (rev 109665)
+++ 
trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php    
    2012-01-21 00:11:12 UTC (rev 109666)
@@ -48,6 +48,9 @@
        public function testCheckoutCheckin() {
                $first = new ConcurrencyCheck( 'CCUnitTest',  
self::$users['user1']->user );
                $second = new ConcurrencyCheck( 'CCUnitTest',  
self::$users['user2']->user );
+               $firstId = self::$users['user1']->user->getId();
+               $secondId = self::$users['user2']->user->getId();
+               
                $testKey = 1337;
 
                // clean up after any previously failed tests
@@ -56,11 +59,18 @@
 
                // tests
                $this->assertTrue( $first->checkout( $testKey ), "Initial 
checkout" );
+               $res = $first->checkoutResult();
+               $this->assertEquals( $firstId, $res['userId'], "User matches on 
success");
+               $this->assertTrue( array_key_exists( 'expiration', $res ), 
"Expiration is present");
+
                $this->assertTrue( $first->checkout( $testKey ), "Cache hit" );
                $this->assertFalse(
                        $second->checkout( $testKey ),
                        "Checkout of locked resource fails as different user"
                );
+               $res = $second->checkoutResult();
+               $this->assertEquals( $firstId, $res['userId'], "Actual owner 
matches on failure");
+               
                $this->assertTrue(
                        $first->checkout( $testKey ),
                        "Checkout of locked resource succeeds as original user"


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

Reply via email to