Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UnInvalidateImageSuggestion
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace GrowthExperiments\Maintenance;
4
5use CirrusSearch\CirrusSearchServices;
6use GrowthExperiments\GrowthExperimentsServices;
7use GrowthExperiments\NewcomerTasks\ImageRecommendationFilter;
8use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationBaseTaskType;
9use Maintenance;
10
11class UnInvalidateImageSuggestion extends Maintenance {
12
13    public function __construct() {
14        parent::__construct();
15        $this->requireExtension( 'GrowthExperiments' );
16        $this->addDescription( 'Add an image suggestion to the task list. '
17            . 'For development setups only. Updates the search index and clears caches; '
18            . 'the user must ensure that the API returns a recommendation for the given page '
19            . '(e.g. by using SubpageImageRecommendationProvider).' );
20        $this->addOption( 'title', 'Page title', true, true );
21        $this->addOption( 'task-type', 'Task type ID (defaults to image-recommendation)', false, true );
22        $this->addOption( 'userid', 'User ID (for resetting the user\'s task  cache), defaults to 1', false, true );
23    }
24
25    public function execute() {
26        $services = $this->getServiceContainer();
27        $growthExperimentsServices = GrowthExperimentsServices::wrap( $services );
28        $cirrusSearchServices = CirrusSearchServices::wrap( $services );
29
30        if ( !$growthExperimentsServices->getGrowthConfig()->get( 'GEDeveloperSetup' ) ) {
31            $this->fatalError( 'This script is only for development setups.' );
32        }
33
34        $title = $services->getTitleFactory()->newFromText( $this->getOption( 'title' ) );
35        if ( !$title || !$title->exists() ) {
36            $this->fatalError( 'Invalid title' );
37        }
38        $taskTypeId = $this->getOption( 'task-type', 'image-recommendation' );
39        $taskTypes = $growthExperimentsServices->getNewcomerTasksConfigurationLoader()->getTaskTypes();
40        if ( !array_key_exists( $taskTypeId, $taskTypes )
41            || !( $taskTypes[$taskTypeId] instanceof ImageRecommendationBaseTaskType )
42        ) {
43            $this->fatalError( 'Invalid task type ID' );
44        }
45        $userId = (int)$this->getOption( 'userid', 1 );
46
47        $taskTypeHandler = $growthExperimentsServices->getTaskTypeHandlerRegistry()->get( $taskTypeId );
48        // update weighted tag in CirrusSearch index
49        $cirrusSearchServices->getCirrusSearch()->updateWeightedTags(
50            $title->toPageIdentity(),
51            // @phan-suppress-next-line PhanUndeclaredConstantOfClass
52            $taskTypeHandler::WEIGHTED_TAG_PREFIX
53        );
54        // clear cache used by ImageRecommendationFilter
55        $cache = $services->getMainWANObjectCache();
56        $cache->delete( ImageRecommendationFilter::makeKey(
57            $cache,
58            // @phan-suppress-next-line PhanUndeclaredConstantOfClass
59            $taskTypeHandler::TASK_TYPE_ID,
60            $title->getDBkey()
61        ) );
62        // clear user's CacheDecorator cache
63        $cache->delete( $cache->makeKey( 'GrowthExperiments-NewcomerTasks-TaskSet', $userId ) );
64    }
65
66}
67
68return UnInvalidateImageSuggestion::class;