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';
80        /** @var ImageRecommendationBaseTaskType $taskType */
81
82        if ( isset( $data['pages'] ) || isset( $data['rows'] ) || isset( $data['query']['pages'] ) ) {
83            // This is the format used by the Image Suggestions API. It is not really useful
84            // as a serialization format but easy to obtain for actual wiki pages so allow it
85            // as a convenience.
86            return ServiceImageRecommendationProvider::processApiResponseData(
87                $taskType,
88                $title,
89                $title->getPrefixedText(),
90                $this->apiHandler->getSuggestionDataFromApiResponse( $data, $taskType ),
91                $this->metadataProvider,
92                null
93            );
94        } else {
95            return ImageRecommendation::fromArray( $data );
96        }
97    }
98
99    /** @inheritDoc */
100    public static function onMediaWikiServices( MediaWikiServices $services ) {
101        $growthServices = GrowthExperimentsServices::wrap( $services );
102        $services->addServiceManipulator( static::$serviceName,
103            static function (
104                RecommendationProvider $recommendationProvider,
105                MediaWikiServices $services
106            ) use ( $growthServices ) {
107                return new static(
108                    $services->getWikiPageFactory(),
109                    $recommendationProvider,
110                    new StaticImageRecommendationMetadataProvider(
111                        $growthServices->getImageRecommendationMetadataService(),
112                        $services->getContentLanguageCode()->toString(),
113                        $services->getContentLanguage()->getFallbackLanguages(),
114                        $services->getLanguageNameUtils(),
115                        new DerivativeContext( RequestContext::getMain() ),
116                        $services->getSiteStore()
117                    ),
118                    $growthServices->getImageRecommendationApiHandler()
119                );
120            }
121        );
122    }
123
124}