r108864 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108863‎ | r108864 | r108865 >
Date:22:17, 13 January 2012
Author:siebrand
Status:ok
Tags:miscextensions 
Comment:
stylize.php and some minor identation and whitespace updates.
L10n update
Modified paths:
  • /trunk/extensions/InterfaceConcurrency/ApiConcurrency.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.hooks.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.i18n.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/tests/phpunit/ApiConcurrencyTest.php (modified) (history)
  • /trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.i18n.php
@@ -12,9 +12,9 @@
1313 * @author Ian Baker
1414 */
1515 $messages['en'] = array(
16 - 'interfaceconcurrency-desc' => 'Provides backend for UI that shows which resources are in use',
 16+ 'interfaceconcurrency-desc' => 'Provides a backend for the user interface that shows which resources are in use',
1717 );
1818
1919 $messages['qqq'] = array(
20 - 'interfaceconcurrency-desc' => 'Description of this extension',
 20+ 'interfaceconcurrency-desc' => '{{desc}}',
2121 );
Index: trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.php
@@ -24,7 +24,7 @@
2525 // Check environment
2626 if ( !defined( 'MEDIAWIKI' ) ) {
2727 echo( "This is an extension to MediaWiki and cannot be run standalone.\n" );
28 - die( - 1 );
 28+ die( -1 );
2929 }
3030
3131 // Credits
Index: trunk/extensions/InterfaceConcurrency/tests/phpunit/ApiConcurrencyTest.php
@@ -78,7 +78,7 @@
7979
8080 $wgUser = self::$users['one']->user;
8181 /* commenting these out since i need to go home and they're breakin CI. See commit summary for details.
82 -
 82+
8383 list( $result, , $session ) = $this->doApiRequestWithToken( array(
8484 'action' => 'concurrency',
8585 'ccaction' => 'checkout',
@@ -142,7 +142,7 @@
143143 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['two'], self::$users['two']->user );
144144
145145 $this->assertEquals( "success", $result['concurrency']['result'] );
146 - */
 146+ */
