Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RelatedStoriesRestRoutes | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| needsWriteAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParamSettings | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Wikistories; |
| 4 | |
| 5 | use MediaWiki\Page\PageLookup; |
| 6 | use MediaWiki\Rest\LocalizedHttpException; |
| 7 | use MediaWiki\Rest\Response; |
| 8 | use MediaWiki\Rest\SimpleHandler; |
| 9 | use MediaWiki\Title\MalformedTitleException; |
| 10 | use MediaWiki\Title\TitleFormatter; |
| 11 | use MediaWiki\Title\TitleParser; |
| 12 | use Wikimedia\Message\MessageValue; |
| 13 | use Wikimedia\Message\ParamType; |
| 14 | use Wikimedia\Message\ScalarParam; |
| 15 | use 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 | */ |
| 29 | class RelatedStoriesRestRoutes extends SimpleHandler { |
| 30 | |
| 31 | public function __construct( |
| 32 | private readonly TitleFormatter $titleFormatter, |
| 33 | private readonly TitleParser $titleParser, |
| 34 | private readonly PageLookup $pageLookup, |
| 35 | private readonly StoriesCache $storiesCache, |
| 36 | ) { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @throws LocalizedHttpException |
| 41 | * @throws MalformedTitleException |
| 42 | */ |
| 43 | public function run( string $title ): Response { |
| 44 | $titleValue = $this->titleParser->parseTitle( $title ); |
| 45 | |
| 46 | if ( $titleValue->getNamespace() !== NS_MAIN ) { |
| 47 | $ns = $this->titleFormatter->getNamespaceName( $titleValue->getNamespace(), $titleValue->getText() ); |
| 48 | throw new LocalizedHttpException( |
| 49 | new MessageValue( 'wikistories-rest-unsupported-namespace', |
| 50 | [ new ScalarParam( ParamType::PLAINTEXT, $ns ) ] |
| 51 | ), |
| 52 | 422 |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | $page = $this->pageLookup->getExistingPageByText( $titleValue->getText() ); |
| 57 | if ( !$page ) { |
| 58 | throw new LocalizedHttpException( |
| 59 | new MessageValue( 'rest-nonexistent-title', |
| 60 | [ new ScalarParam( ParamType::PLAINTEXT, $title ) ] |
| 61 | ), |
| 62 | 404 |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $stories = $this->storiesCache->getRelatedStories( $page->getDBkey(), $page->getId() ); |
| 67 | return $this->getResponseFactory()->createJson( $stories ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @inheritDoc |
| 72 | */ |
| 73 | public function needsWriteAccess(): bool { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @inheritDoc |
| 79 | */ |
| 80 | public function getParamSettings(): array { |
| 81 | return [ |
| 82 | 'title' => [ |
| 83 | self::PARAM_SOURCE => 'path', |
| 84 | ParamValidator::PARAM_TYPE => 'string', |
| 85 | ParamValidator::PARAM_REQUIRED => true, |
| 86 | ], |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | } |