-
Notifications
You must be signed in to change notification settings - Fork 27
/
ExampleTemplate.php
646 lines (588 loc) · 17.7 KB
/
ExampleTemplate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
/**
* BaseTemplate class for the Example skin
*
* @ingroup Skins
*/
class ExampleTemplate extends BaseTemplate {
/**
* Outputs the entire contents of the page
*/
public function execute() {
$html = '';
$html .= $this->get( 'headelement' );
$html .= Html::rawElement( 'div', [ 'id' => 'mw-wrapper' ],
Html::rawElement( 'div', [ 'class' => 'mw-body', 'id' => 'content', 'role' => 'main' ],
$this->getSiteNotice() .
$this->getNewTalk() .
$this->getIndicators() .
Html::rawElement( 'h1',
[
'class' => 'firstHeading',
'lang' => $this->get( 'pageLanguage' )
],
$this->get( 'title' )
) .
Html::rawElement( 'div', [ 'id' => 'siteSub' ],
$this->getMsg( 'tagline' )->parse()
) .
Html::rawElement( 'div', [ 'class' => 'mw-body-content' ],
Html::rawElement( 'div', [ 'id' => 'contentSub' ],
$this->getPageSubtitle() .
Html::rawElement(
'p',
[],
$this->get( 'undelete' )
)
) .
$this->get( 'bodycontent' ) .
$this->getClear() .
Html::rawElement( 'div', [ 'class' => 'printfooter' ],
$this->get( 'printfooter' )
) .
$this->getCategoryLinks()
) .
$this->getDataAfterContent() .
$this->get( 'debughtml' )
) .
Html::rawElement( 'div', [ 'id' => 'mw-navigation' ],
Html::rawElement(
'h2',
[],
$this->getMsg( 'navigation-heading' )->parse()
) .
$this->getLogo() .
$this->getSearch() .
// User profile links
Html::rawElement(
'div',
[ 'id' => 'user-tools' ],
$this->getUserLinks()
) .
// Page editing and tools
Html::rawElement(
'div',
[ 'id' => 'page-tools' ],
$this->getPageLinks()
) .
// Site navigation/sidebar
Html::rawElement(
'div',
[ 'id' => 'site-navigation' ],
$this->getSiteNavigation()
)
) .
$this->getFooterBlock()
);
$html .= $this->getTrail();
$html .= Html::closeElement( 'body' );
$html .= Html::closeElement( 'html' );
echo $html;
}
/**
* Generates the logo and (optionally) site title
* @param string $id
* @param bool $imageOnly Whether or not to generate the logo with only the image,
* or with a text link as well
*
* @return string html
*/
protected function getLogo( $id = 'p-logo', $imageOnly = false ) {
$html = Html::openElement(
'div',
[
'id' => $id,
'class' => 'mw-portlet',
'role' => 'banner'
]
);
$html .= Html::element(
'a',
[
'href' => $this->data['nav_urls']['mainpage']['href'],
'class' => 'mw-wiki-logo',
] + Linker::tooltipAndAccesskeyAttribs( 'p-logo' )
);
if ( !$imageOnly ) {
$language = $this->getSkin()->getLanguage();
$siteTitle = $language->convert( $this->getMsg( 'sitetitle' )->escaped() );
$html .= Html::rawElement(
'a',
[
'id' => 'p-banner',
'class' => 'mw-wiki-title',
'href' => $this->data['nav_urls']['mainpage']['href']
] + Linker::tooltipAndAccesskeyAttribs( 'p-logo' ),
$siteTitle
);
}
$html .= Html::closeElement( 'div' );
return $html;
}
/**
* Generates the search form
* @return string html
*/
protected function getSearch() {
$html = Html::openElement(
'form',
[
'action' => $this->get( 'wgScript' ),
'role' => 'search',
'class' => 'mw-portlet',
'id' => 'p-search'
]
);
$html .= Html::hidden( 'title', $this->get( 'searchtitle' ) );
$html .= Html::rawElement(
'h3',
[],
Html::label( $this->getMsg( 'search' )->text(), 'searchInput' )
);
$html .= $this->makeSearchInput( [ 'id' => 'searchInput' ] );
$html .= $this->makeSearchButton( 'go', [ 'id' => 'searchGoButton', 'class' => 'searchButton' ] );
$html .= Html::closeElement( 'form' );
return $html;
}
/**
* Generates the sidebar
* Set the elements to true to allow them to be part of the sidebar
* Or get rid of this entirely, and take the specific bits to use wherever you actually want them
* * Toolbox is the page/site tools that appears under the sidebar in vector
* * Languages is the interlanguage links on the page via en:... es:... etc
* * Default is each user-specified box as defined on MediaWiki:Sidebar; you will still need a foreach loop
* to parse these.
* @return string html
*/
protected function getSiteNavigation() {
$html = '';
$sidebar = $this->getSidebar();
$sidebar['SEARCH'] = false;
$sidebar['TOOLBOX'] = true;
$sidebar['LANGUAGES'] = true;
foreach ( $sidebar as $name => $content ) {
if ( $content === false ) {
continue;
}
// Numeric strings gets an integer when set as key, cast back - T73639
$name = (string)$name;
switch ( $name ) {
case 'SEARCH':
$html .= $this->getSearch();
break;
case 'TOOLBOX':
$html .= $this->getPortlet( 'tb', $this->getToolbox(), 'toolbox' );
break;
case 'LANGUAGES':
$html .= $this->getLanguageLinks();
break;
default:
// @phan-suppress-next-line SecurityCheck-DoubleEscaped
$html .= $this->getPortlet( $name, $content['content'] );
break;
}
}
return $html;
}
/**
* In other languages list
*
* @return string html
*/
protected function getLanguageLinks() {
$html = '';
if ( $this->data['language_urls'] !== false ) {
$html .= $this->getPortlet( 'lang', $this->data['language_urls'], 'otherlanguages' );
}
return $html;
}
/**
* Language variants. Displays list for converting between different scripts in the same language,
* if using a language where this is applicable (such as latin vs cyric display for serbian).
*
* @return string html
*/
protected function getVariants() {
$html = '';
if ( count( $this->data['content_navigation']['variants'] ) > 0 ) {
$html .= $this->getPortlet(
'variants',
$this->data['content_navigation']['variants']
);
}
return $html;
}
/**
* Generates page-related tools/links
* You will probably want to split this up and move all of these to somewhere that makes sense for your skin.
* @return string html
*/
protected function getPageLinks() {
// Namespaces: links for 'content' and 'talk' for namespaces with talkpages. Otherwise is just the content.
// Usually rendered as tabs on the top of the page.
$html = $this->getPortlet(
'namespaces',
$this->data['content_navigation']['namespaces']
);
// Language variant options
$html .= $this->getVariants();
// 'View' actions for the page: view, edit, view history, etc
$html .= $this->getPortlet(
'views',
$this->data['content_navigation']['views']
);
// Other actions for the page: move, delete, protect, everything else
$html .= $this->getPortlet(
'actions',
$this->data['content_navigation']['actions']
);
return $html;
}
/**
* Generates user tools menu
* @return string html
*/
protected function getUserLinks() {
// Basic list output
return $this->getPortlet(
'personal',
$this->getPersonalTools(),
'personaltools'
);
// If your skin uses a dropdown or similar for the user links,
// consider uncommenting and using the following instead for compatibility
// with certain extensions (Echo especially):
/*
$personalTools = $this->getPersonalTools();
$html = '';
// Move Echo badges out of default list - they should be visible outside of dropdown;
// may not even work at all inside one
$extraTools = [];
if ( isset( $personalTools['notifications-alert'] ) ) {
$extraTools['notifications-alert'] = $personalTools['notifications-alert'];
unset( $personalTools['notifications-alert'] );
}
if ( isset( $personalTools['notifications-notice'] ) ) {
$extraTools['notifications-notice'] = $personalTools['notifications-notice'];
unset( $personalTools['notifications-notice'] );
}
// Move ULS trigger if you want to better support the user options trigger
// if ( isset( $personalTools['uls'] ) ) {
// $extraTools['uls'] = $personalTools['uls'];
// unset( $personalTools['uls'] );
// }
$html .= Html::openElement( 'div', [ 'id' => 'mw-user-links' ] );
// Place the extra icons/outside stuff
if ( !empty( $extraTools ) ) {
$iconList = '';
foreach ( $extraTools as $key => $item ) {
$iconList .= $this->makeListItem( $key, $item );
}
$html .= Html::rawElement(
'div',
[ 'id' => 'p-personal-extra', 'class' => 'p-body' ],
Html::rawElement( 'ul', [], $iconList )
);
}
$html .= $this->getPortlet( 'personal', $personalTools, 'personaltools' );
$html .= Html::closeElement( 'div' );
return $html;
*/
}
/**
* Generates siteNotice, if any
* @return string html
*/
protected function getSiteNotice() {
return $this->getIfExists( 'sitenotice', [
'wrapper' => 'div',
'parameters' => [ 'id' => 'siteNotice' ]
] );
}
/**
* Generates new talk message banner, if any
* @return string html
*/
protected function getNewTalk() {
return $this->getIfExists( 'newtalk', [
'wrapper' => 'div',
'parameters' => [ 'class' => 'usermessage' ]
] );
}
/**
* Generates subtitle stuff, if any
* @return string html
*/
protected function getPageSubtitle() {
return $this->getIfExists( 'subtitle', [ 'wrapper' => 'p' ] );
}
/**
* Generates category links, if any
* @return string html
*/
protected function getCategoryLinks() {
return $this->getIfExists( 'catlinks' );
}
/**
* Generates data after content stuff, if any
* @return string html
*/
protected function getDataAfterContent() {
return $this->getIfExists( 'dataAfterContent' );
}
/**
* Simple wrapper for random if-statement-wrapped $this->data things
*
* @param string $object name of thing
* @param array $setOptions
*
* @return string html
*/
protected function getIfExists( $object, $setOptions = [] ) {
$options = $setOptions + [
'wrapper' => 'none',
'parameters' => []
];
'@phan-var array{wrapper:string,parameters:array} $options';
$html = '';
if ( $this->data[$object] ) {
if ( $options['wrapper'] === 'none' ) {
$html .= $this->get( $object );
} else {
$html .= Html::rawElement(
$options['wrapper'],
$options['parameters'],
$this->get( $object )
);
}
}
return $html;
}
/**
* Generates a block of navigation links with a header
*
* @param string $name
* @param array|string $content array of links for use with makeListItem, or a block of text
* @param null|string|array $msg
* @param array $setOptions random crap to rename/do/whatever
*
* @return string html
*/
protected function getPortlet( $name, $content, $msg = null, $setOptions = [] ) {
// random stuff to override with any provided options
$options = $setOptions + [
// extra classes/ids
'id' => 'p-' . $name,
'class' => 'mw-portlet',
'extra-classes' => '',
// what to wrap the body list in, if anything
'body-wrapper' => 'div',
'body-id' => null,
'body-class' => 'mw-portlet-body',
// makeListItem options
'list-item' => [ 'text-wrapper' => [ 'tag' => 'span' ] ],
// option to stick arbitrary stuff at the beginning of the ul
'list-prepend' => '',
// old toolbox hook support (use: [ 'SkinTemplateToolboxEnd' => [ &$skin, true ] ])
'hooks' => ''
];
'@phan-var array{id:string,class:string|array,extra-classes:string|array,body-wrapper:string,body-id:?string,body-class:string,list-item:array,list-prepend:string, hooks:string|array} $options';
// Handle the different $msg possibilities
if ( $msg === null ) {
$msg = $name;
} elseif ( is_array( $msg ) ) {
$msgString = array_shift( $msg );
$msgParams = $msg;
$msg = $msgString;
}
$msgObj = $this->getMsg( $msg );
if ( $msgObj->exists() ) {
if ( isset( $msgParams ) && !empty( $msgParams ) ) {
$msgString = $this->getMsg( $msg, $msgParams )->parse();
} else {
$msgString = $msgObj->parse();
}
} else {
$msgString = htmlspecialchars( $msg );
}
$labelId = Sanitizer::escapeIdForAttribute( "p-$name-label" );
if ( is_array( $content ) ) {
$contentText = Html::openElement( 'ul',
[ 'lang' => $this->get( 'userlang' ), 'dir' => $this->get( 'dir' ) ]
);
$contentText .= $options['list-prepend'];
foreach ( $content as $key => $item ) {
$contentText .= $this->makeListItem( $key, $item, $options['list-item'] );
}
// Compatibility with extensions still using SkinTemplateToolboxEnd or similar
if ( is_array( $options['hooks'] ) ) {
foreach ( $options['hooks'] as $hook ) {
if ( is_string( $hook ) ) {
$hookOptions = [];
} else {
// it should only be an array otherwise
$hookOptions = array_values( $hook )[0];
$hook = array_keys( $hook )[0];
}
$contentText .= $this->deprecatedHookHack( $hook, $hookOptions );
}
}
$contentText .= Html::closeElement( 'ul' );
} else {
$contentText = $content;
}
// Special handling for role=search and other weird things
$divOptions = [
'role' => 'navigation',
'id' => Sanitizer::escapeIdForAttribute( $options['id'] ),
'title' => Linker::titleAttrib( $options['id'] ),
'aria-labelledby' => $labelId
];
$class = is_array( $options['class'] )
? $options['class'] : [ $options['class'] ];
$extraClasses = is_array( $options['extra-classes'] )
? $options['extra-classes'] : [ $options['extra-classes'] ];
$divOptions['class'] = array_merge( $class, $extraClasses );
$labelOptions = [
'id' => $labelId,
'lang' => $this->get( 'userlang' ),
'dir' => $this->get( 'dir' )
];
if ( $options['body-wrapper'] !== 'none' ) {
$bodyDivOptions = [ 'class' => $options['body-class'] ];
if ( is_string( $options['body-id'] ) ) {
$bodyDivOptions['id'] = $options['body-id'];
}
$body = Html::rawElement( $options['body-wrapper'], $bodyDivOptions,
$contentText .
$this->getAfterPortlet( $name )
);
} else {
$body = $contentText . $this->getAfterPortlet( $name );
}
$html = Html::rawElement( 'div', $divOptions,
Html::rawElement( 'h3', $labelOptions, $msgString ) .
$body
);
return $html;
}
/**
* Wrapper to catch output of old hooks expecting to write directly to page
* We no longer do things that way.
*
* @param string $hook event
* @param array $hookOptions args
*
* @return string html
*/
protected function deprecatedHookHack( $hook, $hookOptions = [] ) {
$hookContents = '';
ob_start();
Hooks::run( $hook, $hookOptions );
$hookContents = ob_get_contents();
ob_end_clean();
if ( !trim( $hookContents ) ) {
$hookContents = '';
}
return $hookContents;
}
/**
* Better renderer for getFooterIcons and getFooterLinks, based on Vector
*
* @param array $setOptions Miscellaneous other options
* * 'id' for footer id
* * 'class' for footer class
* * 'order' to determine whether icons or links appear first: 'iconsfirst' or links, though in
* practice we currently only check if it is or isn't 'iconsfirst'
* * 'link-prefix' to set the prefix for all link and block ids; most skins use 'f' or 'footer',
* as in id='f-whatever' vs id='footer-whatever'
* * 'icon-style' to pass to getFooterIcons: "icononly", "nocopyright"
* * 'link-style' to pass to getFooterLinks: "flat" to disable categorisation of links in a
* nested array
*
* @return string html
*/
protected function getFooterBlock( $setOptions = [] ) {
// Set options and fill in defaults
$options = $setOptions + [
'id' => 'footer',
'class' => 'mw-footer',
'order' => 'iconsfirst',
'link-prefix' => 'footer',
'icon-style' => 'icononly',
'link-style' => null
];
'@phan-var array{id:string,class:string,order:string,link-prefix:string,icon-style:string,link-style:?string} $options';
$validFooterIcons = $this->getFooterIcons( $options['icon-style'] );
$validFooterLinks = $this->getFooterLinks( $options['link-style'] );
$html = '';
$html .= Html::openElement( 'div', [
'id' => $options['id'],
'class' => $options['class'],
'role' => 'contentinfo',
'lang' => $this->get( 'userlang' ),
'dir' => $this->get( 'dir' )
] );
$iconsHTML = '';
if ( count( $validFooterIcons ) > 0 ) {
$iconsHTML .= Html::openElement( 'ul', [ 'id' => "{$options['link-prefix']}-icons" ] );
foreach ( $validFooterIcons as $blockName => $footerIcons ) {
$iconsHTML .= Html::openElement( 'li', [
'id' => Sanitizer::escapeIdForAttribute(
"{$options['link-prefix']}-{$blockName}ico"
),
'class' => 'footer-icons'
] );
foreach ( $footerIcons as $icon ) {
$iconsHTML .= $this->getSkin()->makeFooterIcon( $icon );
}
$iconsHTML .= Html::closeElement( 'li' );
}
$iconsHTML .= Html::closeElement( 'ul' );
}
$linksHTML = '';
if ( count( $validFooterLinks ) > 0 ) {
if ( $options['link-style'] === 'flat' ) {
$linksHTML .= Html::openElement( 'ul', [
'id' => "{$options['link-prefix']}-list",
'class' => 'footer-places'
] );
foreach ( $validFooterLinks as $link ) {
$linksHTML .= Html::rawElement(
'li',
[ 'id' => Sanitizer::escapeIdForAttribute( $link ) ],
$this->get( $link )
);
}
$linksHTML .= Html::closeElement( 'ul' );
} else {
$linksHTML .= Html::openElement( 'div', [ 'id' => "{$options['link-prefix']}-list" ] );
foreach ( $validFooterLinks as $category => $links ) {
$linksHTML .= Html::openElement( 'ul',
[ 'id' => Sanitizer::escapeIdForAttribute(
"{$options['link-prefix']}-{$category}"
) ]
);
foreach ( $links as $link ) {
$linksHTML .= Html::rawElement(
'li',
[ 'id' => Sanitizer::escapeIdForAttribute(
"{$options['link-prefix']}-{$category}-{$link}"
) ],
$this->get( $link )
);
}
$linksHTML .= Html::closeElement( 'ul' );
}
$linksHTML .= Html::closeElement( 'div' );
}
}
if ( $options['order'] === 'iconsfirst' ) {
$html .= $iconsHTML . $linksHTML;
} else {
$html .= $linksHTML . $iconsHTML;
}
$html .= $this->getClear() . Html::closeElement( 'div' );
return $html;
}
}