SpecialSpecialpages.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. use MediaWiki\MediaWikiServices;
  24. /**
  25. * A special page that lists special pages
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialSpecialpages extends UnlistedSpecialPage {
  30. function __construct() {
  31. parent::__construct( 'Specialpages' );
  32. }
  33. function execute( $par ) {
  34. $out = $this->getOutput();
  35. $this->setHeaders();
  36. $this->outputHeader();
  37. $out->allowClickjacking();
  38. $out->addModuleStyles( 'mediawiki.special' );
  39. $groups = $this->getPageGroups();
  40. if ( $groups === false ) {
  41. return;
  42. }
  43. $this->addHelpLink( 'Help:Special pages' );
  44. $this->outputPageList( $groups );
  45. }
  46. private function getPageGroups() {
  47. $pages = MediaWikiServices::getInstance()->getSpecialPageFactory()->
  48. getUsablePages( $this->getUser() );
  49. if ( $pages === [] ) {
  50. # Yeah, that was pointless. Thanks for coming.
  51. return false;
  52. }
  53. /** Put them into a sortable array */
  54. $groups = [];
  55. /** @var SpecialPage $page */
  56. foreach ( $pages as $page ) {
  57. if ( $page->isListed() ) {
  58. $group = $page->getFinalGroupName();
  59. if ( !isset( $groups[$group] ) ) {
  60. $groups[$group] = [];
  61. }
  62. $groups[$group][$page->getDescription()] = [
  63. $page->getPageTitle(),
  64. $page->isRestricted(),
  65. $page->isCached()
  66. ];
  67. }
  68. }
  69. /** Sort */
  70. foreach ( $groups as $group => $sortedPages ) {
  71. ksort( $groups[$group] );
  72. }
  73. /** Always move "other" to end */
  74. if ( array_key_exists( 'other', $groups ) ) {
  75. $other = $groups['other'];
  76. unset( $groups['other'] );
  77. $groups['other'] = $other;
  78. }
  79. return $groups;
  80. }
  81. private function outputPageList( $groups ) {
  82. $out = $this->getOutput();
  83. $includesRestrictedPages = false;
  84. $includesCachedPages = false;
  85. foreach ( $groups as $group => $sortedPages ) {
  86. if ( strpos( $group, '/' ) !== false ) {
  87. list( $group, $subGroup ) = explode( '/', $group, 2 );
  88. $out->wrapWikiMsg(
  89. "<h3 class=\"mw-specialpagessubgroup\">$1</h3>\n",
  90. "specialpages-group-$group-$subGroup"
  91. );
  92. } else {
  93. $out->wrapWikiMsg(
  94. "<h2 class=\"mw-specialpagesgroup\" id=\"mw-specialpagesgroup-$group\">$1</h2>\n",
  95. "specialpages-group-$group"
  96. );
  97. }
  98. $out->addHTML(
  99. Html::openElement( 'div', [ 'class' => 'mw-specialpages-list' ] )
  100. . '<ul>'
  101. );
  102. foreach ( $sortedPages as $desc => $specialpage ) {
  103. list( $title, $restricted, $cached ) = $specialpage;
  104. $pageClasses = [];
  105. if ( $cached ) {
  106. $includesCachedPages = true;
  107. $pageClasses[] = 'mw-specialpagecached';
  108. }
  109. if ( $restricted ) {
  110. $includesRestrictedPages = true;
  111. $pageClasses[] = 'mw-specialpagerestricted';
  112. }
  113. $link = $this->getLinkRenderer()->makeKnownLink( $title, $desc );
  114. $out->addHTML( Html::rawElement(
  115. 'li',
  116. [ 'class' => implode( ' ', $pageClasses ) ],
  117. $link
  118. ) . "\n" );
  119. }
  120. $out->addHTML(
  121. Html::closeElement( 'ul' ) .
  122. Html::closeElement( 'div' )
  123. );
  124. }
  125. // add legend
  126. $notes = [];
  127. if ( $includesRestrictedPages ) {
  128. $restricedMsg = $this->msg( 'specialpages-note-restricted' );
  129. if ( !$restricedMsg->isDisabled() ) {
  130. $notes[] = $restricedMsg->plain();
  131. }
  132. }
  133. if ( $includesCachedPages ) {
  134. $cachedMsg = $this->msg( 'specialpages-note-cached' );
  135. if ( !$cachedMsg->isDisabled() ) {
  136. $notes[] = $cachedMsg->plain();
  137. }
  138. }
  139. if ( $notes !== [] ) {
  140. $out->wrapWikiMsg(
  141. "<h2 class=\"mw-specialpages-note-top\">$1</h2>", 'specialpages-note-top'
  142. );
  143. $out->wrapWikiTextAsInterface(
  144. 'mw-specialpages-notes',
  145. implode( "\n", $notes )
  146. );
  147. }
  148. }
  149. }