Special case opus mime detction
[mediawiki.git] / includes / specials / SpecialAllPages.php
blob4b8446a795d9fcd17a4fc5d4b410bf952c6780c7
1 <?php
2 /**
3 * Implements Special:Allpages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Implements Special:Allpages
27 * @ingroup SpecialPage
28 * @todo Rewrite using IndexPager
30 class SpecialAllPages extends IncludableSpecialPage {
32 /**
33 * Maximum number of pages to show on single subpage.
35 * @var int $maxPerPage
37 protected $maxPerPage = 345;
39 /**
40 * Determines, which message describes the input field 'nsfrom'.
42 * @var string $nsfromMsg
44 protected $nsfromMsg = 'allpagesfrom';
46 /**
47 * Constructor
49 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
51 function __construct( $name = 'Allpages' ) {
52 parent::__construct( $name );
55 /**
56 * Entry point : initialise variables and call subfunctions.
58 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
60 function execute( $par ) {
61 $request = $this->getRequest();
62 $out = $this->getOutput();
64 $this->setHeaders();
65 $this->outputHeader();
66 $out->allowClickjacking();
68 # GET values
69 $from = $request->getVal( 'from', null );
70 $to = $request->getVal( 'to', null );
71 $namespace = $request->getInt( 'namespace' );
72 $hideredirects = $request->getBool( 'hideredirects', false );
74 $namespaces = $this->getLanguage()->getNamespaces();
76 $out->setPageTitle(
77 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
78 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
79 $this->msg( 'allarticles' )
81 $out->addModuleStyles( 'mediawiki.special' );
83 if ( $par !== null ) {
84 $this->showChunk( $namespace, $par, $to, $hideredirects );
85 } elseif ( $from !== null && $to === null ) {
86 $this->showChunk( $namespace, $from, $to, $hideredirects );
87 } else {
88 $this->showToplevel( $namespace, $from, $to, $hideredirects );
92 /**
93 * Outputs the HTMLForm used on this page
95 * @param int $namespace A namespace constant (default NS_MAIN).
96 * @param string $from DbKey we are starting listing at.
97 * @param string $to DbKey we are ending listing at.
98 * @param bool $hideRedirects Dont show redirects (default false)
100 protected function outputHTMLForm( $namespace = NS_MAIN,
101 $from = '', $to = '', $hideRedirects = false
103 $fields = [
104 'from' => [
105 'type' => 'text',
106 'name' => 'from',
107 'id' => 'nsfrom',
108 'size' => 30,
109 'label-message' => 'allpagesfrom',
110 'default' => str_replace( '_', ' ', $from ),
112 'to' => [
113 'type' => 'text',
114 'name' => 'to',
115 'id' => 'nsto',
116 'size' => 30,
117 'label-message' => 'allpagesto',
118 'default' => str_replace( '_', ' ', $to ),
120 'namespace' => [
121 'type' => 'namespaceselect',
122 'name' => 'namespace',
123 'id' => 'namespace',
124 'label-message' => 'namespace',
125 'all' => null,
126 'value' => $namespace,
128 'hideredirects' => [
129 'type' => 'check',
130 'name' => 'hideredirects',
131 'id' => 'hidredirects',
132 'label-message' => 'allpages-hide-redirects',
133 'value' => $hideRedirects,
136 $form = HTMLForm::factory( 'table', $fields, $this->getContext() );
137 $form->setMethod( 'get' )
138 ->setWrapperLegendMsg( 'allpages' )
139 ->setSubmitTextMsg( 'allpagessubmit' )
140 ->prepareForm()
141 ->displayForm( false );
145 * @param int $namespace (default NS_MAIN)
146 * @param string $from List all pages from this name
147 * @param string $to List all pages to this name
148 * @param bool $hideredirects Dont show redirects (default false)
150 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
151 $from = Title::makeTitleSafe( $namespace, $from );
152 $to = Title::makeTitleSafe( $namespace, $to );
153 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
154 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
156 $this->showChunk( $namespace, $from, $to, $hideredirects );
160 * @param int $namespace Namespace (Default NS_MAIN)
161 * @param string $from List all pages from this name (default false)
162 * @param string $to List all pages to this name (default false)
163 * @param bool $hideredirects Dont show redirects (default false)
165 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
166 $output = $this->getOutput();
168 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
169 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
170 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
171 $n = 0;
172 $prevTitle = null;
174 if ( !$fromList || !$toList ) {
175 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
176 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
177 // Show errormessage and reset to NS_MAIN
178 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
179 $namespace = NS_MAIN;
180 } else {
181 list( $namespace, $fromKey, $from ) = $fromList;
182 list( , $toKey, $to ) = $toList;
184 $dbr = wfGetDB( DB_REPLICA );
185 $filterConds = [ 'page_namespace' => $namespace ];
186 if ( $hideredirects ) {
187 $filterConds['page_is_redirect'] = 0;
190 $conds = $filterConds;
191 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
192 if ( $toKey !== "" ) {
193 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
196 $res = $dbr->select( 'page',
197 [ 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ],
198 $conds,
199 __METHOD__,
201 'ORDER BY' => 'page_title',
202 'LIMIT' => $this->maxPerPage + 1,
203 'USE INDEX' => 'name_title',
207 $linkRenderer = $this->getLinkRenderer();
208 if ( $res->numRows() > 0 ) {
209 $out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
211 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
212 $t = Title::newFromRow( $s );
213 if ( $t ) {
214 $out .= '<li' .
215 ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
216 '>' .
217 $linkRenderer->makeLink( $t ) .
218 "</li>\n";
219 } else {
220 $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
222 $n++;
224 $out .= Html::closeElement( 'ul' );
226 if ( $res->numRows() > 2 ) {
227 // Only apply CSS column styles if there's more than 2 entries.
228 // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
229 $out = Html::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
231 } else {
232 $out = '';
235 if ( $fromKey !== '' && !$this->including() ) {
236 # Get the first title from previous chunk
237 $prevConds = $filterConds;
238 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
239 $prevKey = $dbr->selectField(
240 'page',
241 'page_title',
242 $prevConds,
243 __METHOD__,
244 [ 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 ]
247 if ( $prevKey === false ) {
248 # The previous chunk is not complete, need to link to the very first title
249 # available in the database
250 $prevKey = $dbr->selectField(
251 'page',
252 'page_title',
253 $prevConds,
254 __METHOD__,
255 [ 'ORDER BY' => 'page_title' ]
259 if ( $prevKey !== false ) {
260 $prevTitle = Title::makeTitle( $namespace, $prevKey );
265 if ( $this->including() ) {
266 $output->addHTML( $out );
267 return;
270 $navLinks = [];
271 $self = $this->getPageTitle();
273 $linkRenderer = $this->getLinkRenderer();
274 // Generate a "previous page" link if needed
275 if ( $prevTitle ) {
276 $query = [ 'from' => $prevTitle->getText() ];
278 if ( $namespace ) {
279 $query['namespace'] = $namespace;
282 if ( $hideredirects ) {
283 $query['hideredirects'] = $hideredirects;
286 $navLinks[] = $linkRenderer->makeKnownLink(
287 $self,
288 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
290 $query
295 // Generate a "next page" link if needed
296 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
297 # $s is the first link of the next chunk
298 $t = Title::makeTitle( $namespace, $s->page_title );
299 $query = [ 'from' => $t->getText() ];
301 if ( $namespace ) {
302 $query['namespace'] = $namespace;
305 if ( $hideredirects ) {
306 $query['hideredirects'] = $hideredirects;
309 $navLinks[] = $linkRenderer->makeKnownLink(
310 $self,
311 $this->msg( 'nextpage', $t->getText() )->text(),
313 $query
317 $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
319 if ( count( $navLinks ) ) {
320 // Add pagination links
321 $pagination = Html::rawElement( 'div',
322 [ 'class' => 'mw-allpages-nav' ],
323 $this->getLanguage()->pipeList( $navLinks )
326 $output->addHTML( $pagination );
327 $out .= Html::element( 'hr' ) . $pagination; // Footer
330 $output->addHTML( $out );
334 * @param int $ns The namespace of the article
335 * @param string $text The name of the article
336 * @return array( int namespace, string dbkey, string pagename ) or null on error
338 protected function getNamespaceKeyAndText( $ns, $text ) {
339 if ( $text == '' ) {
340 # shortcut for common case
341 return [ $ns, '', '' ];
344 $t = Title::makeTitleSafe( $ns, $text );
345 if ( $t && $t->isLocal() ) {
346 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
347 } elseif ( $t ) {
348 return null;
351 # try again, in case the problem was an empty pagename
352 $text = preg_replace( '/(#|$)/', 'X$1', $text );
353 $t = Title::makeTitleSafe( $ns, $text );
354 if ( $t && $t->isLocal() ) {
355 return [ $t->getNamespace(), '', '' ];
356 } else {
357 return null;
362 * Return an array of subpages beginning with $search that this special page will accept.
364 * @param string $search Prefix to search for
365 * @param int $limit Maximum number of results to return (usually 10)
366 * @param int $offset Number of results to skip (usually 0)
367 * @return string[] Matching subpages
369 public function prefixSearchSubpages( $search, $limit, $offset ) {
370 return $this->prefixSearchString( $search, $limit, $offset );
373 protected function getGroupName() {
374 return 'pages';