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
21 * @ingroup SpecialPage
25 * Implements Special:Prefixindex
27 * @ingroup SpecialPage
29 class SpecialPrefixindex
extends SpecialAllPages
{
32 * Whether to remove the searched prefix from the displayed link. Useful
33 * for inclusion of a set of sub pages in a root page.
35 protected $stripPrefix = false;
37 protected $hideRedirects = false;
39 // Inherit $maxPerPage
41 function __construct() {
42 parent
::__construct( 'Prefixindex' );
46 * Entry point : initialise variables and call subfunctions.
47 * @param string $par Becomes "FOO" when called like Special:Prefixindex/FOO (default null)
49 function execute( $par ) {
53 $this->outputHeader();
55 $out = $this->getOutput();
56 $out->addModuleStyles( 'mediawiki.special' );
59 $request = $this->getRequest();
60 $from = $request->getVal( 'from', '' );
61 $prefix = $request->getVal( 'prefix', '' );
62 $ns = $request->getIntOrNull( 'namespace' );
63 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
64 $this->hideRedirects
= $request->getBool( 'hideredirects', $this->hideRedirects
);
65 $this->stripPrefix
= $request->getBool( 'stripprefix', $this->stripPrefix
);
67 $namespaces = $wgContLang->getNamespaces();
69 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
70 ?
$this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
71 : $this->msg( 'prefixindex' )
75 if ( $par !== null ) {
77 } elseif ( $prefix != '' ) {
79 } elseif ( $from != '' && $ns === null ) {
80 // For back-compat with Special:Allpages
81 // Don't do this if namespace is passed, so paging works when doing NS views.
85 // Bug 27864: if transcluded, show all pages instead of the form.
86 if ( $this->including() ||
$showme != '' ||
$ns !== null ) {
87 $this->showPrefixChunk( $namespace, $showme, $from );
89 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
94 * HTML for the top form
95 * @param int $namespace A namespace constant (default NS_MAIN).
96 * @param string $from DbKey we are starting listing at.
99 protected function namespacePrefixForm( $namespace = NS_MAIN
, $from = '' ) {
100 $out = Xml
::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
101 $out .= Xml
::openElement(
103 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
105 $out .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
106 $out .= Xml
::openElement( 'fieldset' );
107 $out .= Xml
::element( 'legend', null, $this->msg( 'allpages' )->text() );
108 $out .= Xml
::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 <td class='mw-label'>" .
111 Xml
::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
113 <td class='mw-input'>" .
114 Xml
::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
118 <td class='mw-label'>" .
119 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
121 <td class='mw-input'>" .
122 Html
::namespaceSelector( array(
123 'selected' => $namespace,
125 'name' => 'namespace',
127 'class' => 'namespaceselector',
130 $this->msg( 'allpages-hide-redirects' )->text(),
136 $this->msg( 'prefixindex-strip' )->text(),
141 Xml
::submitButton( $this->msg( 'prefixindex-submit' )->text() ) .
144 $out .= Xml
::closeElement( 'table' );
145 $out .= Xml
::closeElement( 'fieldset' );
146 $out .= Xml
::closeElement( 'form' );
147 $out .= Xml
::closeElement( 'div' );
153 * @param int $namespace Default NS_MAIN
154 * @param string $prefix
155 * @param string $from List all pages from this name (default false)
157 protected function showPrefixChunk( $namespace = NS_MAIN
, $prefix, $from = null ) {
160 if ( $from === null ) {
164 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
165 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
166 $namespaces = $wgContLang->getNamespaces();
169 if ( !$prefixList ||
!$fromList ) {
170 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
171 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
172 // Show errormessage and reset to NS_MAIN
173 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
174 $namespace = NS_MAIN
;
176 list( $namespace, $prefixKey, $prefix ) = $prefixList;
177 list( /* $fromNS */, $fromKey, ) = $fromList;
179 # ## @todo FIXME: Should complain if $fromNs != $namespace
181 $dbr = wfGetDB( DB_SLAVE
);
184 'page_namespace' => $namespace,
185 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
186 'page_title >= ' . $dbr->addQuotes( $fromKey ),
189 if ( $this->hideRedirects
) {
190 $conds['page_is_redirect'] = 0;
193 $res = $dbr->select( 'page',
194 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
198 'ORDER BY' => 'page_title',
199 'LIMIT' => $this->maxPerPage +
1,
200 'USE INDEX' => 'name_title',
204 // @todo FIXME: Side link to previous
207 if ( $res->numRows() > 0 ) {
208 $out = Html
::openElement( 'ul', array( 'class' => 'mw-prefixindex-list' ) );
210 $prefixLength = strlen( $prefix );
211 while ( ( $n < $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
212 $t = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
214 $displayed = $t->getText();
215 // Try not to generate unclickable links
216 if ( $this->stripPrefix
&& $prefixLength !== strlen( $displayed ) ) {
217 $displayed = substr( $displayed, $prefixLength );
219 $link = ( $s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
222 htmlspecialchars( $displayed ),
223 $s->page_is_redirect ?
array( 'class' => 'mw-redirect' ) : array()
225 ( $s->page_is_redirect ?
'</div>' : '' );
227 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
230 $out .= "<li>$link</li>\n";
234 $out .= Html
::closeElement( 'ul' );
236 if ( $res->numRows() > 2 ) {
237 // Only apply CSS column styles if there's more than 2 entries.
238 // Otherwise rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
239 $out = Html
::rawElement( 'div', array( 'class' => 'mw-prefixindex-body' ), $out );
246 $output = $this->getOutput();
248 if ( $this->including() ) {
249 // We don't show the nav-links and the form when included into other
250 // pages so let's just finish here.
251 $output->addHTML( $out );
255 $topOut = $this->namespacePrefixForm( $namespace, $prefix );
257 if ( $res && ( $n == $this->maxPerPage
) && ( $s = $res->fetchObject() ) ) {
259 'from' => $s->page_title
,
261 'hideredirects' => $this->hideRedirects
,
262 'stripprefix' => $this->stripPrefix
,
265 if ( $namespace ||
$prefix == '' ) {
266 // Keep the namespace even if it's 0 for empty prefixes.
267 // This tells us we're not just a holdover from old links.
268 $query['namespace'] = $namespace;
271 $nextLink = Linker
::linkKnown(
272 $this->getPageTitle(),
273 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title
) )->escaped(),
278 // Link shown at the top of the page below the form
279 $topOut .= Html
::rawElement( 'div',
280 array( 'class' => 'mw-prefixindex-nav' ),
284 // Link shown at the footer
285 $out .= "\n" . Html
::element( 'hr' ) .
288 array( 'class' => 'mw-prefixindex-nav' ),
294 $output->addHTML( $topOut . $out );
298 * Return an array of subpages beginning with $search that this special page will accept.
300 * @param string $search Prefix to search for
301 * @param int $limit Maximum number of results to return (usually 10)
302 * @param int $offset Number of results to skip (usually 0)
303 * @return string[] Matching subpages
305 public function prefixSearchSubpages( $search, $limit, $offset ) {
306 $title = Title
::newFromText( $search );
307 if ( !$title ||
!$title->canExist() ) {
308 // No prefix suggestion in special and media namespace
311 // Autocomplete subpage the same as a normal search
312 $prefixSearcher = new StringPrefixSearch
;
313 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
317 protected function getGroupName() {