MediaWiki  master
SpecialSpecialpages.php
Go to the documentation of this file.
1 <?php
26 use Wikimedia\Parsoid\Core\SectionMetadata;
27 use Wikimedia\Parsoid\Core\TOCData;
28 
35 
36  public function __construct() {
37  parent::__construct( 'Specialpages' );
38  }
39 
40  public function execute( $par ) {
41  $out = $this->getOutput();
42  $this->setHeaders();
43  $this->outputHeader();
44  $out->setPreventClickjacking( false );
45  $out->addModuleStyles( 'mediawiki.special' );
46 
47  $groups = $this->getPageGroups();
48 
49  if ( $groups === false ) {
50  return;
51  }
52 
53  $this->addHelpLink( 'Help:Special pages' );
54  $this->outputPageList( $groups );
55  }
56 
57  private function getPageGroups() {
58  $pages = $this->getSpecialPageFactory()->getUsablePages( $this->getUser() );
59 
60  if ( $pages === [] ) {
61  // Yeah, that was pointless. Thanks for coming.
62  return false;
63  }
64 
65  // Put them into a sortable array
66  $groups = [];
68  foreach ( $pages as $page ) {
69  $group = $page->getFinalGroupName();
70  $groups[$group][$page->getDescription()] = [
71  $page->getPageTitle(),
72  $page->isRestricted(),
73  $page->isCached()
74  ];
75  }
76 
77  // Sort
78  foreach ( $groups as $group => $sortedPages ) {
79  ksort( $groups[$group] );
80  }
81 
82  // Always move "other" to end
83  if ( array_key_exists( 'other', $groups ) ) {
84  $other = $groups['other'];
85  unset( $groups['other'] );
86  $groups['other'] = $other;
87  }
88 
89  return $groups;
90  }
91 
92  private function outputPageList( $groups ) {
93  $out = $this->getOutput();
94 
95  // Legend
96  $includesRestrictedPages = false;
97  $includesCachedPages = false;
98  foreach ( $groups as $group => $sortedPages ) {
99  foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
100  if ( $cached ) {
101  $includesCachedPages = true;
102  }
103  if ( $restricted ) {
104  $includesRestrictedPages = true;
105  }
106  }
107  }
108 
109  $notes = [];
110  if ( $includesRestrictedPages ) {
111  $restricedMsg = $this->msg( 'specialpages-note-restricted' );
112  if ( !$restricedMsg->isDisabled() ) {
113  $notes[] = $restricedMsg->parse();
114  }
115  }
116  if ( $includesCachedPages ) {
117  $cachedMsg = $this->msg( 'specialpages-note-cached' );
118  if ( !$cachedMsg->isDisabled() ) {
119  $notes[] = $cachedMsg->parse();
120  }
121  }
122  if ( $notes !== [] ) {
123  $legendHeading = $this->msg( 'specialpages-note-top' )->parse();
124 
125  $legend = Html::rawElement(
126  'div',
127  [ 'class' => [ 'mw-changeslist-legend', 'mw-collapsible', 'mw-specialpages-notes' ] ],
128  $legendHeading .
129  Html::rawElement( 'div', [ 'class' => 'mw-collapsible-content' ], implode( "\n", $notes ) )
130  );
131 
132  $out->addHTML( $legend );
133  $out->addModuleStyles( 'mediawiki.special.changeslist.legend' );
134  }
135 
136  // Format table of contents
137  $tocData = new TOCData();
138  $tocLength = 0;
139  foreach ( $groups as $group => $sortedPages ) {
140  if ( !str_contains( $group, '/' ) ) {
141  ++$tocLength;
142  $tocData->addSection( new SectionMetadata(
143  1,
144  2,
145  $this->msg( "specialpages-group-$group" )->escaped(),
146  $this->getLanguage()->formatNum( $tocLength ),
147  (string)$tocLength,
148  null,
149  null,
150  "mw-specialpagesgroup-$group",
151  "mw-specialpagesgroup-$group"
152  ) );
153  }
154  }
155 
156  $pout = new ParserOutput;
157  $pout->setTOCData( $tocData );
158  $pout->setOutputFlag( ParserOutputFlags::SHOW_TOC );
159  $pout->setText( Parser::TOC_PLACEHOLDER );
160  $out->addParserOutput( $pout );
161 
162  // Format contents
163  foreach ( $groups as $group => $sortedPages ) {
164  if ( str_contains( $group, '/' ) ) {
165  [ $group, $subGroup ] = explode( '/', $group, 2 );
166  $out->addHTML( Html::element(
167  'h3',
168  [ 'class' => "mw-specialpagessubgroup" ],
169  $this->msg( "specialpages-group-$group-$subGroup" )->text()
170  ) . "\n" );
171  } else {
172  $out->addHTML( Html::element(
173  'h2',
174  [ 'class' => "mw-specialpagesgroup", 'id' => "mw-specialpagesgroup-$group" ],
175  $this->msg( "specialpages-group-$group" )->text()
176  ) . "\n" );
177  }
178  $out->addHTML(
179  Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
180  . '<ul>'
181  );
182  foreach ( $sortedPages as $desc => [ $title, $restricted, $cached ] ) {
183  $pageClasses = [];
184  if ( $cached ) {
185  $pageClasses[] = 'mw-specialpagecached';
186  }
187  if ( $restricted ) {
188  $pageClasses[] = 'mw-specialpagerestricted';
189  }
190 
191  $link = $this->getLinkRenderer()->makeKnownLink( $title, $desc );
192  $out->addHTML( Html::rawElement(
193  'li',
194  [ 'class' => $pageClasses ],
195  $link
196  ) . "\n" );
197  }
198  $out->addHTML(
199  Html::closeElement( 'ul' ) .
200  Html::closeElement( 'div' )
201  );
202  }
203  }
204 }
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
setTOCData(TOCData $tocData)
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that lists special pages.
execute( $par)
Default execute method Checks user permissions.
Shortcut to construct a special page which is unlisted by default.