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