Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maintenance/PopulateRenamedUsersTable.php #3

Open
wants to merge 1 commit into
base: wikiteq_REL1_39
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
"Hooks": {
"ShowMissingArticle": "main",
"ContributionsToolLinks": "main",
"GetLogTypesOnUser": "main"
"GetLogTypesOnUser": "main",
"LoadExtensionSchemaUpdates": "schemaUpdates"
},
"HookHandlers": {
"main": {
"class": "MediaWiki\\Extension\\Renameuser\\Hooks",
"services": [
"PermissionManager"
]
},
"schemaUpdates": {
"class": "MediaWiki\\Extension\\Renameuser\\SchemaUpdatesHook"
}
},
"JobClasses": {
Expand Down
1 change: 1 addition & 0 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LogEventsList;
use MediaWiki\Hook\ContributionsToolLinksHook;
use MediaWiki\Hook\GetLogTypesOnUserHook;
use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;
use MediaWiki\Page\Hook\ShowMissingArticleHook;
use MediaWiki\Permissions\PermissionManager;
use SpecialPage;
Expand Down
11 changes: 11 additions & 0 deletions includes/SchemaUpdatesHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace MediaWiki\Extension\Renameuser;

use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;

class SchemaUpdatesHook implements LoadExtensionSchemaUpdatesHook {
public function onLoadExtensionSchemaUpdates( $updater ) {
$updater->addExtensionTable( 'renamed_users', __DIR__ . '/../sql/renamed_users.sql' );
}
}
137 changes: 137 additions & 0 deletions maintenance/PopulateRenamedUsersTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserFactory;

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

class PopulateRenamedUsersTable extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Populate the renamed_users table based on the log records' );
}

public function execute() {
$db = $this->getDB( DB_PRIMARY );
$res = $db->newSelectQueryBuilder()
->select( 'log_params' )
->from( 'logging' )
->where( [ 'log_type' => 'renameuser', 'log_action' => 'renameuser' ] )
->caller( __METHOD__ )
->fetchResultSet();

$count = $res->numRows();

if ( !$count ) {
$this->output( "No renameuser log records were found\n" );
return;
}

$services = MediaWikiServices::getInstance();
$userFactory = $services->getUserFactory();

$numUpdated = 0;

foreach ( $res as $row ) {
$blob = $row->log_params;
$params = LogEntryBase::extractParams( $blob );
$oldUser = $params['4::olduser'];
$newUser = $params['5::newuser'];
$user = $userFactory->newFromName( $newUser );
$userId = $user ? $user->getId() : null;

$resDirect = $db->newSelectQueryBuilder()
->select( 'mediate' )
->from( 'renamed_users' )
->where( [ 'user_old_name' => $oldUser, 'user_new_name' => $newUser ] )
->caller( __METHOD__ )
->fetchRow();

if ( !$resDirect ) {
$db->insert(
'renamed_users',
[
'user_old_name' => $oldUser,
'user_new_name' => $newUser,
'mediate' => 0,
'user_id' => $userId,
],
__METHOD__
);
$numUpdated++;
} elseif ( $resDirect->mediate ) {
$db->update(
'renamed_users',
[
'mediate' => 0,
'user_id' => $userId,
],
[
'user_old_name' => $oldUser,
'user_new_name' => $newUser,
],
__METHOD__
);
}

$resMediate = $db->newSelectQueryBuilder()
->select( 'user_old_name' )
->from( 'renamed_users' )
->where( [ 'user_new_name' => $oldUser ] )
->caller( __METHOD__ )
->fetchFieldValues();

if ( $resMediate ) {
$createMediate = $db->newSelectQueryBuilder()
->select( 'user_old_name' )
->from( 'renamed_users' )
->where( [
'user_old_name IN (' . $db->makeList( $resMediate ) . ')',
'user_new_name != ' . $db->addQuotes( $newUser ),
] )
->caller( __METHOD__ )
->fetchFieldValues();
if ( $createMediate ) {
$insertRows = [];
foreach ( $createMediate as $name ) {
if ( $name === $newUser ) {
continue;
}
$insertRows[] = [
'user_old_name' => $name,
'user_new_name' => $newUser,
'user_id' => $userId,
'mediate' => 1,
];
}
if ( $insertRows ) {
$db->insert(
'renamed_users',
$insertRows,
__METHOD__
);
}
}
}


if ( $userId ) {
$db->update(
'renamed_users',
[ 'user_id' => $userId ],
[ 'user_old_name' => $oldUser, 'user_id' => null ],
__METHOD__
);
}
}

$this->output( "Updated $numUpdated renamed users\n" );
}
}

$maintClass = PopulateRenamedUsersTable::class;
require_once RUN_MAINTENANCE_IF_MAIN;
13 changes: 13 additions & 0 deletions sql/renamed_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BEGIN;

CREATE TABLE /*_*/renamed_users (
user_old_name varbinary(255) NOT NULL,
user_new_name varbinary(255) NOT NULL,
mediate bool default 0,
user_id INT UNSIGNED NULL
)/*$wgDBTableOptions*/;

CREATE UNIQUE INDEX /*i*/renamed_users_primary ON /*_*/renamed_users (user_old_name, user_new_name);
CREATE INDEX /*i*/user_id ON /*_*/renamed_users(user_id);

COMMIT;