1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File holding the PageTools class |
4
|
|
|
* |
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 - 2014, Stephan Gambke |
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
9
|
|
|
* |
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
13
|
|
|
* later version. |
14
|
|
|
* |
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18
|
|
|
* details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU General Public License along |
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
* @file |
24
|
|
|
* @ingroup Skins |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Components; |
28
|
|
|
|
29
|
|
|
use Action; |
30
|
|
|
use MWNamespace; |
31
|
|
|
use Skins\Chameleon\ChameleonTemplate; |
32
|
|
|
use Skins\Chameleon\IdRegistry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The PageTools class. |
36
|
|
|
* |
37
|
|
|
* A unordered list containing content navigation links (Page, Discussion, |
38
|
|
|
* Edit, History, Move, ...) |
39
|
|
|
* |
40
|
|
|
* The tab list is a list of lists: '<ul id="p-contentnavigation"> |
41
|
|
|
* |
42
|
|
|
* @author Stephan Gambke |
43
|
|
|
* @since 1.0 |
44
|
|
|
* @ingroup Skins |
45
|
|
|
*/ |
46
|
|
|
class PageTools extends Component { |
47
|
|
|
|
48
|
|
|
private $mFlat = false; |
49
|
|
|
private $mPageToolsStructure = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ChameleonTemplate $template |
53
|
|
|
* @param \DOMElement|null $domElement |
54
|
|
|
* @param int $indent |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct( ChameleonTemplate $template, \DOMElement $domElement = null, $indent = 0 ) { |
57
|
|
|
|
58
|
1 |
|
parent::__construct( $template, $domElement, $indent ); |
59
|
|
|
|
60
|
|
|
// add classes for the normal case where the page tools are displayed as a first class element; |
61
|
|
|
// these classes should be removed if the page tools are part of another element, e.g. nav bar |
62
|
1 |
|
$this->addClasses( 'list-inline text-center' ); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Builds the HTML code for this component |
67
|
|
|
* |
68
|
|
|
* @return string the HTML code |
69
|
|
|
*/ |
70
|
7 |
|
public function getHtml() { |
71
|
|
|
|
72
|
7 |
|
$contentNavigation = $this->getPageToolsStructure(); |
73
|
|
|
|
74
|
7 |
|
if ( $this->hideSelectedNamespace() ) { |
75
|
4 |
|
unset( $contentNavigation[ 'namespaces' ][ $this->getNamespaceKey() ] ); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
7 |
|
$ret = ''; |
79
|
|
|
|
80
|
7 |
|
$this->indent( 2 ); |
81
|
7 |
|
foreach ( $contentNavigation as $category => $tabsDescription ) { |
82
|
7 |
|
$ret .= $this->buildTabGroup( $category, $tabsDescription ); |
83
|
7 |
|
} |
84
|
7 |
|
$this->indent( -2 ); |
85
|
|
|
|
86
|
7 |
|
if ( $ret !== '' ) { |
87
|
|
|
$ret = |
88
|
7 |
|
$this->indent( 1 ) . '<!-- Content navigation -->' . |
89
|
7 |
|
$this->indent() . \Html::openElement( 'ul', |
90
|
|
|
array( |
91
|
7 |
|
'class' => 'p-contentnavigation ' . $this->getClassString(), |
92
|
7 |
|
'id' => IdRegistry::getRegistry()->getId( 'p-contentnavigation' ), |
93
|
7 |
|
) ) . |
94
|
7 |
|
$ret . |
95
|
7 |
|
$this->indent() . '</ul>'; |
96
|
7 |
|
} |
97
|
|
|
|
98
|
7 |
|
return $ret; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function &getPageToolsStructure() { |
105
|
|
|
if ( $this->mPageToolsStructure === null ) { |
106
|
|
|
$this->mPageToolsStructure = $this->getSkinTemplate()->data[ 'content_navigation' ]; |
107
|
|
|
} |
108
|
|
|
return $this->mPageToolsStructure; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
7 |
|
protected function hideSelectedNamespace() { |
115
|
|
|
return |
116
|
7 |
|
$this->getDomElement() !== null && |
117
|
7 |
|
filter_var( $this->getDomElement()->getAttribute( 'hideSelectedNameSpace' ), FILTER_VALIDATE_BOOLEAN ) && |
118
|
7 |
|
Action::getActionName( $this->getSkin() ) === 'view'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generate strings used for xml 'id' names in tabs |
123
|
|
|
* |
124
|
|
|
* Stolen from MW's Title::getNamespaceKey() |
125
|
|
|
* |
126
|
|
|
* Difference: This function here reports the actual namespace while the |
127
|
|
|
* one in Title reports the subject namespace, i.e. no talk namespaces |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getNamespaceKey() { |
132
|
|
|
global $wgContLang; |
133
|
|
|
|
134
|
|
|
// Gets the subject namespace if this title |
135
|
|
|
$namespace = $this->getSkinTemplate()->getSkin()->getTitle()->getNamespace(); |
136
|
|
|
|
137
|
|
|
// Checks if canonical namespace name exists for namespace |
138
|
|
|
if ( MWNamespace::exists( $this->getSkinTemplate()->getSkin()->getTitle()->getNamespace() ) ) { |
139
|
|
|
// Uses canonical namespace name |
140
|
|
|
$namespaceKey = MWNamespace::getCanonicalName( $namespace ); |
141
|
|
|
} else { |
142
|
|
|
// Uses text of namespace |
143
|
|
|
$namespaceKey = $this->getSkinTemplate()->getSkin()->getTitle()->getNsText(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Makes namespace key lowercase |
147
|
|
|
$namespaceKey = $wgContLang->lc( $namespaceKey ); |
148
|
|
|
// Uses main |
149
|
|
|
if ( $namespaceKey == '' ) { |
150
|
|
|
$namespaceKey = 'main'; |
151
|
|
|
} |
152
|
|
|
// Changes file to image for backwards compatibility |
153
|
|
|
if ( $namespaceKey == 'file' ) { |
154
|
|
|
$namespaceKey = 'image'; |
155
|
|
|
} |
156
|
|
|
return $namespaceKey; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $category |
161
|
|
|
* @param mixed[][] $tabsDescription |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
7 |
|
protected function buildTabGroup( $category, $tabsDescription ) { |
166
|
|
|
// TODO: visually group all links of one category (e.g. some space between categories) |
167
|
|
|
|
168
|
7 |
|
if ( empty( $tabsDescription ) ) { |
169
|
|
|
return ''; |
170
|
|
|
} |
171
|
|
|
|
172
|
7 |
|
$ret = $this->indent() . '<!-- ' . $category . ' -->'; |
173
|
|
|
|
174
|
7 |
|
if ( !$this->mFlat ) { |
175
|
7 |
|
$ret .= $this->buildTabGroupOpeningTags( $category ); |
176
|
|
|
|
177
|
7 |
|
} |
178
|
|
|
|
179
|
7 |
|
foreach ( $tabsDescription as $key => $tabDescription ) { |
180
|
7 |
|
$ret .= $this->buildTab( $tabDescription, $key ); |
181
|
7 |
|
} |
182
|
|
|
|
183
|
7 |
|
if ( !$this->mFlat ) { |
184
|
7 |
|
$ret .= $this->buildTabGroupClosingTags(); |
185
|
7 |
|
} |
186
|
7 |
|
return $ret; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $category |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
7 |
|
protected function buildTabGroupOpeningTags( $category ) { |
195
|
|
|
// output the name of the current category (e.g. 'namespaces', 'views', ...) |
196
|
7 |
|
$ret = $this->indent() . |
197
|
7 |
|
\Html::openElement( 'li', array( 'id' => IdRegistry::getRegistry()->getId( 'p-' . $category ) ) ) . |
198
|
7 |
|
$this->indent( 1 ) . '<ul class="list-inline" >'; |
199
|
|
|
|
200
|
7 |
|
$this->indent( 1 ); |
201
|
7 |
|
return $ret; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param mixed[] $tabDescription |
206
|
|
|
* @param string $key |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
7 |
|
protected function buildTab( $tabDescription, $key ) { |
211
|
|
|
|
212
|
|
|
// skip redundant links (i.e. the 'view' link) |
213
|
|
|
// TODO: make this dependent on an option |
214
|
7 |
|
if ( array_key_exists( 'redundant', $tabDescription ) && $tabDescription[ 'redundant' ] === true ) { |
215
|
7 |
|
return ''; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// apply a link class if specified, e.g. for the currently active namespace |
219
|
7 |
|
$options = array(); |
220
|
7 |
|
if ( array_key_exists( 'class', $tabDescription ) ) { |
221
|
7 |
|
$options[ 'link-class' ] = $tabDescription[ 'class' ]; |
222
|
7 |
|
} |
223
|
|
|
|
224
|
7 |
|
return $this->indent() . $this->getSkinTemplate()->makeListItem( $key, $tabDescription, $options ); |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
7 |
|
protected function buildTabGroupClosingTags() { |
232
|
7 |
|
return $this->indent( -1 ) . '</ul>' . |
233
|
7 |
|
$this->indent( -1 ) . '</li>'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set the page tool menu to have submenus or not |
238
|
|
|
* |
239
|
|
|
* @param boolean $flat |
240
|
|
|
*/ |
241
|
|
|
public function setFlat( $flat ) { |
242
|
|
|
$this->mFlat = $flat; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set the page tool menu to have submenus or not |
247
|
|
|
* |
248
|
|
|
* @param string|string[] $tools |
249
|
|
|
*/ |
250
|
|
|
public function setRedundant( $tools ) { |
251
|
|
|
if ( is_string( $tools ) ) { |
252
|
|
|
$tools = array( $tools ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$pageToolsStructure = &$this->getPageToolsStructure(); |
256
|
|
|
|
257
|
|
|
foreach ( $tools as $tool ) { |
258
|
|
|
foreach ( $pageToolsStructure as $group => $groupStructure ) { |
259
|
|
|
if ( array_key_exists( $tool, $groupStructure ) ) { |
260
|
|
|
$pageToolsStructure[ $group ][ $tool ][ 'redundant' ] = true; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
} |
268
|
|
|
|