r24583 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24582‎ | r24583 | r24584 >
Date:14:41, 4 August 2007
Author:brion
Status:old
Tags:
Comment:
Add localnames and globalnames tables to give me more warm fuzzies, to more cleanly handle the optimized lists of existing accounts.
Lazy init just has to check globalnames, and localnames lists *all* whether attached or not.
Modified paths:
  • /trunk/extensions/CentralAuth/CentralAuthUser.php (modified) (history)
  • /trunk/extensions/CentralAuth/central-auth.sql (modified) (history)
  • /trunk/extensions/CentralAuth/sample-data.sql (deleted) (history)

Diff [purge]

Index: trunk/extensions/CentralAuth/sample-data.sql
@@ -1,10 +0,0 @@
2 -insert
3 -into globaluser
4 - (gu_id,gu_name,gu_email,gu_email_authenticated,
5 - gu_salt,gu_password,gu_locked,gu_hidden,
6 - gu_registration)
7 -values
8 - (1, 'Duderino', 'dude@localhost', '20060719012345',
9 - '34', MD5(CONCAT('34', '-', MD5('mycoolpass'))), 0, 0,
10 - '20060719012345');
11 -
Index: trunk/extensions/CentralAuth/CentralAuthUser.php
@@ -98,7 +98,7 @@
9999 $localuser = self::tableName( 'localuser' );
100100
101101 $sql =
102 - "SELECT gu_id, lu_attached
 102+ "SELECT gu_id, lu_dbname
