| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Skin; |
| 20 |
|
| 21 |
use MediaWiki\Output\OutputPage; |
| 22 |
use MediaWiki\Parser\ParserOutputFlags; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class SkinComponentTableOfContents implements SkinComponent { |
| 29 |
|
| 30 |
private $output; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct( OutputPage $output ) { |
| 36 |
$this->output = $output; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private function getSectionsDataInternal( array $sections, int $toclevel = 1 ): array { |
| 47 |
$data = []; |
| 48 |
foreach ( $sections as $i => $section ) { |
| 49 |
|
| 50 |
if ( $section->tocLevel < $toclevel ) { |
| 51 |
return $data; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private function getTOCDataInternal(): array { |
| 82 |
$tocData = $this->output->getTOCData(); |
| 83 |
|
| 84 |
if ( $tocData === null ) { |
| 85 |
return []; |
| 86 |
} |
| 87 |
|
| 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 |
|
| 102 |
|
| 103 |
public function getTemplateData(): array { |
| 104 |
return $this->getTOCDataInternal(); |
| 105 |
} |
| 106 |
} |