147147 }
148148
149149 /**
@@ -152,17 +152,17 @@
153153 $exception = false;
154154 try {
155155 global $wgUser;
156 -
 156+
157157 $wgUser = self::$users['one']->user;
158 -
 158+
159159 list( $result, , $session ) = $this->doApiRequestWithToken( array(
160160 'action' => 'concurrency',
161161 'ccaction' => 'checkinX',
162162 'record' => 1,
163 - 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['one'], self::$users['one']->user );
 163+ 'resourcetype' => 'responding-to-moodbar-feedback' ), $sessionArray['one'], self::$users['one']->user );
164164 } catch ( UsageException $e ) {
165165 $exception = true;
166 - $this->assertEquals("Unrecognized value for parameter 'ccaction': checkinX",
 166+ $this->assertEquals( "Unrecognized value for parameter 'ccaction': checkinX",
167167 $e->getMessage() );
168168 }
169169 $this->assertTrue( $exception, "Got exception" );
Index: trunk/extensions/InterfaceConcurrency/tests/phpunit/ConcurrencyCheckTest.php
@@ -10,7 +10,7 @@
1111
1212 public function setUp() {
1313 parent::setUp();
14 -
 14+
1515 self::$users = array(
1616 'user1' => new ApiTestUser(
1717 'Concurrencychecktestuser1',
@@ -43,38 +43,38 @@
4444 }
4545
4646 // Actual tests from here down
47 -
 47+
4848 public function testCheckoutCheckin() {
4949 $first = new ConcurrencyCheck( 'CCUnitTest', self::$users['user1']->user );
5050 $second = new ConcurrencyCheck( 'CCUnitTest', self::$users['user2']->user );
5151 $testKey = 1337;
5252
5353 // clean up after any previously failed tests
54 - $first->checkin($testKey);
55 - $second->checkin($testKey);
 54+ $first->checkin( $testKey );
 55+ $second->checkin( $testKey );
5656
5757 // tests
58 - $this->assertTrue( $first->checkout($testKey), "Initial checkout" );
59 - $this->assertTrue( $first->checkout($testKey), "Cache hit" );
60 - $this->assertFalse( $second->checkout($testKey), "Checkout of locked resource fails as different user" );
61 - $this->assertTrue( $first->checkout($testKey), "Checkout of locked resource succeeds as original user" );
62 - $this->assertFalse( $second->checkin($testKey), "Checkin of locked resource fails as different user" );
63 - $this->assertTrue( $first->checkin($testKey), "Checkin of locked resource succeeds as original user" );
64 - $second->setExpirationTime(-5);
65 - $this->assertTrue( $second->checkout($testKey), "Checked-in resource is now available to second user" );
 58+ $this->assertTrue( $first->checkout( $testKey ), "Initial checkout" );
 59+ $this->assertTrue( $first->checkout( $testKey ), "Cache hit" );
 60+ $this->assertFalse( $second->checkout( $testKey ), "Checkout of locked resource fails as different user" );
 61+ $this->assertTrue( $first->checkout( $testKey ), "Checkout of locked resource succeeds as original user" );
 62+ $this->assertFalse( $second->checkin( $testKey ), "Checkin of locked resource fails as different user" );
 63+ $this->assertTrue( $first->checkin( $testKey ), "Checkin of locked resource succeeds as original user" );
 64+ $second->setExpirationTime( -5 );
 65+ $this->assertTrue( $second->checkout( $testKey ), "Checked-in resource is now available to second user" );
6666 $second->setExpirationTime();
67 - $this->assertTrue( $first->checkout($testKey), "Checkout of expired resource succeeds as first user");
68 - $this->assertTrue( $second->checkout($testKey, true), "Checkout override" );
69 - $this->assertFalse( $first->checkout($testKey), "Checkout of overriden resource fails as different user" );
 67+ $this->assertTrue( $first->checkout( $testKey ), "Checkout of expired resource succeeds as first user" );
 68+ $this->assertTrue( $second->checkout( $testKey, true ), "Checkout override" );
 69+ $this->assertFalse( $first->checkout( $testKey ), "Checkout of overriden resource fails as different user" );
7070
7171 // cleanup
72 - $this->assertTrue( $second->checkin($testKey), "Checkin of record with changed ownership" );
73 -
 72+ $this->assertTrue( $second->checkin( $testKey ), "Checkin of record with changed ownership" );
 73+
7474 }
7575
7676 public function testExpire() {
7777 $cc = new ConcurrencyCheck( 'CCUnitTest', self::$users['user1']->user );
78 - $cc->setExpirationTime(-1);
 78+ $cc->setExpirationTime( -1 );
7979 $cc->checkout( 1338 ); // these numbers are test record ids.
8080 $cc->checkout( 1339 );
8181 $cc->setExpirationTime();
@@ -82,14 +82,14 @@
8383
8484 // tests
8585 $this->assertEquals( 2, $cc->expire(), "Resource expiration" );
86 - $this->assertTrue( $cc->checkin( 13310 ), "Checkin succeeds after expiration" );
 86+ $this->assertTrue( $cc->checkin( 13310 ), "Checkin succeeds after expiration" );
8787 }
88 -
 88+
8989 public function testStatus() {
9090 $cc = new ConcurrencyCheck( 'CCUnitTest', self::$users['user1']->user );
9191 $cc->checkout( 1337 );
9292 $cc->checkout( 1338 );
93 - $cc->setExpirationTime(-5);
 93+ $cc->setExpirationTime( -5 );
9494 $cc->checkout( 1339 );
9595 $cc->setExpirationTime();
9696
@@ -97,8 +97,8 @@
9898 $output = $cc->status( array( 1337, 1338, 1339, 13310 ) );
9999 $this->assertEquals( true, is_array( $output ), "Status returns values" );
100100 $this->assertEquals( 4, count( $output ), "Output has the correct number of records" );
101 - $this->assertEquals( 'valid', $output[1337]['status'], "Current checkouts are listed as valid");
102 - $this->assertEquals( 'invalid', $output[1339]['status'], "Expired checkouts are invalid");
103 - $this->assertEquals( 'invalid', $output[13310]['status'], "Missing checkouts are invalid");
 101+ $this->assertEquals( 'valid', $output[1337]['status'], "Current checkouts are listed as valid" );
 102+ $this->assertEquals( 'invalid', $output[1339]['status'], "Expired checkouts are invalid" );
 103+ $this->assertEquals( 'invalid', $output[13310]['status'], "Missing checkouts are invalid" );
104104 }
105105 }
Index: trunk/extensions/InterfaceConcurrency/InterfaceConcurrency.hooks.php
@@ -1,18 +1,16 @@
22 <?php
33
44 class InterfaceConcurrencyHooks {
5 -
65 /**
76 * Runs InterfaceConcurrency schema updates#
87 *
98 * @param $updater DatabaseUpdater
109 */
1110 public static function onLoadExtensionSchemaUpdates( $updater = null ) {
12 - $dir = dirname(__FILE__) . '/sql';
 11+ $dir = dirname( __FILE__ ) . '/sql';
1312
1413 $updater->addExtensionTable( 'concurrencycheck', "$dir/concurrencycheck.sql" );
15 -
 14+
1615 return true;
1716 }
18 -
19 -}
\ No newline at end of file
 17+}
