r60602 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60601‎ | r60602 | r60603 >
Date:14:58, 4 January 2010
Author:ialex
Status:deferred
Tags:
Comment:
* (bug 17636) Implemented memcached store
* (bug 17637) Implemented MySQL, PostgreSQL and SQLite store (automatically selected depending on $wgDBtype; only tested MySQL store)
Modified paths:
  • /trunk/extensions/OpenID/DatabaseConnection.php (added) (history)
  • /trunk/extensions/OpenID/MemcachedStore.php (added) (history)
  • /trunk/extensions/OpenID/OpenID.setup.php (modified) (history)
  • /trunk/extensions/OpenID/SpecialOpenID.body.php (modified) (history)
  • /trunk/extensions/OpenID/TODO (modified) (history)

Diff [purge]

Index: trunk/extensions/OpenID/SpecialOpenID.body.php
@@ -32,7 +32,7 @@
3333 class SpecialOpenID extends SpecialPage {
3434
3535 function getOpenIDStore( $storeType, $prefix, $options ) {
36 - global $wgOut;
 36+ global $wgOut, $wgMemc, $wgDBtype;
3737
3838 # FIXME: support other kinds of store
3939 # XXX: used to support memc, now use memcached from php-openid
@@ -49,6 +49,30 @@
5050 }
5151 return new Auth_OpenID_FileStore( $options['path'] );
5252
 53+ case 'db':
 54+ if ( $wgDBtype == 'sqlite' ) {
 55+ $db = new MediaWikiOpenIDDatabaseConnection( wfGetDB( DB_MASTER ) );
 56+ require_once( 'Auth/OpenID/SQLiteStore.php' );
 57+ return new Auth_OpenID_SQLiteStore( $db );
 58+ } else {
 59+ $lb = wfGetLBFactory()->newMainLB();
 60+ $db = new MediaWikiOpenIDDatabaseConnection( $lb->getConnection( DB_MASTER ) );
 61+ switch( $wgDBtype ) {
 62+ case 'mysql':
 63+ require_once( 'Auth/OpenID/MySQLStore.php' );
 64+ return new Auth_OpenID_MySQLStore( $db );
 65+ case 'postgres':
 66+ require_once( 'Auth/OpenID/PostgreSQLStore.php' );
 67+ return new Auth_OpenID_PostgreSQLStore( $db );
 68+ default:
 69+ $wgOut->showErrorPage( 'openidconfigerror', 'openidconfigerrortext' );
 70+ return NULL;
 71+ }
 72+ }
 73+
 74+ case 'memcached':
 75+ return new MediaWikiOpenIDMemcachedStore( $wgMemc );
 76+
5377 default:
5478 $wgOut->showErrorPage( 'openidconfigerror', 'openidconfigerrortext' );
5579 }
Index: trunk/extensions/OpenID/OpenID.setup.php
@@ -55,7 +55,8 @@
5656 $wgOpenIDServerForceAllowTrust = array();
5757
5858 /**
59 - * Where to store transitory data. Only supported type is 'file'.
 59+ * Where to store transitory data.
 60+ * Supported types are 'file', 'memcached', 'db'.
6061 */
6162 $wgOpenIDServerStoreType = 'file';
6263
@@ -91,7 +92,8 @@
9293 $wgOpenIDConsumerDeny = array();
9394
9495 /**
95 - * Where to store transitory data. Only supported type is 'file'.
 96+ * Where to store transitory data.
 97+ * Supported types are 'file', 'memcached', 'db'.
9698 */
9799 $wgOpenIDConsumerStoreType = 'file';
98100
@@ -177,6 +179,9 @@
178180 # Gets stored in the session, needs to be reified before our setup
179181 $wgAutoloadClasses['Auth_OpenID_CheckIDRequest'] = OpenIDGetServerPath();
180182
 183+$wgAutoloadClasses['MediaWikiOpenIDDatabaseConnection'] = $dir . 'DatabaseConnection.php';
 184+$wgAutoloadClasses['MediaWikiOpenIDMemcachedStore'] = $dir . 'MemcachedStore.php';
 185+
