1 | <?php |
||
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 ) { |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 7 | protected function buildTabGroupClosingTags() { |
|
235 | |||
236 | /** |
||
237 | * Set the page tool menu to have submenus or not |
||
238 | * |
||
239 | * @param boolean $flat |
||
240 | */ |
||
241 | public function setFlat( $flat ) { |
||
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 ) ) { |
||
265 | |||
266 | |||
267 | } |
||
268 |