Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelatedStoriesRestRoutes
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 needsWriteAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParamSettings
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Wikistories;
4
5use MediaWiki\Page\PageLookup;
6use MediaWiki\Rest\LocalizedHttpException;
7use MediaWiki\Rest\Response;
8use MediaWiki\Rest\SimpleHandler;
9use MediaWiki\Title\MalformedTitleException;
10use MediaWiki\Title\TitleFormatter;
11use MediaWiki\Title\TitleParser;
12use Wikimedia\Message\MessageValue;
13use Wikimedia\Message\ParamType;
14use Wikimedia\Message\ScalarParam;
15use Wikimedia\ParamValidator\ParamValidator;
16
17/**
18 * Class RelatedStoriesRestRoutes
19 *
20 * Handles the route to get the stories related to an article.
21 *
22 * const rest = new mw.Rest();
23 * rest.get( '/wikistories/v0/page/<article title>/stories' ).done( function ( stories ) {
24 *     // ...
25 * } );
26 *
27 * @package MediaWiki\Extension\Wikistories
28 */
29class RelatedStoriesRestRoutes extends SimpleHandler {
30
31    /** @var TitleFormatter */
32    private $titleFormatter;
33
34    /** @var TitleParser */
35    private $titleParser;
36
37    /** @var PageLookup */
38    private $pageLookup;
39
40    /** @var StoriesCache */
41    private $storiesCache;
42
43    /**
44     * @param TitleFormatter $titleFormatter
45     * @param TitleParser $titleParser
46     * @param PageLookup $pageLookup
47     * @param StoriesCache $storiesCache
48     */
49    public function __construct(
50        TitleFormatter $titleFormatter,
51        TitleParser $titleParser,
52        PageLookup $pageLookup,
53        StoriesCache $storiesCache
54    ) {
55        $this->titleFormatter = $titleFormatter;
56        $this->titleParser = $titleParser;
57        $this->pageLookup = $pageLookup;
58        $this->storiesCache = $storiesCache;
59    }
60
61    /**
62     * @param string $title
63     * @return Response
64     * @throws LocalizedHttpException
65     * @throws MalformedTitleException
66     */
67    public function run( $title ) {
68        $titleValue = $this->titleParser->parseTitle( $title );
69
70        if ( $titleValue->getNamespace() !== NS_MAIN ) {
71            $ns = $this->titleFormatter->getNamespaceName( $titleValue->getNamespace(), $titleValue->getText() );
72            throw new LocalizedHttpException(
73                new MessageValue( 'wikistories-rest-unsupported-namespace',
74                    [ new ScalarParam( ParamType::PLAINTEXT, $ns ) ]
75                ),
76                422
77            );
78        }
79
80        $page = $this->pageLookup->getExistingPageByText( $titleValue->getText() );
81        if ( !$page ) {
82            throw new LocalizedHttpException(
83                new MessageValue( 'rest-nonexistent-title',
84                    [ new ScalarParam( ParamType::PLAINTEXT, $title ) ]
85                ),
86                404
87            );
88        }
89
90        $stories = $this->storiesCache->getRelatedStories( $page->getDBkey(), $page->getId() );
91        return $this->getResponseFactory()->createJson( $stories );
92    }
93
94    /**
95     * @inheritDoc
96     */
97    public function needsWriteAccess() {
98        return false;
99    }
100
101    /**
102     * @inheritDoc
103     */
104    public function getParamSettings() {
105        return [
106            'title' => [
107                self::PARAM_SOURCE => 'path',
108                ParamValidator::PARAM_TYPE => 'string',
109                ParamValidator::PARAM_REQUIRED => true,
110            ],
111        ];
112    }
113
114}