jenkins-bot has submitted this change and it was merged.

Change subject: Add script to notify people who are going to be renamed
......................................................................


Add script to notify people who are going to be renamed

Depends on the MassMessage extension

Change-Id: If4e76945757124c35e8a54b311e85f7c44cda579
---
M includes/UsersToRename/UsersToRenameDatabaseUpdates.php
M maintenance/forceRenameUsers.php
A maintenance/sendForceRenameNotification.php
3 files changed, 120 insertions(+), 14 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/UsersToRename/UsersToRenameDatabaseUpdates.php 
b/includes/UsersToRename/UsersToRenameDatabaseUpdates.php
index 4305ddd..a1e148e 100644
--- a/includes/UsersToRename/UsersToRenameDatabaseUpdates.php
+++ b/includes/UsersToRename/UsersToRenameDatabaseUpdates.php
@@ -14,13 +14,21 @@
                $this->db = $db;
        }
 
-       public function markRenamed( $name, $wiki ) {
+       protected function updateStatus( $name, $wiki, $status ) {
                $this->db->update(
                        'users_to_rename',
-                       array( 'utr_status' => self::RENAMED ),
+                       array( 'utr_status' => $status ),
                        array( 'utr_wiki' => $wiki, 'utr_name' => $name ),
                        __METHOD__
                );
+       }
+
+       public function markNotified( $name, $wiki ) {
+               $this->updateStatus( $name, $wiki, self::NOTIFIED );
+       }
+
+       public function markRenamed( $name, $wiki ) {
+               $this->updateStatus( $name, $wiki, self::RENAMED );
        }
 
        public function remove( $name, $wiki ) {
@@ -63,4 +71,16 @@
                        array( 'IGNORE' )
                );
        }
+
+       public function findUsers( $wiki, $status, $limit ) {
+               $rows = $this->db->select(
+                       'users_to_rename',
+                       array( 'utr_name', 'utr_wiki' ),
+                       array( 'utr_status' => $status, 'utr_wiki' => $wiki ),
+                       __METHOD__,
+                       array( 'LIMIT' => $limit )
+               );
+
+               return $rows; // @todo this shouldn't return prefixed field 
names
+       }
 }