Index: trunk/extensions/InterfaceConcurrency/includes/ConcurrencyCheck.php
@@ -18,7 +18,6 @@
1919 * @author Ian Baker <ian@wikimedia.org>
2020 */
2121 class ConcurrencyCheck {
22 -
2322 protected $expirationTime;
2423
2524 /**
@@ -28,7 +27,7 @@
2928
3029 /**
3130 * Constructor
32 - *
 31+ *
3332 * @var $resourceType String The calling application or type of resource, conceptually like a namespace
3433 * @var $user User object, the current user
3534 * @var $expirationTime Integer (optional) How long should a checkout last, in seconds
@@ -62,16 +61,16 @@
6362
6463 // when operating with a single memcached cluster, it's reasonable to check the cache here.
6564 global $wgConcurrency;
66 - if( $wgConcurrency['TrustMemc'] ) {
67 - $cached = $wgMemc->get( $cacheKey );
68 - if( $cached ) {
69 - if( ! $override && $cached['userId'] != $userId && $cached['expiration'] > time() ) {
 65+ if ( $wgConcurrency['TrustMemc'] ) {
 66+ $cached = $wgMemc->get( $cacheKey );
 67+ if ( $cached ) {
 68+ if ( ! $override && $cached['userId'] != $userId && $cached['expiration'] > time() ) {
7069 // this is already checked out.
7170 return false;
7271 }
7372 }
7473 }
75 -
 74+
7675 // attempt an insert, check success (this is atomic)
7776 $insertError = null;
7877 $res = $dbw->insert(
@@ -87,7 +86,7 @@
8887 );
8988
9089 // if the insert succeeded, checkout is done.
91 - if( $dbw->affectedRows() === 1 ) {
 90+ if ( $dbw->affectedRows() === 1 ) {
9291 // delete any existing cache key. can't create a new key here
9392 // since the insert didn't happen inside a transaction.
9493 $wgMemc->delete( $cacheKey );
@@ -103,14 +102,14 @@
104103 array(
105104 'cc_resource_type' => $this->resourceType,
106105 'cc_record' => $record,
107 - '(cc_user = ' . $userId . ' OR cc_expiration <= ' . $dbw->addQuotes(wfTimestamp( TS_MW )) . ')', // only the owner can perform a checkin
 106+ '(cc_user = ' . $userId . ' OR cc_expiration <= ' . $dbw->addQuotes( wfTimestamp( TS_MW ) ) . ')', // only the owner can perform a checkin
108107 ),
109108 __METHOD__,
110109 array()
111110 );
112 -
 111+
113112 // delete failed: not checked out by current user, checkout is unexpired, override is unset
114 - if( $dbw->affectedRows() !== 1 && ! $override) {
 113+ if ( $dbw->affectedRows() !== 1 && ! $override ) {
115114 // fetch the existing data to cache it
116115 $row = $dbw->selectRow(
117116 'concurrencycheck',
@@ -122,7 +121,7 @@
123122 __METHOD__,
124123 array()
125124 );
126 -
 125+
127126 // this was a cache miss. populate the cache with data from the db.
128127 // cache is set to expire at the same time as the checkout, since it'll become invalid then anyway.
129128 // inside this transaction, a row-level lock is established which ensures cache concurrency
@@ -149,7 +148,7 @@
150149
151150 // cache the result.
152151 $wgMemc->set( $cacheKey, array( 'userId' => $userId, 'expiration' => $expiration ), $this->expirationTime );
153 -
 152+
154153 $dbw->commit();
155154 return true;
156155 }
@@ -162,7 +161,7 @@
163162 */
164163 public function checkin( $record ) {
165164 global $wgMemc;
166 - $this->validateId( $record );
 165+ $this->validateId( $record );
167166 $dbw = $this->dbw;
168167 $userId = $this->user->getId();
169168 $cacheKey = wfMemcKey( 'concurrencycheck', $this->resourceType, $record );
@@ -179,7 +178,7 @@
180179 );
181180
182181 // check row count (this is atomic, select would not be)
183 - if( $dbw->affectedRows() === 1 ) {
 182+ if ( $dbw->affectedRows() === 1 ) {
184183 $wgMemc->delete( $cacheKey );
185184 return true;
186185 }
@@ -220,11 +219,11 @@
221220 $toSelect = array();
222221
223222 // validate keys, attempt to retrieve from cache.
224 - foreach( $keys as $key ) {
 223+ foreach ( $keys as $key ) {
225224 $this->validateId( $key );
226225
227226 $cached = $wgMemc->get( wfMemcKey( 'concurrencycheck', $this->resourceType, $key ) );
228 - if( $cached && $cached['expiration'] > $now ) {
 227+ if ( $cached && $cached['expiration'] > $now ) {
229228 $checkouts[$key] = array(
230229 'status' => 'valid',
231230 'cc_resource_type' => $this->resourceType,
@@ -239,11 +238,11 @@
240239 }
241240
242241 // if there were cache misses...
243 - if( $toSelect ) {
 242+ if ( $toSelect ) {
244243 // If it's time to go to the database, go ahead and expire old rows.
245244 $this->expire();
246 -
247 -
 245+
 246+
248247 // Why LOCK IN SHARE MODE, you might ask? To avoid a race condition: Otherwise, it's possible for
249248 // a checkin and/or checkout to occur between this select and the value being stored in cache, which
250249 // makes for an incorrect cache. This, in turn, could make checkout() above (which uses the cache)
@@ -257,13 +256,13 @@
258257 // It appears all the DBMSes we use support LOCK IN SHARE MODE, but if that's not the case, the second
259258 // solution above could be implemented instead.
260259 $queryParams = array();
261 - if( $wgDBtype === 'mysql' ) {
 260+ if ( $wgDBtype === 'mysql' ) {
262261 $queryParamsp[] = 'LOCK IN SHARE MODE';
263262
264263 // the transaction seems incongruous, I know, but it's to keep the cache update atomic.
265264 $dbw->begin();
266265 }
267 -
 266+
268267 $res = $dbw->select(
269268 'concurrencycheck',
270269 array( '*' ),
@@ -276,13 +275,13 @@
277276 $queryParams
278277 );
279278
280 - while( $res && $record = $res->fetchRow() ) {
 279+ while ( $res && $record = $res->fetchRow() ) {
281280 $record['status'] = 'valid';
282281 $checkouts[ $record['cc_record'] ] = $record;
283282
284283 // TODO: implement strategy #2 above, determine which DBMSes need which method.
285284 // for now, disable adding to cache here for databases that don't support read locking
286 - if( $wgDBtype !== 'mysql' ) {
 285+ if ( $wgDBtype !== 'mysql' ) {
287286 // safe to store values since this is inside the transaction
288287 $wgMemc->set(
289288 wfMemcKey( 'concurrencycheck', $this->resourceType, $record['cc_record'] ),
@@ -292,7 +291,7 @@
293292 }
294293 }
295294
296 - if( $wgDBtype === 'mysql' ) {
 295+ if ( $wgDBtype === 'mysql' ) {
297296 // end the transaction.
298297 $dbw->rollback();
299298 }
@@ -300,8 +299,8 @@
301300
302301 // if a key was passed in but has no (unexpired) checkout, include it in the
303302 // result set to make things easier and more consistent on the client-side.
304 - foreach( $keys as $key ) {
305 - if( ! array_key_exists( $key, $checkouts ) ) {
 303+ foreach ( $keys as $key ) {
 304+ if ( ! array_key_exists( $key, $checkouts ) ) {
306305 $checkouts[$key]['status'] = 'invalid';
307306 }
308307 }
@@ -322,11 +321,11 @@
323322
324323 public function setExpirationTime( $expirationTime = null ) {
325324 global $wgConcurrency;
326 -
 325+
327326 // check to make sure the time is a number
328327 // negative number are allowed, though mostly only used for testing
329 - if( $expirationTime && (int) $expirationTime == $expirationTime ) {
330 - if( $expirationTime > $wgConcurrency['ExpirationMax'] ) {
 328+ if ( $expirationTime && (int) $expirationTime == $expirationTime ) {
 329+ if ( $expirationTime > $wgConcurrency['ExpirationMax'] ) {
331330 $this->expirationTime = $wgConcurrency['ExpirationMax']; // if the number is too high, limit it to the max value.
332331 } elseif ( $expirationTime < $wgConcurrency['ExpirationMin'] ) {
333332 $this->expirationTime = $wgConcurrency['ExpirationMin']; // low limit, default -1 min
@@ -346,7 +345,7 @@
347346 * @return boolean
348347 */
349348 private static function validateId ( $record ) {
350 - if( (int) $record !== $record || $record <= 0 ) {
 349+ if ( (int) $record !== $record || $record <= 0 ) {
351350 throw new ConcurrencyCheckBadRecordIdException( 'Record ID ' . $record . ' must be a positive integer' );
352351 }
353352
Index: trunk/extensions/InterfaceConcurrency/ApiConcurrency.php
@@ -92,7 +92,7 @@
9393 public function getVersion() {
9494 return __CLASS__ . ': $Id: ApiConcurrency.php 108560 2012-01-10 23:12:00Z reedy $';
9595 }
96 -
 96+
9797 private function checkPermission( $user ) {
9898 if ( $user->isAnon() ) {
9999 $this->dieUsage( "You don't have permission to do that", 'permission-denied' );
@@ -101,5 +101,4 @@
102102 $this->dieUsageMsg( array( 'blockedtext' ) );
103103 }
104104 }
105 -
106105 }

Status & tagging log