Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoryViewAction
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 4
30
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
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onView
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
6
 addLinkPreviewTags
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Wikistories;
4
5use Article;
6use FormlessAction;
7use IContextSource;
8use MediaWiki\Html\Html;
9use MediaWiki\Utils\UrlUtils;
10
11class StoryViewAction extends FormlessAction {
12
13    /** @var StoriesCache */
14    private $storiesCache;
15
16    /** @var UrlUtils */
17    private $urlUtils;
18
19    /**
20     * @param Article $article
21     * @param IContextSource $context
22     * @param StoriesCache $storiesCache
23     * @param UrlUtils $urlUtils
24     */
25    public function __construct(
26        Article $article,
27        IContextSource $context,
28        StoriesCache $storiesCache,
29        UrlUtils $urlUtils
30    ) {
31        parent::__construct( $article, $context );
32        $this->storiesCache = $storiesCache;
33        $this->urlUtils = $urlUtils;
34    }
35
36    /**
37     * @return string
38     */
39    public function getName() {
40        return 'storyview';
41    }
42
43    /**
44     * @inheritDoc
45     */
46    public function onView() {
47        $out = $this->getOutput();
48        $out->setArticleBodyOnly( true );
49        $out->setPageTitle( $this->getTitle()->getText() );
50        $out->addMeta( 'viewport', 'width=device-width, initial-scale=1' );
51        $bodyHtml = '';
52
53        $storyExists = $this->getWikiPage()->getId() > 0;
54        if ( $storyExists ) {
55            $storyData = $this->storiesCache->getStory( $this->getArticle()->getPage()->getId() );
56            $this->addLinkPreviewTags( $storyData );
57            $out->addJsConfigVars( [ 'wgStory' => $storyData ] );
58            $out->addModules( [ 'ext.wikistories.viewaction' ] );
59        } else {
60            $out->addModuleStyles( [ 'ext.wikistories.viewaction.styles' ] );
61            $bodyHtml = Html::rawElement(
62                'div',
63                [ 'class' => 'ext-wikistories-storyviewaction-notfound' ],
64                Html::element(
65                    'div',
66                    [ 'class' => 'ext-wikistories-storyviewaction-notfound-icon' ]
67                ) .
68                Html::element(
69                    'div',
70                    [ 'class' => 'ext-wikistories-storyviewaction-notfound-message' ],
71                    $this->msg( 'ext-wikistories-storyviewaction-notfound-message' )->text()
72                )
73            );
74        }
75
76        return $out->headElement( $out->getSkin() ) .
77            $bodyHtml .
78            $out->getBottomScripts() .
79            '</body></html>';
80    }
81
82    /**
83     * @param array $storyData
84     */
85    private function addLinkPreviewTags( array $storyData ) {
86        $out = $this->getOutput();
87        // Open graph: https://ogp.me/
88        $out->addMeta( 'og:title', $storyData[ 'storyTitle' ] );
89        $out->addMeta( 'og:url', $storyData[ 'shareUrl' ] );
90        $out->addMeta( 'og:description', $storyData[ 'frames' ][ 0 ][ 'text' ] );
91        $out->addMeta( 'og:image', $storyData[ 'frames' ][ 0 ][ 'url' ] );
92
93        // Twitter: https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup
94        $out->addMeta( 'twitter:card', 'summary_large_image' );
95        $out->addMeta( 'twitter:domain', $this->urlUtils->getServer( PROTO_HTTPS ) ?? '' );
96        $out->addMeta( 'twitter:url', $storyData[ 'shareUrl' ] );
97        $out->addMeta( 'twitter:title', $storyData[ 'storyTitle' ] );
98        $out->addMeta( 'twitter:description', $storyData[ 'frames' ][ 0 ][ 'text' ] );
99        $out->addMeta( 'twitter:image', $storyData[ 'frames' ][ 0 ][ 'url' ] );
100    }
101
102}