<commit_before>#!/usr/bin/php
<?php
/**
 * Batch that merges 2 users
 *
 * The first user will be merged into the second one, and then the first user will be deleted
 */

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'frontend');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG',       false);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

// needed for doctrine connection to work
sfContext::getInstance();

if ($argc != 3 
    || in_array($argv[1], array('--help', '-help', '-h', '-?'))
    || !is_numeric($argv[1])
    || !is_numeric($argv[2]))
{
    echo "Usage: $argv[0] <user_id_to_wipe> <user_id_to_remain>\n";
    exit;
}

$userid_to_wipe = $argv[1];
$userid_to_remain = $argv[2];

// Check that inputs are ok
if ($userid_to_remain == $userid_to_wipe)
    die("User to wipe and user to remain have the same id. Exiting...\n");

$user_to_wipe_document = Document::find('User', $userid_to_wipe, array('is_protected', 'module'));
if (!$user_to_wipe_document || ($user_to_wipe_document->get('module') != 'users'))
    die("User to wipe is not a valid user. Exiting...\n");
if ($user_to_wipe_document->get('is_protected'))
    die("User to wipe is protected. Exiting...\n");

$user_to_remain_document = Document::find('User', $userid_to_remain, array('is_protected', 'module'));
if (!$user_to_remain_document || ($user_to_remain_document->get('module') != 'users'))
    die("User to remain is not a valid user. Exiting...\n");
if ($user_to_remain_document->get('is_protected'))
    die("User to remain is protected. Exiting...\n");

// retrieve forum names and emails
$private_data = Doctrine_Query::create()
    ->select('pd.username, pd.email')
    ->from('UserPrivateData pd')
    ->where('id = ?', array($userid_to_wipe))
    ->execute()
    ->getFirst();
$username_to_wipe = addslashes($private_data->get('username'));
$email_to_wipe = $private_data->get('email');

$private_data = Doctrine_Query::create()
    ->select('pd.username')
    ->from('UserPrivateData pd')
    ->where('id = ?', array($userid_to_remain))
    ->execute()
    ->getFirst();
$username_to_remain = addslashes($private_data->get('username'));

// display infos
echo "User to wipe is $userid_to_wipe ($username_to_wipe). Its email is $email_to_wipe\n";
echo "User to remain is $userid_to_remain ($username_to_remain)\n";

// Everything seems ok for merging...
$conn = sfDoctrine::Connection();
try
{
    $conn->beginTransaction();

    // forum part inspired by http://punbb.org/download/plugins/AP_User_Merge.zip

    $conn->standaloneQuery("UPDATE punbb_forums SET last_poster='$username_to_remain' WHERE last_poster='$username_to_wipe'");

    $result = $conn->standaloneQuery("SELECT id, moderators FROM punbb_forums")->fetchAll();
    foreach ($result as $row)
    {
        $cur_moderators = ($row['moderators'] != '') ? unserialize($row['moderators']) : array();
        if (in_array($userid_to_wipe, $cur_moderators))
        {
            $username = array_search($userid_to_wipe, $cur_moderators);
            unset($cur_moderators[$username]);
            $cur_moderators[$username_to_remain] = $userid_to_remain;
            ksort($cur_moderators);
            $cur_moderators = (!empty($cur_moderators)) ? '\''.$db->escape(serialize($cur_moderators)).'\'' : 'NULL';
            $conn->standaloneQuery("UPDATE punbb_forums SET moderators=".$cur_moderators." WHERE id=".$cur_forum['id']);
        }
    }

    //$conn->standaloneQuery("UPDATE punbb_online SET user_id='$userid_to_remain', ident='$username_to_remain' WHERE user_id='$userid_to_wipe'");
    $conn->standaloneQuery("DELETE FROM punbb_online WHERE user_id='$userid_to_wipe'");
<commit_msg>prompt user confirmation when merging users<commit_after>#!/usr/bin/php
<?php
/**
 * Batch that merges 2 users
 *
 * The first user will be merged into the second one, and then the first user will be deleted
 */

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'frontend');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG',       false);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