181186 $wgHooks['PersonalUrls'][] = 'OpenIDHooks::onPersonalUrls';
182187 $wgHooks['BeforePageDisplay'][] = 'OpenIDHooks::onBeforePageDisplay';
183188 $wgHooks['ArticleViewHeader'][] = 'OpenIDHooks::onArticleViewHeader';
Index: trunk/extensions/OpenID/TODO
@@ -1,5 +1,5 @@
22 + Add a Storage class for $wgMemc
3 -+ Prevent running OpenIDLogin, OpenIDFinish when you're already logged in
 3++ Prevent running OpenIDLogin when you're already logged in
44 + Add a server mode
55 + Prevent serving OpenID accounts
66 + Prevent trusting the same server
@@ -36,8 +36,8 @@
3737 like http://getopenid.com/evanprodromou or
3838 http://profile.typekey.com/EvanProdromou/
3939 + Allow specifying a FileStore
40 -- Allow specifying a MySQLStore
41 -- Allow specifying a memcached store
 40++ Allow specifying a MySQLStore
 41++ Allow specifying a memcached store
4242 + Fall back to FileStore if Memc is bogus
4343 + Unit test for MemcStore
4444 + for just-a-hostname OpenIDs, if the first element is not
Index: trunk/extensions/OpenID/MemcachedStore.php
@@ -0,0 +1,87 @@
 2+<?php
 3+
 4+require_once( 'Auth/OpenID/MemcachedStore.php' );
 5+
 6+class MediaWikiOpenIDMemcachedStore extends Auth_OpenID_MemcachedStore {
 7+
 8+ function __construct( $connection ) {
 9+ $this->connection = $connection;
 10+ }
 11+
 12+ function storeAssociation( $server_url, $association ) {
 13+ $associationKey = $this->associationKey( $server_url,
 14+ $association->handle );
 15+ $serverKey = $this->associationServerKey( $server_url );
 16+
 17+ $serverAssociations = $this->connection->get( $serverKey );
 18+
 19+ if ( !$serverAssociations ) {
 20+ $serverAssociations = array();
 21+ }
 22+
 23+ $serverAssociations[$association->issued] = $associationKey;
 24+
 25+ $this->connection->set( $serverKey, $serverAssociations );
 26+
 27+ $this->connection->set( $associationKey, $association,
 28+ $association->issued + $association->lifetime );
 29+ }
 30+
 31+ function getAssociation( $server_url, $handle = null ) {
 32+ if ( $handle !== null ) {
 33+ $association = $this->connection->get(
 34+ $this->associationKey( $server_url, $handle ) );
 35+ return $association ? $association : null;
 36+ }
 37+
 38+ $serverKey = $this->associationServerKey( $server_url );
 39+
 40+ $serverAssociations = $this->connection->get( $serverKey );
 41+ if ( !$serverAssociations ) {
 42+ return null;
 43+ }
 44+
 45+ $keys = array_keys( $serverAssociations );
 46+ sort( $keys );
 47+ $lastKey = $serverAssociations[array_pop( $keys )];
 48+
 49+ $association = $this->connection->get( $lastKey );
 50+ return $association ? $association : null;
 51+ }
 52+
 53+ function removeAssociation( $server_url, $handle ) {
 54+ $serverKey = $this->associationServerKey( $server_url );
 55+ $associationKey = $this->associationKey( $server_url,
 56+ $handle );
 57+
 58+ $serverAssociations = $this->connection->get( $serverKey );
 59+ if ( !$serverAssociations ) {
 60+ return false;
 61+ }
 62+
 63+ $serverAssociations = array_flip( $serverAssociations );
 64+ if ( !array_key_exists( $associationKey, $serverAssociations ) ) {
 65+ return false;
 66+ }
 67+
 68+ unset( $serverAssociations[$associationKey] );
 69+ $serverAssociations = array_flip( $serverAssociations );
 70+
 71+ $this->connection->set( $serverKey, $serverAssociations );
 72+
 73+ return $this->connection->delete( $associationKey );
 74+ }
 75+
 76+ function useNonce( $server_url, $timestamp, $salt ) {
 77+ global $Auth_OpenID_SKEW;
 78+
 79+ if ( abs( $timestamp - time() ) > $Auth_OpenID_SKEW ) {
 80+ return false;
 81+ }
 82+
 83+ return $this->connection->add(
 84+ 'openid_nonce_' . sha1( $server_url ) . '_' . sha1( $salt ),
 85+ 1, $Auth_OpenID_SKEW );
 86+ }
 87+
 88+}
