SpecialPrefixindex.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Implements Special:Prefixindex
  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. * Implements Special:Prefixindex
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialPrefixindex extends SpecialAllPages {
  30. /**
  31. * Whether to remove the searched prefix from the displayed link. Useful
  32. * for inclusion of a set of sub pages in a root page.
  33. */
  34. protected $stripPrefix = false;
  35. protected $hideRedirects = false;
  36. // Inherit $maxPerPage
  37. function __construct() {
  38. parent::__construct( 'Prefixindex' );
  39. }
  40. /**
  41. * Entry point : initialise variables and call subfunctions.
  42. * @param string $par Becomes "FOO" when called like Special:Prefixindex/FOO (default null)
  43. */
  44. function execute( $par ) {
  45. $this->setHeaders();
  46. $this->outputHeader();
  47. $out = $this->getOutput();
  48. $out->addModuleStyles( 'mediawiki.special' );
  49. # GET values
  50. $request = $this->getRequest();
  51. $from = $request->getVal( 'from', '' );
  52. $prefix = $request->getVal( 'prefix', '' );
  53. $ns = $request->getIntOrNull( 'namespace' );
  54. $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
  55. $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
  56. $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
  57. $namespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
  58. $out->setPageTitle(
  59. ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
  60. ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
  61. : $this->msg( 'prefixindex' )
  62. );
  63. $showme = '';
  64. if ( $par !== null ) {
  65. $showme = $par;
  66. } elseif ( $prefix != '' ) {
  67. $showme = $prefix;
  68. } elseif ( $from != '' && $ns === null ) {
  69. // For back-compat with Special:Allpages
  70. // Don't do this if namespace is passed, so paging works when doing NS views.
  71. $showme = $from;
  72. }
  73. // T29864: if transcluded, show all pages instead of the form.
  74. if ( $this->including() || $showme != '' || $ns !== null ) {
  75. $this->showPrefixChunk( $namespace, $showme, $from );
  76. } else {
  77. $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
  78. }
  79. }
  80. /**
  81. * HTML for the top form
  82. * @param int $namespace A namespace constant (default NS_MAIN).
  83. * @param string $from DbKey we are starting listing at.
  84. * @return string
  85. */
  86. protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
  87. $formDescriptor = [
  88. 'prefix' => [
  89. 'label-message' => 'allpagesprefix',
  90. 'name' => 'prefix',
  91. 'id' => 'nsfrom',
  92. 'type' => 'text',
  93. 'size' => '30',
  94. 'default' => str_replace( '_', ' ', $from ),
  95. ],
  96. 'namespace' => [
  97. 'type' => 'namespaceselect',
  98. 'name' => 'namespace',
  99. 'id' => 'namespace',
  100. 'label-message' => 'namespace',
  101. 'all' => null,
  102. 'default' => $namespace,
  103. ],
  104. 'hidedirects' => [
  105. 'class' => 'HTMLCheckField',
  106. 'name' => 'hideredirects',
  107. 'label-message' => 'allpages-hide-redirects',
  108. ],
  109. 'stripprefix' => [
  110. 'class' => 'HTMLCheckField',
  111. 'name' => 'stripprefix',
  112. 'label-message' => 'prefixindex-strip',
  113. ],
  114. ];
  115. $context = new DerivativeContext( $this->getContext() );
  116. $context->setTitle( $this->getPageTitle() ); // Remove subpage
  117. $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $context );
  118. $htmlForm
  119. ->setMethod( 'get' )
  120. ->setWrapperLegendMsg( 'prefixindex' )
  121. ->setSubmitTextMsg( 'prefixindex-submit' );
  122. return $htmlForm->prepareForm()->getHTML( false );
  123. }
  124. /**
  125. * @param int $namespace
  126. * @param string $prefix
  127. * @param string|null $from List all pages from this name (default false)
  128. */
  129. protected function showPrefixChunk( $namespace, $prefix, $from = null ) {
  130. if ( $from === null ) {
  131. $from = $prefix;
  132. }
  133. $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
  134. $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
  135. $namespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
  136. $res = null;
  137. $n = 0;
  138. $nextRow = null;
  139. if ( !$prefixList || !$fromList ) {
  140. $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
  141. } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
  142. // Show errormessage and reset to NS_MAIN
  143. $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
  144. $namespace = NS_MAIN;
  145. } else {
  146. list( $namespace, $prefixKey, $prefix ) = $prefixList;
  147. list( /* $fromNS */, $fromKey, ) = $fromList;
  148. # ## @todo FIXME: Should complain if $fromNs != $namespace
  149. $dbr = wfGetDB( DB_REPLICA );
  150. $conds = [
  151. 'page_namespace' => $namespace,
  152. 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
  153. 'page_title >= ' . $dbr->addQuotes( $fromKey ),
  154. ];
  155. if ( $this->hideRedirects ) {
  156. $conds['page_is_redirect'] = 0;
  157. }
  158. $res = $dbr->select( 'page',
  159. array_merge(
  160. [ 'page_namespace', 'page_title' ],
  161. LinkCache::getSelectFields()
  162. ),
  163. $conds,
  164. __METHOD__,
  165. [
  166. 'ORDER BY' => 'page_title',
  167. 'LIMIT' => $this->maxPerPage + 1,
  168. 'USE INDEX' => 'name_title',
  169. ]
  170. );
  171. // @todo FIXME: Side link to previous
  172. if ( $res->numRows() > 0 ) {
  173. $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
  174. $linkCache = MediaWikiServices::getInstance()->getLinkCache();
  175. $prefixLength = strlen( $prefix );
  176. foreach ( $res as $row ) {
  177. if ( $n >= $this->maxPerPage ) {
  178. $nextRow = $row;
  179. break;
  180. }
  181. $title = Title::newFromRow( $row );
  182. // Make sure it gets into LinkCache
  183. $linkCache->addGoodLinkObjFromRow( $title, $row );
  184. $displayed = $title->getText();
  185. // Try not to generate unclickable links
  186. if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
  187. $displayed = substr( $displayed, $prefixLength );
  188. }
  189. $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
  190. $this->getLinkRenderer()->makeKnownLink(
  191. $title,
  192. $displayed
  193. ) .
  194. ( $title->isRedirect() ? '</div>' : '' );
  195. $out .= "<li>$link</li>\n";
  196. $n++;
  197. }
  198. $out .= Html::closeElement( 'ul' );
  199. if ( $res->numRows() > 2 ) {
  200. // Only apply CSS column styles if there's more than 2 entries.
  201. // Otherwise rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
  202. $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
  203. }
  204. } else {
  205. $out = '';
  206. }
  207. }
  208. $output = $this->getOutput();
  209. if ( $this->including() ) {
  210. // We don't show the nav-links and the form when included into other
  211. // pages so let's just finish here.
  212. $output->addHTML( $out );
  213. return;
  214. }
  215. $topOut = $this->namespacePrefixForm( $namespace, $prefix );
  216. if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
  217. $query = [
  218. 'from' => $nextRow->page_title,
  219. 'prefix' => $prefix,
  220. 'hideredirects' => $this->hideRedirects,
  221. 'stripprefix' => $this->stripPrefix,
  222. ];
  223. if ( $namespace || $prefix == '' ) {
  224. // Keep the namespace even if it's 0 for empty prefixes.
  225. // This tells us we're not just a holdover from old links.
  226. $query['namespace'] = $namespace;
  227. }
  228. $nextLink = $this->getLinkRenderer()->makeKnownLink(
  229. $this->getPageTitle(),
  230. $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(),
  231. [],
  232. $query
  233. );
  234. // Link shown at the top of the page below the form
  235. $topOut .= Html::rawElement( 'div',
  236. [ 'class' => 'mw-prefixindex-nav' ],
  237. $nextLink
  238. );
  239. // Link shown at the footer
  240. $out .= "\n" . Html::element( 'hr' ) .
  241. Html::rawElement(
  242. 'div',
  243. [ 'class' => 'mw-prefixindex-nav' ],
  244. $nextLink
  245. );
  246. }
  247. $output->addHTML( $topOut . $out );
  248. }
  249. /**
  250. * Return an array of subpages beginning with $search that this special page will accept.
  251. *
  252. * @param string $search Prefix to search for
  253. * @param int $limit Maximum number of results to return (usually 10)
  254. * @param int $offset Number of results to skip (usually 0)
  255. * @return string[] Matching subpages
  256. */
  257. public function prefixSearchSubpages( $search, $limit, $offset ) {
  258. return $this->prefixSearchString( $search, $limit, $offset );
  259. }
  260. protected function getGroupName() {
  261. return 'pages';
  262. }
  263. }