Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SkinComponentTableOfContents
96.30% covered (success)
96.30%
26 / 27
75.00% covered (warning)
75.00%
3 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSectionsDataInternal
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 getTOCDataInternal
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 getTemplateData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19namespace MediaWiki\Skin;
20
21use MediaWiki\Output\OutputPage;
22use MediaWiki\Parser\ParserOutputFlags;
23
24/**
25 * @internal for use inside Skin and SkinTemplate classes only
26 * @unstable
27 */
28class SkinComponentTableOfContents implements SkinComponent {
29    /** @var OutputPage */
30    private $output;
31
32    public function __construct( OutputPage $output ) {
33        $this->output = $output;
34    }
35
36    /**
37     * Nests child sections within their parent sections.
38     *
39     * @param array $sections
40     * @param int $toclevel
41     * @return array
42     */
43    private function getSectionsDataInternal( array $sections, int $toclevel = 1 ): array {
44        $data = [];
45        foreach ( $sections as $i => $section ) {
46            // Child section belongs to a higher parent.
47            if ( $section->tocLevel < $toclevel ) {
48                return $data;
49            }
50
51            // Set all the parent sections at the current top level.
52            if ( $section->tocLevel === $toclevel ) {
53                $childSections = $this->getSectionsDataInternal(
54                    array_slice( $sections, $i + 1 ),
55                    $toclevel + 1
56                );
57                $data[] = $section->toLegacy() + [
58                    'array-sections' => $childSections,
59                    'is-top-level-section' => $toclevel === 1,
60                    'is-parent-section' => $childSections !== []
61                ];
62            }
63        }
64        return $data;
65    }
66
67    /**
68     * Get table of contents template data
69     *
70     * Enriches section data by nesting child elements within parent elements
71     * such that the table of contents can be rendered in Mustache.
72     *
73     * For an example of how to render the data, see TableOfContents.mustache in
74     * the Vector skin.
75     */
76    private function getTOCDataInternal(): array {
77        $tocData = $this->output->getTOCData();
78        // Return data only if TOC present T298796.
79        if ( $tocData === null ) {
80            return [];
81        }
82        // Respect __NOTOC__
83        if ( $this->output->getOutputFlag( ParserOutputFlags::NO_TOC ) ) {
84            return [];
85        }
86
87        $outputSections = $tocData->getSections();
88
89        return count( $outputSections ) > 0 ? [
90            'number-section-count' => count( $outputSections ),
91            'array-sections' => $this->getSectionsDataInternal( $outputSections, 1 ),
92        ] : [];
93    }
94
95    /**
96     * @inheritDoc
97     */
98    public function getTemplateData(): array {
99        return $this->getTOCDataInternal();
100    }
101}