Inject services into ApiScribuntoConsole and Hooks

Change-Id: Iafb24d774c7cbc13b4856863bdd458fd08d1bedc
This commit is contained in:
Fomafix 2024-10-26 20:39:00 +00:00 committed by jenkins-bot
parent 4e77f0e335
commit 9a64973201
4 changed files with 31 additions and 13 deletions

View file

@ -16,6 +16,7 @@
"scribunto-console": { "scribunto-console": {
"class": "MediaWiki\\Extension\\Scribunto\\ApiScribuntoConsole", "class": "MediaWiki\\Extension\\Scribunto\\ApiScribuntoConsole",
"services": [ "services": [
"ObjectCacheFactory",
"ParserFactory" "ParserFactory"
] ]
} }
@ -110,7 +111,10 @@
"main": { "main": {
"class": "MediaWiki\\Extension\\Scribunto\\Hooks", "class": "MediaWiki\\Extension\\Scribunto\\Hooks",
"services": [ "services": [
"MainConfig" "MainConfig",
"ContentHandlerFactory",
"ObjectCacheFactory",
"StatsFactory"
] ]
}, },
"codeeditor": { "codeeditor": {

View file

@ -5,11 +5,11 @@ namespace MediaWiki\Extension\Scribunto;
use MediaWiki\Api\ApiBase; use MediaWiki\Api\ApiBase;
use MediaWiki\Api\ApiMain; use MediaWiki\Api\ApiMain;
use MediaWiki\Html\Html; use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserFactory; use MediaWiki\Parser\ParserFactory;
use MediaWiki\Parser\ParserOptions; use MediaWiki\Parser\ParserOptions;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use ObjectCacheFactory;
use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\ParamValidator;
/** /**
@ -18,14 +18,17 @@ use Wikimedia\ParamValidator\ParamValidator;
class ApiScribuntoConsole extends ApiBase { class ApiScribuntoConsole extends ApiBase {
private const SC_MAX_SIZE = 500000; private const SC_MAX_SIZE = 500000;
private const SC_SESSION_EXPIRY = 3600; private const SC_SESSION_EXPIRY = 3600;
private ObjectCacheFactory $objectCacheFactory;
private ParserFactory $parserFactory; private ParserFactory $parserFactory;
public function __construct( public function __construct(
ApiMain $main, ApiMain $main,
string $action, string $action,
ObjectCacheFactory $objectCacheFactory,
ParserFactory $parserFactory ParserFactory $parserFactory
) { ) {
parent::__construct( $main, $action ); parent::__construct( $main, $action );
$this->objectCacheFactory = $objectCacheFactory;
$this->parserFactory = $parserFactory; $this->parserFactory = $parserFactory;
} }
@ -46,9 +49,7 @@ class ApiScribuntoConsole extends ApiBase {
$sessionId = mt_rand( 0, 0x7fffffff ); $sessionId = mt_rand( 0, 0x7fffffff );
} }
$services = MediaWikiServices::getInstance(); $cache = $this->objectCacheFactory->getInstance( CACHE_ANYTHING );
$cache = $services->getObjectCacheFactory()->getInstance( CACHE_ANYTHING );
$sessionKey = $cache->makeKey( 'scribunto-console', $this->getUser()->getId(), $sessionId ); $sessionKey = $cache->makeKey( 'scribunto-console', $this->getUser()->getId(), $sessionId );
$session = null; $session = null;
$sessionIsNew = false; $sessionIsNew = false;

View file

@ -27,6 +27,7 @@ namespace MediaWiki\Extension\Scribunto;
use Article; use Article;
use MediaWiki\Config\Config; use MediaWiki\Config\Config;
use MediaWiki\Content\Content; use MediaWiki\Content\Content;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Context\IContextSource; use MediaWiki\Context\IContextSource;
use MediaWiki\EditPage\EditPage; use MediaWiki\EditPage\EditPage;
use MediaWiki\Hook\EditFilterMergedContentHook; use MediaWiki\Hook\EditFilterMergedContentHook;
@ -40,7 +41,6 @@ use MediaWiki\Hook\ParserLimitReportFormatHook;
use MediaWiki\Hook\ParserLimitReportPrepareHook; use MediaWiki\Hook\ParserLimitReportPrepareHook;
use MediaWiki\Hook\SoftwareInfoHook; use MediaWiki\Hook\SoftwareInfoHook;
use MediaWiki\Html\Html; use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage; use MediaWiki\Output\OutputPage;
use MediaWiki\Page\Hook\ArticleViewHeaderHook; use MediaWiki\Page\Hook\ArticleViewHeaderHook;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
@ -52,9 +52,11 @@ use MediaWiki\Status\Status;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MediaWiki\User\User; use MediaWiki\User\User;
use MediaWiki\WikiMap\WikiMap; use MediaWiki\WikiMap\WikiMap;
use ObjectCacheFactory;
use UtfNormal\Validator; use UtfNormal\Validator;
use Wikimedia\ObjectCache\EmptyBagOStuff; use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\PSquare; use Wikimedia\PSquare;
use Wikimedia\Stats\StatsFactory;
/** /**
* Hooks for the Scribunto extension. * Hooks for the Scribunto extension.
@ -74,11 +76,20 @@ class Hooks implements
ContentHandlerDefaultModelForHook ContentHandlerDefaultModelForHook
{ {
private Config $config; private Config $config;
private IContentHandlerFactory $contentHandlerFactory;
private ObjectCacheFactory $objectCacheFactory;
private StatsFactory $statsFactory;
public function __construct( public function __construct(
Config $config Config $config,
IContentHandlerFactory $contentHandlerFactory,
ObjectCacheFactory $objectCacheFactory,
StatsFactory $statsFactory
) { ) {
$this->config = $config; $this->config = $config;
$this->contentHandlerFactory = $contentHandlerFactory;
$this->objectCacheFactory = $objectCacheFactory;
$this->statsFactory = $statsFactory;
} }
/** /**
@ -245,10 +256,9 @@ class Hooks implements
return; return;
} }
$objectcachefactory = MediaWikiServices::getInstance()->getObjectCacheFactory();
static $cache; static $cache;
if ( !$cache ) { if ( !$cache ) {
$cache = $objectcachefactory->getLocalServerInstance( CACHE_NONE ); $cache = $this->objectCacheFactory->getLocalServerInstance( CACHE_NONE );
} }
@ -277,7 +287,7 @@ class Hooks implements
static $stats; static $stats;
if ( !$stats ) { if ( !$stats ) {
$stats = MediaWikiServices::getInstance()->getStatsFactory(); $stats = $this->statsFactory;
} }
$statAction = WikiMap::getCurrentWikiId() . '__' . $moduleName . '__' . $functionName; $statAction = WikiMap::getCurrentWikiId() . '__' . $moduleName . '__' . $functionName;
$stats->getTiming( 'scribunto_traces_seconds' ) $stats->getTiming( 'scribunto_traces_seconds' )
@ -396,8 +406,7 @@ class Hooks implements
if ( !$content instanceof ScribuntoContent ) { if ( !$content instanceof ScribuntoContent ) {
return true; return true;
} }
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
$contentHandler = $contentHandlerFactory->getContentHandler( $content->getModel() );
'@phan-var ScribuntoContentHandler $contentHandler'; '@phan-var ScribuntoContentHandler $contentHandler';
$validateStatus = $contentHandler->validate( $content, $title ); $validateStatus = $contentHandler->validate( $content, $title );

View file

@ -33,8 +33,12 @@ class HooksTest extends TestCase {
) { ) {
$title = Title::newFromText( $name ); $title = Title::newFromText( $name );
$model = $before; $model = $before;
$services = MediaWikiServices::getInstance();
( new Hooks( ( new Hooks(
MediaWikiServices::getInstance()->getMainConfig() $services->getMainConfig(),
$services->getContentHandlerFactory(),
$services->getObjectCacheFactory(),
$services->getStatsFactory()
) )->onContentHandlerDefaultModelFor( $title, $model ); ) )->onContentHandlerDefaultModelFor( $title, $model );
$this->assertSame( $expected, $model ); $this->assertSame( $expected, $model );
} }