Braces and spaces
[lhc/web/wiklou.git] / maintenance / userDupes.inc
index 9af66f1..668d0bc 100644 (file)
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
+/**
+ * @file
+ * @ingroup Maintenance
+ */
+
 /**
  * Look for duplicate user table entries and optionally prune them.
+ * @ingroup Maintenance
  */
 class UserDupes {
        var $db;
@@ -38,8 +44,8 @@ class UserDupes {
        function hasUniqueIndex() {
                $fname = 'UserDupes::hasUniqueIndex';
                $info = $this->db->indexInfo( 'user', 'user_name', $fname );
-               if( !$info ) {
-                       echo "WARNING: doesn't seem to have user_name index at all!\n";
+               if ( !$info ) {
+                       wfOut( "WARNING: doesn't seem to have user_name index at all!\n" );
                        return false;
                }
 
@@ -74,60 +80,60 @@ class UserDupes {
         * not requested. (If doing resolution, edits may be reassigned.)
         * Status information will be echo'd to stdout.
         *
-        * @param bool $doDelete pass true to actually remove things
-        *                       from the database; false to just check.
+        * @param $doDelete bool: pass true to actually remove things
+        *                  from the database; false to just check.
         * @return bool
         */
        function checkDupes( $doDelete = false ) {
-               if( $this->hasUniqueIndex() ) {
-                       echo wfWikiID()." already has a unique index on its user table.\n";
+               if ( $this->hasUniqueIndex() ) {
+                       echo wfWikiID() . " already has a unique index on its user table.\n";
                        return true;
                }
 
                $this->lock();
 
-               echo "Checking for duplicate accounts...\n";
+               wfOut( "Checking for duplicate accounts...\n" );
                $dupes = $this->getDupes();
                $count = count( $dupes );
 
-               echo "Found $count accounts with duplicate records on ".wfWikiID().".\n";
+               wfOut( "Found $count accounts with duplicate records on " . wfWikiID() . ".\n" );
                $this->trimmed    = 0;
                $this->reassigned = 0;
                $this->failed     = 0;
-               foreach( $dupes as $name ) {
+               foreach ( $dupes as $name ) {
                        $this->examine( $name, $doDelete );
                }
 
                $this->unlock();
 
-               echo "\n";
+               wfOut( "\n" );
 
-               if( $this->reassigned > 0 ) {
-                       if( $doDelete ) {
-                               echo "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n";
+               if ( $this->reassigned > 0 ) {
+                       if ( $doDelete ) {
+                               wfOut( "$this->reassigned duplicate accounts had edits reassigned to a canonical record id.\n" );
                        } else {
-                               echo "$this->reassigned duplicate accounts need to have edits reassigned.\n";
+                               wfOut( "$this->reassigned duplicate accounts need to have edits reassigned.\n" );
                        }
                }
 
-               if( $this->trimmed > 0 ) {
-                       if( $doDelete ) {
-                               echo "$this->trimmed duplicate user records were deleted from ".wfWikiID().".\n";
+               if ( $this->trimmed > 0 ) {
+                       if ( $doDelete ) {
+                               wfOut( "$this->trimmed duplicate user records were deleted from " . wfWikiID() . ".\n" );
                        } else {
-                               echo "$this->trimmed duplicate user accounts were found on ".wfWikiID()." which can be removed safely.\n";
+                               wfOut( "$this->trimmed duplicate user accounts were found on " . wfWikiID() . " which can be removed safely.\n" );
                        }
                }
 
-               if( $this->failed > 0 ) {
-                       echo "Something terribly awry; $this->failed duplicate accounts were not removed.\n";
+               if ( $this->failed > 0 ) {
+                       wfOut( "Something terribly awry; $this->failed duplicate accounts were not removed.\n" );
                        return false;
                }
 
-               if( $this->trimmed == 0 || $doDelete ) {
-                       echo "It is now safe to apply the unique index on user_name.\n";
+               if ( $this->trimmed == 0 || $doDelete ) {
+                       wfOut( "It is now safe to apply the unique index on user_name.\n" );
                        return true;
                } else {
-                       echo "Run this script again with the --fix option to automatically delete them.\n";
+                       wfOut( "Run this script again with the --fix option to automatically delete them.\n" );
                        return false;
                }
        }
@@ -138,7 +144,7 @@ class UserDupes {
         */
        function lock() {
                $fname = 'UserDupes::lock';
-               if( $this->newSchema() ) {
+               if ( $this->newSchema() ) {
                        $set = array( 'user', 'revision' );
                } else {
                        $set = array( 'user', 'cur', 'old' );
@@ -146,7 +152,7 @@ class UserDupes {
                $names = array_map( array( $this, 'lockTable' ), $set );
                $tables = implode( ',', $names );
 
-               $result = $this->db->query( "LOCK TABLES $tables", $fname );
+               $this->db->query( "LOCK TABLES $tables", $fname );
        }
 
        function lockTable( $table ) {
@@ -166,7 +172,7 @@ class UserDupes {
         */
        function unlock() {
                $fname = 'UserDupes::unlock';
-               $result = $this->db->query( "UNLOCK TABLES", $fname );
+               $this->db->query( "UNLOCK TABLES", $fname );
        }
 
        /**
@@ -184,11 +190,9 @@ class UserDupes {
                          HAVING n > 1", $fname );
 
                $list = array();
-               while( $row = $this->db->fetchObject( $result ) ) {
+               while ( $row = $this->db->fetchObject( $result ) ) {
                        $list[] = $row->user_name;
                }
-               $this->db->freeResult( $result );
-
                return $list;
        }
 
@@ -196,8 +200,8 @@ class UserDupes {
         * Examine user records for the given name. Try to see which record
         * will be the one that actually gets used, then check remaining records
         * for edits. If the dupes have no edits, we can safely remove them.
-        * @param string $name
-        * @param bool $doDelete
+        * @param $name string
+        * @param $doDelete bool
         * @access private
         */
        function examine( $name, $doDelete ) {
@@ -209,50 +213,49 @@ class UserDupes {
 
                $firstRow = $this->db->fetchObject( $result );
                $firstId  = $firstRow->user_id;
-               echo "Record that will be used for '$name' is user_id=$firstId\n";
+               wfOut( "Record that will be used for '$name' is user_id=$firstId\n" );
 
-               while( $row = $this->db->fetchObject( $result ) ) {
+               while ( $row = $this->db->fetchObject( $result ) ) {
                        $dupeId = $row->user_id;
-                       echo "... dupe id $dupeId: ";
+                       wfOut( "... dupe id $dupeId: " );
                        $edits = $this->editCount( $dupeId );
-                       if( $edits > 0 ) {
+                       if ( $edits > 0 ) {
                                $this->reassigned++;
-                               echo "has $edits edits! ";
-                               if( $doDelete ) {
+                               wfOut( "has $edits edits! " );
+                               if ( $doDelete ) {
                                        $this->reassignEdits( $dupeId, $firstId );
                                        $newEdits = $this->editCount( $dupeId );
-                                       if( $newEdits == 0 ) {
-                                               echo "confirmed cleaned. ";
+                                       if ( $newEdits == 0 ) {
+                                               wfOut( "confirmed cleaned. " );
                                        } else {
                                                $this->failed++;
-                                               echo "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n";
+                                               wfOut( "WARNING! $newEdits remaining edits for $dupeId; NOT deleting user.\n" );
                                                continue;
                                        }
                                } else {
-                                       echo "(will need to reassign edits on fix)";
+                                       wfOut( "(will need to reassign edits on fix)" );
                                }
                        } else {
-                               echo "ok, no edits. ";
+                               wfOut( "ok, no edits. " );
                        }
                        $this->trimmed++;
-                       if( $doDelete ) {
+                       if ( $doDelete ) {
                                $this->trimAccount( $dupeId );
                        }
-                       echo "\n";
+                       wfOut( "\n" );
                }
-               $this->db->freeResult( $result );
        }
 
        /**
         * Count the number of edits attributed to this user.
         * Does not currently check log table or other things
         * where it might show up...
-        * @param int $userid
+        * @param $userid int
         * @return int
         * @access private
         */
        function editCount( $userid ) {
-               if( $this->newSchema() ) {
+               if ( $this->newSchema() ) {
                        return $this->editCountOn( 'revision', 'rev_user', $userid );
                } else {
                        return $this->editCountOn( 'cur', 'cur_user', $userid ) +
@@ -262,9 +265,9 @@ class UserDupes {
 
        /**
         * Count the number of hits on a given table for this account.
-        * @param string $table
-        * @param string $field
-        * @param int $userid
+        * @param $table string
+        * @param $field string
+        * @param $userid int
         * @return int
         * @access private
         */
@@ -278,49 +281,46 @@ class UserDupes {
        }
 
        /**
-        * @param int $from
-        * @param int $to
+        * @param $from int
+        * @param $to int
         * @access private
         */
        function reassignEdits( $from, $to ) {
                $set = $this->newSchema()
                        ? array( 'revision' => 'rev_user' )
                        : array( 'cur' => 'cur_user', 'old' => 'old_user' );
-               foreach( $set as $table => $field ) {
+               foreach ( $set as $table => $field ) {
                        $this->reassignEditsOn( $table, $field, $from, $to );
                }
        }
 
        /**
-        * @param string $table
-        * @param string $field
-        * @param int $from
-        * @param int $to
+        * @param $table string
+        * @param $field string
+        * @param $from int
+        * @param $to int
         * @access private
         */
        function reassignEditsOn( $table, $field, $from, $to ) {
                $fname = 'UserDupes::reassignEditsOn';
-               echo "reassigning on $table... ";
-               $result = $this->db->update( $table,
+               wfOut( "reassigning on $table... " );
+               $this->db->update( $table,
                        array( $field => $to ),
                        array( $field => $from ),
                        $fname );
-               echo "ok. ";
+               wfOut( "ok. " );
        }
 
        /**
         * Remove a user account line.
-        * @param int $userid
+        * @param $userid int
         * @access private
         */
        function trimAccount( $userid ) {
                $fname = 'UserDupes::trimAccount';
-               echo "deleting...";
+               wfOut( "deleting..." );
                $this->db->delete( 'user', array( 'user_id' => $userid ), $fname );
-               echo " ok";
+               wfOut( " ok" );
        }
 
 }
-
-
-?>