Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
QueryStatsActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use ApiBase;
7use ApiQuery;
8use ApiQueryBase;
9use DeferredUpdates;
10use IJobSpecification;
11use JobQueueGroup;
13use Wikimedia\ParamValidator\ParamValidator;
14
23abstract class QueryStatsActionApi extends ApiQueryBase {
25 private $jobQueueGroup;
26
27 public function __construct(
28 ApiQuery $queryModule,
29 string $moduleName,
30 string $paramPrefix,
31 JobQueueGroup $jobQueueGroup
32 ) {
33 parent::__construct( $queryModule, $moduleName, $paramPrefix );
34 $this->jobQueueGroup = $jobQueueGroup;
35 }
36
37 public function getCacheMode( $params ): string {
38 return 'public';
39 }
40
45 abstract protected function validateTargetParamater( array $params ): string;
46
53 abstract protected function loadStatistics( string $target, int $flags = 0 ): array;
54
56 abstract protected function makeStatsItem( string $item, array $stats ): ?array;
57
58 public function execute() {
59 $params = $this->extractRequestParams();
60
61 $target = $this->validateTargetParamater( $params );
62 $cache = $this->loadStatistics( $target, MessageGroupStats::FLAG_CACHE_ONLY );
63
64 $result = $this->getResult();
65 $incomplete = false;
66
67 foreach ( $cache as $item => $stats ) {
68 if ( $item < $params['offset'] ) {
69 continue;
70 }
71
72 if ( $stats[MessageGroupStats::TOTAL] === null ) {
73 $incomplete = true;
74 $this->setContinueEnumParameter( 'offset', $item );
75 break;
76 }
77
78 $data = $this->makeStatsItem( $item, $stats );
79 if ( $data === null ) {
80 continue;
81 }
82 $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
83 }
84
85 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'stats' );
86
87 if ( $incomplete ) {
88 DeferredUpdates::addCallableUpdate( function () use ( $target ): void {
89 $this->jobQueueGroup->push( $this->getCacheRebuildJob( $target ) );
90 } );
91 }
92 }
93
94 protected function makeItem( array $stats ): array {
95 return [
96 'total' => $stats[MessageGroupStats::TOTAL],
97 'translated' => $stats[MessageGroupStats::TRANSLATED],
98 'fuzzy' => $stats[MessageGroupStats::FUZZY],
99 'proofread' => $stats[MessageGroupStats::PROOFREAD],
100 ];
101 }
102
103 abstract protected function getCacheRebuildJob( string $target ): IJobSpecification;
104
105 protected function getAllowedParams(): array {
106 return [
107 'offset' => [
108 ParamValidator::PARAM_DEFAULT => '0',
109 ParamValidator::PARAM_TYPE => 'string',
110 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
111 ],
112 ];
113 }
114}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
A base module for querying message group related stats.
makeStatsItem(string $item, array $stats)
Implement this to load each individual stat item.
loadStatistics(string $target, int $flags=0)
Implement this to load stats.
validateTargetParamater(array $params)
Implement this to implement input validation and return the name of the target that is then given to ...
This class abstract MessageGroup statistics calculation and storing.