Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SubpageImageRecommendationProvider
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 createRecommendation
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 onMediaWikiServices
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\AddImage;
4
5use GrowthExperiments\GrowthExperimentsServices;
6use GrowthExperiments\NewcomerTasks\RecommendationProvider;
7use GrowthExperiments\NewcomerTasks\SubpageRecommendationProvider;
8use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationBaseTaskType;
9use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationTaskType;
10use GrowthExperiments\NewcomerTasks\TaskType\SectionImageRecommendationTaskType;
11use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
12use MediaWiki\Context\DerivativeContext;
13use MediaWiki\Context\RequestContext;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Page\WikiPageFactory;
16use MediaWiki\Title\Title;
17use StatusValue;
18use Wikimedia\Assert\Assert;
19
20/**
21 * Enable by adding the following to LocalSettings.php or a similar location:
22 *     $class = \GrowthExperiments\NewcomerTasks\AddImage\SubpageLinkRecommendationProvider::class;
23 *     $wgHooks['MediaWikiServices'][] = "$class::onMediaWikiServices";
24 *     $wgHooks['ContentHandlerDefaultModelFor'][] = "$class::onContentHandlerDefaultModelFor";
25 *
26 * @inheritDoc
27 */
28class SubpageImageRecommendationProvider
29    extends SubpageRecommendationProvider
30    implements ImageRecommendationProvider
31{
32
33    /** @var ImageRecommendationMetadataProvider */
34    private $metadataProvider;
35
36    /** @var ImageRecommendationApiHandler */
37    private $apiHandler;
38
39    /**
40     * @param WikiPageFactory $wikiPageFactory
41     * @param RecommendationProvider $fallbackRecommendationProvider
42     * @param ImageRecommendationMetadataProvider $metadataProvider
43     * @param ImageRecommendationApiHandler $imageRecommendationApiHandler
44     */
45    public function __construct(
46        WikiPageFactory $wikiPageFactory,
47        RecommendationProvider $fallbackRecommendationProvider,
48        ImageRecommendationMetadataProvider $metadataProvider,
49        ImageRecommendationApiHandler $imageRecommendationApiHandler
50    ) {
51        parent::__construct( $wikiPageFactory, $fallbackRecommendationProvider );
52        $this->metadataProvider = $metadataProvider;
53        $this->apiHandler = $imageRecommendationApiHandler;
54    }
55
56    /** @inheritDoc */
57    protected static $subpageName = 'addimage';
58
59    /** @inheritDoc */
60    protected static $serviceName = 'GrowthExperimentsImageRecommendationProvider';
61
62    /** @inheritDoc */
63    protected static $recommendationTaskTypeClass = [
64        ImageRecommendationTaskType::class,
65        SectionImageRecommendationTaskType::class,
66    ];
67
68    /**
69     * @inheritDoc
70     * @return ImageRecommendation|StatusValue
71     */
72    public function createRecommendation(
73        Title $title,
74        TaskType $taskType,
75        array $data,
76        array $suggestionFilters = []
77    ) {
78        Assert::parameterType( ImageRecommendationBaseTaskType::class, $taskType, '$taskType' );
79        '@phan-var ImageRecommendationBaseTaskType $taskType';/** @var ImageRecommendationBaseTaskType $taskType */
80
81        if ( isset( $data['pages'] ) || isset( $data['rows'] ) || isset( $data['query']['pages'] ) ) {
82            // This is the format used by the Image Suggestions API. It is not really useful
83            // as a serialization format but easy to obtain for actual wiki pages so allow it
84            // as a convenience.
85            return ServiceImageRecommendationProvider::processApiResponseData(
86                $taskType,
87                $title,
88                $title->getPrefixedText(),
89                $this->apiHandler->getSuggestionDataFromApiResponse( $data, $taskType ),
90                $this->metadataProvider,
91                null
92            );
93        } else {
94            return ImageRecommendation::fromArray( $data );
95        }
96    }
97
98    /** @inheritDoc */
99    public static function onMediaWikiServices( MediaWikiServices $services ) {
100        $growthServices = GrowthExperimentsServices::wrap( $services );
101        $services->addServiceManipulator( static::$serviceName,
102            static function (
103                RecommendationProvider $recommendationProvider,
104                MediaWikiServices $services
105            ) use ( $growthServices ) {
106                return new static(
107                    $services->getWikiPageFactory(),
108                    $recommendationProvider,
109                    new StaticImageRecommendationMetadataProvider(
110                        $growthServices->getImageRecommendationMetadataService(),
111                        $services->getContentLanguageCode()->toString(),
112                        $services->getContentLanguage()->getFallbackLanguages(),
113                        $services->getLanguageNameUtils(),
114                        new DerivativeContext( RequestContext::getMain() ),
115                        $services->getSiteStore()
116                    ),
117                    $growthServices->getImageRecommendationApiHandler()
118                );
119            }
120        );
121    }
122
123}