API: Some raw message cleanup
[mediawiki.git] / includes / specials / SpecialPrefixindex.php
blob87a5b27be099dc48450acc235aedd56984ff7159
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
23 use MediaWiki\MediaWikiServices;
25 /**
26 * Implements Special:Prefixindex
28 * @ingroup SpecialPage
30 class SpecialPrefixindex extends SpecialAllPages {
32 /**
33 * Whether to remove the searched prefix from the displayed link. Useful
34 * for inclusion of a set of sub pages in a root page.
36 protected $stripPrefix = false;
38 protected $hideRedirects = false;
40 // Inherit $maxPerPage
42 function __construct() {
43 parent::__construct( 'Prefixindex' );
46 /**
47 * Entry point : initialise variables and call subfunctions.
48 * @param string $par Becomes "FOO" when called like Special:Prefixindex/FOO (default null)
50 function execute( $par ) {
51 global $wgContLang;
53 $this->setHeaders();
54 $this->outputHeader();
56 $out = $this->getOutput();
57 $out->addModuleStyles( 'mediawiki.special' );
59 # GET values
60 $request = $this->getRequest();
61 $from = $request->getVal( 'from', '' );
62 $prefix = $request->getVal( 'prefix', '' );
63 $ns = $request->getIntOrNull( 'namespace' );
64 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
65 $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
66 $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
68 $namespaces = $wgContLang->getNamespaces();
69 $out->setPageTitle(
70 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
71 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
72 : $this->msg( 'prefixindex' )
75 $showme = '';
76 if ( $par !== null ) {
77 $showme = $par;
78 } elseif ( $prefix != '' ) {
79 $showme = $prefix;
80 } elseif ( $from != '' && $ns === null ) {
81 // For back-compat with Special:Allpages
82 // Don't do this if namespace is passed, so paging works when doing NS views.
83 $showme = $from;
86 // Bug 27864: if transcluded, show all pages instead of the form.
87 if ( $this->including() || $showme != '' || $ns !== null ) {
88 $this->showPrefixChunk( $namespace, $showme, $from );
89 } else {
90 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
94 /**
95 * HTML for the top form
96 * @param int $namespace A namespace constant (default NS_MAIN).
97 * @param string $from DbKey we are starting listing at.
98 * @return string
100 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
101 $out = Xml::openElement( 'div', [ 'class' => 'namespaceoptions' ] );
102 $out .= Xml::openElement(
103 'form',
104 [ 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) ]
106 $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
107 $out .= Xml::openElement( 'fieldset' );
108 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
109 $out .= Xml::openElement( 'table', [ 'id' => 'nsselect', 'class' => 'allpages' ] );
110 $out .= "<tr>
111 <td class='mw-label'>" .
112 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
113 "</td>
114 <td class='mw-input'>" .
115 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), [ 'id' => 'nsfrom' ] ) .
116 "</td>
117 </tr>
118 <tr>
119 <td class='mw-label'>" .
120 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
121 "</td>
122 <td class='mw-input'>" .
123 Html::namespaceSelector( [
124 'selected' => $namespace,
125 ], [
126 'name' => 'namespace',
127 'id' => 'namespace',
128 'class' => 'namespaceselector',
129 ] ) .
130 Xml::checkLabel(
131 $this->msg( 'allpages-hide-redirects' )->text(),
132 'hideredirects',
133 'hideredirects',
134 $this->hideRedirects
135 ) . ' ' .
136 Xml::checkLabel(
137 $this->msg( 'prefixindex-strip' )->text(),
138 'stripprefix',
139 'stripprefix',
140 $this->stripPrefix
141 ) . ' ' .
142 Xml::submitButton( $this->msg( 'prefixindex-submit' )->text() ) .
143 "</td>
144 </tr>";
145 $out .= Xml::closeElement( 'table' );
146 $out .= Xml::closeElement( 'fieldset' );
147 $out .= Xml::closeElement( 'form' );
148 $out .= Xml::closeElement( 'div' );
150 return $out;
154 * @param int $namespace Default NS_MAIN
155 * @param string $prefix
156 * @param string $from List all pages from this name (default false)
158 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
159 global $wgContLang;
161 if ( $from === null ) {
162 $from = $prefix;
165 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
166 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
167 $namespaces = $wgContLang->getNamespaces();
168 $res = null;
169 $n = 0;
170 $nextRow = null;
172 if ( !$prefixList || !$fromList ) {
173 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
174 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
175 // Show errormessage and reset to NS_MAIN
176 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
177 $namespace = NS_MAIN;
178 } else {
179 list( $namespace, $prefixKey, $prefix ) = $prefixList;
180 list( /* $fromNS */, $fromKey, ) = $fromList;
182 # ## @todo FIXME: Should complain if $fromNs != $namespace
184 $dbr = wfGetDB( DB_SLAVE );
186 $conds = [
187 'page_namespace' => $namespace,
188 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
189 'page_title >= ' . $dbr->addQuotes( $fromKey ),
192 if ( $this->hideRedirects ) {
193 $conds['page_is_redirect'] = 0;
196 $res = $dbr->select( 'page',
197 array_merge(
198 [ 'page_namespace', 'page_title' ],
199 LinkCache::getSelectFields()
201 $conds,
202 __METHOD__,
204 'ORDER BY' => 'page_title',
205 'LIMIT' => $this->maxPerPage + 1,
206 'USE INDEX' => 'name_title',
210 // @todo FIXME: Side link to previous
212 if ( $res->numRows() > 0 ) {
213 $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
214 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
216 $prefixLength = strlen( $prefix );
217 foreach ( $res as $row ) {
218 if ( $n >= $this->maxPerPage ) {
219 $nextRow = $row;
220 break;
222 $title = Title::newFromRow( $row );
223 // Make sure it gets into LinkCache
224 $linkCache->addGoodLinkObjFromRow( $title, $row );
225 $displayed = $title->getText();
226 // Try not to generate unclickable links
227 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
228 $displayed = substr( $displayed, $prefixLength );
230 $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
231 Linker::linkKnown(
232 $title,
233 htmlspecialchars( $displayed )
235 ( $title->isRedirect() ? '</div>' : '' );
237 $out .= "<li>$link</li>\n";
238 $n++;
241 $out .= Html::closeElement( 'ul' );
243 if ( $res->numRows() > 2 ) {
244 // Only apply CSS column styles if there's more than 2 entries.
245 // Otherwise rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
246 $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
248 } else {
249 $out = '';
253 $output = $this->getOutput();
255 if ( $this->including() ) {
256 // We don't show the nav-links and the form when included into other
257 // pages so let's just finish here.
258 $output->addHTML( $out );
259 return;
262 $topOut = $this->namespacePrefixForm( $namespace, $prefix );
264 if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
265 $query = [
266 'from' => $nextRow->page_title,
267 'prefix' => $prefix,
268 'hideredirects' => $this->hideRedirects,
269 'stripprefix' => $this->stripPrefix,
272 if ( $namespace || $prefix == '' ) {
273 // Keep the namespace even if it's 0 for empty prefixes.
274 // This tells us we're not just a holdover from old links.
275 $query['namespace'] = $namespace;
278 $nextLink = Linker::linkKnown(
279 $this->getPageTitle(),
280 $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->escaped(),
282 $query
285 // Link shown at the top of the page below the form
286 $topOut .= Html::rawElement( 'div',
287 [ 'class' => 'mw-prefixindex-nav' ],
288 $nextLink
291 // Link shown at the footer
292 $out .= "\n" . Html::element( 'hr' ) .
293 Html::rawElement(
294 'div',
295 [ 'class' => 'mw-prefixindex-nav' ],
296 $nextLink
301 $output->addHTML( $topOut . $out );
305 * Return an array of subpages beginning with $search that this special page will accept.
307 * @param string $search Prefix to search for
308 * @param int $limit Maximum number of results to return (usually 10)
309 * @param int $offset Number of results to skip (usually 0)
310 * @return string[] Matching subpages
312 public function prefixSearchSubpages( $search, $limit, $offset ) {
313 return $this->prefixSearchString( $search, $limit, $offset );
316 protected function getGroupName() {
317 return 'pages';