Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
33.33% |
6 / 18 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
AddLinkSuggestionsHandler | |
33.33% |
6 / 18 |
|
0.00% |
0 / 4 |
21.52 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 | |||
needsWriteAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getParamSettings | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Rest\Handler; |
4 | |
5 | use GrowthExperiments\ErrorException; |
6 | use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendationHelper; |
7 | use GrowthExperiments\Util; |
8 | use MediaWiki\Context\RequestContext; |
9 | use MediaWiki\Linker\LinkTarget; |
10 | use MediaWiki\ParamValidator\TypeDef\TitleDef; |
11 | use MediaWiki\Rest\HttpException; |
12 | use MediaWiki\Rest\Response; |
13 | use MediaWiki\Rest\SimpleHandler; |
14 | use Wikimedia\ParamValidator\ParamValidator; |
15 | |
16 | /** |
17 | * Provide stored recommendations for a given page. |
18 | */ |
19 | class AddLinkSuggestionsHandler extends SimpleHandler { |
20 | |
21 | /** @var LinkRecommendationHelper */ |
22 | private $linkRecommendationHelper; |
23 | |
24 | /** |
25 | * @param LinkRecommendationHelper $linkRecommendationHelper |
26 | */ |
27 | public function __construct( |
28 | LinkRecommendationHelper $linkRecommendationHelper |
29 | ) { |
30 | $this->linkRecommendationHelper = $linkRecommendationHelper; |
31 | } |
32 | |
33 | /** |
34 | * Entry point. |
35 | * @param LinkTarget $title |
36 | * @return Response|mixed A Response or a scalar passed to ResponseFactory::createFromReturnValue |
37 | * @throws HttpException |
38 | */ |
39 | public function run( LinkTarget $title ) { |
40 | if ( !Util::areLinkRecommendationsEnabled( RequestContext::getMain() ) ) { |
41 | throw new HttpException( 'Disabled', 404 ); |
42 | } |
43 | try { |
44 | $recommendation = $this->linkRecommendationHelper->getLinkRecommendation( $title ); |
45 | } catch ( ErrorException $e ) { |
46 | throw new HttpException( $e->getErrorMessageInEnglish() ); |
47 | } |
48 | if ( !$recommendation ) { |
49 | throw new HttpException( 'The recommendation has already been invalidated', 409 ); |
50 | } |
51 | return [ 'recommendation' => $recommendation->toArray() ]; |
52 | } |
53 | |
54 | /** @inheritDoc */ |
55 | public function needsWriteAccess() { |
56 | return false; |
57 | } |
58 | |
59 | /** @inheritDoc */ |
60 | public function getParamSettings() { |
61 | return [ |
62 | 'title' => [ |
63 | self::PARAM_SOURCE => 'path', |
64 | ParamValidator::PARAM_TYPE => 'title', |
65 | ParamValidator::PARAM_REQUIRED => true, |
66 | TitleDef::PARAM_RETURN_OBJECT => true, |
67 | ], |
68 | ]; |
69 | } |
70 | |
71 | } |