Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / specials / SpecialSpecialpages.php
1 <?php
2 /**
3 * Implements Special:Specialpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * A special page that lists special pages
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialSpecialpages extends UnlistedSpecialPage {
32
33 function __construct() {
34 parent::__construct( 'Specialpages' );
35 }
36
37 function execute( $par ) {
38 $out = $this->getOutput();
39 $this->setHeaders();
40 $this->outputHeader();
41 $out->allowClickjacking();
42 $out->addModuleStyles( 'mediawiki.special' );
43
44 $groups = $this->getPageGroups();
45
46 if ( $groups === false ) {
47 return;
48 }
49
50 $this->addHelpLink( 'Help:Special pages' );
51 $this->outputPageList( $groups );
52 }
53
54 private function getPageGroups() {
55 $pages = MediaWikiServices::getInstance()->getSpecialPageFactory()->
56 getUsablePages( $this->getUser() );
57
58 if ( !count( $pages ) ) {
59 # Yeah, that was pointless. Thanks for coming.
60 return false;
61 }
62
63 /** Put them into a sortable array */
64 $groups = [];
65 /** @var SpecialPage $page */
66 foreach ( $pages as $page ) {
67 if ( $page->isListed() ) {
68 $group = $page->getFinalGroupName();
69 if ( !isset( $groups[$group] ) ) {
70 $groups[$group] = [];
71 }
72 $groups[$group][$page->getDescription()] = [
73 $page->getPageTitle(),
74 $page->isRestricted(),
75 $page->isCached()
76 ];
77 }
78 }
79
80 /** Sort */
81 foreach ( $groups as $group => $sortedPages ) {
82 ksort( $groups[$group] );
83 }
84
85 /** Always move "other" to end */
86 if ( array_key_exists( 'other', $groups ) ) {
87 $other = $groups['other'];
88 unset( $groups['other'] );
89 $groups['other'] = $other;
90 }
91
92 return $groups;
93 }
94
95 private function outputPageList( $groups ) {
96 $out = $this->getOutput();
97
98 $includesRestrictedPages = false;
99 $includesCachedPages = false;
100
101 foreach ( $groups as $group => $sortedPages ) {
102 $out->wrapWikiMsg(
103 "<h2 class=\"mw-specialpagesgroup\" id=\"mw-specialpagesgroup-$group\">$1</h2>\n",
104 "specialpages-group-$group"
105 );
106 $out->addHTML(
107 Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
108 . '<ul>'
109 );
110 foreach ( $sortedPages as $desc => $specialpage ) {
111 list( $title, $restricted, $cached ) = $specialpage;
112
113 $pageClasses = [];
114 if ( $cached ) {
115 $includesCachedPages = true;
116 $pageClasses[] = 'mw-specialpagecached';
117 }
118 if ( $restricted ) {
119 $includesRestrictedPages = true;
120 $pageClasses[] = 'mw-specialpagerestricted';
121 }
122
123 $link = $this->getLinkRenderer()->makeKnownLink( $title, $desc );
124 $out->addHTML( Html::rawElement(
125 'li',
126 [ 'class' => implode( ' ', $pageClasses ) ],
127 $link
128 ) . "\n" );
129 }
130 $out->addHTML(
131 Html::closeElement( 'ul' ) .
132 Html::closeElement( 'div' )
133 );
134 }
135
136 // add legend
137 $notes = [];
138 if ( $includesRestrictedPages ) {
139 $restricedMsg = $this->msg( 'specialpages-note-restricted' );
140 if ( !$restricedMsg->isDisabled() ) {
141 $notes[] = $restricedMsg->plain();
142 }
143 }
144 if ( $includesCachedPages ) {
145 $cachedMsg = $this->msg( 'specialpages-note-cached' );
146 if ( !$cachedMsg->isDisabled() ) {
147 $notes[] = $cachedMsg->plain();
148 }
149 }
150 if ( $notes !== [] ) {
151 $out->wrapWikiMsg(
152 "<h2 class=\"mw-specialpages-note-top\">$1</h2>", 'specialpages-note-top'
153 );
154 $out->addWikiText(
155 "<div class=\"mw-specialpages-notes\">\n" .
156 implode( "\n", $notes ) .
157 "\n</div>"
158 );
159 }
160 }
161 }