Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / specials / SpecialPrefixindex.php
blob1322d546fe3ab286e1829f0cc68575e4494dd569
1 <?php
2 /**
3 * Implements Special:Prefixindex
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:Prefixindex
27 * @ingroup SpecialPage
29 class SpecialPrefixindex extends SpecialAllpages {
30 // Inherit $maxPerPage
32 function __construct() {
33 parent::__construct( 'Prefixindex' );
36 /**
37 * Entry point : initialise variables and call subfunctions.
38 * @param string $par becomes "FOO" when called like Special:Prefixindex/FOO (default null)
40 function execute( $par ) {
41 global $wgContLang;
43 $this->setHeaders();
44 $this->outputHeader();
46 $out = $this->getOutput();
47 $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 $hideredirects = $request->getBool( 'hideredirects', false );
57 $namespaces = $wgContLang->getNamespaces();
58 $out->setPageTitle(
59 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
60 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
61 : $this->msg( 'prefixindex' )
64 $showme = '';
65 if ( isset( $par ) ) {
66 $showme = $par;
67 } elseif ( $prefix != '' ) {
68 $showme = $prefix;
69 } elseif ( $from != '' && $ns === null ) {
70 // For back-compat with Special:Allpages
71 // Don't do this if namespace is passed, so paging works when doing NS views.
72 $showme = $from;
75 // Bug 27864: if transcluded, show all pages instead of the form.
76 if ( $this->including() || $showme != '' || $ns !== null ) {
77 $this->showPrefixChunk( $namespace, $showme, $from, $hideredirects );
78 } else {
79 $out->addHTML( $this->namespacePrefixForm( $namespace, null, $hideredirects ) );
83 /**
84 * HTML for the top form
85 * @param $namespace Integer: a namespace constant (default NS_MAIN).
86 * @param string $from dbKey we are starting listing at.
87 * @param bool $hideredirects hide redirects (default FALSE)
88 * @return string
90 function namespacePrefixForm( $namespace = NS_MAIN, $from = '', $hideredirects = false ) {
91 global $wgScript;
93 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
94 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
95 $out .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
96 $out .= Xml::openElement( 'fieldset' );
97 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
98 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
99 $out .= "<tr>
100 <td class='mw-label'>" .
101 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
102 "</td>
103 <td class='mw-input'>" .
104 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
105 "</td>
106 </tr>
107 <tr>
108 <td class='mw-label'>" .
109 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
110 "</td>
111 <td class='mw-input'>" .
112 Html::namespaceSelector( array(
113 'selected' => $namespace,
114 ), array(
115 'name' => 'namespace',
116 'id' => 'namespace',
117 'class' => 'namespaceselector',
118 ) ) .
119 Xml::checkLabel(
120 $this->msg( 'allpages-hide-redirects' )->text(),
121 'hideredirects',
122 'hideredirects',
123 $hideredirects
124 ) . ' ' .
125 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
126 "</td>
127 </tr>";
128 $out .= Xml::closeElement( 'table' );
129 $out .= Xml::closeElement( 'fieldset' );
130 $out .= Xml::closeElement( 'form' );
131 $out .= Xml::closeElement( 'div' );
133 return $out;
137 * @param $namespace Integer, default NS_MAIN
138 * @param $prefix String
139 * @param string $from list all pages from this name (default FALSE)
140 * @param bool $hideredirects hide redirects (default FALSE)
142 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null, $hideredirects = false ) {
143 global $wgContLang;
145 if ( $from === null ) {
146 $from = $prefix;
149 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
150 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
151 $namespaces = $wgContLang->getNamespaces();
153 if ( !$prefixList || !$fromList ) {
154 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
155 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
156 // Show errormessage and reset to NS_MAIN
157 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
158 $namespace = NS_MAIN;
159 } else {
160 list( $namespace, $prefixKey, $prefix ) = $prefixList;
161 list( /* $fromNS */, $fromKey, ) = $fromList;
163 ### @todo FIXME: Should complain if $fromNs != $namespace
165 $dbr = wfGetDB( DB_SLAVE );
167 $conds = array(
168 'page_namespace' => $namespace,
169 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
170 'page_title >= ' . $dbr->addQuotes( $fromKey ),
173 if ( $hideredirects ) {
174 $conds['page_is_redirect'] = 0;
177 $res = $dbr->select( 'page',
178 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
179 $conds,
180 __METHOD__,
181 array(
182 'ORDER BY' => 'page_title',
183 'LIMIT' => $this->maxPerPage + 1,
184 'USE INDEX' => 'name_title',
188 ### @todo FIXME: Side link to previous
190 $n = 0;
191 if ( $res->numRows() > 0 ) {
192 $out = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-list-table' ) );
194 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
195 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
196 if ( $t ) {
197 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
198 Linker::linkKnown(
200 htmlspecialchars( $t->getText() ),
201 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
203 ( $s->page_is_redirect ? '</div>' : '' );
204 } else {
205 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
207 if ( $n % 3 == 0 ) {
208 $out .= '<tr>';
210 $out .= "<td>$link</td>";
211 $n++;
212 if ( $n % 3 == 0 ) {
213 $out .= '</tr>';
217 if ( $n % 3 != 0 ) {
218 $out .= '</tr>';
221 $out .= Xml::closeElement( 'table' );
222 } else {
223 $out = '';
227 $footer = '';
228 if ( $this->including() ) {
229 $out2 = '';
230 } else {
231 $nsForm = $this->namespacePrefixForm( $namespace, $prefix, $hideredirects );
232 $self = $this->getTitle();
233 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
234 '<tr>
235 <td>' .
236 $nsForm .
237 '</td>
238 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
240 if ( isset( $res ) && $res && ( $n == $this->maxPerPage ) &&
241 ( $s = $res->fetchObject() )
243 $query = array(
244 'from' => $s->page_title,
245 'prefix' => $prefix,
246 'hideredirects' => $hideredirects,
249 if ( $namespace || $prefix == '' ) {
250 // Keep the namespace even if it's 0 for empty prefixes.
251 // This tells us we're not just a holdover from old links.
252 $query['namespace'] = $namespace;
255 $nextLink = Linker::linkKnown(
256 $self,
257 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
258 array(),
259 $query
262 $out2 .= $nextLink;
264 $footer = "\n" . Html::element( 'hr' ) .
265 Html::rawElement(
266 'div',
267 array( 'class' => 'mw-prefixindex-nav' ),
268 $nextLink
271 $out2 .= "</td></tr>" .
272 Xml::closeElement( 'table' );
275 $this->getOutput()->addHTML( $out2 . $out . $footer );
278 protected function getGroupName() {
279 return 'pages';