Debian Codegraph @

includes/skins/components/SkinComponentTableOfContents.php

raw symbols SCIP
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
19 namespace MediaWiki\Skin;
20
21 use MediaWiki\Output\OutputPage;
22 use MediaWiki\Parser\ParserOutputFlags;
23
24 /**
25 * @internal for use inside Skin and SkinTemplate classes only
26 * @unstable
27 */
28 class SkinComponentTableOfContents implements SkinComponent {
29 /** @var OutputPage */
30 private $output;
31
32 /**
33 * @param OutputPage $output
34 */
35 public function __construct( OutputPage $output ) {
36 $this->output = $output;
37 }
38
39 /**
40 * Nests child sections within their parent sections.
41 *
42 * @param array $sections
43 * @param int $toclevel
44 * @return array
45 */
46 private function getSectionsDataInternal( array $sections, int $toclevel = 1 ): array {
47 $data = [];
48 foreach ( $sections as $i => $section ) {
49 // Child section belongs to a higher parent.
50 if ( $section->tocLevel < $toclevel ) {
51 return $data;
52 }
53
54 // Set all the parent sections at the current top level.
55 if ( $section->tocLevel === $toclevel ) {
56 $childSections = $this->getSectionsDataInternal(
57 array_slice( $sections, $i + 1 ),
58 $toclevel + 1
59 );
60 $data[] = $section->toLegacy() + [
61 'array-sections' => $childSections,
62 'is-top-level-section' => $toclevel === 1,
63 'is-parent-section' => $childSections !== []
64 ];
65 }
66 }
67 return $data;
68 }
69
70 /**
71 * Get table of contents template data
72 *
73 * Enriches section data by nesting child elements within parent elements
74 * such that the table of contents can be rendered in Mustache.
75 *
76 * For an example of how to render the data, see TableOfContents.mustache in
77 * the Vector skin.
78 *
79 * @return array
80 */
81 private function getTOCDataInternal(): array {
82 $tocData = $this->output->getTOCData();
83 // Return data only if TOC present T298796.
84 if ( $tocData === null ) {
85 return [];
86 }
87 // Respect __NOTOC__
88 if ( $this->output->getOutputFlag( ParserOutputFlags::NO_TOC ) ) {
89 return [];
90 }
91
92 $outputSections = $tocData->getSections();
93
94 return count( $outputSections ) > 0 ? [
95 'number-section-count' => count( $outputSections ),
96 'array-sections' => $this->getSectionsDataInternal( $outputSections, 1 ),
97 ] : [];
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function getTemplateData(): array {
104 return $this->getTOCDataInternal();
105 }
106 }