Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 shouldAddLink
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 onSidebarBeforeOutput
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\CiteThisPage;
4
5use MediaWiki\Config\Config;
6use MediaWiki\SpecialPage\SpecialPage;
7use MediaWiki\Title\Title;
8
9class Hooks implements \MediaWiki\Hook\SidebarBeforeOutputHook {
10    /**
11     * Checks, if the "cite this page" link should be added. By default the link is added to all
12     * pages in the main namespace, and additionally to pages, which are in one of the namespaces
13     * named in $wgCiteThisPageAdditionalNamespaces.
14     *
15     * @param Title|null $title
16     * @param Config|null $config
17     * @return bool
18     */
19    private static function shouldAddLink( ?Title $title, ?Config $config ) {
20        if ( !$title || !$config ) {
21            return false;
22        }
23
24        $additionalNamespaces = $config->get( 'CiteThisPageAdditionalNamespaces' );
25
26        return $title->isContentPage() ||
27            (
28                isset( $additionalNamespaces[$title->getNamespace()] ) &&
29                $additionalNamespaces[$title->getNamespace()]
30            );
31    }
32
33    /** @inheritDoc */
34    public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
35        $out = $skin->getOutput();
36        $title = $out->getTitle();
37
38        if ( !self::shouldAddLink( $title, $out->getConfig() ) ) {
39            return;
40        }
41
42        $revid = $out->getRevisionId();
43
44        if ( $revid === 0 || $revid === null ) {
45            return;
46        }
47
48        $specialPage = SpecialPage::getTitleFor( 'CiteThisPage' );
49        $citeURL = $specialPage->getLocalURL( [
50                'page' => $title->getPrefixedDBkey(),
51                'id' => $revid,
52                'wpFormIdentifier' => 'titleform'
53            ]
54        );
55
56        $citeThisPageLink = [
57            'id' => 't-cite',
58            'href' => $citeURL,
59            'icon' => 'quotes',
60            'text' => $skin->msg( 'citethispage-link' )->text(),
61            // Message keys: 'tooltip-citethispage', 'accesskey-citethispage'
62            'single-id' => 'citethispage',
63        ];
64
65        // Append link
66        $sidebar['TOOLBOX']['citethispage'] = $citeThisPageLink;
67    }
68}