Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SidebarBeforeOutputHandler
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 3
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onSidebarBeforeOutput
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
240
 getLinks
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Wikisource\HookHandler;
4
5use ExtensionRegistry;
6use IContextSource;
7use Language;
8use MediaWiki\Config\Config;
9use MediaWiki\Extension\Gadgets\GadgetRepo;
10use MediaWiki\Extension\Wikisource\WsExport;
11use MediaWiki\Hook\SidebarBeforeOutputHook;
12use Skin;
13
14class SidebarBeforeOutputHandler implements SidebarBeforeOutputHook {
15
16    /** @var WsExport */
17    private $wsExport;
18
19    /**
20     * @param Config $config
21     * @param Language $contentLanguage
22     */
23    public function __construct( Config $config, Language $contentLanguage ) {
24        $this->wsExport = new WsExport(
25            $contentLanguage,
26            $config->get( 'WikisourceWsExportUrl' ),
27            $config->get( 'ServerName' )
28        );
29    }
30
31    /**
32     * Handle the SidebarBeforeOutput hook.
33     * @param Skin $skin
34     * @param array &$sidebar
35     */
36    public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
37        // Do not add the export links if the user has a gadget that does the same.
38        // @TODO Remove this after all these gadgets have been removed.
39        if ( ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ) {
40            // @phan-suppress-next-line PhanUndeclaredClassMethod
41            $gadgetRepo = GadgetRepo::singleton();
42            // Gadget names are case sensitive. See T256392 for a list.
43            $exportGadgets = [ 'ePubDownloadLink', 'WSexport' ];
44            foreach ( $exportGadgets as $exportGadget ) {
45                if ( array_search( $exportGadget, $gadgetRepo->getGadgetIds() ) === false ) {
46                    continue;
47                }
48                if ( $gadgetRepo->getGadget( $exportGadget )->isEnabled( $skin->getUser() ) ) {
49                    return;
50                }
51            }
52        }
53
54        // Do not add export links to non-content namespaces, the main page, pages that don't exist, or during editing.
55        $exportNamespaceIds = $skin->getConfig()->get( 'ContentNamespaces' );
56        if (
57            !in_array( $skin->getTitle()->getNamespace(), $exportNamespaceIds )
58            || $skin->getTitle()->isMainPage()
59            || !$skin->getTitle()->exists()
60            || in_array( $skin->getRequest()->getVal( 'action' ), [ 'edit', 'submit' ] )
61        ) {
62            return;
63        }
64
65        // Add the links to the sidebar.
66        $collectionPortletId = 'coll-print_export';
67        $electronPdfPortletId = 'electronpdfservice-sidebar-portlet-heading';
68        if ( isset( $sidebar[$collectionPortletId] ) || isset( $sidebar[ $electronPdfPortletId ] ) ) {
69            // If the Collection or ElectronPdfService extension is installed, first remove its PDF link,
70            $portletId = isset( $sidebar[$collectionPortletId] ) ? $collectionPortletId : $electronPdfPortletId;
71            foreach ( $sidebar[$portletId] as $linkIndex => $collectionsLink ) {
72                if ( in_array( $collectionsLink['id'], [ 'coll-download-as-rl', 'electron-print_pdf' ] ) ) {
73                    unset( $sidebar[$portletId][ $linkIndex ] );
74                }
75            }
76            // and then add our links to the print/export portlet.
77            $sidebar[$portletId] += $this->getLinks( $skin );
78        } else {
79            // If Collection isn't installed, add a new portlet with our links.
80            $sidebar['wikisource-export-portlet'] = $this->getLinks( $skin );
81            // Move the 'printable' link into this portlet for consistency. The Collection extension also does this.
82            if ( isset( $sidebar['TOOLBOX']['print'] ) ) {
83                $sidebar['wikisource-export-portlet'][] = $sidebar['TOOLBOX']['print'];
84                unset( $sidebar['TOOLBOX']['print'] );
85            }
86        }
87    }
88
89    /**
90     * Get the structure array for the list of sidebar links.
91     *
92     * @param IContextSource $context
93     * @return array[]
94     */
95    private function getLinks( IContextSource $context ): array {
96        $links = [
97            'wikisource-export-epub' => [
98                'msg' => 'wikisource-download-epub',
99                'title' => $context->msg( 'wikisource-download-epub-tooltip' )->text(),
100                'id' => 'wikisource-download-epub',
101                'href' => $this->wsExport->getExportUrl( $context->getTitle(), 'epub' ),
102            ],
103            'wikisource-export-mobi' => [
104                'msg' => 'wikisource-download-mobi',
105                'title' => $context->msg( 'wikisource-download-mobi-tooltip' )->text(),
106                'id' => 'wikisource-download-mobi',
107                'href' => $this->wsExport->getExportUrl( $context->getTitle(), 'mobi' ),
108            ],
109            'wikisource-export-pdf' => [
110                'msg' => 'wikisource-download-pdf',
111                'title' => $context->msg( 'wikisource-download-pdf-tooltip' )->text(),
112                'id' => 'wikisource-download-pdf',
113                'href' => $this->wsExport->getExportUrl( $context->getTitle(), 'pdf' ),
114            ],
115            'wikisource-export-any' => [
116                'msg' => 'wikisource-download-choose',
117                'title' => $context->msg( 'wikisource-download-choose-tooltip' )->text(),
118                'id' => 'wikisource-download-choose',
119                'href' => $this->wsExport->getExportUrl( $context->getTitle() ),
120            ],
121        ];
122        return $links;
123    }
124}