2019-01-09 11:57:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( getenv( 'MW_INSTALL_PATH' ) ) {
|
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
} else {
|
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
|
|
|
/**
|
|
|
|
* Fix old log entries with log_type = 'abusefilter' where log_params are imploded with '\n'
|
|
|
|
* instead of "\n" (using single quotes), which causes a broken display.
|
2019-08-11 16:46:58 +00:00
|
|
|
* This was caused by the addMissingLoggingEntries script creating broken entries, see T208931
|
|
|
|
* and T228655.
|
|
|
|
* It also fixes a problem which caused addMissingLoggingEntries to insert duplicate rows foreach
|
|
|
|
* non-legacy entries
|
2019-01-09 11:57:04 +00:00
|
|
|
*/
|
|
|
|
class FixOldLogEntries extends LoggedUpdateMaintenance {
|
2019-08-11 16:46:58 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $dryRun;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2019-01-09 11:57:04 +00:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2019-08-19 16:53:57 +00:00
|
|
|
$this->addDescription( 'Fix old rows in logging which hold broken log_params' );
|
2019-01-09 11:57:04 +00:00
|
|
|
|
2019-07-23 10:19:58 +00:00
|
|
|
$this->addOption( 'verbose', 'Print some more debug info' );
|
2019-01-09 11:57:04 +00:00
|
|
|
$this->addOption( 'dry-run', 'Perform a dry run' );
|
|
|
|
$this->requireExtension( 'Abuse Filter' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getUpdateKey() {
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-11 16:46:58 +00:00
|
|
|
* This method will delete duplicated logging rows created by addMissingLoggingEntries. This
|
|
|
|
* happened because the script couldn't recognize non-legacy entries, and considered them to be
|
|
|
|
* absent if the script was ran after the format update. See T228655#5360754 and T228655#5408193
|
|
|
|
*
|
|
|
|
* @return int[] The IDs of the affected rows
|
2019-01-09 11:57:04 +00:00
|
|
|
*/
|
2019-08-11 16:46:58 +00:00
|
|
|
private function deleteDuplicatedRows() {
|
|
|
|
$dbr = wfGetDB( DB_REPLICA, 'vslow' );
|
|
|
|
$newFormatLike = $dbr->buildLike( $dbr->anyString(), 'historyId', $dbr->anyString() );
|
|
|
|
// Select all non-legacy entries
|
|
|
|
$res = $dbr->select(
|
|
|
|
'logging',
|
|
|
|
[ 'log_params' ],
|
|
|
|
[
|
|
|
|
'log_type' => 'abusefilter',
|
|
|
|
"log_params $newFormatLike"
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
$legacyParams = [];
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$params = unserialize( $row->log_params );
|
|
|
|
// The script always inserted duplicates with the wrong '\n'
|
|
|
|
$legacyParams[] = $params['historyId'] . '\n' . $params['newId'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do a delete already, as it would have poor performance and could kill the DB
|
|
|
|
$deleteIDs = $dbr->selectFieldValues(
|
|
|
|
'logging',
|
|
|
|
'log_id',
|
|
|
|
[
|
|
|
|
'log_type' => 'abusefilter',
|
|
|
|
'log_params' => $legacyParams
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$this->dryRun ) {
|
|
|
|
// Note that we delete entries with legacy format, which are the ones erroneously inserted
|
|
|
|
// by the script.
|
|
|
|
wfGetDB( DB_MASTER )->delete(
|
|
|
|
'logging',
|
|
|
|
[ 'log_id' => $deleteIDs ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $deleteIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change single-quote newlines to double-quotes newlines
|
|
|
|
*
|
|
|
|
* @return int[] Affected log_id's
|
|
|
|
*/
|
|
|
|
private function changeNewlineType() {
|
2019-01-09 11:57:04 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA, 'vslow' );
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
$res = $dbr->select(
|
|
|
|
'logging',
|
|
|
|
[ 'log_id', 'log_params' ],
|
|
|
|
[
|
|
|
|
'log_type' => 'abusefilter',
|
|
|
|
'log_params ' . $dbr->buildLike(
|
|
|
|
$dbr->anyString(),
|
|
|
|
'\n',
|
|
|
|
$dbr->anyString()
|
|
|
|
)
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2019-07-23 10:19:58 +00:00
|
|
|
$updated = [];
|
2019-01-09 11:57:04 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$par = explode( '\n', $row->log_params );
|
|
|
|
if ( count( $par ) === 2 ) {
|
|
|
|
// Keep the entries legacy
|
|
|
|
$newVal = implode( "\n", $par );
|
|
|
|
|
2019-08-11 16:46:58 +00:00
|
|
|
if ( !$this->dryRun ) {
|
2019-01-09 11:57:04 +00:00
|
|
|
$dbw->update(
|
|
|
|
'logging',
|
|
|
|
[ 'log_params' => $newVal ],
|
|
|
|
[ 'log_id' => $row->log_id ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
2019-07-23 10:19:58 +00:00
|
|
|
$updated[] = $row->log_id;
|
2019-01-09 11:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 16:46:58 +00:00
|
|
|
return $updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function doDBUpdates() {
|
|
|
|
$this->dryRun = $this->hasOption( 'dry-run' );
|
|
|
|
|
|
|
|
$deleted = $this->deleteDuplicatedRows();
|
|
|
|
|
|
|
|
$deleteVerb = $this->dryRun ? 'would delete' : 'deleted';
|
|
|
|
$numDel = count( $deleted );
|
|
|
|
$this->output(
|
|
|
|
__CLASS__ . " $deleteVerb $numDel rows.\n"
|
|
|
|
);
|
|
|
|
if ( $deleted && $this->hasOption( 'verbose' ) ) {
|
|
|
|
$this->output( 'The affected log_id\'s are: ' . implode( ', ', $deleted ) . "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$updated = $this->changeNewlineType();
|
2019-01-09 11:57:04 +00:00
|
|
|
|
2019-08-11 16:46:58 +00:00
|
|
|
$updateVerb = $this->dryRun ? 'would update' : 'updated';
|
2019-07-23 10:19:58 +00:00
|
|
|
$numUpd = count( $updated );
|
|
|
|
$this->output(
|
2019-08-11 16:46:58 +00:00
|
|
|
__CLASS__ . " $updateVerb $numUpd rows.\n"
|
2019-07-23 10:19:58 +00:00
|
|
|
);
|
2019-07-24 11:22:57 +00:00
|
|
|
if ( $updated && $this->hasOption( 'verbose' ) ) {
|
2019-08-11 16:46:58 +00:00
|
|
|
$this->output( 'The affected log_id\'s are: ' . implode( ', ', $updated ) . "\n" );
|
2019-07-23 10:19:58 +00:00
|
|
|
}
|
2019-08-11 16:46:58 +00:00
|
|
|
return !$this->dryRun;
|
2019-01-09 11:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = 'FixOldLogEntries';
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|