MarkAHershberger has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/180645

Change subject: Move RemoveUnusedAccount's isInactiveAccount() and make it 
public.
......................................................................

Move RemoveUnusedAccount's isInactiveAccount() and make it public.

The test here should be more widely accessible.

Change-Id: If9613e5e06c44182fdf0a044082cb88d74a54dc1
---
M includes/User.php
M maintenance/removeUnusedAccounts.php
2 files changed, 64 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/45/180645/1

diff --git a/includes/User.php b/includes/User.php
index bda825f..f007827 100644
--- a/includes/User.php
+++ b/includes/User.php
@@ -4650,6 +4650,43 @@
        }
 
        /**
+        * Could the user account be deemed inactive?
+        * (No edits, no deleted edits, no log entries, no current/old uploads)
+        *
+        * @param bool $master Perform checking on the master
+        * @return bool
+        */
+       public function isInactiveAccount( $master = false ) {
+               $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
+               $checks = array(
+                       'revision' => 'rev',
+                       'archive' => 'ar',
+                       'image' => 'img',
+                       'oldimage' => 'oi',
+                       'filearchive' => 'fa'
+               );
+               $count = 0;
+
+               $dbo->begin( __METHOD__ );
+               foreach ( $checks as $table => $fprefix ) {
+                       $conds = array( $fprefix . '_user' => $this->getID() );
+                       $count += (int)$dbo->selectField( $table, 'COUNT(*)', 
$conds,
+                               __METHOD__ );
+               }
+
+               $conds = array(
+                       'log_user' => $this->getID(),
+                       'log_type != ' . $dbo->addQuotes( 'newusers' )
+               );
+               $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds,
+                       __METHOD__ );
+
+               $dbo->commit( __METHOD__ );
+
+               return $count == 0;
+       }
+
+       /**
         * Initialize user_editcount from data out of the revision table
         *
         * @param int $add Edits to add to the count from the revision table
diff --git a/maintenance/removeUnusedAccounts.php 
b/maintenance/removeUnusedAccounts.php
index 90dc622..544a99b 100644
--- a/maintenance/removeUnusedAccounts.php
+++ b/maintenance/removeUnusedAccounts.php
@@ -34,8 +34,10 @@
        public function __construct() {
                parent::__construct();
                $this->addOption( 'delete', 'Actually delete the account' );
-               $this->addOption( 'ignore-groups', 'List of comma-separated 
groups to exclude', false, true );
-               $this->addOption( 'ignore-touched', 'Skip accounts touched in 
last N days', false, true );
+               $this->addOption( 'ignore-groups', 'List of comma-separated 
groups ' .
+                       'to exclude', false, true );
+               $this->addOption( 'ignore-touched', 'Skip accounts touched in 
last N ' .
+                       ' days', false, true );
        }
 
        public function execute() {
@@ -46,24 +48,29 @@
                $this->output( "Checking for unused user accounts...\n" );
                $del = array();
                $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'user', array( 'user_id', 'user_name', 
'user_touched' ), '', __METHOD__ );
+               $res = $dbr->select( 'user',
+                       array( 'user_id', 'user_name', 'user_touched' ), '', 
__METHOD__ );
                if ( $this->hasOption( 'ignore-groups' ) ) {
-                       $excludedGroups = explode( ',', $this->getOption( 
'ignore-groups' ) );
+                       $excludedGroups = explode( ',',
+                               $this->getOption( 'ignore-groups' ) );
                } else {
                        $excludedGroups = array();
                }
                $touched = $this->getOption( 'ignore-touched', "1" );
                if ( !ctype_digit( $touched ) ) {
-                       $this->error( "Please put a valid positive integer on 
the --ignore-touched parameter.", true );
+                       $this->error( "Please put a valid positive integer on 
the " .
+                               "--ignore-touched parameter.", true );
                }
                $touchedSeconds = 86400 * $touched;
                foreach ( $res as $row ) {
                        # Check the account, but ignore it if it's within a 
$excludedGroups
                        # group or if it's touched within the $touchedSeconds 
seconds.
                        $instance = User::newFromId( $row->user_id );
-                       if ( count( array_intersect( 
$instance->getEffectiveGroups(), $excludedGroups ) ) == 0
-                               && $this->isInactiveAccount( $row->user_id, 
true )
-                               && wfTimestamp( TS_UNIX, $row->user_touched ) < 
wfTimestamp( TS_UNIX, time() - $touchedSeconds )
+                       if ( count( array_intersect( 
$instance->getEffectiveGroups(),
+                                               $excludedGroups ) ) == 0
+                               && $instance->isInactiveAccount( $row->user_id, 
true )
+                               && wfTimestamp( TS_UNIX, $row->user_touched ) <
+                               wfTimestamp( TS_UNIX, time() - $touchedSeconds )
                        ) {
                                # Inactive; print out the name and flag it
                                $del[] = $row->user_id;
@@ -78,14 +85,19 @@
                        $this->output( "\nDeleting unused accounts..." );
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->delete( 'user', array( 'user_id' => $del ), 
__METHOD__ );
-                       $dbw->delete( 'user_groups', array( 'ug_user' => $del 
), __METHOD__ );
-                       $dbw->delete( 'user_former_groups', array( 'ufg_user' 
=> $del ), __METHOD__ );
-                       $dbw->delete( 'user_properties', array( 'up_user' => 
$del ), __METHOD__ );
+                       $dbw->delete( 'user_groups', array( 'ug_user' => $del ),
+                               __METHOD__ );
+                       $dbw->delete( 'user_former_groups', array( 'ufg_user' 
=> $del ),
+                               __METHOD__ );
+                       $dbw->delete( 'user_properties', array( 'up_user' => 
$del ),
+                               __METHOD__ );
                        $dbw->delete( 'logging', array( 'log_user' => $del ), 
__METHOD__ );
-                       $dbw->delete( 'recentchanges', array( 'rc_user' => $del 
), __METHOD__ );
+                       $dbw->delete( 'recentchanges', array( 'rc_user' => $del 
),
+                               __METHOD__ );
                        $this->output( "done.\n" );
                        # Update the site_stats.ss_users field
-                       $users = $dbw->selectField( 'user', 'COUNT(*)', 
array(), __METHOD__ );
+                       $users = $dbw->selectField( 'user', 'COUNT(*)', array(),
+                               __METHOD__ );
                        $dbw->update(
                                'site_stats',
                                array( 'ss_users' => $users ),
@@ -93,42 +105,10 @@
                                __METHOD__
                        );
                } elseif ( $count > 0 ) {
-                       $this->output( "\nRun the script again with --delete to 
remove them from the database.\n" );
+                       $this->output( "\nRun the script again with --delete to 
remove " .
+                               "them from the database.\n" );
                }
                $this->output( "\n" );
-       }
-
-       /**
-        * Could the specified user account be deemed inactive?
-        * (No edits, no deleted edits, no log entries, no current/old uploads)
-        *
-        * @param int $id User's ID
-        * @param bool $master Perform checking on the master
-        * @return bool
-        */
-       private function isInactiveAccount( $id, $master = false ) {
-               $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
-               $checks = array(
-                       'revision' => 'rev',
-                       'archive' => 'ar',
-                       'image' => 'img',
-                       'oldimage' => 'oi',
-                       'filearchive' => 'fa'
-               );
-               $count = 0;
-
-               $dbo->begin( __METHOD__ );
-               foreach ( $checks as $table => $fprefix ) {
-                       $conds = array( $fprefix . '_user' => $id );
-                       $count += (int)$dbo->selectField( $table, 'COUNT(*)', 
$conds, __METHOD__ );
-               }
-
-               $conds = array( 'log_user' => $id, 'log_type != ' . 
$dbo->addQuotes( 'newusers' ) );
-               $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', 
$conds, __METHOD__ );
-
-               $dbo->commit( __METHOD__ );
-
-               return $count == 0;
        }
 }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/180645
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If9613e5e06c44182fdf0a044082cb88d74a54dc1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to