Page MenuHomePhabricator
Paste P69320

Maintenance script to create a lot of RecentChange events
ActivePublic

Authored by dom_walden on Sep 19 2024, 10:25 AM.
<?php
namespace MediaWiki\CheckUser\Maintenance;
use Maintenance;
use RecentChange;
use Exception;
use MediaWiki\Context\RequestContext;
if ( getenv( 'MW_INSTALL_PATH' ) ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = dirname( __DIR__, 3 );
}
require_once "$IP/maintenance/Maintenance.php";
class testUpdateCheckUserDataFromRecentChanges extends Maintenance {
public function __construct() {
parent::__construct();
$this->requireExtension( 'CheckUser' );
}
public function execute() {
$query = "WITH random_revisions AS
(SELECT foo.*, row_number() OVER(PARTITION BY rc_namespace, rc_minor, rc_bot, rc_new, rc_type, rc_source, rc_patrolled, rc_deleted, rc_log_type, rc_log_action ORDER BY RAND()) AS random_sort
FROM
(SELECT log_timestamp AS rc_timestamp,
actor_id AS rc_actor,
page_namespace AS rc_namespace,
page_title AS rc_title,
111 AS rc_comment_id,
rev_minor_edit AS rc_minor,
0 AS rc_bot,
IF(rev_parent_id = 0, 1, 0) AS rc_new,
page_id AS rc_cur_id,
rev_id AS rc_this_oldid,
rev_parent_id AS rc_last_oldid,
ttype AS rc_type,
ssource AS rc_source,
patrolled AS rc_patrolled,
INET_NTOA(FLOOR(RAND()*POW(2, 32))) AS rc_ip,
123 AS rc_old_len,
234 AS rc_new_len,
deleted AS rc_deleted,
log_id AS rc_logid,
log_type AS rc_log_type,
log_action AS rc_log_action
FROM (SELECT * FROM (SELECT *, row_number() OVER(PARTITION BY user_is_temp ORDER BY RAND()) AS random_sort FROM actor INNER JOIN user ON actor_user = user_id) AS f WHERE random_sort <= 5) AS e
LEFT JOIN (SELECT * FROM page ORDER BY RAND() LIMIT 5) AS a ON TRUE
LEFT JOIN (SELECT * FROM revision ORDER BY RAND() LIMIT 5) AS b ON TRUE
LEFT JOIN (SELECT * FROM logging GROUP BY log_type, log_action ORDER BY RAND() LIMIT 5) AS c ON TRUE
LEFT JOIN (SELECT 0 AS ttype UNION SELECT 1 AS ttype UNION SELECT 2 AS ttype UNION SELECT 3 AS ttype UNION SELECT 4 AS ttype UNION SELECT 5 AS ttype UNION SELECT 6 AS ttype) AS t ON TRUE
LEFT JOIN (SELECT 'mw.edit' AS ssource UNION SELECT 'mw.new' AS ssource UNION SELECT 'mw.log' AS ssource UNION SELECT 'mw.external' AS ssource UNION SELECT 'mw.categorize' AS ssource) AS s ON TRUE
LEFT JOIN (SELECT 0 AS patrolled UNION SELECT 1 AS patrolled UNION SELECT 2 AS patrolled) AS p ON TRUE
LEFT JOIN (SELECT 0 AS deleted UNION SELECT 1 AS deleted UNION SELECT 2 AS deleted) AS d ON TRUE
) AS foo)
SELECT rc_timestamp,rc_actor,rc_namespace,rc_title,rc_comment_id,rc_minor,rc_bot,rc_new,rc_cur_id,rc_this_oldid,rc_last_oldid,rc_type,rc_source,rc_patrolled,rc_ip,rc_old_len,rc_new_len,rc_deleted,rc_logid,rc_log_type,rc_log_action FROM random_revisions WHERE random_sort <= 1;";
$dbr = $this->getPrimaryDB();
$res = $dbr->query( $query );
$success = 0;
$fail = 0;
$first = true;
foreach ( $res as $row ) {
try {
$rc = RecentChange::newFromRow( $row );
RequestContext::getMain()->getRequest()->setIP( $rc->mAttribs[ 'rc_ip' ] );
$rc->save();
if ( $first ) {
print("First rc_id: {$rc->mAttribs['rc_id']}\n");
$first = false;
}
$success = $success + 1;
} catch (Exception $e) {
if ( $first ) {
print("First rc_id: {$rc->mAttribs['rc_id']}\n");
$first = false;
}
$fail = $fail + 1;
}
}
print("Successes: {$success}\n");
print("Failures: {$fail}\n");
}
}
$maintClass = testUpdateCheckUserDataFromRecentChanges::class;
require_once RUN_MAINTENANCE_IF_MAIN;