Renamed group table to groups, and renamed the fields from group_xxx to gr_xxx. Added...
authorTim Starling <tstarling@users.mediawiki.org>
Sun, 15 May 2005 06:18:48 +0000 (06:18 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sun, 15 May 2005 06:18:48 +0000 (06:18 +0000)
includes/Group.php
includes/SpecialListusers.php
maintenance/archives/patch-rename-group.sql [new file with mode: 0644]
maintenance/archives/patch-userlevels-defaultgroups.sql
maintenance/archives/patch-userlevels-rights.sql
maintenance/archives/patch-userlevels.sql
maintenance/parserTests.php
maintenance/tables.sql
maintenance/updaters.inc

index 958cfe0..cfaeb46 100644 (file)
@@ -52,9 +52,9 @@ class Group {
                if($this->id) {
                        // By ID
                        $dbr =& wfGetDB( DB_SLAVE );
-                       $r = $dbr->selectRow('group',
-                               array('group_id', 'group_name', 'group_description', 'group_rights'),
-                               array( 'group_id' => $this->id ),
+                       $r = $dbr->selectRow('groups',
+                               array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
+                               array( 'gr_id' => $this->id ),
                                $fname );
                        if ( $r ) {
                                $this->loadFromRow( $r );
@@ -65,9 +65,9 @@ class Group {
                } else {
                        // By name
                        $dbr =& wfGetDB( DB_SLAVE );
-                       $r = $dbr->selectRow('group',
-                               array('group_id', 'group_name', 'group_description', 'group_rights'),
-                               array( 'group_name' => $this->name ),
+                       $r = $dbr->selectRow('groups',
+                               array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
+                               array( 'gr_name' => $this->name ),
                                $fname );
                        if ( $r ) {
                                $this->loadFromRow( $r );
@@ -80,10 +80,10 @@ class Group {
 
        /** Initialise from a result row */
        function loadFromRow( &$row ) {
-               $this->id = $row->group_id;
-               $this->name = $row->group_name;
-               $this->description = $row->group_description;
-               $this->rights = $row->group_rights;
+               $this->id = $row->gr_id;
+               $this->name = $row->gr_name;
+               $this->description = $row->gr_description;
+               $this->rights = $row->gr_rights;
                $this->dataLoaded = true;
        }               
        
@@ -95,18 +95,20 @@ class Group {
 
                $fname = 'Group::addToDatabase';
                $dbw =& wfGetDB( DB_MASTER );
-               $dbw->insert( 'group',
+               $dbw->insert( 'groups',
                        array(
-                               'group_name' => $this->name,
-                               'group_description' => $this->description,
-                               'group_rights' => $this->rights
+                               'gr_name' => $this->name,
+                               'gr_description' => $this->description,
+                               'gr_rights' => $this->rights
                        ), $fname
                );
                $this->id = $dbw->insertId();
        }
 
-       /** Save the group datas into database */
+       /** Save the group data into database */
        function save() {
+               global $wgMemc;
+
                if ( Group::getStaticGroups() ) {
                        wfDebugDieBacktrace( "Can't modify groups in static mode" );
                }
@@ -115,19 +117,24 @@ class Group {
                $fname = 'Group::save';
                $dbw =& wfGetDB( DB_MASTER );
                
-               $dbw->update( 'group',
+               $dbw->update( 'groups',
                        array( /* SET */
-                               'group_name' => $this->name,
-                               'group_description' => $this->description,
-                               'group_rights' => $this->rights
+                               'gr_name' => $this->name,
+                               'gr_description' => $this->description,
+                               'gr_rights' => $this->rights
                        ), array( /* WHERE */
-                               'group_id' => $this->id
+                               'gr_id' => $this->id
                        ), $fname
-               );              
+               );
+       
+               $wgMemc->set( Group::getCacheKey( $this->id ), $this, 3600 );
        }
 
+
        /** Delete a group */
        function delete() {
+               global $wgMemc;
+
                if ( Group::getStaticGroups() ) {
                        wfDebugDieBacktrace( "Can't modify groups in static mode" );
                }
@@ -140,9 +147,20 @@ class Group {
                $dbw->delete( 'user_group', array( 'ug_group' => $this->id ), $fname );
 
                // Now delete the group
-               $dbw->delete( 'group', array( 'group_id' => $this->id ), $fname );
+               $dbw->delete( 'groups', array( 'gr_id' => $this->id ), $fname );
+
+               $wgMemc->delete( Group::getCacheKey( $this->id ) );
        }
        
+       /** 
+        * Get memcached key 
+        * @static
+        */
+       function getCacheKey( $id ) {
+               global $wgDBname;
+               return "$wgDBname:groups:id:$id";
+       }
+
 // Factories
        /**
         * Uses Memcached if available.
@@ -161,7 +179,8 @@ class Group {
                        }
                }
 
-               $key = "$wgDBname:groups:id:$id";
+               $key = Group::getCacheKey( $id );
+
                if( $group = $wgMemc->get( $key ) ) {
                        wfDebug( "$fname loaded group $id from cache\n" );
                        return $group;
@@ -222,8 +241,8 @@ class Group {
                wfProfileIn( $fname );
 
                $dbr =& wfGetDB( DB_SLAVE );
-               $groupTable = $dbr->tableName( 'group' );
-               $sql = "SELECT group_id, group_name, group_description, group_rights FROM $groupTable";
+               $groupTable = $dbr->tableName( 'groups' );
+               $sql = "SELECT gr_id, gr_name, gr_description, gr_rights FROM $groupTable";
                $res = $dbr->query($sql, $fname);
 
                $groups = array();
@@ -231,7 +250,7 @@ class Group {
                while($row = $dbr->fetchObject( $res ) ) {
                        $group = new Group;
                        $group->loadFromRow( $row );
-                       $groups[$row->group_id] = $group;
+                       $groups[$row->gr_id] = $group;
                }
 
                wfProfileOut( $fname );
@@ -282,7 +301,7 @@ class Group {
                if ( $staticGroups ) {
                        foreach( $staticGroups as $id => $group ) {
                                if ( $group->getName() === $name ) {
-                                       return $group->getID();
+                                       return $group->getId();
                                }
                        }
                        return 0;
@@ -290,12 +309,12 @@ class Group {
 
 
                $dbr =& wfGetDB( DB_SLAVE );
-               $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname );
+               $r = $dbr->selectRow( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname );
 
                if($r === false) {
                        return 0;
                } else {
-                       return $r->group_id;
+                       return $r->gr_id;
                }
        }
 
index 3794c85..28837db 100644 (file)
@@ -66,19 +66,16 @@ class ListUsersPage extends QueryPage {
                                '<input type="hidden" name="title" value="'.$special.'" />' .
                                wfMsg( 'grouplevels-editgroup-name' ) . '<select name="group">';
 
-               // get all group names and id
-               $dbr = & wfGetDB( DB_SLAVE );
-               $group = $dbr->tableName( 'group' );
-               $sql = "SELECT group_id, group_name FROM $group;";
-               $result = $dbr->query($sql);
+               // get all group names and IDs
+               $groups =& Group::getAllGroups();
                
                // we want a default empty group
                $out.= '<option value=""></option>';
                
                // build the dropdown list menu using datas from the database
-               while($agroup = $dbr->fetchObject( $result )) {
-                       $selected = ($agroup->group_id == $this->requestedGroup) ? ' selected ' : '' ;
-                       $out.= '<option value="'.$agroup->group_id.'" '.$selected.'>'.$agroup->group_name.'</option>';
+               foreach ( $groups as $group ) {
+                       $selected = ($group->getId() == $this->requestedGroup) ? ' selected ' : '' ;
+                       $out.= '<option value="'.$group->getId().'" '.$selected.'>'.$group->getExpandedName().'</option>';
                }
                $out .= '</select> ';
 
@@ -101,17 +98,15 @@ class ListUsersPage extends QueryPage {
        */
        /** Show groups instead */
                $user = $dbr->tableName( 'user' );
-               $group = $dbr->tableName( 'group' );
                $user_groups = $dbr->tableName( 'user_groups' );
                
                $userspace = NS_USER;
-               $sql = "SELECT group_name as type, $userspace AS namespace, user_name AS title, user_name as value " .
+               $sql = "SELECT CONCAT('Listusers ', ug_group) as type, $userspace AS namespace, user_name AS title, user_name as value " .
                        "FROM $user ".
-                       "LEFT JOIN $user_groups ON user_id =ug_user " .
-                       "LEFT JOIN $group ON ug_group = group_id ";
+                       "LEFT JOIN $user_groups ON user_id =ug_user ";
 
                if($this->requestedGroup != '') {
-                       $sql .=  "WHERE group_id= '" . IntVal( $this->requestedGroup ) . "' ";
+                       $sql .=  "WHERE ug_group = '" . IntVal( $this->requestedGroup ) . "' ";
                        if($this->requestedUser != '') {
                                $sql .= "AND user_name = " . $dbr->addQuotes( $this->requestedUser ) . ' ';
                        }
@@ -119,7 +114,7 @@ class ListUsersPage extends QueryPage {
                        if($this->requestedUser !='') {
                                $sql .= "WHERE user_name = " . $dbr->addQuotes( $this->requestedUser ) . ' ';
                        }       
-               }                               
+               }
                
                return $sql;
        }
@@ -160,7 +155,11 @@ class ListUsersPage extends QueryPage {
                }
 
                if( is_object( $result ) && $result->type != '') {
-                       $this->appendGroups( $skin->makeLink( wfMsgForContent( 'administrators' ), $result->type ) );
+                       $group = Group::newFromId( intval( strstr( $result->type, ' ' ) ) );
+                       if ( $group ) {
+                               $groupName = $group->getExpandedName();
+                               $this->appendGroups( $skin->makeLink( wfMsgForContent( 'administrators' ), $groupName ) );
+                       }
                }
 
                $this->previousResult = $result;
diff --git a/maintenance/archives/patch-rename-group.sql b/maintenance/archives/patch-rename-group.sql
new file mode 100644 (file)
index 0000000..026b60b
--- /dev/null
@@ -0,0 +1,10 @@
+-- Rename groups table to groups, which is not a keyword
+-- It was called group in a few alpha versions
+
+RENAME TABLE /*$wgDBprefix*/`group` TO /*$wgDBprefix*/groups;
+ALTER TABLE /*$wgDBprefix*/groups 
+       CHANGE group_id gr_id int(5) unsigned NOT NULL auto_increment,
+       CHANGE group_name gr_name varchar(50) NOT NULL default '',
+       CHANGE group_description gr_description varchar(255) NOT NULL default '',
+       CHANGE group_rights gr_rights tinyblob;
+
index 242c94e..065653d 100644 (file)
@@ -3,27 +3,27 @@
 -- Should probably be inserted when someone create a new database
 --
 
-INSERT INTO /*$wgDBprefix*/`group` (group_id,group_name,group_description,group_rights)
+INSERT INTO /*$wgDBprefix*/groups (gr_id,gr_name,gr_description,gr_rights)
        VALUES (
                1,':group-anon-name',':group-anon-desc',
                'read,edit,createaccount'
        );
-INSERT INTO /*$wgDBprefix*/`group` (group_id,group_name,group_description,group_rights)
+INSERT INTO /*$wgDBprefix*/groups (gr_id,gr_name,gr_description,gr_rights)
        VALUES (
                2,':group-loggedin-name',':group-loggedin-desc',
                'read,edit,move,upload,validate,createaccount'
        );
-INSERT INTO /*$wgDBprefix*/`group` (group_id,group_name,group_description,group_rights)
+INSERT INTO /*$wgDBprefix*/groups (gr_id,gr_name,gr_description,gr_rights)
        VALUES (
                3,':group-admin-name',':group-admin-desc',
                'read,edit,move,upload,validate,createaccount,delete,undelete,protect,block,upload,asksql,rollback,patrol,editinterface,import'
        );
-INSERT INTO /*$wgDBprefix*/`group` (group_id,group_name,group_description,group_rights)
+INSERT INTO /*$wgDBprefix*/groups (gr_id,gr_name,gr_description,gr_rights)
        VALUES (
                4,':group-bureaucrat-name',':group-bureaucrat-desc',
                'read,edit,move,upload,validate,createaccount,delete,undelete,protect,block,upload,asksql,rollback,patrol,editinterface,import,makesysop'
        );
-INSERT INTO /*$wgDBprefix*/`group` (group_id,group_name,group_description,group_rights)
+INSERT INTO /*$wgDBprefix*/groups (gr_id,gr_name,gr_description,gr_rights)
        VALUES (
                5,':group-steward-name',':group-steward-desc',
                'read,edit,move,upload,validate,createaccount,delete,undelete,protect,block,upload,asksql,rollback,patrol,editinterface,import,makesysop,userrights,grouprights,siteadmin'
index aff208d..7f1cabf 100644 (file)
@@ -1,5 +1,5 @@
 -- Oct. 24 2004
--- Adds the group_rights field missing from early dev work
+-- Adds the gr_rights field missing from early dev work
 
 -- Hold group name and description
-ALTER TABLE /*$wgDBprefix*/`group` ADD group_rights tinyblob;
+ALTER TABLE /*$wgDBprefix*/groups ADD gr_rights tinyblob;
index 96db38e..ab3a9a7 100644 (file)
@@ -4,12 +4,12 @@
 -- This is under development to provide a showcase in HEAD :o)
 
 -- Hold group name and description
-CREATE TABLE /*$wgDBprefix*/`group` (
-  group_id int(5) unsigned NOT NULL auto_increment,
-  group_name varchar(50) NOT NULL default '',
-  group_description varchar(255) NOT NULL default '',
-  group_rights tinyblob,
-  PRIMARY KEY  (group_id)
+CREATE TABLE /*$wgDBprefix*/groups (
+  gr_id int(5) unsigned NOT NULL auto_increment,
+  gr_name varchar(50) NOT NULL default '',
+  gr_description varchar(255) NOT NULL default '',
+  gr_rights tinyblob,
+  PRIMARY KEY  (gr_id)
 
 ) TYPE=InnoDB;
 
index 9199252..184f876 100644 (file)
@@ -327,7 +327,7 @@ class ParserTest {
                        'recentchanges',
                        'watchlist', 'math', 'searchindex',
                        'interwiki', 'querycache',
-                       'objectcache', 'group'
+                       'objectcache', 'groups'
                );
        }
        
@@ -405,11 +405,11 @@ class ParserTest {
                                ) );
 
                        # Hack: initialize a group
-                       $db->insert( 'group', array(
-                               'group_id' => 1,
-                               'group_name' => 'Anonymous',
-                               'group_description' => 'Anonymous users',
-                               'group_rights' => 'read' ) );
+                       $db->insert( 'groups', array(
+                               'gr_id' => 1,
+                               'gr_name' => 'Anonymous',
+                               'gr_description' => 'Anonymous users',
+                               'gr_rights' => 'read' ) );
                        
                        # Hack: Insert an image to work with
                        $db->insert( 'image', array(
index 00e88a4..9926966 100644 (file)
@@ -816,12 +816,12 @@ CREATE TABLE /*$wgDBprefix*/logging (
 
 
 -- Hold group name and description
-CREATE TABLE /*$wgDBprefix*/`group` (
-  group_id int(5) unsigned NOT NULL auto_increment,
-  group_name varchar(50) NOT NULL default '',
-  group_description varchar(255) NOT NULL default '',
-  group_rights tinyblob,
-  PRIMARY KEY  (group_id)
+CREATE TABLE /*$wgDBprefix*/groups (
+  gr_id int(5) unsigned NOT NULL auto_increment,
+  gr_name varchar(50) NOT NULL default '',
+  gr_description varchar(255) NOT NULL default '',
+  gr_rights tinyblob,
+  PRIMARY KEY  (gr_id)
 
 ) TYPE=InnoDB;
 
index 6a0f24e..4399f52 100644 (file)
@@ -9,6 +9,11 @@
 require_once 'convertLinks.inc';
 require_once 'InitialiseMessages.inc';
 
+$wgRenamedTables = array(
+#           from             to                  patch file
+       array( 'group',         'groups',           'patch-rename-group.sql' ),
+);
+
 $wgNewTables = array(
 #            table          patch file (in maintenance/archives)
        array( 'linkscc',       'patch-linkscc.sql' ),
@@ -18,7 +23,7 @@ $wgNewTables = array(
        array( 'categorylinks', 'patch-categorylinks.sql' ),
        array( 'logging',       'patch-logging.sql' ),
        array( 'user_rights',   'patch-user_rights.sql' ),
-       array( 'group',   'patch-userlevels.sql' ),
+       array( 'groups',        'patch-userlevels.sql' ),
 );
 
 $wgNewFields = array(
@@ -33,7 +38,7 @@ $wgNewFields = array(
        array( 'user',          'user_token',       'patch-user_token.sql' ),
        array( 'user',          'user_email_token', 'patch-user_email_token.sql' ),
        array( 'user_rights',   'ur_user',          'patch-rename-user_groups-and_rights.sql' ),
-       array( 'group',         'group_rights',     'patch-userlevels-rights.sql' ),
+       array( 'groups',        'gr_rights',        'patch-userlevels-rights.sql' ),
        array( 'logging',       'log_params',       'patch-log_params.sql' ),
        array( 'archive',       'ar_rev_id',        'patch-archive-rev_id.sql' ),
        array( 'archive',       'ar_text_id',       'patch-archive-text_id.sql' ),
@@ -43,6 +48,23 @@ $wgNewFields = array(
        array( 'image',         'img_metadata',     'patch-img_metadata.sql' ),
 );
 
+function rename_table( $from, $to, $patch ) {
+       global $wgDatabase;
+       if ( $wgDatabase->tableExists( $from ) ) {
+               if ( $wgDatabase->tableExists( $to ) ) {
+                       echo "...can't move table $from to $to, $to already exists.\n";
+               } else {
+                       echo "Moving table $from to $to...";
+                       dbsource( "maintenance/archives/$patch", $wgDatabase );
+                       echo "ok\n";
+               }
+       } else {
+               // Source table does not exist
+               // Renames are done before creations, so this is typical for a new installation
+               // Ignore silently
+       }
+}
+
 function add_table( $name, $patch ) {
        global $wgDatabase;
        if ( $wgDatabase->tableExists( $name ) ) {
@@ -226,32 +248,17 @@ function do_user_update() {
        }
 }
 
-# Assumes that the group table has been added.
+# Assumes that the groups table has been added.
 function do_group_update() {
        global $wgDatabase;
        $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
-               $wgDatabase->tableName( 'group' ) );
+               $wgDatabase->tableName( 'groups' ) );
        $row = $wgDatabase->fetchObject( $res );
        $wgDatabase->freeResult( $res );
        if( $row->c == 0 ) {
                echo "Adding default group definitions... ";
                dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
                echo "ok\n";
-       } else {
-               echo "...group definitions already in place.\n";
-               $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
-                                                WHERE group_name IN ('Sysops','Bureaucrat')
-                                                  AND group_rights NOT LIKE '%sysop%'",
-                                              $wgDatabase->tableName( 'group' ) );
-               $row = $wgDatabase->fetchObject( $res );
-               $wgDatabase->freeResult( $res );
-               if( $row->n ) {
-                       echo "Fixing sysops group permissions and add group editing right... ";
-                       dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
-                       echo "ok\n";
-               } else {
-                       echo "...sysop group permissions look ok.\n";
-               }
        }
 }
 
@@ -500,8 +507,13 @@ function do_namespace_size_on( $table, $prefix ) {
 }
 
 function do_all_updates() {
-       global $wgNewTables, $wgNewFields;
+       global $wgNewTables, $wgNewFields, $wgRenamedTables;
        
+       # Rename tables
+       foreach ( $wgRenamedTables as $tableRecord ) {
+               rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
+       }
+
        # Add missing tables
        foreach ( $wgNewTables as $tableRecord ) {
                add_table( $tableRecord[0], $tableRecord[1] );