Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
GetImageSuggestionData | |
0.00% |
0 / 45 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Maintenance; |
4 | |
5 | use FormatJson; |
6 | use GrowthExperiments\GrowthExperimentsServices; |
7 | use GrowthExperiments\NewcomerTasks\AddImage\ServiceImageRecommendationProvider; |
8 | use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationTaskTypeHandler; |
9 | use GrowthExperiments\NewcomerTasks\TaskType\SectionImageRecommendationTaskTypeHandler; |
10 | use Maintenance; |
11 | use MediaWiki\Linker\LinkTarget; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\Status\Status; |
14 | use StatusValue; |
15 | |
16 | $IP = getenv( 'MW_INSTALL_PATH' ); |
17 | if ( $IP === false ) { |
18 | $IP = __DIR__ . '/../../..'; |
19 | } |
20 | require_once "$IP/maintenance/Maintenance.php"; |
21 | |
22 | /** |
23 | * Script to allow obtaining image suggestion data. Useful for verifying what |
24 | * ServiceImageRecommendationProvider is doing. |
25 | */ |
26 | class GetImageSuggestionData extends Maintenance { |
27 | |
28 | /** @inheritDoc */ |
29 | public function __construct() { |
30 | parent::__construct(); |
31 | $this->requireExtension( 'GrowthExperiments' ); |
32 | $this->addDescription( 'Get image suggestion data via ServiceImageRecommendationProvider' ); |
33 | $this->addOption( |
34 | 'title', |
35 | 'The page title to use in fetching image suggestion data.', |
36 | true |
37 | ); |
38 | $this->addOption( |
39 | 'section-level', |
40 | 'Get section-level suggestions instead of top-level suggestions.' |
41 | ); |
42 | $this->addOption( |
43 | 'max-suggestions', |
44 | 'The maximum number of valid image suggestions to process per title.', |
45 | false, |
46 | true |
47 | ); |
48 | } |
49 | |
50 | /** @inheritDoc */ |
51 | public function execute() { |
52 | $services = MediaWikiServices::getInstance(); |
53 | $growthServices = GrowthExperimentsServices::wrap( MediaWikiServices::getInstance() ); |
54 | $serviceImageRecommendationProvider = $growthServices->getImageRecommendationProviderUncached(); |
55 | if ( !$serviceImageRecommendationProvider instanceof ServiceImageRecommendationProvider ) { |
56 | // This isn't really necessary, but done to make phan happy. |
57 | $this->fatalError( |
58 | get_class( $serviceImageRecommendationProvider ) . ' is not an instance of ' . |
59 | ServiceImageRecommendationProvider::class |
60 | ); |
61 | } |
62 | // Not needed in production, but simplifies local environment/production usage. |
63 | global $wgGEImageRecommendationServiceUseTitles; |
64 | $wgGEImageRecommendationServiceUseTitles = true; |
65 | $title = $services->getTitleFactory()->newFromText( $this->getOption( 'title' ) ); |
66 | if ( !$title instanceof LinkTarget ) { |
67 | $this->fatalError( 'Unable to make a LinkTarget from ' . $this->getOption( 'title' ) ); |
68 | } |
69 | $maxSuggestions = (int)$this->getOption( 'max-suggestions', 1 ); |
70 | if ( $maxSuggestions < 1 ) { |
71 | $this->fatalError( 'max-suggestions needs to be > 1' ); |
72 | } |
73 | $serviceImageRecommendationProvider->setMaxSuggestionsToProcess( $maxSuggestions ); |
74 | if ( $this->hasOption( 'section-level' ) ) { |
75 | $taskType = $growthServices->getNewcomerTasksConfigurationLoader() |
76 | ->getTaskTypes()[ SectionImageRecommendationTaskTypeHandler::TASK_TYPE_ID ]; |
77 | } else { |
78 | $taskType = $growthServices->getNewcomerTasksConfigurationLoader() |
79 | ->getTaskTypes()[ ImageRecommendationTaskTypeHandler::TASK_TYPE_ID ]; |
80 | } |
81 | $result = $serviceImageRecommendationProvider->get( $title, $taskType ); |
82 | if ( $result instanceof StatusValue ) { |
83 | $this->fatalError( Status::wrap( $result )->getWikiText( false, false, 'en' ) ); |
84 | } else { |
85 | $jsonData = FormatJson::encode( $result, true ); |
86 | $this->output( $jsonData . PHP_EOL ); |
87 | } |
88 | } |
89 | } |
90 | |
91 | $maintClass = GetImageSuggestionData::class; |
92 | require_once RUN_MAINTENANCE_IF_MAIN; |