MediaWiki 1.42.1
SpecialSpecialPages.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
32use Wikimedia\Parsoid\Core\SectionMetadata;
33use Wikimedia\Parsoid\Core\TOCData;
34
41
42 public function __construct() {
43 parent::__construct( 'Specialpages' );
44 }
45
46 public function execute( $par ) {
47 $out = $this->getOutput();
48 $this->setHeaders();
49 $this->outputHeader();
50 $out->setPreventClickjacking( false );
51 $out->addModuleStyles( 'mediawiki.special' );
52
53 $groups = $this->getPageGroups();
54
55 if ( $groups === false ) {
56 return;
57 }
58
59 $this->addHelpLink( 'Help:Special pages' );
60 $this->outputPageList( $groups );
61 }
62
63 private function getPageGroups() {
64 $pages = $this->getSpecialPageFactory()->getUsablePages( $this->getUser() );
65
66 if ( $pages === [] ) {
67 // Yeah, that was pointless. Thanks for coming.
68 return false;
69 }
70
71 // Put them into a sortable array
72 $groups = [];
74 foreach ( $pages as $page ) {
75 $group = $page->getFinalGroupName();
76 $desc = $page->getDescription();
77 // T343849
78 if ( is_string( $desc ) ) {
79 wfDeprecated( "string return from {$page->getName()}::getDescription()", '1.41' );
80 $desc = ( new RawMessage( '$1' ) )->rawParams( $desc );
81 }
82 $groups[$group][$desc->text()] = [
83 $page->getPageTitle(),
84 $page->isRestricted(),
85 $page->isCached()
86 ];
87 }
88
89 // Sort
90 foreach ( $groups as $group => $sortedPages ) {
91 ksort( $groups[$group] );
92 }
93
94 // Always move "other" to end
95 if ( array_key_exists( 'other', $groups ) ) {
96 $other = $groups['other'];
97 unset( $groups['other'] );
98 $groups['other'] = $other;
99 }
100
101 return $groups;
102 }
103
104 private function outputPageList( $groups ) {
105 $out = $this->getOutput();
106
107 // Legend
108 $includesRestrictedPages = false;
109 $includesCachedPages = false;
110 foreach ( $groups as $group => $sortedPages ) {
111 foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
112 if ( $cached ) {
113 $includesCachedPages = true;
114 }
115 if ( $restricted ) {
116 $includesRestrictedPages = true;
117 }
118 }
119 }
120
121 $notes = [];
122 if ( $includesRestrictedPages ) {
123 $restricedMsg = $this->msg( 'specialpages-note-restricted' );
124 if ( !$restricedMsg->isDisabled() ) {
125 $notes[] = $restricedMsg->parse();
126 }
127 }
128 if ( $includesCachedPages ) {
129 $cachedMsg = $this->msg( 'specialpages-note-cached' );
130 if ( !$cachedMsg->isDisabled() ) {
131 $notes[] = $cachedMsg->parse();
132 }
133 }
134 if ( $notes !== [] ) {
135 $legendHeading = $this->msg( 'specialpages-note-top' )->parse();
136
137 $legend = Html::rawElement(
138 'div',
139 [ 'class' => 'mw-changeslist-legend mw-specialpages-notes' ],
140 $legendHeading . implode( "\n", $notes )
141 );
142
143 $out->addHTML( $legend );
144 $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
145 }
146
147 // Format table of contents
148 $tocData = new TOCData();
149 $tocLength = 0;
150 foreach ( $groups as $group => $sortedPages ) {
151 if ( !str_contains( $group, '/' ) ) {
152 ++$tocLength;
153 $tocData->addSection( new SectionMetadata(
154 1,
155 2,
156 $this->msg( "specialpages-group-$group" )->escaped(),
157 $this->getLanguage()->formatNum( $tocLength ),
158 (string)$tocLength,
159 null,
160 null,
161 "mw-specialpagesgroup-$group",
162 "mw-specialpagesgroup-$group"
163 ) );
164 }
165 }
166
167 $pout = new ParserOutput;
168 $pout->setTOCData( $tocData );
169 $pout->setOutputFlag( ParserOutputFlags::SHOW_TOC );
170 $pout->setRawText( Parser::TOC_PLACEHOLDER );
171 $out->addParserOutput( $pout );
172
173 // Format contents
174 foreach ( $groups as $group => $sortedPages ) {
175 if ( str_contains( $group, '/' ) ) {
176 [ $group, $subGroup ] = explode( '/', $group, 2 );
177 $out->addHTML( Html::element(
178 'h3',
179 [ 'class' => "mw-specialpagessubgroup" ],
180 $this->msg( "specialpages-group-$group-$subGroup" )->text()
181 ) . "\n" );
182 } else {
183 $out->addHTML( Html::element(
184 'h2',
185 [ 'class' => "mw-specialpagesgroup", 'id' => "mw-specialpagesgroup-$group" ],
186 $this->msg( "specialpages-group-$group" )->text()
187 ) . "\n" );
188 }
189 $out->addHTML(
190 Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
191 . '<ul>'
192 );
193 foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
194 $pageClasses = [];
195 if ( $cached ) {
196 $pageClasses[] = 'mw-specialpagecached';
197 }
198 if ( $restricted ) {
199 $pageClasses[] = 'mw-specialpagerestricted';
200 }
201
202 $link = $this->getLinkRenderer()->makeKnownLink( $title, $desc );
203 $out->addHTML( Html::rawElement(
204 'li',
205 [ 'class' => $pageClasses ],
206 $link
207 ) . "\n" );
208 }
209 $out->addHTML(
210 Html::closeElement( 'ul' ) .
211 Html::closeElement( 'div' )
212 );
213 }
214 }
215}
216
221class_alias( SpecialSpecialPages::class, 'SpecialSpecialpages' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Variant of the Message class.
Rendered output of a wiki page, as parsed from wikitext.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:156
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getUser()
Shortcut to get the User executing this instance.
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 Per default the message key is the canonical name o...
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.
element(SerializerNode $parent, SerializerNode $node, $contents)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...