diff --git a/maintenance/forceRenameUsers.php b/maintenance/forceRenameUsers.php
index 0168011..9530336 100644
--- a/maintenance/forceRenameUsers.php
+++ b/maintenance/forceRenameUsers.php
@@ -27,9 +27,8 @@
 
        public function execute() {
                $dbw = CentralAuthUser::getCentralDB();
-               $dbr = CentralAuthUser::getCentralSlaveDB();
                while ( true ) {
-                       $rowsToRename = $this->findUsers( $this->getOption( 
'dbname' ), $dbr, $dbw );
+                       $rowsToRename = $this->findUsers( $this->getOption( 
'dbname' ), $dbw );
                        if ( !$rowsToRename ) {
                                break;
                        }
@@ -92,21 +91,13 @@
 
        /**
         * @param string $wiki
-        * @param DatabaseBase $dbr
         * @param DatabaseBase $dbw
         * @return stdClass[]
         */
-       protected function findUsers( $wiki, DatabaseBase $dbr, DatabaseBase 
$dbw ) {
-               $rows = $dbr->select(
-                       'users_to_rename',
-                       array( 'utr_name', 'utr_wiki' ),
-                       array( 'utr_status' => 
UsersToRenameDatabaseUpdates::NOTIFIED, 'utr_wiki' => $wiki ),
-                       __METHOD__,
-                       array( 'LIMIT' => 50 )
-               );
-
+       protected function findUsers( $wiki, DatabaseBase $dbw ) {
                $rowsToRename = array();
                $updates = new UsersToRenameDatabaseUpdates( $dbw );
+               $rows = $updates->findUsers( $wiki, 
UsersToRenameDatabaseUpdates::NOTIFIED, 50 );
 
                foreach ( $rows as $row ) {
                        $caUser = new CentralAuthUser( $row->utr_name );
diff --git a/maintenance/sendForceRenameNotification.php 
b/maintenance/sendForceRenameNotification.php
new file mode 100644
index 0000000..a3667f3
--- /dev/null
+++ b/maintenance/sendForceRenameNotification.php
@@ -0,0 +1,95 @@
+<?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+if ( $IP === false ) {
+       $IP = __DIR__ . '/../../..';
+}
+require_once "$IP/maintenance/Maintenance.php";
+
+/**
+ * Script to notify users listed in the users_to_rename table
+ * that they will be renamed. Requires the MassMessage extension
+ *
+ * Setup:
+ *   - A file with your message in it, probably using a giant #switch to be 
localized.
+ *     The string "{{WIKI}}" will be replaced by the wiki's database name.
+ *   - A directory with localized subject files, named in the format of 
"$langCode.txt".
+ *     A file for "en" must exist as it is the base fallback.
+ */
+class ForceRenameNotification extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->addOption( 'message', 'Location of message', true, true 
);
+               $this->addOption( 'subject', 'Location of directory with 
subjects', true, true );
+               $this->addOption( 'sleep', 'How long to sleep for', false, true 
);
+               $this->setBatchSize( 50 );
+       }
+
+       public function execute() {
+               if ( !class_exists( 'MassMessageServerSideJob' ) ) {
+                       $this->error( 'This script requires the MassMessage 
extension', 1 );
+               }
+               $messageLocation = $this->getOption( 'message' );
+               $message = file_get_contents( $messageLocation );
+               $sleep = (int)$this->getOption( 'sleep', 1 );
+               if ( !$message ) {
+                       $this->error( "Could not read $messageLocation", 1 );
+               }
+               $message = str_replace( '{{WIKI}}', wfWikiID(), $message );
+               $dbw = CentralAuthUser::getCentralDB();
+               $updates = new UsersToRenameDatabaseUpdates( $dbw );
+               $commonParams = array(
+                       'class' => 'MassMessageServerSideJob',
+                       'data' => array(
+                               'subject' => $this->getSubject(),
+                               'message' => $message,
+                       ),
+               );
+               while ( true ) {
+                       $pages = array();
+                       $markNotified = array();
+                       $rows = $updates->findUsers( wfWikiID(), 0, 
$this->mBatchSize );
+                       if ( $rows->numRows() === 0 ) {
+                               break;
+                       }
+                       foreach ( $rows as $row ) {
+                               $pages[] = array(
+                                       'title' => 'User talk:' . 
$row->utr_name,
+                                       'wiki' => wfWikiID(),
+                               );
+                               $this->output( "Will notify {$row->utr_name}\n" 
);
+                               $markNotified[] = $row;
+                       }
+
+                       $job = new MassMessageSubmitJob(
+                               Title::newFromText( __CLASS__ ),
+                               $commonParams + array( 'pages' => $pages )
+                       );
+                       $count = count( $pages );
+                       $this->output( "Queued job for $count users.\n" );
+                       JobQueueGroup::singleton()->push( $job );
+                       foreach ( $markNotified as $row ) {
+                               $updates->markNotified( $row->utr_name, 
$row->utr_wiki );
+                       }
+                       $this->output( "Sleeping for $sleep seconds...\n" );
+                       sleep( $sleep );
+               }
+       }
+
+       protected function getSubject() {
+               $langCode = $this->getConfig()->get( 'LanguageCode' );
+               $fallbacks = Language::getFallbacksFor( $langCode );
+               array_unshift( $fallbacks, $langCode );
+               $dir = $this->getOption( 'subject' );
+               foreach ( $fallbacks as $code ) {
+                       if ( file_exists( "$dir/$code.txt" ) ) {
+                               return trim( file_get_contents( 
"$dir/$code.txt" ) );
+                       }
+               }
+
+               $this->error( "Could not find a valid subject for $langCode.", 
1 );
+       }
+}
+
+$maintClass = 'ForceRenameNotification';
+require_once RUN_MAINTENANCE_IF_MAIN;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If4e76945757124c35e8a54b311e85f7c44cda579
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to