-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Skin.php
2514 lines (2291 loc) · 80.9 KB
/
Skin.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\Context\ContextSource;
use MediaWiki\Debug\MWDebug;
use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
use MediaWiki\Html\Html;
use MediaWiki\Language\Language;
use MediaWiki\Language\LanguageCode;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use MediaWiki\Parser\Sanitizer;
use MediaWiki\ResourceLoader as RL;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Skin\SkinComponent;
use MediaWiki\Skin\SkinComponentFooter;
use MediaWiki\Skin\SkinComponentLink;
use MediaWiki\Skin\SkinComponentListItem;
use MediaWiki\Skin\SkinComponentMenu;
use MediaWiki\Skin\SkinComponentRegistry;
use MediaWiki\Skin\SkinComponentRegistryContext;
use MediaWiki\Skin\SkinComponentUtils;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Specials\SpecialUserRights;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleValue;
use MediaWiki\User\User;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\WikiMap\WikiMap;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* @defgroup Skins Skins
*/
/**
* The base class for all skins.
*
* See docs/Skin.md for more information.
*
* @stable to extend
* @ingroup Skins
*/
abstract class Skin extends ContextSource {
use ProtectedHookAccessorTrait;
/**
* @var array link options used in Skin::makeLink. Can be set by skin option `link`.
*/
private $defaultLinkOptions;
/**
* @var string|null
*/
protected $skinname = null;
/**
* @var array Skin options passed into constructor
*/
protected $options = [];
/** @var Title|null */
protected $mRelevantTitle = null;
/**
* @var UserIdentity|null|false
*/
private $mRelevantUser = false;
/** The current major version of the skin specification. */
protected const VERSION_MAJOR = 1;
/** @var array|null Cache for getLanguages() */
private $languageLinks;
/** @var array|null Cache for buildSidebar() */
private $sidebar;
/**
* @var SkinComponentRegistry Initialised in constructor.
*/
private $componentRegistry = null;
/**
* Get the current major version of Skin. This is used to manage changes
* to underlying data and for providing support for older and new versions of code.
*
* @since 1.36
* @return int
*/
public static function getVersion() {
return self::VERSION_MAJOR;
}
/**
* @internal use in Skin.php, SkinTemplate.php or SkinMustache.php
* @param string $name
* @return SkinComponent
*/
final protected function getComponent( string $name ): SkinComponent {
return $this->componentRegistry->getComponent( $name );
}
/**
* @stable to extend. Subclasses may extend this method to add additional
* template data.
* @internal this method should never be called outside Skin and its subclasses
* as it can be computationally expensive and typically has side effects on the Skin
* instance, through execution of hooks.
*
* The data keys should be valid English words. Compound words should
* be hyphenated except if they are normally written as one word. Each
* key should be prefixed with a type hint, this may be enforced by the
* class PHPUnit test.
*
* Plain strings are prefixed with 'html-', plain arrays with 'array-'
* and complex array data with 'data-'. 'is-' and 'has-' prefixes can
* be used for boolean variables.
* Messages are prefixed with 'msg-', followed by their message key.
* All messages specified in the skin option 'messages' will be available
* under 'msg-MESSAGE-NAME'.
*
* @return array Data for a mustache template
*/
public function getTemplateData() {
$title = $this->getTitle();
$out = $this->getOutput();
$user = $this->getUser();
$isMainPage = $title->isMainPage();
$blankedHeading = false;
// Heading can only be blanked on "views". It should
// still show on action=edit, diff pages and action=history
$isHeadingOverridable = $this->getContext()->getActionName() === 'view' &&
!$this->getRequest()->getRawVal( 'diff' );
if ( $isMainPage && $isHeadingOverridable ) {
// Special casing for the main page to allow more freedom to editors, to
// design their home page differently. This came up in T290480.
// The parameter for logged in users is optional and may
// or may not be used.
$titleMsg = $user->isAnon() ?
$this->msg( 'mainpage-title' ) :
$this->msg( 'mainpage-title-loggedin', $user->getName() );
// T298715: Use content language rather than user language so that
// the custom page heading is shown to all users, not just those that have
// their interface set to the site content language.
//
// T331095: Avoid Message::inContentLanuguage and, just like Parser,
// pick the language variant based on the current URL and/or user
// preference if their variant relates to the content language.
$forceUIMsgAsContentMsg = $this->getConfig()
->get( MainConfigNames::ForceUIMsgAsContentMsg );
if ( !in_array( $titleMsg->getKey(), (array)$forceUIMsgAsContentMsg ) ) {
$services = MediaWikiServices::getInstance();
$contLangVariant = $services->getLanguageConverterFactory()
->getLanguageConverter( $services->getContentLanguage() )
->getPreferredVariant();
$titleMsg->inLanguage( $contLangVariant );
}
$titleMsg->setInterfaceMessageFlag( true );
$blankedHeading = $titleMsg->isBlank();
if ( !$titleMsg->isDisabled() ) {
$htmlTitle = $titleMsg->parse();
} else {
$htmlTitle = $out->getPageTitle();
}
} else {
$htmlTitle = $out->getPageTitle();
}
$data = [
// raw HTML
'html-title-heading' => Html::rawElement(
'h1',
[
'id' => 'firstHeading',
'class' => 'firstHeading mw-first-heading',
'style' => $blankedHeading ? 'display: none' : null
] + $this->getUserLanguageAttributes(),
$htmlTitle
),
'html-title' => $htmlTitle ?: null,
// Boolean values
'is-title-blank' => $blankedHeading, // @since 1.38
'is-anon' => $user->isAnon(),
'is-article' => $out->isArticle(),
'is-mainpage' => $isMainPage,
'is-specialpage' => $title->isSpecialPage(),
'canonical-url' => $this->getCanonicalUrl(),
];
$components = $this->componentRegistry->getComponents();
foreach ( $components as $componentName => $component ) {
$data['data-' . $componentName] = $component->getTemplateData();
}
return $data;
}
/**
* Normalize a skin preference value to a form that can be loaded.
*
* If a skin can't be found, it will fall back to the configured default ($wgDefaultSkin), or the
* hardcoded default ($wgFallbackSkin) if the default skin is unavailable too.
*
* @param string $key 'monobook', 'vector', etc.
* @return string
*/
public static function normalizeKey( string $key ) {
$config = MediaWikiServices::getInstance()->getMainConfig();
$defaultSkin = $config->get( MainConfigNames::DefaultSkin );
$fallbackSkin = $config->get( MainConfigNames::FallbackSkin );
$skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
$skinNames = $skinFactory->getInstalledSkins();
// Make keys lowercase for case-insensitive matching.
$skinNames = array_change_key_case( $skinNames, CASE_LOWER );
$key = strtolower( $key );
$defaultSkin = strtolower( $defaultSkin );
$fallbackSkin = strtolower( $fallbackSkin );
if ( $key == '' || $key == 'default' ) {
// Don't return the default immediately;
// in a misconfiguration we need to fall back.
$key = $defaultSkin;
}
if ( isset( $skinNames[$key] ) ) {
return $key;
}
// Older versions of the software used a numeric setting
// in the user preferences.
$fallback = [
0 => $defaultSkin,
2 => 'cologneblue'
];
if ( isset( $fallback[$key] ) ) {
// @phan-suppress-next-line PhanTypeMismatchDimFetch False positive
$key = $fallback[$key];
}
if ( isset( $skinNames[$key] ) ) {
return $key;
} elseif ( isset( $skinNames[$defaultSkin] ) ) {
return $defaultSkin;
} else {
return $fallbackSkin;
}
}
/**
* @since 1.31
* @param string|array|null $options Options for the skin can be an array (since 1.35).
* When a string is passed, it's the skinname.
* When an array is passed:
*
* - `name`: Required. Internal skin name, generally in lowercase to comply with conventions
* for interface message keys and CSS class names which embed this value.
*
* - `format`: Enable rendering of skin as json or html.
*
* Since: MW 1.43
* Default: `html`
*
* - `styles`: ResourceLoader style modules to load on all pages. Default: `[]`
*
* - `scripts`: ResourceLoader script modules to load on all pages. Default: `[]`
*
* - `toc`: Whether a table of contents is included in the main article content
* area. If your skin has place a table of contents elsewhere (for example, the sidebar),
* set this to `false`.
*
* See ParserOutput::getText() for the implementation logic.
*
* Default: `true`
*
* - `bodyClasses`: An array of extra class names to add to the HTML `<body>` element.
* Default: `[]`
*
* - `clientPrefEnabled`: Enable support for mw.user.clientPrefs.
* This instructs OutputPage and ResourceLoader\ClientHtml to include an inline script
* in web responses for unregistered users to switch HTML classes as needed.
*
* Since: MW 1.41
* Default: `false`
*
* - `wrapSiteNotice`: Enable support for standard site notice wrapper.
* This instructs the Skin to wrap banners in div#siteNotice.
*
* Since: MW 1.42
* Default: `false`
*
* - `responsive`: Whether the skin supports responsive behaviour and wants a viewport meta
* tag to be added to the HTML head. Note, users can disable this feature via a user
* preference.
*
* Default: `false`
*
* - `supportsMwHeading`: Whether the skin supports new HTML markup for headings, which uses
* `<div class="mw-heading">` tags (https://www.mediawiki.org/wiki/Heading_HTML_changes).
* If false, MediaWiki will output the legacy markup instead.
*
* Since: MW 1.43
* Default: `false` (will become `true` in and then will be removed in the future)
*
* - `link`: An array of link option overriddes. See Skin::makeLink for the available options.
*
* Default: `[]`
*
* - `tempUserBanner`: Whether to display a banner on page views by in temporary user sessions.
* This will prepend SkinComponentTempUserBanner to the `<body>` above everything else.
* See also MediaWiki\MainConfigSchema::AutoCreateTempUser and User::isTemp.
*
* Default: `false`
*
* - `menus`: Which menus the skin supports, to allow features like SpecialWatchlist
* to render their own navigation in the skins that don't support certain menus.
* For any key in the array, the skin is promising to render an element e.g. the
* presence of `associated-pages` means the skin will render a menu
* compatible with mw.util.addPortletLink which has the ID p-associated-pages.
*
* Default: `['namespaces', 'views', 'actions', 'variants']`
*
* Opt-in menus:
* - `associated-pages`
* - `notifications`
* - `user-interface-preferences`
* - `user-page`
* - `user-menu`
*/
public function __construct( $options = null ) {
if ( is_string( $options ) ) {
$this->skinname = $options;
} elseif ( $options ) {
$name = $options['name'] ?? null;
if ( !$name ) {
throw new SkinException( 'Skin name must be specified' );
}
// Defaults are set in Skin::getOptions()
$this->options = $options;
$this->skinname = $name;
}
$this->defaultLinkOptions = $this->getOptions()['link'];
$this->componentRegistry = new SkinComponentRegistry(
new SkinComponentRegistryContext( $this )
);
}
/**
* @return string|null Skin name
*/
public function getSkinName() {
return $this->skinname;
}
/**
* Indicates if this skin is responsive.
* Responsive skins have skin--responsive added to <body> by OutputPage,
* and a viewport <meta> tag set by Skin::initPage.
*
* @since 1.36
* @stable to override
* @return bool
*/
public function isResponsive() {
$isSkinResponsiveCapable = $this->getOptions()['responsive'];
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
return $isSkinResponsiveCapable &&
$userOptionsLookup->getBoolOption( $this->getUser(), 'skin-responsive' );
}
/**
* @stable to override
* @param OutputPage $out
*/
public function initPage( OutputPage $out ) {
$skinMetaTags = $this->getConfig()->get( MainConfigNames::SkinMetaTags );
$siteName = $this->getConfig()->get( MainConfigNames::Sitename );
$this->preloadExistence();
if ( $this->isResponsive() ) {
$out->addMeta(
'viewport',
'width=device-width, initial-scale=1.0, ' .
'user-scalable=yes, minimum-scale=0.25, maximum-scale=5.0'
);
} else {
// Force the desktop experience on an iPad by resizing the mobile viewport to
// the value of @min-width-breakpoint-desktop (1120px).
// This is as @min-width-breakpoint-desktop-wide usually tends to optimize
// for larger screens with max-widths and margins.
// The initial-scale SHOULD NOT be set here as defining it will impact zoom
// on mobile devices. To allow font-size adjustment in iOS devices (see T311795)
// we will define a zoom in JavaScript on certain devices (see resources/src/mediawiki.page.ready/ready.js)
$out->addMeta(
'viewport',
'width=1120'
);
}
$tags = [
'og:site_name' => $siteName,
'og:title' => $out->getHTMLTitle(),
'twitter:card' => 'summary_large_image',
'og:type' => 'website',
];
// Support sharing on platforms such as Facebook and Twitter
foreach ( $tags as $key => $value ) {
if ( in_array( $key, $skinMetaTags ) ) {
$out->addMeta( $key, $value );
}
}
}
/**
* Defines the ResourceLoader modules that should be added to the skin
* It is recommended that skins wishing to override call parent::getDefaultModules()
* and substitute out any modules they wish to change by using a key to look them up
*
* Any modules defined with the 'styles' key will be added as render blocking CSS via
* Output::addModuleStyles. Similarly, each key should refer to a list of modules
*
* @stable to override
* @return array Array of modules with helper keys for easy overriding
*/
public function getDefaultModules() {
$out = $this->getOutput();
$user = $this->getUser();
// Modules declared in the $modules literal are loaded
// for ALL users, on ALL pages, in ALL skins.
// Keep this list as small as possible!
$modules = [
// The 'styles' key sets render-blocking style modules
// Unlike other keys in $modules, this is an associative array
// where each key is its own group pointing to a list of modules
'styles' => [
'skin' => $this->getOptions()['styles'],
'core' => [],
'content' => [],
'syndicate' => [],
'user' => []
],
'core' => [
'site',
'mediawiki.page.ready',
],
// modules that enhance the content in some way
'content' => [],
// modules relating to search functionality
'search' => [],
// Skins can register their own scripts
'skin' => $this->getOptions()['scripts'],
// modules relating to functionality relating to watching an article
'watch' => [],
// modules which relate to the current users preferences
'user' => [],
// modules relating to RSS/Atom Feeds
'syndicate' => [],
];
// Preload jquery.tablesorter for mediawiki.page.ready
if ( strpos( $out->getHTML(), 'sortable' ) !== false ) {
$modules['content'][] = 'jquery.tablesorter';
$modules['styles']['content'][] = 'jquery.tablesorter.styles';
}
// Preload jquery.makeCollapsible for mediawiki.page.ready
if ( strpos( $out->getHTML(), 'mw-collapsible' ) !== false ) {
$modules['content'][] = 'jquery.makeCollapsible';
$modules['styles']['content'][] = 'jquery.makeCollapsible.styles';
}
// Load relevant styles on wiki pages that use mw-ui-button.
// Since 1.26, this no longer loads unconditionally. Special pages
// and extensions should load this via addModuleStyles() instead.
if ( strpos( $out->getHTML(), 'mw-ui-button' ) !== false ) {
$modules['styles']['content'][] = 'mediawiki.ui.button';
}
// Since 1.41, styling for mw-message-box is only required for
// messages that appear in article content.
// This should only be removed when a suitable alternative exists
// e.g. https://phabricator.wikimedia.org/T363607 is resolved.
if ( strpos( $out->getHTML(), 'mw-message-box' ) !== false ) {
$modules['styles']['content'][] = 'mediawiki.legacy.messageBox';
}
// If the page is using Codex message box markup load Codex styles.
// Since 1.41. Skins can unset this if they prefer to handle this via other
// means.
// This is intended for extensions.
// For content, this should not be considered stable, and will likely
// be removed when https://phabricator.wikimedia.org/T363607 is resolved.
if ( strpos( $out->getHTML(), 'cdx-message' ) !== false ) {
$modules['styles']['content'][] = 'mediawiki.codex.messagebox.styles';
}
if ( $out->isTOCEnabled() ) {
$modules['content'][] = 'mediawiki.toc';
}
$authority = $this->getAuthority();
$relevantTitle = $this->getRelevantTitle();
if ( $authority->getUser()->isRegistered()
&& $authority->isAllowedAll( 'viewmywatchlist', 'editmywatchlist' )
&& $relevantTitle && $relevantTitle->canExist()
) {
$modules['watch'][] = 'mediawiki.page.watch.ajax';
}
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
if ( $userOptionsLookup->getBoolOption( $user, 'editsectiononrightclick' )
|| ( $out->isArticle() && $userOptionsLookup->getOption( $user, 'editondblclick' ) )
) {
$modules['user'][] = 'mediawiki.misc-authed-pref';
}
if ( $out->isSyndicated() ) {
$modules['styles']['syndicate'][] = 'mediawiki.feedlink';
}
if ( $user->isTemp() ) {
$modules['user'][] = 'mediawiki.tempUserBanner';
$modules['styles']['user'][] = 'mediawiki.tempUserBanner.styles';
}
if ( $this->getTitle() && $this->getTitle()->getNamespace() === NS_FILE ) {
$modules['styles']['core'][] = 'filepage'; // local Filepage.css, T31277, T356505
}
return $modules;
}
/**
* Preload the existence of three commonly-requested pages in a single query
*/
private function preloadExistence() {
$titles = [];
// User/talk link
$user = $this->getUser();
if ( $user->isRegistered() ) {
$titles[] = $user->getUserPage();
$titles[] = $user->getTalkPage();
}
// Check, if the page can hold some kind of content, otherwise do nothing
$title = $this->getRelevantTitle();
if ( $title && $title->canExist() && $title->canHaveTalkPage() ) {
$namespaceInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
if ( $title->isTalkPage() ) {
$titles[] = $namespaceInfo->getSubjectPage( $title );
} else {
$titles[] = $namespaceInfo->getTalkPage( $title );
}
}
// Preload for self::getCategoryLinks
$allCats = $this->getOutput()->getCategoryLinks();
if ( isset( $allCats['normal'] ) && $allCats['normal'] !== [] ) {
$catLink = Title::newFromText( $this->msg( 'pagecategorieslink' )->inContentLanguage()->text() );
if ( $catLink ) {
// If this is a special page, the LinkBatch would skip it
$titles[] = $catLink;
}
}
$this->getHookRunner()->onSkinPreloadExistence( $titles, $this );
if ( $titles ) {
$linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
$lb = $linkBatchFactory->newLinkBatch( $titles );
$lb->setCaller( __METHOD__ );
$lb->execute();
}
}
/**
* @see self::getRelevantTitle()
* @param Title $t
*/
public function setRelevantTitle( $t ) {
$this->mRelevantTitle = $t;
}
/**
* Return the "relevant" title.
* A "relevant" title is not necessarily the actual title of the page.
* Special pages like Special:MovePage use set the page they are acting on
* as their "relevant" title, this allows the skin system to display things
* such as content tabs which belong to that page instead of displaying
* a basic special page tab which has almost no meaning.
*
* @return Title|null the title is null when no relevant title was set, as this
* falls back to ContextSource::getTitle
*/
public function getRelevantTitle() {
return $this->mRelevantTitle ?? $this->getTitle();
}
/**
* @see self::getRelevantUser()
* @param UserIdentity|null $u
*/
public function setRelevantUser( ?UserIdentity $u ) {
$this->mRelevantUser = $u;
}
/**
* Return the "relevant" user.
* A "relevant" user is similar to a relevant title. Special pages like
* Special:Contributions mark the user which they are relevant to so that
* things like the toolbox can display the information they usually are only
* able to display on a user's userpage and talkpage.
*
* @return UserIdentity|null Null if there's no relevant user or the viewer cannot view it.
*/
public function getRelevantUser(): ?UserIdentity {
if ( $this->mRelevantUser === false ) {
$this->mRelevantUser = null; // false indicates we never attempted to load it.
$title = $this->getRelevantTitle();
if ( $title->hasSubjectNamespace( NS_USER ) ) {
$services = MediaWikiServices::getInstance();
$rootUser = $title->getRootText();
$userNameUtils = $services->getUserNameUtils();
if ( $userNameUtils->isIP( $rootUser ) ) {
$this->mRelevantUser = UserIdentityValue::newAnonymous( $rootUser );
} else {
$user = $services->getUserIdentityLookup()->getUserIdentityByName( $rootUser );
$this->mRelevantUser = $user && $user->isRegistered() ? $user : null;
}
}
}
// The relevant user should only be set if it exists. However, if it exists but is hidden,
// and the viewer cannot see hidden users, this exposes the fact that the user exists;
// pretend like the user does not exist in such cases, by setting it to null. T120883
if ( $this->mRelevantUser && $this->mRelevantUser->isRegistered() ) {
$userBlock = MediaWikiServices::getInstance()
->getBlockManager()
->getBlock( $this->mRelevantUser, null );
if ( $userBlock && $userBlock->getHideName() &&
!$this->getAuthority()->isAllowed( 'hideuser' )
) {
$this->mRelevantUser = null;
}
}
return $this->mRelevantUser;
}
/**
* Outputs the HTML for the page.
* @internal Only to be called by OutputPage.
*/
final public function outputPageFinal( OutputPage $out ) {
// generate body
ob_start();
$this->outputPage();
$html = ob_get_contents();
ob_end_clean();
// T259955: OutputPage::headElement must be called last
// as it calls OutputPage::getRlClient, which freezes the ResourceLoader
// modules queue for the current page load.
// Since Skins can add ResourceLoader modules via OutputPage::addModule
// and OutputPage::addModuleStyles changing this order can lead to
// bugs.
$head = $out->headElement( $this );
$tail = $out->tailElement( $this );
echo $head . $html . $tail;
}
/**
* Outputs the HTML generated by other functions.
*/
abstract public function outputPage();
/**
* TODO: document
* @param Title $title
* @return string
*/
public function getPageClasses( $title ) {
$services = MediaWikiServices::getInstance();
$ns = $title->getNamespace();
$numeric = 'ns-' . $ns;
if ( $title->isSpecialPage() ) {
$type = 'ns-special';
// T25315: provide a class based on the canonical special page name without subpages
[ $canonicalName ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
if ( $canonicalName ) {
$type .= ' ' . Sanitizer::escapeClass( "mw-special-$canonicalName" );
} else {
$type .= ' mw-invalidspecialpage';
}
} else {
if ( $title->isTalkPage() ) {
$type = 'ns-talk';
} else {
$type = 'ns-subject';
}
// T208315: add HTML class when the user can edit the page
if ( $this->getAuthority()->probablyCan( 'edit', $title ) ) {
$type .= ' mw-editable';
}
}
$titleFormatter = $services->getTitleFormatter();
$name = Sanitizer::escapeClass( 'page-' . $titleFormatter->getPrefixedText( $title ) );
$root = Sanitizer::escapeClass( 'rootpage-' . $titleFormatter->formatTitle( $ns, $title->getRootText() ) );
// Add a static class that is not subject to translation to allow extensions/skins/global code to target main
// pages reliably (T363281)
if ( $title->isMainPage() ) {
$name .= ' page-Main_Page';
}
return "$numeric $type $name $root";
}
/**
* Return values for <html> element
* @return array Array of associative name-to-value elements for <html> element
*/
public function getHtmlElementAttributes() {
$lang = $this->getLanguage();
return [
'lang' => $lang->getHtmlCode(),
'dir' => $lang->getDir(),
'class' => 'client-nojs',
];
}
/**
* @return string HTML
*/
public function getCategoryLinks() {
$out = $this->getOutput();
$allCats = $out->getCategoryLinks();
$title = $this->getTitle();
$services = MediaWikiServices::getInstance();
$linkRenderer = $services->getLinkRenderer();
if ( $allCats === [] ) {
return '';
}
$embed = "<li>";
$pop = "</li>";
$s = '';
$colon = $this->msg( 'colon-separator' )->escaped();
if ( !empty( $allCats['normal'] ) ) {
$t = $embed . implode( $pop . $embed, $allCats['normal'] ) . $pop;
$msg = $this->msg( 'pagecategories' )->numParams( count( $allCats['normal'] ) );
$linkPage = $this->msg( 'pagecategorieslink' )->inContentLanguage()->text();
$pageCategoriesLinkTitle = Title::newFromText( $linkPage );
if ( $pageCategoriesLinkTitle ) {
$link = $linkRenderer->makeLink( $pageCategoriesLinkTitle, $msg->text() );
} else {
$link = $msg->escaped();
}
$s .= Html::rawElement(
'div',
[ 'id' => 'mw-normal-catlinks', 'class' => 'mw-normal-catlinks' ],
$link . $colon . Html::rawElement( 'ul', [], $t )
);
}
# Hidden categories
if ( isset( $allCats['hidden'] ) ) {
$userOptionsLookup = $services->getUserOptionsLookup();
if ( $userOptionsLookup->getBoolOption( $this->getUser(), 'showhiddencats' ) ) {
$class = ' mw-hidden-cats-user-shown';
} elseif ( $title->inNamespace( NS_CATEGORY ) ) {
$class = ' mw-hidden-cats-ns-shown';
} else {
$class = ' mw-hidden-cats-hidden';
}
$s .= Html::rawElement(
'div',
[ 'id' => 'mw-hidden-catlinks', 'class' => "mw-hidden-catlinks$class" ],
$this->msg( 'hidden-categories' )->numParams( count( $allCats['hidden'] ) )->escaped() .
$colon .
Html::rawElement(
'ul',
[],
$embed . implode( $pop . $embed, $allCats['hidden'] ) . $pop
)
);
}
return $s;
}
/**
* @return string HTML
*/
public function getCategories() {
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
$showHiddenCats = $userOptionsLookup->getBoolOption( $this->getUser(), 'showhiddencats' );
$catlinks = $this->getCategoryLinks();
// Check what we're showing
$allCats = $this->getOutput()->getCategoryLinks();
$showHidden = $showHiddenCats || $this->getTitle()->inNamespace( NS_CATEGORY );
$classes = [ 'catlinks' ];
if ( empty( $allCats['normal'] ) && !( !empty( $allCats['hidden'] ) && $showHidden ) ) {
$classes[] = 'catlinks-allhidden';
}
return Html::rawElement(
'div',
[ 'id' => 'catlinks', 'class' => $classes, 'data-mw' => 'interface' ],
$catlinks
);
}
/**
* This runs a hook to allow extensions placing their stuff after content
* and article metadata (e.g. categories).
* Note: This function has nothing to do with afterContent().
*
* This hook is placed here in order to allow using the same hook for all
* skins, both the SkinTemplate based ones and the older ones, which directly
* use this class to get their data.
*
* The output of this function gets processed in SkinTemplate::outputPage() for
* the SkinTemplate based skins, all other skins should directly echo it.
*
* @return string Empty by default, if not changed by any hook function.
*/
protected function afterContentHook() {
$data = '';
if ( $this->getHookRunner()->onSkinAfterContent( $data, $this ) ) {
// adding just some spaces shouldn't toggle the output
// of the whole <div/>, so we use trim() here
if ( trim( $data ) != '' ) {
// Doing this here instead of in the skins to
// ensure that the div has the same ID in all
// skins
$data = "<div id='mw-data-after-content'>\n" .
"\t$data\n" .
"</div>\n";
}
} else {
wfDebug( "Hook SkinAfterContent changed output processing." );
}
return $data;
}
/**
* Get the canonical URL (permalink) for the page including oldid if present.
*
* @return string
*/
private function getCanonicalUrl() {
$title = $this->getTitle();
$oldid = $this->getOutput()->getRevisionId();
if ( $oldid ) {
return $title->getCanonicalURL( 'oldid=' . $oldid );
} else {
// oldid not available for non existing pages
return $title->getCanonicalURL();
}
}
/**
* Text with the permalink to the source page,
* usually shown on the footer of a printed page
*
* @stable to override
* @return string HTML text with an URL
*/
public function printSource() {
$urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
$url = htmlspecialchars( $urlUtils->expandIRI( $this->getCanonicalUrl() ) ?? '' );
return $this->msg( 'retrievedfrom' )
->rawParams( '<a dir="ltr" href="' . $url . '">' . $url . '</a>' )
->parse();
}
/**
* @return string HTML
*/
public function getUndeleteLink() {
$action = $this->getRequest()->getRawVal( 'action' ) ?? 'view';
$title = $this->getTitle();
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
if ( ( !$title->exists() || $action == 'history' ) &&
$this->getAuthority()->probablyCan( 'deletedhistory', $title )
) {
$n = $title->getDeletedEditsCount();
if ( $n ) {
if ( $this->getAuthority()->probablyCan( 'undelete', $title ) ) {
$msg = 'thisisdeleted';
} else {
$msg = 'viewdeleted';
}
$subtitle = $this->msg( $msg )->rawParams(
$linkRenderer->makeKnownLink(
SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedDBkey() ),
$this->msg( 'restorelink' )->numParams( $n )->text() )
)->escaped();
$links = [];
// Add link to page logs, unless we're on the history page (which
// already has one)
if ( $action !== 'history' ) {
$links[] = $linkRenderer->makeKnownLink(
SpecialPage::getTitleFor( 'Log' ),
$this->msg( 'viewpagelogs-lowercase' )->text(),
[],
[ 'page' => $title->getPrefixedText() ]
);
}
// Allow extensions to add more links
$this->getHookRunner()->onUndeletePageToolLinks(
$this->getContext(), $linkRenderer, $links );
if ( $links ) {
$subtitle .= ''
. $this->msg( 'word-separator' )->escaped()
. $this->msg( 'parentheses' )
->rawParams( $this->getLanguage()->pipeList( $links ) )
->escaped();
}
return Html::rawElement( 'div', [ 'class' => 'mw-undelete-subtitle' ], $subtitle );
}
}
return '';
}
/**
* @return string
*/
private function subPageSubtitleInternal() {
$services = MediaWikiServices::getInstance();
$linkRenderer = $services->getLinkRenderer();
$out = $this->getOutput();
$title = $out->getTitle();
$subpages = '';
if ( !$this->getHookRunner()->onSkinSubPageSubtitle( $subpages, $this, $out ) ) {
return $subpages;
}
$hasSubpages = $services->getNamespaceInfo()->hasSubpages( $title->getNamespace() );
if ( !$out->isArticle() || !$hasSubpages ) {
return $subpages;
}
$ptext = $title->getPrefixedText();
if ( strpos( $ptext, '/' ) !== false ) {
$links = explode( '/', $ptext );
array_pop( $links );
$count = 0;
$growingLink = '';
$display = '';
$lang = $this->getLanguage();