Page MenuHomeMiraheze
Paste P491

fixMissingRevisionComment.php (T11366)
ActivePublic

Authored by Void on Nov 3 2023, 02:08.
Tags
None
Referenced Files
F2433638: fixMissingRevisionComment.php (T11366)
Nov 3 2023, 02:08
Subscribers
None
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
*/
use MediaWiki\MediaWikiServices;
require_once __DIR__ . '/Maintenance.php';
/**
* Fixes missing rev_comment_id entries. Hacky one-time script.
* Inserts empty comment for missing
*
* @ingroup Maintenance
*/
class FixMissingRevisionComment extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Fixes instances of missing rev_comment_id with no corresponding revcomment_rev with an empty comment' );
}
public function execute() {
$dbw = $this->getDB( DB_PRIMARY );
$query = $dbw->newSelectQueryBuilder()
->select( '*' )
->from( 'revision' )
->leftJoin( 'revision_comment_temp', null, ['revision.rev_id' => 'revision_comment_temp.revcomment_rev'] )
->where( [
'revision.rev_comment_id' => 0,
'revision_comment_temp.revcomment_rev' => null,
] )
->caller( __METHOD__ );
$res = $query->fetchResultSet();
if ( $res->count() > 0 ) {
// Creates empty comment we can use
$cs = MediaWikiServices::getInstance()->getCommentStore();
$dummyComment = $cs->createComment( $dbw, '' );
// Attach comment to each revision
foreach ( $res as $row ) {
$update = $dbw->newUpdateQueryBuilder()
->update('revision')
->set( [
'revision.rev_comment_id' => $dummyComment->id,
] )
->where( [
'revision.rev_id' => $row->rev_id,
] )
->caller( __METHOD__ )
->execute();
if ( !$update ) {
$this->output( "WARNING: Could not update revision {$row->rev_id} (comment_id: {$dummyComment->id})\n" );
}
// Assumes SCHEMA_COMPAT_WRITE_BOTH
$dbw->insert(
'revision_comment_temp',
[
'revcomment_rev' => $row->rev_id,
'revcomment_comment_id' => $dummyComment->id,
],
__METHOD__
);
$this->output( "Added comment_id {$dummyComment->id} to revision {$row->rev_id}\n" );
}
}
}
}
$maintClass = FixMissingRevisionComment::class;
require_once RUN_MAINTENANCE_IF_MAIN;

Event Timeline