Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 81 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MigrateCentralWikiLogs | |
0.00% |
0 / 81 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 76 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This script should be run as part of migrating to a new central OAuth wiki in your |
| 4 | * cluster. See the notes in migrateCentralWikiLogs.php for the complete process. |
| 5 | * This script is intended to be run on the new central wiki after the tables have already |
| 6 | * been migrated. This will fill in the logs from newest to oldest, and tries to do sane |
| 7 | * things if you need to stop it and restart it later. |
| 8 | * |
| 9 | * @ingroup Maintenance |
| 10 | */ |
| 11 | // @codeCoverageIgnoreStart |
| 12 | if ( getenv( 'MW_INSTALL_PATH' ) ) { |
| 13 | $IP = getenv( 'MW_INSTALL_PATH' ); |
| 14 | } else { |
| 15 | $IP = __DIR__ . '/../../..'; |
| 16 | } |
| 17 | |
| 18 | require_once "$IP/maintenance/Maintenance.php"; |
| 19 | // @codeCoverageIgnoreEnd |
| 20 | |
| 21 | use MediaWiki\Logging\LogEntryBase; |
| 22 | use MediaWiki\Logging\ManualLogEntry; |
| 23 | use MediaWiki\Maintenance\Maintenance; |
| 24 | use MediaWiki\MediaWikiServices; |
| 25 | use MediaWiki\Title\Title; |
| 26 | use MediaWiki\WikiMap\WikiMap; |
| 27 | use Wikimedia\Rdbms\SelectQueryBuilder; |
| 28 | |
| 29 | class MigrateCentralWikiLogs extends Maintenance { |
| 30 | public function __construct() { |
| 31 | parent::__construct(); |
| 32 | $this->addDescription( "Import central wiki logs to this wiki" ); |
| 33 | $this->addOption( 'old', 'Previous central wiki', true, true ); |
| 34 | $this->setBatchSize( 200 ); |
| 35 | $this->requireExtension( "OAuth" ); |
| 36 | } |
| 37 | |
| 38 | public function execute() { |
| 39 | $oldWiki = $this->getOption( 'old' ); |
| 40 | $targetWiki = WikiMap::getCurrentWikiId(); |
| 41 | |
| 42 | $this->output( "Moving OAuth logs from '$oldWiki' to '$targetWiki'\n" ); |
| 43 | |
| 44 | // We only read from $oldDb, but we do want to make sure we get the most recent logs. |
| 45 | $connectionProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 46 | $oldDb = $connectionProvider->getPrimaryDatabase( $oldWiki ); |
| 47 | $targetDb = $connectionProvider->getPrimaryDatabase( $targetWiki ); |
| 48 | |
| 49 | $targetMinTS = $targetDb->newSelectQueryBuilder() |
| 50 | ->select( 'MIN(log_timestamp)' ) |
| 51 | ->from( 'logging' ) |
| 52 | ->where( [ 'log_type' => 'mwoauthconsumer' ] ) |
| 53 | ->caller( __METHOD__ ) |
| 54 | ->fetchField(); |
| 55 | |
| 56 | $lastMinTimestamp = null; |
| 57 | if ( $targetMinTS !== false ) { |
| 58 | $lastMinTimestamp = $targetMinTS; |
| 59 | } |
| 60 | |
| 61 | $commentStore = MediaWikiServices::getInstance()->getCommentStore(); |
| 62 | $commentQuery = $commentStore->getJoin( 'log_comment' ); |
| 63 | |
| 64 | do { |
| 65 | $conds = [ 'log_type' => 'mwoauthconsumer' ]; |
| 66 | |
| 67 | // This assumes that we don't have more than mBatchSize oauth log entries |
| 68 | // with the same timestamp. Otherwise this will go into an infinite loop. |
| 69 | if ( $lastMinTimestamp !== null ) { |
| 70 | $conds[] = $oldDb->expr( 'log_timestamp', '<', $oldDb->timestamp( $lastMinTimestamp ) ); |
| 71 | } |
| 72 | |
| 73 | $oldLoggs = $oldDb->newSelectQueryBuilder() |
| 74 | ->select( [ |
| 75 | 'log_id', 'log_action', 'log_timestamp', 'log_params', 'log_deleted', |
| 76 | 'actor_id', 'actor_name', 'actor_user' |
| 77 | ] ) |
| 78 | ->from( 'logging' ) |
| 79 | ->join( 'actor', null, 'actor_id=log_actor' ) |
| 80 | ->where( $conds ) |
| 81 | ->queryInfo( $commentQuery ) |
| 82 | ->orderBy( 'log_timestamp', SelectQueryBuilder::SORT_DESC ) |
| 83 | ->limit( $this->mBatchSize + 1 ) |
| 84 | ->caller( __METHOD__ ) |
| 85 | ->fetchResultSet(); |
| 86 | |
| 87 | $rowCount = $oldLoggs->numRows(); |
| 88 | |
| 89 | if ( $rowCount == $this->mBatchSize + 1 ) { |
| 90 | $first = $oldLoggs->fetchObject(); |
| 91 | $oldLoggs->seek( $rowCount - 2 ); |
| 92 | $last = $oldLoggs->fetchObject(); |
| 93 | if ( $first->log_timestamp === $last->log_timestamp ) { |
| 94 | $this->fatalError( "Batch size too low to avoid infinite loop.\n" ); |
| 95 | } |
| 96 | $extra = $oldLoggs->fetchObject(); |
| 97 | if ( $last->log_timestamp === $extra->log_timestamp ) { |
| 98 | $this->fatalError( "We hit an edge case. Please increase the batch " . |
| 99 | " size and restart the transfer.\n" ); |
| 100 | } |
| 101 | $oldLoggs->rewind(); |
| 102 | } |
| 103 | |
| 104 | $targetDb->begin( __METHOD__ ); |
| 105 | foreach ( $oldLoggs as $key => $row ) { |
| 106 | // Skip if this is the extra row we selected |
| 107 | if ( $key > $this->mBatchSize ) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $lastMinTimestamp = $row->log_timestamp; |
| 112 | |
| 113 | $this->output( "Migrating log {$row->log_id}...\n" ); |
| 114 | if ( !$row->actor_user ) { |
| 115 | $this->output( |
| 116 | "Cannot transfer log_id: {$row->log_id}, the log user doesn't exist" |
| 117 | ); |
| 118 | continue; |
| 119 | } |
| 120 | $logUser = MediaWikiServices::getInstance()->getActorNormalization() |
| 121 | ->newActorFromRow( $row ); |
| 122 | $params = LogEntryBase::extractParams( $row->log_params ); |
| 123 | if ( !isset( $params['4:consumer'] ) ) { |
| 124 | $this->output( "Cannot transfer log_id: {$row->log_id}, param isn't correct" ); |
| 125 | continue; |
| 126 | } |
| 127 | $logEntry = new ManualLogEntry( 'mwoauthconsumer', $row->log_action ); |
| 128 | $logEntry->setPerformer( $logUser ); |
| 129 | $logEntry->setTarget( Title::makeTitleSafe( NS_USER, $row->actor_name ) ); |
| 130 | $logEntry->setComment( $commentStore->getComment( 'log_comment', $row )->text ); |
| 131 | $logEntry->setParameters( $params ); |
| 132 | $logEntry->setRelations( [ |
| 133 | 'OAuthConsumer' => [ $params['4:consumer'] ] |
| 134 | ] ); |
| 135 | // ManualLogEntry::insert() calls $dbw->timestamp on the value |
| 136 | $logEntry->setTimestamp( $row->log_timestamp ); |
| 137 | // @TODO: Maybe this will do something some day. Sigh. |
| 138 | $logEntry->setDeleted( $row->log_deleted ); |
| 139 | $logEntry->insert( $targetDb ); |
| 140 | } |
| 141 | $targetDb->commit( __METHOD__ ); |
| 142 | |
| 143 | $this->waitForReplication(); |
| 144 | |
| 145 | } while ( $rowCount ); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | // @codeCoverageIgnoreStart |
| 151 | $maintClass = MigrateCentralWikiLogs::class; |
| 152 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 153 | // @codeCoverageIgnoreEnd |