Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.00% covered (danger)
30.00%
6 / 20
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndexTemplateStyles
30.00% covered (danger)
30.00%
6 / 20
40.00% covered (danger)
40.00%
2 / 5
44.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasStylesSupport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTemplateStylesPage
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getAssociatedIndexPage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexTemplateStyles
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace ProofreadPage\Index;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\Registration\ExtensionRegistry;
7use MediaWiki\Title\Title;
8use MediaWiki\Xml\Xml;
9
10/**
11 * @license GPL-2.0-or-later
12 *
13 * Returns the TemplateStyles sheets associated with an Index
14 */
15class IndexTemplateStyles {
16
17    /**
18     * @var Title
19     */
20    private $indexTitle;
21
22    /**
23     * @var bool
24     */
25    private $haveStyleSupport;
26
27    /**
28     * Sets up an index TemplateStyles provider
29     * @param Title $indexTitle the title of the index (or the subpage of an
30     * index)
31     */
32    public function __construct( Title $indexTitle ) {
33        // Use the base page so all subpages of an index share the same stylesheet
34        $this->indexTitle = $indexTitle->getBaseTitle();
35        $this->haveStyleSupport = ExtensionRegistry::getInstance()->isLoaded( 'TemplateStyles' );
36    }
37
38    /**
39     * @return bool whether the given index has styles support
40     */
41    public function hasStylesSupport() {
42        return $this->haveStyleSupport;
43    }
44
45    /**
46     * Return TemplateStyles page for the current index
47     * This may or may not exist.
48     *
49     * @return Title|null the title of the styles page, or null if no styles support
50     */
51    public function getTemplateStylesPage() {
52        $stylesPage = null;
53
54        if ( $this->haveStyleSupport ) {
55            $stylesPage = $this->indexTitle->getSubpage( "styles.css" );
56        }
57
58        return $stylesPage;
59    }
60
61    /**
62     * Returns the canonical index page associated with this page. This is the
63     * root Index page.
64     *
65     * @return Title the main index page
66     */
67    public function getAssociatedIndexPage() {
68        return $this->indexTitle;
69    }
70
71    /**
72     * Return TemplateStyles for the given index
73     * @param string|null $wrapper optional CSS selector to limit the style scope
74     * @return string
75     */
76    public function getIndexTemplateStyles( ?string $wrapper ) {
77        $cssTitle = $this->getTemplateStylesPage();
78        $styles = '';
79
80        if ( $cssTitle && $cssTitle->exists() ) {
81            // if this is a normal redirect, follow it, because TS will not
82            // do that for the final page
83            $cssTitle = MediaWikiServices::getInstance()->getWikiPageFactory()
84                ->newFromTitle( $cssTitle )->getRedirectTarget() ?: $cssTitle;
85
86            $ts_attribs = [
87                "src" => $cssTitle->getFullText()
88            ];
89
90            if ( $wrapper ) {
91                $ts_attribs["wrapper"] = $wrapper;
92            }
93
94            $styles .= Xml::element( "templatestyles", $ts_attribs, "" );
95        }
96        return $styles;
97    }
98}