Property changes on: trunk/extensions/OpenID/MemcachedStore.php
___________________________________________________________________
Name: svn:eol-style
189 + native
Index: trunk/extensions/OpenID/DatabaseConnection.php
@@ -0,0 +1,87 @@
 2+<?php
 3+
 4+require_once( 'Auth/OpenID/DatabaseConnection.php' );
 5+require_once( 'PEAR.php' );
 6+
 7+class MediaWikiOpenIDDatabaseConnection extends Auth_OpenID_DatabaseConnection {
 8+
 9+ function __construct( $db ) {
 10+ $this->db = $db;
 11+ }
 12+
 13+ function autoCommit( $mode ) {
 14+ $old = $this->db->getFlag( DBO_TRX );
 15+ if ( $mode ) {
 16+ $this->db->setFlag( DBO_TRX );
 17+ } else {
 18+ $this->db->clearFlag( DBO_TRX );
 19+ }
 20+ return $old;
 21+ }
 22+
 23+ function query( $sql, $params = array() ) {
 24+ try {
 25+ $res = $this->db->safeQuery( $sql, $params );
 26+ } catch( DBQueryError $e ) {
 27+ $ret = PEAR::raiseError( $e->getMessage(), 1, PEAR_ERROR_RETURN );
 28+ }
 29+ return $res;
 30+ }
 31+
 32+ function begin() {
 33+ $this->db->commit();
 34+ }
 35+
 36+ function commit() {
 37+ $this->db->commit();
 38+ }
 39+
 40+ function rollback() {
 41+ $this->db->rollback();
 42+ }
 43+
 44+ function getOne( $sql, $params = array() ) {
 45+ $res = $this->query( $sql, $params );
 46+
 47+ if ( !$res instanceof ResultWrapper )
 48+ return $res;
 49+ if ( !$res->numRows() )
 50+ return false;
 51+
 52+ $row = $res->fetchRow();
 53+ if ( $row !== false ) {
 54+ $res->free();
 55+ return reset( $row );
 56+ } else {
 57+ return false;
 58+ }
 59+ }
 60+
 61+ function getRow( $sql, $params = array() ) {
 62+ $res = $this->query( $sql, $params );
 63+
 64+ if ( !$res instanceof ResultWrapper )
 65+ return $res;
 66+ if ( !$res->numRows() )
 67+ return false;
 68+
 69+ $row = $res->fetchRow();
 70+ $res->free();
 71+ return $row;
 72+ }
 73+
 74+ function getAll( $sql, $params = array() ) {
 75+ $res = $this->query( $sql, $params );
 76+
 77+ if ( !$res instanceof ResultWrapper )
 78+ return $res;
 79+ if ( !$res->numRows() )
 80+ return false;
 81+
 82+ $ret = array();
 83+ foreach( $res as $row ) {
 84+ $ret[] = (array)$row;
 85+ }
 86+ return $ret;
 87+ }
 88+}
Property changes on: trunk/extensions/OpenID/DatabaseConnection.php
___________________________________________________________________
Name: svn:eol-style
189 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r60603Follow-up r60602: this comment is no longer neededialex15:20, 4 January 2010
r60609One more follow-up r60602: seems we have a README file :)ialex18:39, 4 January 2010

Status & tagging log