wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialPrefixindex.php
bloba6c0423b27b8b3cf3622507e74f67691b059b647
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 {
31 /**
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' );
45 /**
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 ) {
50 global $wgContLang;
52 $this->setHeaders();
53 $this->outputHeader();
55 $out = $this->getOutput();
56 $out->addModuleStyles( 'mediawiki.special' );
58 # GET values
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();
68 $out->setPageTitle(
69 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
70 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
71 : $this->msg( 'prefixindex' )
74 $showme = '';
75 if ( $par !== null ) {
76 $showme = $par;
77 } elseif ( $prefix != '' ) {
78 $showme = $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.
82 $showme = $from;
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 );
88 } else {
89 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
93 /**
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.
97 * @return string
99 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
100 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
101 $out .= Xml::openElement(
102 'form',
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' ) );
109 $out .= "<tr>
110 <td class='mw-label'>" .
111 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
112 "</td>
113 <td class='mw-input'>" .
114 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
115 "</td>
116 </tr>
117 <tr>
118 <td class='mw-label'>" .
119 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
120 "</td>
121 <td class='mw-input'>" .
122 Html::namespaceSelector( array(
123 'selected' => $namespace,
124 ), array(
125 'name' => 'namespace',
126 'id' => 'namespace',
127 'class' => 'namespaceselector',
128 ) ) .
129 Xml::checkLabel(
130 $this->msg( 'allpages-hide-redirects' )->text(),
131 'hideredirects',
132 'hideredirects',
133 $this->hideRedirects
134 ) . ' ' .
135 Xml::checkLabel(
136 $this->msg( 'prefixindex-strip' )->text(),
137 'stripprefix',
138 'stripprefix',
139 $this->stripPrefix
140 ) . ' ' .
141 Xml::submitButton( $this->msg( 'prefixindex-submit' )->text() ) .
142 "</td>
143 </tr>";
144 $out .= Xml::closeElement( 'table' );
145 $out .= Xml::closeElement( 'fieldset' );
146 $out .= Xml::closeElement( 'form' );
147 $out .= Xml::closeElement( 'div' );
149 return $out;
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 ) {
158 global $wgContLang;
160 if ( $from === null ) {
161 $from = $prefix;
164 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
165 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
166 $namespaces = $wgContLang->getNamespaces();
167 $res = null;
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;
175 } else {
176 list( $namespace, $prefixKey, $prefix ) = $prefixList;
177 list( /* $fromNS */, $fromKey, ) = $fromList;
179 # ## @todo FIXME: Should complain if $fromNs != $namespace
181 $dbr = wfGetDB( DB_SLAVE );
183 $conds = array(
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' ),
195 $conds,
196 __METHOD__,
197 array(
198 'ORDER BY' => 'page_title',
199 'LIMIT' => $this->maxPerPage + 1,
200 'USE INDEX' => 'name_title',
204 // @todo FIXME: Side link to previous
206 $n = 0;
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 );
213 if ( $t ) {
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">' : '' ) .
220 Linker::linkKnown(
222 htmlspecialchars( $displayed ),
223 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
225 ( $s->page_is_redirect ? '</div>' : '' );
226 } else {
227 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
230 $out .= "<li>$link</li>\n";
231 $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 );
241 } else {
242 $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 );
252 return;
255 $topOut = $this->namespacePrefixForm( $namespace, $prefix );
257 if ( $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
258 $query = array(
259 'from' => $s->page_title,
260 'prefix' => $prefix,
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(),
274 array(),
275 $query
278 // Link shown at the top of the page below the form
279 $topOut .= Html::rawElement( 'div',
280 array( 'class' => 'mw-prefixindex-nav' ),
281 $nextLink
284 // Link shown at the footer
285 $out .= "\n" . Html::element( 'hr' ) .
286 Html::rawElement(
287 'div',
288 array( 'class' => 'mw-prefixindex-nav' ),
289 $nextLink
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
309 return array();
311 // Autocomplete subpage the same as a normal search
312 $prefixSearcher = new StringPrefixSearch;
313 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
314 return $result;
317 protected function getGroupName() {
318 return 'pages';