MediaWiki master
SpecialSpecialPages.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
10use MediaWiki\Html\TocGeneratorTrait;
13use OOUI\FieldLayout;
14use OOUI\SearchInputWidget;
15
22 use TocGeneratorTrait;
23
24 public function __construct() {
25 parent::__construct( 'Specialpages' );
26 }
27
29 public function execute( $par ) {
30 $out = $this->getOutput();
31 $this->setHeaders();
32 $this->outputHeader();
33 $out->getMetadata()->setPreventClickjacking( false );
34 $out->addModuleStyles( 'mediawiki.special' );
35
36 $groups = $this->getPageGroups();
37
38 if ( $groups === false ) {
39 return;
40 }
41
42 $this->addHelpLink( 'Help:Special pages' );
43 $this->outputPageList( $groups );
44 }
45
47 private function getPageGroups() {
48 $pages = $this->getSpecialPageFactory()->getUsablePages( $this->getUser(), $this->getContext() );
49
50 if ( $pages === [] ) {
51 // Yeah, that was pointless. Thanks for coming.
52 return false;
53 }
54
55 // Put them into a sortable array
56 $groups = [];
57 foreach ( $pages as $page ) {
58 $group = $page->getFinalGroupName();
59 $desc = $page->getDescription();
60 // (T360723) Only show an entry if the message isn't blanked, to allow on-wiki unlisting
61 if ( !$desc->isDisabled() ) {
62 $groups[$group][$desc->text()] = [
63 $page->getPageTitle(),
64 $page->isRestricted(),
65 $page->isCached()
66 ];
67 }
68 }
69
70 // Sort
71 foreach ( $groups as $group => $sortedPages ) {
72 ksort( $groups[$group] );
73 }
74
75 // Always move "other" to end
76 if ( array_key_exists( 'other', $groups ) ) {
77 $other = $groups['other'];
78 unset( $groups['other'] );
79 $groups['other'] = $other;
80 }
81
82 return $groups;
83 }
84
85 private function outputPageList( array $groups ) {
86 $out = $this->getOutput();
87 $aliases = $this->getSpecialPageFactory()->getAliasList();
88 $out->addModules( 'mediawiki.special.specialpages' );
89 $out->enableOOUI();
90
91 // Legend
92 $includesRestrictedPages = false;
93 $includesCachedPages = false;
94 foreach ( $groups as $group => $sortedPages ) {
95 foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
96 if ( $cached ) {
97 $includesCachedPages = true;
98 }
99 if ( $restricted ) {
100 $includesRestrictedPages = true;
101 }
102 }
103 }
104
105 $notes = [];
106 if ( $includesRestrictedPages ) {
107 $restricedMsg = $this->msg( 'specialpages-note-restricted' );
108 if ( !$restricedMsg->isDisabled() ) {
109 $notes[] = $restricedMsg->parse();
110 }
111 }
112 if ( $includesCachedPages ) {
113 $cachedMsg = $this->msg( 'specialpages-note-cached' );
114 if ( !$cachedMsg->isDisabled() ) {
115 $notes[] = $cachedMsg->parse();
116 }
117 }
118 if ( $notes !== [] ) {
119 $legendHeading = $this->msg( 'specialpages-note-top' )->parse();
120
121 $legend = Html::rawElement(
122 'div',
123 [ 'class' => [ 'mw-changeslist-legend', 'mw-specialpages-notes' ] ],
124 $legendHeading . implode( "\n", $notes )
125 );
126
127 $out->addHTML( $legend );
128 $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
129 }
130
131 $out->addHTML( ( new FieldLayout(
132 new SearchInputWidget( [
133 'placeholder' => $this->msg( 'specialpages-header-search' )->text(),
134 ] ),
135 [
136 'classes' => [ 'mw-special-pages-search' ],
137 'label' => $this->msg( 'specialpages-header-search' )->text(),
138 'invisibleLabel' => true,
139 'infusable' => true,
140 ]
141 ) )->toString() );
142
143 // Format table of contents
144 foreach ( $groups as $group => $sortedPages ) {
145 if ( !str_contains( $group, '/' ) ) {
146 $this->addTocSection(
147 id: "mw-specialpagesgroup-$group",
148 msg: "specialpages-group-$group"
149 );
150 }
151 }
152 $out->addTOCPlaceholder( $this->getTocData() );
153
154 // Format contents
155 $language = $this->getLanguage();
156 foreach ( $groups as $group => $sortedPages ) {
157 if ( str_contains( $group, '/' ) ) {
158 [ $group, $subGroup ] = explode( '/', $group, 2 );
159 $out->addHTML( Html::element(
160 'h3',
161 [ 'class' => "mw-specialpagessubgroup" ],
162 $this->msg( "specialpages-group-$group-$subGroup" )->text()
163 ) . "\n" );
164 } else {
165 $out->addHTML( Html::element(
166 'h2',
167 [ 'class' => "mw-specialpagesgroup", 'id' => "mw-specialpagesgroup-$group" ],
168 $this->msg( "specialpages-group-$group" )->text()
169 ) . "\n" );
170 }
171 $out->addHTML(
172 Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
173 . '<ul>'
174 );
175 foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
176 $indexAttr = [ 'data-search-index-0' => $language->lc( $title->getText() ) ];
177 $c = 1;
178 foreach ( $aliases as $alias => $target ) {
180 if (
181 $target == $title->getText() &&
182 $language->lc( $alias ) !== $language->lc( $title->getText() )
183 ) {
184 $indexAttr['data-search-index-' . $c ] = $language->lc( $alias );
185 ++$c;
186 }
187 }
188 $pageClasses = [];
189 if ( $cached ) {
190 $pageClasses[] = 'mw-specialpagecached';
191 }
192 if ( $restricted ) {
193 $pageClasses[] = 'mw-specialpagerestricted';
194 }
195
196 $link = $this->getLinkRenderer()->makeKnownLink( $title, $desc );
197 $out->addHTML( Html::rawElement(
198 'li',
199 $indexAttr + [ 'class' => $pageClasses ],
200 $link
201 ) . "\n" );
202 }
203 $out->addHTML(
204 Html::closeElement( 'ul' ) .
205 Html::closeElement( 'div' )
206 );
207 }
208 }
209}
210
215class_alias( SpecialSpecialPages::class, 'SpecialSpecialpages' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Shortcut to construct a special page which is unlisted by default.
A special page that lists special pages.
execute( $par)
Default execute method Checks user permissions.This must be overridden by subclasses; it will be made...
Represents a title within MediaWiki.
Definition Title.php:69
element(SerializerNode $parent, SerializerNode $node, $contents)
addTocSection(string $id, string $msg,... $params)
Add a section to the table of contents.