Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 92 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
CollaborationHubTOC | |
0.00% |
0 / 92 |
|
0.00% |
0 / 5 |
156 | |
0.00% |
0 / 1 |
getToCLinkID | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
resetToCLinks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
renderToC | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 | |||
renderSubpageToC | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | use MediaWiki\MediaWikiServices; |
4 | use MediaWiki\Revision\SlotRecord; |
5 | |
6 | /** |
7 | * Helper class to generate table of contents for CollaborationHubContent. |
8 | * |
9 | * @file |
10 | */ |
11 | |
12 | class CollaborationHubTOC { |
13 | |
14 | /** @var array ids/links for ToC items that have been used already */ |
15 | protected $tocLinks; |
16 | |
17 | /** |
18 | * Get unique id for ToC Link/header |
19 | * |
20 | * @param string $header |
21 | * @return string |
22 | */ |
23 | public function getToCLinkID( $header ) { |
24 | $link = Sanitizer::escapeIdForLink( htmlspecialchars( $header ) ); |
25 | $link2 = $link; |
26 | $linkCounter = 1; |
27 | while ( in_array( $link2, $this->tocLinks ) ) { |
28 | $link2 = $link . '_' . $linkCounter; |
29 | } |
30 | $this->tocLinks[] = $link2; |
31 | return $link2; |
32 | } |
33 | |
34 | public function resetToCLinks() { |
35 | $this->tocLinks = []; |
36 | } |
37 | |
38 | public function __construct() { |
39 | $this->resetToCLinks(); |
40 | } |
41 | |
42 | /** |
43 | * ToC rendering for CollaborationHubContent |
44 | * |
45 | * @param array $content Array block from CollaborationHubContent |
46 | * @return string HTML |
47 | */ |
48 | public function renderToC( $content ) { |
49 | $html = Html::openElement( 'div', [ 'class' => 'mw-ck-toc-container' ] ); |
50 | $html .= Html::rawElement( |
51 | 'div', |
52 | [ 'class' => 'mw-ck-toc-label' ], |
53 | wfMessage( 'collaborationkit-hub-toc-label' ) |
54 | ->inContentLanguage() |
55 | ->text() |
56 | ); |
57 | $html .= Html::openElement( 'ul' ); |
58 | |
59 | foreach ( $content as $item ) { |
60 | if ( $item['title'] == '' ) { |
61 | continue; |
62 | } |
63 | $title = Title::newFromText( $item['title'] ); |
64 | if ( isset( $item['displayTitle'] ) ) { |
65 | $displayTitle = $item['displayTitle']; |
66 | } else { |
67 | $displayTitle = $title->getSubpageText(); |
68 | } |
69 | $linkTarget = Title::newFromText( '#' |
70 | . $this->getToCLinkID( $displayTitle ) |
71 | ); |
72 | $image = $item['image'] ?? null; |
73 | |
74 | $link = CollaborationKitImage::makeImage( |
75 | $image, |
76 | 50, |
77 | [ 'link' => $linkTarget, 'label' => $displayTitle ] |
78 | ); |
79 | |
80 | $html .= Html::rawElement( |
81 | 'li', |
82 | [ 'class' => 'mw-ck-toc-item' ], |
83 | $link |
84 | ); |
85 | } |
86 | |
87 | $html .= Html::closeElement( 'ul' ); |
88 | $html .= Html::closeElement( 'div' ); |
89 | return $html; |
90 | } |
91 | |
92 | /** |
93 | * ToC rendering for other pages such as subpages |
94 | * |
95 | * @param Title $title Hub the ToC is generated from |
96 | * @return string html |
97 | */ |
98 | public function renderSubpageToC( Title $title ) { |
99 | // We assume $title is sane. This is supposed to be called with a $title |
100 | // gotten from CollaborationHubContent::getParentHub, which already |
101 | // checks if it is. |
102 | $rev = MediaWikiServices::getInstance() |
103 | ->getRevisionLookup() |
104 | ->getRevisionByTitle( $title ); |
105 | /** @var CollaborationHubContent $content */ |
106 | $content = $rev->getContent( SlotRecord::MAIN ); |
107 | $colour = $content->getThemeColour(); |
108 | $image = $content->getImage(); |
109 | |
110 | $html = Html::openElement( 'div', [ 'class' => "mw-ck-theme-$colour" ] ); |
111 | $html .= Html::openElement( 'div', [ 'class' => [ 'mw-ck-subpage-toc', 'toc' ] ] ); |
112 | |
113 | // ToC label |
114 | |
115 | $label = Html::rawElement( |
116 | 'div', |
117 | [ 'class' => 'mw-ck-toc-label' ], |
118 | Html::rawElement( |
119 | 'span', |
120 | [], |
121 | wfMessage( 'collaborationkit-subpage-toc-label' ) |
122 | ->inContentLanguage() |
123 | ->escaped() |
124 | ) |
125 | ); |
126 | |
127 | // hubpage |
128 | $name = $content->getDisplayName() == '' ? |
129 | $title->getText() : $content->getDisplayName(); |
130 | $linkRenderer = MediaWiki\MediaWikiServices::getInstance()->getLinkRenderer(); |
131 | $link = $linkRenderer->makeLink( $title, $name ); |
132 | $hubPage = Html::rawElement( |
133 | 'div', |
134 | [ 'class' => 'mw-ck-toc-subpage-hub' ], |
135 | $link |
136 | ); |
137 | |
138 | $html .= Html::rawElement( 'div', [ 'class' => 'mw-ck-toc-header' ], $label . $hubPage ); |
139 | |
140 | // Contents |
141 | $html .= Html::openElement( 'ul', [ 'class' => 'mw-ck-toc-contents' ] ); |
142 | |
143 | foreach ( $content->getContent() as $item ) { |
144 | $itemTitle = Title::newFromText( $item['title'] ); |
145 | |
146 | if ( isset( $item['display_title'] ) ) { |
147 | $itemDisplayTitle = $item['display_title']; |
148 | } else { |
149 | $itemDisplayTitle = $itemTitle->getSubpageText(); |
150 | } |
151 | $itemImage = $item['image'] ?? $itemDisplayTitle; |
152 | |
153 | $itemLink = CollaborationKitImage::makeImage( |
154 | $itemImage, |
155 | 16, |
156 | [ 'link' => $itemTitle, 'label' => $itemDisplayTitle, 'colour' => $colour ] |
157 | ); |
158 | |
159 | $html .= Html::rawElement( |
160 | 'li', |
161 | [ 'class' => 'mw-ck-toc-item' ], |
162 | $itemLink |
163 | ); |
164 | } |
165 | |
166 | $html .= Html::closeElement( 'ul' ); |
167 | $html .= Html::closeElement( 'div' ); |
168 | // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement |
169 | $html .= Html::closeElement( 'div' ); |
170 | return $html; |
171 | } |
172 | } |