// needed for doctrine connection to work
sfContext::getInstance();

if ($argc != 3 
    || in_array($argv[1], array('--help', '-help', '-h', '-?'))
    || !is_numeric($argv[1])
    || !is_numeric($argv[2]))
{
    echo "Usage: $argv[0] <user_id_to_wipe> <user_id_to_remain>\n";
    exit;
}

$userid_to_wipe = $argv[1];
$userid_to_remain = $argv[2];

// Check that inputs are ok
if ($userid_to_remain == $userid_to_wipe)
    die("User to wipe and user to remain have the same id. Exiting...\n");

$user_to_wipe_document = Document::find('User', $userid_to_wipe, array('is_protected', 'module'));
if (!$user_to_wipe_document || ($user_to_wipe_document->get('module') != 'users'))
    die("User to wipe is not a valid user. Exiting...\n");
if ($user_to_wipe_document->get('is_protected'))
    die("User to wipe is protected. Exiting...\n");

$user_to_remain_document = Document::find('User', $userid_to_remain, array('is_protected', 'module'));
if (!$user_to_remain_document || ($user_to_remain_document->get('module') != 'users'))
    die("User to remain is not a valid user. Exiting...\n");
if ($user_to_remain_document->get('is_protected'))
    die("User to remain is protected. Exiting...\n");

// retrieve forum names and emails
$private_data = Doctrine_Query::create()
    ->select('pd.username, pd.email')
    ->from('UserPrivateData pd')
    ->where('id = ?', array($userid_to_wipe))
    ->execute()
    ->getFirst();
$username_to_wipe = addslashes($private_data->get('username'));
$email_to_wipe = $private_data->get('email');

$private_data = Doctrine_Query::create()
    ->select('pd.username')
    ->from('UserPrivateData pd')
    ->where('id = ?', array($userid_to_remain))
    ->execute()
    ->getFirst();
$username_to_remain = addslashes($private_data->get('username'));
$email_to_remain = $private_data->get('email');

// display infos
echo "User to wipe is $userid_to_wipe ($username_to_wipe <$email_to_wipe>)\n";
echo "User to remain is $userid_to_remain ($username_to_remain <$email_to_remain>)\n";

echo "Are you sure you want to merge user $userid_to_wipe into $userid_to_remain? [y/N]: ";

flush();
ob_flush();

if (trim(fgets(STDIN)) !== 'y')
{
    echo "Aborting...\n";
    exit(0);
}

// Everything seems ok for merging...
$conn = sfDoctrine::Connection();
try
{
    $conn->beginTransaction();

    // forum part inspired by http://punbb.org/download/plugins/AP_User_Merge.zip

    $conn->standaloneQuery("UPDATE punbb_forums SET last_poster='$username_to_remain' WHERE last_poster='$username_to_wipe'");

    $result = $conn->standaloneQuery("SELECT id, moderators FROM punbb_forums")->fetchAll();
    foreach ($result as $row)
    {
        $cur_moderators = ($row['moderators'] != '') ? unserialize($row['moderators']) : array();
        if (in_array($userid_to_wipe, $cur_moderators))
        {
            $username = array_search($userid_to_wipe, $cur_moderators);
            unset($cur_moderators[$username]);
            $cur_moderators[$username_to_remain] = $userid_to_remain;
            ksort($cur_moderators);
            $cur_moderators = (!empty($cur_moderators)) ? '\''.$db->escape(serialize($cur_moderators)).'\'' : 'NULL';
            $conn->standaloneQuery("UPDATE punbb_forums SET moderators=".$cur_moderators." WHERE id=".$cur_forum['id']);
        }
    }

    //$conn->standaloneQuery("UPDATE punbb_online SET user_id='$userid_to_remain', ident='$username_to_remain' WHERE user_id='$userid_to_wipe'");
    $conn->standaloneQuery("DELETE FROM punbb_online WHERE user_id='$userid_to_wipe'");

{'fraction_non_alphanumeric': 0.174993627326026, 'fraction_numerical': 0.0021667091511598266, 'mean_word_length': 4.653458213256484, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6302643', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2702, 'n_words': 522}