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": {
"class": "MediaWiki\\Extension\\Scribunto\\ApiScribuntoConsole",
"services": [
"ObjectCacheFactory",
"ParserFactory"
]
}
@ -110,7 +111,10 @@
"main": {
"class": "MediaWiki\\Extension\\Scribunto\\Hooks",
"services": [
"MainConfig"
"MainConfig",
"ContentHandlerFactory",
"ObjectCacheFactory",
"StatsFactory"
]
},
"codeeditor": {

View file

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

View file

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

View file

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