103103 FROM $globaluser
104104 LEFT OUTER JOIN $localuser
105105 ON gu_name=lu_name
@@ -109,8 +109,8 @@
110110 $dbr->freeResult( $result );
111111
112112 if( $row ) {
113 - $this->mGlobalId = $row->gu_id;
114 - $this->mIsAttached = (bool)$row->lu_attached;
 113+ $this->mGlobalId = intval( $row->gu_id );
 114+ $this->mIsAttached = ($row->lu_dbname !== null);
115115 } else {
116116 $this->mGlobalId = 0;
117117 $this->mIsAttached = false;
@@ -596,8 +596,7 @@
597597 $valid = $this->validateList( $list );
598598
599599 $dbw = self::getCentralDB();
600 - $dbw->update( self::tableName( 'localuser' ),
601 - array( 'lu_attached' => 0 ),
 600+ $dbw->delete( self::tableName( 'localuser' ),
602601 array(
603602 'lu_name' => $this->mName,
604603 'lu_dbname' => $valid ),
@@ -627,39 +626,27 @@
628627 */
629628 public function attach( $dbname, $method='new' ) {
630629 $dbw = self::getCentralDB();
631 - $dbw->update( self::tableName( 'localuser' ),
 630+ $dbw->insert( self::tableName( 'localuser' ),
632631 array(
633 - 'lu_attached' => 1,
 632+ 'lu_dbname' => $dbname,
 633+ 'lu_name' => $this->mName ,
634634 'lu_attached_timestamp' => $dbw->timestamp(),
635635 'lu_attached_method' => $method ),
636 - array(
637 - 'lu_dbname' => $dbname,
638 - 'lu_name' => $this->mName ),
639 - __METHOD__ );
 636+ __METHOD__,
 637+ array( 'IGNORE' ) );
 638+
640639 if( $dbw->affectedRows() == 0 ) {
641 - // *frowny face*
642 -
643 - $dbw->insert( self::tableName( 'localuser' ),
644 - array(
645 - 'lu_dbname' => $dbname,
646 - 'lu_name' => $this->mName ,
647 - 'lu_attached' => 1,
648 - 'lu_attached_timestamp' => $dbw->timestamp(),
649 - 'lu_attached_method' => $method ),
650 - __METHOD__,
651 - array( 'IGNORE' ) );
652 -
653 - if( $dbw->affectedRows() == 0 ) {
654 - throw MWException( "Bogus attach" );
 640+ wfDebugLog( 'CentralAuth',
 641+ "Race condition? Already attached $this->mName@$dbname, just tried by '$method'" );
 642+ } else {
 643+ wfDebugLog( 'CentralAuth',
 644+ "Attaching local user $this->mName@$dbname by '$method'" );
 645+
 646+ global $wgDBname;
 647+ if( $dbname == $wgDBname ) {
 648+ $this->resetState();
655649 }
656650 }
657 - wfDebugLog( 'CentralAuth',
658 - "Attaching local user $this->mName@$dbname by '$method'" );
659 -
660 - global $wgDBname;
661 - if( $dbname == $wgDBname ) {
662 - $this->resetState();
663 - }
664651 }
665652
666653 /**
@@ -746,28 +733,48 @@
747734 * @return array of database name strings
748735 */
749736 public function listUnattached() {
750 - $unattached = $this->listLocalDatabases( 0 );
751 - if( !$unattached ) {
752 - // Nobody? Might not have imported local lists yet...
 737+ $unattached = $this->doListUnattached();
 738+ if( empty( $unattached ) ) {
753739 if( $this->lazyImportLocalNames() ) {
754 - $unattached = $this->listLocalDatabases( 0 );
 740+ $unattached = $this->doListUnattached();
755741 }
756742 }
757743 return $unattached;
758744 }
759745
 746+ function doListUnattached() {
 747+ $dbw = self::getCentralDB();
 748+
 749+ $sql = "
 750+ SELECT ln_dbname
 751+ FROM localnames
 752+ LEFT OUTER JOIN localuser
 753+ ON ln_dbname=lu_dbname AND ln_name=lu_name
 754+ WHERE ln_name=? AND lu_name IS NULL
 755+ ";
 756+ $result = $dbw->safeQuery( $sql, $this->mName );
 757+
 758+ $dbs = array();
 759+ while( $row = $dbw->fetchObject( $result ) ) {
 760+ $dbs[] = $row->ln_dbname;
 761+ }
 762+ $dbw->freeResult( $result );
 763+
 764+ return $dbs;
 765+ }
 766+
760767 function lazyImportLocalNames() {
761768 $dbw = self::getCentralDB();
762769
763 - $result = $dbw->select( self::tableName( 'localuser' ),
764 - array( 'lu_dbname' ),
765 - array( 'lu_name' => $this->mName ),
 770+ $result = $dbw->select( self::tableName( 'globalnames' ),
 771+ array( '1' ),
 772+ array( 'gn_name' => $this->mName ),
766773 __METHOD__,
767774 array( 'LIMIT' => 1 ) );
768 - $any = $result->numRows();
 775+ $known = $result->numRows();
769776 $result->free();
770777
771 - if( $any ) {
 778+ if( $known ) {
772779 // No need...
773780 return false;
774781 }
@@ -775,6 +782,10 @@
776783 return $this->importLocalNames();
777784 }
778785
 786+ /**
 787+ * Troll through the full set of local databases and list those
 788+ * which exist into the 'localnames' table.
 789+ */
779790 function importLocalNames() {
780791 global $wgLocalDatabases;
781792
@@ -788,15 +799,14 @@
789800 __METHOD__ );
790801 if( $id ) {
791802 $rows[] = array(
792 - 'lu_dbname' => $db,
793 - 'lu_name' => $this->mName,
794 - 'lu_attached' => 0 );
 803+ 'ln_dbname' => $db,
 804+ 'ln_name' => $this->mName );
795805 }
796806 }
797807
798808 if( $rows ) {
799809 $dbw = self::getCentralDB();
800 - $dbw->insert( self::tableName( 'localuser' ),
 810+ $dbw->insert( self::tableName( 'localnames' ),
801811 $rows,
802812 __METHOD__,
803813 array( 'IGNORE' ) );
@@ -814,9 +824,25 @@
815825 * @return array database name strings
816826 */
817827 public function listAttached() {
818 - return $this->listLocalDatabases( 1 );
 828+ $dbw = self::getCentralDB();
 829+
 830+ $result = $dbw->select( self::tableName( 'localuser' ),
 831+ array( 'lu_dbname' ),
 832+ array( 'lu_name' => $this->mName ),
 833+ __METHOD__ );
 834+
 835+ $dbs = array();
 836+ while( $row = $result->fetchObject() ) {
 837+ $dbs[] = $row->lu_dbname;
 838+ }
 839+ $dbw->freeResult( $result );
 840+
 841+ return $dbs;
819842 }
820843
 844+ /**
 845+ * Flooobie!
 846+ */
821847 private function listLocalDatabases( $attached ) {
822848 $dbw = self::getCentralDB();
823849
@@ -852,8 +878,7 @@
853879 'lu_attached_timestamp',
854880 'lu_attached_method' ),
855881 array(
856 - 'lu_name' => $this->mName,
857 - 'lu_attached' => 1 ),
 882+ 'lu_name' => $this->mName ),
858883 __METHOD__ );
859884
860885 $dbs = array();
Index: trunk/extensions/CentralAuth/central-auth.sql
@@ -4,9 +4,38 @@
55 -- USE centralauth;
66 -- GRANT all on centralauth.* to 'wikiuser'@'localhost';
77 -- source central-auth.sql
88
99 --
 10+-- This table simply lists all known usernames in the system.
 11+-- If no record is present here when migration processing begins,
 12+-- we know we have to sweep all the local databases and populate
 13+-- the localnames table.
 14+--
 15+CREATE TABLE globalnames (
 16+ gn_name varchar(255) binary not null,
 17+ primary key (gn_name)
 18+) /*$wgDBTableOptions*/;
 19+
 20+--
 21+-- For each known username in globalnames, the presence of an acount
 22+-- on each local database is listed here.
 23+--
 24+-- Email and password information used for migration checks are grabbed
 25+-- from local databases on demand when needed.
 26+--
 27+-- This is an optimization measure, so we don't have to poke on 600+
 28+-- separate databases to look for unmigrated accounts every time we log in;
 29+-- only existing databases not yet migrated have to be loaded.
 30+--
 31+CREATE TABLE localnames (
 32+ ln_dbname varchar(32) binary not null,
 33+ ln_name varchar(255) binary not null,
 34+
 35+ primary key (ln_dbname, ln_name),
 36+ key (ln_name, ln_dbname)
 37+) /*$wgDBTableOptions*/;
 38+
 39+--
1040 -- Global account data.
1141 --
1242 CREATE TABLE globaluser (
@@ -58,18 +87,14 @@
5988 ) /*$wgDBTableOptions*/;
6089
6190 --
 91+-- Local linkage info, listing which wikis the username is attached
 92+-- to the global account.
6293 --
6394 -- All local DBs will be swept on an opt-in check event.
6495 --
6596 CREATE TABLE localuser (
6697 lu_dbname varchar(32) binary not null,
6798 lu_name varchar(255) binary not null,
68 - lu_attached bool not null default 0,
6999
70100 -- Migration status/logging information, to help diagnose issues
71101 lu_attached_timestamp varchar(14) binary,

Status & tagging log