Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArticleViewHeaderHandler
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 onArticleViewHeader
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\Extension\Wikisource\HookHandler;
4
5use Article;
6use ExtensionRegistry;
7use Language;
8use MediaWiki\Config\Config;
9use MediaWiki\Extension\Wikisource\WsExport;
10use MediaWiki\Page\Hook\ArticleViewHeaderHook;
11use MediaWiki\Parser\ParserOutput;
12use OOUI\ButtonWidget;
13
14class ArticleViewHeaderHandler implements ArticleViewHeaderHook {
15
16    /** @var WsExport */
17    private $wsExport;
18
19    /** @var int[] */
20    private $allowNamespaces;
21
22    /**
23     * @param Config $config
24     * @param Language $contentLanguage
25     */
26    public function __construct( Config $config, Language $contentLanguage ) {
27        $this->wsExport = new WsExport(
28            $contentLanguage,
29            $config->get( 'WikisourceWsExportUrl' ),
30            $config->get( 'ServerName' )
31        );
32
33        if ( ExtensionRegistry::getInstance()->isLoaded( 'ProofreadPage' ) ) {
34            $this->allowNamespaces = $config->get( 'ProofreadPageBookNamespaces' );
35        } else {
36            // We should not be here since Wikisource is supposed to be loaded with ProofreadPage,
37            // but just in case we default to only main namespace.
38            $this->allowNamespaces = [ NS_MAIN ];
39        }
40    }
41
42    /**
43     * @param Article $article
44     * @param bool|ParserOutput &$outputDone
45     * @param bool &$pcache
46     * @return bool|void
47     */
48    public function onArticleViewHeader( $article, &$outputDone, &$pcache ) {
49        // Only show on pages that exist (and not the mainpage or a disambiguation page).
50        // and are allowed to host books as (based on a
51        // per-wiki configuration variable, $wgProofreadPageBookNamespaces).
52        if ( !$article->getTitle()->inNamespaces( $this->allowNamespaces )
53            || !$article->getTitle()->exists()
54            || $article->getTitle()->isMainPage()
55            || $article->getParserOutput()->getPageProperty( 'disambiguation' ) !== null
56        ) {
57            return;
58        }
59
60        $out = $article->getContext()->getOutput();
61        $out->enableOOUI();
62        $button = new ButtonWidget( [
63            'href' => $this->wsExport->getExportUrl( $article->getTitle(), 'epub' ),
64            'label' => $article->getContext()->msg( 'wikisource-download-button' )->text(),
65            'title' => $article->getContext()->msg( 'wikisource-download-epub-tooltip' )->text(),
66            'flags' => [ 'primary', 'progressive' ],
67            'classes' => [ 'ext-wikisource-download-button' ],
68            'infusable' => true,
69            'data' => [ 'wsExportUrl' => $this->wsExport->getBaseUrl() ],
70        ] );
71        $out->addModules( [ 'ext.wikisource.download' ] );
72        $out->addModuleStyles( 'ext.wikisource.icons' );
73        // @HACK: Add a tilde to force sorting towards the end of the indicator list,
74        // because there's no way to set the indicators' order.
75        // Its ID ends up as #mw-indicator-.7Eext-wikisource-download
76        $out->setIndicators( [ '~ext-wikisource-download' => $button->toString() ] );
77    }
78}