Aaron Schulz has submitted this change and it was merged.

Change subject: Initial commit of Extension:AccountAudit
......................................................................


Initial commit of Extension:AccountAudit

The extension adds a table for tracking the timestamp of the most recent
login for a user.  This will allow us to determine the (in)activity of a
user and thusly whether or not we can unilaterally rename the user in the
event of clashing local accounts during unification.

Change-Id: I163682c55031e77bba02d67cd62839f9541adc99
---
A AccountAudit.body.php
A AccountAudit.hooks.php
A AccountAudit.i18n.php
A AccountAudit.php
A accountaudit.sql
5 files changed, 152 insertions(+), 0 deletions(-)

Approvals:
  Aaron Schulz: Verified; Looks good to me, approved



diff --git a/AccountAudit.body.php b/AccountAudit.body.php
new file mode 100644
index 0000000..4fe05b6
--- /dev/null
+++ b/AccountAudit.body.php
@@ -0,0 +1,53 @@
+<?php
+
+class AccountAudit {
+
+       /**
+        * Updates the aa_lastlogin value for the specified user
+        *
+        * @param User $user the user that just logged in
+        *
+        * @return bool return True to continue processing hooks
+        */
+       static function updateLastLogin( User $user ) {
+
+               if ( wfReadOnly() ) {
+                       return true;
+               }
+
+               $now = time();
+               $db = wfGetDB( DB_MASTER );
+               $method = __METHOD__;
+
+               $db->onTransactionIdle( function() use ( $user, $now, $db, 
$method ) {
+                       if ( $db->getType() === 'mysql' ) { // MySQL-specific 
implementation
+                               $db->query(
+                                       "INSERT INTO " . $db->tableName( 
'accountaudit_login' ) .
+                                               "( aa_user, aa_lastlogin ) 
VALUES (" .
+                                               $db->addQuotes( $user->getId() 
) . ", " .
+                                               $db->addQuotes( $db->timestamp( 
$now ) ) .
+                                               ") ON DUPLICATE KEY UPDATE 
aa_lastlogin = " .
+                                               $db->addQuotes( $db->timestamp( 
$now ) ),
+                                       $method
+                               );
+                       } else {
+                               $db->update(
+                                       'accountaudit_login',
+                                       array( 'aa_lastlogin' => 
$db->timestamp( $now ) ),
+                                       array( 'aa_user' => $user->getId() ),
+                                       $method
+                               );
+                               if ( $db->affectedRows() == 0 ) { // no row 
existed for that user
+                                       $db->insert(
+                                               'accountaudit_login',
+                                               array( 'aa_user' => 
$user->getId(), 'aa_lastlogin' =>  $db->timestamp( $now ) ),
+                                               $method,
+                                               array( 'IGNORE', )
+                                       );
+                               }
+                       }
+               } );
+               // always return true, this should be a non-blocking hook on 
failure
+               return true;
+       }
+}
diff --git a/AccountAudit.hooks.php b/AccountAudit.hooks.php
new file mode 100644
index 0000000..8867a60
--- /dev/null
+++ b/AccountAudit.hooks.php
@@ -0,0 +1,38 @@
+<?php
+
+class AccountAuditHooks {
+
+       /**
+        * Implementation of the hook for onUserLoginComplete.
+        *
+        * Calls AccountAudit::updateLastLogin to update the timestamp of the 
last
+        * login for the user
+        *
+        * @param User $user
+        * @param $inject_html
+        *
+        * @return bool
+        */
+       static function onUserLoginComplete( User &$user, &$inject_html ) {
+
+               AccountAudit::updateLastLogin( $user );
+
+               // Always return true, we should never block execution on 
failure
+               return true;
+       }
+
+       /**
+        * Implementation of the hook for loadExtensionSchemaUpdates
+        *
+        * Installs the requisite tables for this extension
+        *
+        * @param DatabaseUpdater $updater
+        *
+        * @return bool
+        */
+       static function loadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
+               $updater->addExtensionTable( 'accountaudit_login',
+                       __DIR__ . '/accountaudit.sql', true );
+               return true;
+       }
+}
\ No newline at end of file
diff --git a/AccountAudit.i18n.php b/AccountAudit.i18n.php
new file mode 100644
index 0000000..7d81dd3
--- /dev/null
+++ b/AccountAudit.i18n.php
@@ -0,0 +1,13 @@
+<?php
+
+$messages = array();
+
+$messages['en'] = array(
+       'accountaudit-desc' => 'This extension is used to assist the WMF in 
completing the audit an unification of non-global accounts that exist across 
the WMF\'s projects',
+);
+
+/** Message documentation (Message documentation)
+ */
+$messages['qqq'] = array(
+       'accountaudit-desc' => '{{desc}}',
+);
\ No newline at end of file
diff --git a/AccountAudit.php b/AccountAudit.php
new file mode 100644
index 0000000..7f440fe
--- /dev/null
+++ b/AccountAudit.php
@@ -0,0 +1,35 @@
+<?php
+/*
+ * Extension:AccountAudit. This extension is used to assist the WMF in 
completing the audit
+ * an unification of non-global accounts that exist across the WMF's projects.
+ *
+ * @author Peter Gehres <[email protected]>
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+       echo <<<EOT
+To install the AccountAudit extension, put the following line in 
LocalSettings.php:
+require_once( "\$IP/extensions/AccountAudit/AccountAudit.php" );
+EOT;
+       exit( 1 );
+}
+
+$wgExtensionCredits[ 'other' ][ ] = array(
+       'path'           => __FILE__,
+       'name'           => 'AccountAudit',
+       'author'         => array( 'Peter Gehres', ),
+       'url'            => 
'https://www.mediawiki.org/wiki/Extension:AccountAudit',
+       'descriptionmsg' => 'accountaudit-desc',
+       'version'        => '1.0.0',
+);
+
+$wgAutoloadClasses['AccountAudit'] = __DIR__ . '/AccountAudit.body.php';
+$wgAutoloadClasses['AccountAuditHooks'] = __DIR__ . '/AccountAudit.hooks.php';
+
+$wgHooks['UserLoginComplete'][] = 'AccountAuditHooks::onUserLoginComplete';
+
+$wgExtensionMessagesFiles['AccountAudit'] = __DIR__ . '/AccountAudit.i18n.php';
+
+// Schema updates for update.php
+$wgHooks['LoadExtensionSchemaUpdates'][] = 
'AccountAuditHooks::loadExtensionSchemaUpdates';
+
diff --git a/accountaudit.sql b/accountaudit.sql
new file mode 100644
index 0000000..f228ffc
--- /dev/null
+++ b/accountaudit.sql
@@ -0,0 +1,13 @@
+--
+-- This tables tracks the most recent login action for a user
+-- user_id is an effective foreign key to the user table
+--
+CREATE TABLE /*$wgDBprefix*/accountaudit_login (
+  -- Key to user_id
+  aa_user int unsigned NOT NULL PRIMARY KEY,
+
+  -- This is a timestamp which is updated when a user logs in
+  aa_lastlogin varbinary(14) default null
+) /*$wgDBTableOptions*/;
+
+CREATE INDEX /*i*/aa_lastlogin ON /*_*/accountaudit_login (aa_lastlogin);
\ No newline at end of file

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I163682c55031e77bba02d67cd62839f9541adc99
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/AccountAudit
Gerrit-Branch: master
Gerrit-Owner: Pgehres <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Amgine <[email protected]>
Gerrit-Reviewer: Asher <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Demon <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: MZMcBride <[email protected]>
Gerrit-Reviewer: Maryana <[email protected]>
Gerrit-Reviewer: Pgehres <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>

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

Reply via email to