Set old_content_model & new_content_model for past changes

We might consider adding an in-process cache because there
will be a duplicate database lookup for content model and
wikitext of the same revision.

Bug: T230295
Change-Id: I9723f21069e03a49fa7131bd8f79c6e7e442104b
This commit is contained in:
Matěj Suchánek 2021-09-07 12:09:14 +02:00 committed by DannyS712
parent 396d892c60
commit 3e0d1b0d38
5 changed files with 30 additions and 2 deletions

View file

@ -212,19 +212,23 @@ class RCVariableGenerator extends VariableGenerator {
$this->addUserVars( $userIdentity, $this->rc )
->addTitleVars( $title, 'page', $this->rc );
// @todo Set old_content_model and new_content_model
$this->vars->setVar( 'action', 'edit' );
$this->vars->setVar( 'summary', $this->rc->getAttribute( 'rc_comment' ) );
$this->vars->setLazyLoadVar( 'new_wikitext', 'revision-text-by-id',
[ 'revid' => $this->rc->getAttribute( 'rc_this_oldid' ), 'contextUser' => $this->contextUser ] );
$this->vars->setLazyLoadVar( 'new_content_model', 'content-model-by-id',
[ 'revid' => $this->rc->getAttribute( 'rc_this_oldid' ) ] );
$parentId = $this->rc->getAttribute( 'rc_last_oldid' );
if ( $parentId ) {
$this->vars->setLazyLoadVar( 'old_wikitext', 'revision-text-by-id',
[ 'revid' => $parentId, 'contextUser' => $this->contextUser ] );
$this->vars->setLazyLoadVar( 'old_content_model', 'content-model-by-id',
[ 'revid' => $parentId ] );
} else {
$this->vars->setVar( 'old_wikitext', '' );
$this->vars->setVar( 'old_content_model', '' );
}
$this->addEditVars(

View file

@ -284,7 +284,7 @@ class RunVariableGenerator extends VariableGenerator {
// We only have the upload comment and page text when using the UploadVerifyUpload hook
if ( $summary !== null && $text !== null ) {
// This block is adapted from self::getTextForFiltering()
// This block is adapted from self::getEditTextForFiltering()
$page = $this->wikiPageFactory->newFromTitle( $this->title );
if ( $this->title->exists() ) {
$revRec = $page->getRevisionRecord();

View file

@ -11,7 +11,9 @@ use MediaWiki\Extension\AbuseFilter\TextExtractor;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Permissions\RestrictionStore;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\User\UserEditTracker;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserIdentity;
@ -375,6 +377,10 @@ class LazyVariableComputer {
$v2 = $getVarCB( $parameters['val2-var'] )->toInt();
$result = $v1 - $v2;
break;
case 'content-model-by-id':
$revRec = $this->revisionLookup->getRevisionById( $parameters['revid'] );
$result = $this->getContentModelFromRevision( $revRec );
break;
case 'revision-text-by-id':
$revRec = $this->revisionLookup->getRevisionById( $parameters['revid'] );
$result = $this->textExtractor->revisionToString( $revRec, $parameters['contextUser'] );
@ -473,6 +479,20 @@ class LazyVariableComputer {
);
}
/**
* @param ?RevisionRecord $revision
* @return string
*/
private function getContentModelFromRevision( ?RevisionRecord $revision ): string {
// this is consistent with what is done on various places in RunVariableGenerator
// and RCVariableGenerator
if ( $revision !== null ) {
$content = $revision->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
return $content->getModel();
}
return '';
}
/**
* It's like WikiPage::prepareContentForEdit, but not for editing (old wikitext usually)
*

View file

@ -169,6 +169,7 @@ class VariablesManager {
'user-age',
'user-block',
'revision-text-by-id',
'content-model-by-id',
];
/** @var LazyLoadedVariable[] $missingVars */

View file

@ -65,6 +65,7 @@ class RCVariableGeneratorTest extends MediaWikiIntegrationTestCase {
switch ( $type ) {
case 'create':
$expectedValues['old_wikitext'] = '';
$expectedValues['old_content_model'] = '';
// Fallthrough
case 'edit':
$status = $this->editPage( $title, 'Some new text for testing RC vars.', $summary, NS_MAIN, $user );
@ -245,9 +246,11 @@ class RCVariableGeneratorTest extends MediaWikiIntegrationTestCase {
'page_age' => 10,
'old_wikitext' => $oldText,
'old_size' => strlen( $oldText ),
'old_content_model' => 'wikitext',
'old_links' => [ $oldLink ],
'new_wikitext' => $newText,
'new_size' => strlen( $newText ),
'new_content_model' => 'wikitext',
'all_links' => [ $newLink ],
'timestamp' => (string)$timestamp,
];