Use UserIdentity in VariableGenerator::addEditVars

Change-Id: If0a65d7a86de776e6499d43949bfb217f20d9b07
This commit is contained in:
Umherirrender 2022-06-27 21:50:47 +02:00
parent f13f3dcd2c
commit da4bc8643a
3 changed files with 14 additions and 13 deletions

View file

@ -173,10 +173,10 @@ class VariableGenerator {
/**
* @param WikiPage $page
* @param User $user The current user
* @param UserIdentity $userIdentity The current user
* @return $this For chaining
*/
public function addEditVars( WikiPage $page, User $user ): self {
public function addEditVars( WikiPage $page, UserIdentity $userIdentity ): self {
$this->vars->setLazyLoadVar( 'edit_diff', 'diff',
[ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_wikitext' ] );
$this->vars->setLazyLoadVar( 'edit_diff_pst', 'diff',
@ -206,26 +206,26 @@ class VariableGenerator {
[
'text-var' => 'new_wikitext',
'article' => $page,
'contextUser' => $user
'contextUserIdentity' => $userIdentity
] );
$this->vars->setLazyLoadVar( 'old_links', 'links-from-wikitext-or-database',
[
'article' => $page,
'text-var' => 'old_wikitext',
'contextUser' => $user
'contextUserIdentity' => $userIdentity
] );
$this->vars->setLazyLoadVar( 'new_pst', 'parse-wikitext',
[
'wikitext-var' => 'new_wikitext',
'article' => $page,
'pst' => true,
'contextUser' => $user
'contextUserIdentity' => $userIdentity
] );
$this->vars->setLazyLoadVar( 'new_html', 'parse-wikitext',
[
'wikitext-var' => 'new_wikitext',
'article' => $page,
'contextUser' => $user
'contextUserIdentity' => $userIdentity
] );
return $this;

View file

@ -199,7 +199,7 @@ class LazyVariableComputer {
$editInfo = $article->prepareContentForEdit(
$content,
null,
$parameters['contextUser']
$parameters['contextUserIdentity']
);
$result = array_keys( $editInfo->output->getExternalLinks() );
self::$profilingExtraTime += ( microtime( true ) - $startTime );
@ -225,7 +225,7 @@ class LazyVariableComputer {
$editInfo = $this->parseNonEditWikitext(
$wikitext,
$article,
$parameters['contextUser']
$parameters['contextUserIdentity']
);
$links = array_keys( $editInfo->output->getExternalLinks() );
} else {
@ -267,7 +267,7 @@ class LazyVariableComputer {
$editInfo = $article->prepareContentForEdit(
$content,
null,
$parameters['contextUser']
$parameters['contextUserIdentity']
);
if ( isset( $parameters['pst'] ) && $parameters['pst'] ) {
$result = $editInfo->pstContent->serialize( $editInfo->format );
@ -477,17 +477,17 @@ class LazyVariableComputer {
*
* @param string $wikitext
* @param WikiPage $article
* @param User $user Context user
* @param UserIdentity $userIdentity Context user
*
* @return stdClass
*/
private function parseNonEditWikitext( $wikitext, WikiPage $article, User $user ) {
private function parseNonEditWikitext( $wikitext, WikiPage $article, UserIdentity $userIdentity ) {
static $cache = [];
$cacheKey = md5( $wikitext ) . ':' . $article->getTitle()->getPrefixedText();
if ( !isset( $cache[$cacheKey] ) ) {
$options = ParserOptions::newFromUser( $user );
$options = ParserOptions::newFromUser( $userIdentity );
$cache[$cacheKey] = (object)[
'output' => $this->parser->parse( $wikitext, $article->getTitle(), $options )
];

View file

@ -8,6 +8,7 @@ use MediaWiki\Extension\AbuseFilter\Parser\AFPData;
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGenerator;
use MediaWiki\Extension\AbuseFilter\Variables\LazyLoadedVariable;
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
use MediaWiki\User\UserIdentity;
use MediaWikiUnitTestCase;
use Title;
use User;
@ -175,7 +176,7 @@ class VariableGeneratorTest extends MediaWikiUnitTestCase {
$generator = new VariableGenerator( $this->createMock( AbuseFilterHookRunner::class ) );
$actualVars = $generator->addEditVars(
$this->createMock( WikiPage::class ),
$this->createMock( User::class )
$this->createMock( UserIdentity::class )
)->getVariableHolder()->getVars();
$this->assertArrayEquals( $expectedKeys, array_keys( $actualVars ) );
$this->assertContainsOnlyInstancesOf( LazyLoadedVariable::class, $actualVars, 'lazy-loaded vars' );