Legoktm has uploaded a new change for review.

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


Change subject: [WIP] Allow for global renaming of users
......................................................................

[WIP] Allow for global renaming of users

* Adds a new special page for trusted users to rename
  users
* Uses a new database table to store status and hopefully
  avoid race conditions

TODO:
* Add login hooks to prevent users from logging in
  while renaming is in progress
* Create another special page to view the rename status
* Logging
* Updater stuff
* i18n messages, userrights

Bug: 14862
Change-Id: Id78577e95d73fbcdcb72e80ef25a34106ac91aa2
---
M CentralAuthUser.php
A LocalRenameUserJob.php
A specials/SpecialGlobalRenameUser.php
3 files changed, 176 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralAuth 
refs/changes/68/92468/1

diff --git a/CentralAuthUser.php b/CentralAuthUser.php
index f51dbe5..d282710 100644
--- a/CentralAuthUser.php
+++ b/CentralAuthUser.php
@@ -1705,6 +1705,22 @@
        }
 
        /**
+        * Check if a rename to or from the provided name is in progress
+        * @param string $name
+        * @return bool
+        */
+       public static function renameInProgress( $name ) {
+               $dbw = self::getCentralDB(); // Use a master to avoid race 
conditions
+               $res = $dbw->select(
+                       'renameuser_status',
+                       array( 'newname' ),
+                       array( $dbw->makeList( array( 'oldname' => $name, 
'newname' => $name ), LIST_OR ) ),
+                       __METHOD__
+               );
+               return $res->numRows() != 0;
+       }
+
+       /**
         * Get information about each local user attached to this account
         *
         * @return array Map of database name to property table with members:
diff --git a/LocalRenameUserJob.php b/LocalRenameUserJob.php
new file mode 100644
index 0000000..e3af1bf
--- /dev/null
+++ b/LocalRenameUserJob.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * Job class to rename a user locally
+ * This is intended to be run on each wiki individually
+ */
+class LocalRenameUserJob extends Job {
+
+       public function __construct( $title, $params, $id = 0 ) {
+               parent::__construct( 'LocalRenameUserJob', $title, $params, $id 
);
+       }
+
+       public function run() {
+               if ( !class_exists( 'RenameuserSQL' ) ) {
+                       throw new MWException( 'Extension:Renameuser is not 
installed' );
+               }
+               $this->updateStatus( 'inprogress' );
+               $from = $this->params['from'];
+               $to = $this->params['to'];
+               $oldUser = User::newFromName( $from );
+               //$contribs = User::newFromName( $from )->getEditCount();
+               $rename = new RenameuserSQL(
+                       $from,
+                       $to,
+                       $oldUser->getId(),
+                       array( 'checkIfUserExists' => false )
+               );
+               if ( !$rename->rename() ) {
+                       $this->updateStatus( 'failed' );
+                       throw new MWException( 'Rename failed...?' );
+               }
+               // FIXME: add logging here maybe?
+
+               $this->done();
+               return true;
+       }
+
+       public function done() {
+               $dbw = CentralAuthUser::getCentralDB();
+               $dbw->delete(
+                       'renameuser_status',
+                       array( 'oldname' => $this->params['wiki'], 'wiki' => 
wfWikiID() ),
+                       __METHOD__
+               );
+       }
+
+       public function updateStatus( $status ) {
+               $dbw = CentralAuthUser::getCentralDB();
+               $dbw->update(
+                       'renameuser_status',
+                       array( 'status' => $status ),
+                       array( 'oldname' => $this->params['from'], 'wiki' => 
wfWikiID() ),
+                       __METHOD__
+               );
+       }
+}
diff --git a/specials/SpecialGlobalRenameUser.php 
b/specials/SpecialGlobalRenameUser.php
new file mode 100644
index 0000000..8493a09
--- /dev/null
+++ b/specials/SpecialGlobalRenameUser.php
@@ -0,0 +1,104 @@
+<?php
+
+class SpecialGlobalRenameUser extends FormSpecialPage {
+
+       /** @var array $wikis */
+       protected $wikis;
+
+       function getFormFields() {
+               return array(
+                       'oldname' => array(
+                               'id' => 'mw-globalrenameuser-oldname',
+                               'name' => 'oldname',
+                               'label' => 'Current name', // @todo i18n
+                               'type' => 'text',
+                               'required' => true
+                       ),
+                       'newname' => array(
+                               'id' => 'mw-globalrenameuser-newname',
+                               'name' => 'newname',
+                               'label' => 'New name', // @todo i18n
+                               'type' => 'text',
+                               'required' => true
+                       )
+               );
+       }
+
+       /**
+        * Perform validation on the user submitted data
+        * @param array $data
+        * @return Status
+        */
+       function validate( array $data ) {
+               $old = $data['oldname'];
+               $new = $data['newname'];
+               $caOldUser = CentralAuthUser::getInstance( User::newFromName( 
$old ) );
+               if ( !$caOldUser->exists() ) {
+                       return Status::newFatal( 'centralauth-doesnotexist' );
+               }
+               $caNewUser = CentralAuthUser::getInstance( User::newFromName( 
$new ) );
+               if ( $caNewUser->exists() ) {
+                       return Status::newFatal( 'centralauth-alreadyexists' );
+               }
+               if ( CentralAuthUser::renameInProgress( $old ) ) {
+                       return Status::newFatal( 'centralauth-renameinprogress' 
);
+               }
+               // Check that the new name is a valid name...
+               $newUser = User::newFromName( $new, 'creatable' );
+               if ( $newUser === false ) {
+                       return Status::newFatal( 'centralauth-badusername' );
+               }
+
+               return Status::newGood();
+       }
+
+       /**
+        * @param array $data
+        * @return Status
+        */
+       function onSubmit( array $data ) {
+
+               $valid = $this->validate( $data );
+               if ( !$valid->isOK() ) {
+                       return $valid;
+               }
+
+               $user = User::newFromName( $data['oldname'] );
+               if ( $user->getName() == $this->getUser()->getName() ) {
+                       return Status::newFatal( 
'centralauth-rename-cannotself' );
+               }
+               $caUser = CentralAuthUser::getInstance( $user );
+               $attached = $caUser->queryAttached();
+               $this->wikis = array_keys( $attached );
+               $rows = array();
+               foreach ( $this->wikis as $wiki ) {
+                       $rows[] = array(
+                               'wiki' => $wiki,
+                               'oldname' => $data['oldname'],
+                               'status' => 'queued'
+                       );
+               }
+
+               // Update the db status
+               $dbw = CentralAuthUser::getCentralDB();
+               $dbw->insert(
+                       'renameuser_status',
+                       $rows,
+                       __METHOD__
+               );
+
+               // Submit the jobs.
+               $params = array( 'from' => $data['oldname'], 'to' => 
$data['newname'] );
+               $title = Title::newMainPage(); // This isn't used anywhere!
+               $job = new LocalRenameUserJob( $title, $params );
+               foreach( $this->wikis as $wiki ) {
+                       JobQueueGroup::singleton( $wiki )->push( $job );
+               }
+
+               return Status::newGood();
+       }
+
+       function onSuccess() {
+               // TODO Output some "jobs have been queued on ..$this->wikis"
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id78577e95d73fbcdcb72e80ef25a34106ac91aa2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to