Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / specials / SpecialPrefixindex.php
1 <?php
2 /**
3 * Implements Special:Prefixindex
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Implements Special:Prefixindex
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialPrefixindex extends SpecialAllPages {
31
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.
35 */
36 protected $stripPrefix = false;
37
38 protected $hideRedirects = false;
39
40 // Inherit $maxPerPage
41
42 function __construct() {
43 parent::__construct( 'Prefixindex' );
44 }
45
46 /**
47 * Entry point : initialise variables and call subfunctions.
48 * @param string $par Becomes "FOO" when called like Special:Prefixindex/FOO (default null)
49 */
50 function execute( $par ) {
51 global $wgContLang;
52
53 $this->setHeaders();
54 $this->outputHeader();
55
56 $out = $this->getOutput();
57 $out->addModuleStyles( 'mediawiki.special' );
58
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 );
67
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' )
73 );
74
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;
84 }
85
86 // T29864: 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 ) );
91 }
92 }
93
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
99 */
100 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
101 $formDescriptor = [
102 'prefix' => [
103 'label-message' => 'allpagesprefix',
104 'name' => 'prefix',
105 'type' => 'text',
106 'size' => '30',
107 ],
108 'namespace' => [
109 'type' => 'namespaceselect',
110 'name' => 'namespace',
111 'id' => 'namespace',
112 'label-message' => 'namespace',
113 'all' => null,
114 'value' => $namespace,
115 ],
116 'hidedirects' => [
117 'class' => 'HTMLCheckField',
118 'name' => 'hideredirects',
119 'label-message' => 'allpages-hide-redirects',
120 ],
121 'stripprefix' => [
122 'class' => 'HTMLCheckField',
123 'name' => 'stripprefix',
124 'label-message' => 'prefixindex-strip',
125 ],
126 ];
127 $htmlForm = new HTMLForm( $formDescriptor, $this->getContext() );
128 $htmlForm
129 ->setMethod( 'get' )
130 ->setWrapperLegendMsg( 'prefixindex' )
131 ->setSubmitTextMsg( 'prefixindex-submit' );
132
133 return $htmlForm->prepareForm()->getHTML( false );
134 }
135
136 /**
137 * @param int $namespace Default NS_MAIN
138 * @param string $prefix
139 * @param string $from List all pages from this name (default false)
140 */
141 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
142 global $wgContLang;
143
144 if ( $from === null ) {
145 $from = $prefix;
146 }
147
148 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
149 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
150 $namespaces = $wgContLang->getNamespaces();
151 $res = null;
152 $n = 0;
153 $nextRow = null;
154
155 if ( !$prefixList || !$fromList ) {
156 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
157 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
158 // Show errormessage and reset to NS_MAIN
159 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
160 $namespace = NS_MAIN;
161 } else {
162 list( $namespace, $prefixKey, $prefix ) = $prefixList;
163 list( /* $fromNS */, $fromKey, ) = $fromList;
164
165 # ## @todo FIXME: Should complain if $fromNs != $namespace
166
167 $dbr = wfGetDB( DB_REPLICA );
168
169 $conds = [
170 'page_namespace' => $namespace,
171 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
172 'page_title >= ' . $dbr->addQuotes( $fromKey ),
173 ];
174
175 if ( $this->hideRedirects ) {
176 $conds['page_is_redirect'] = 0;
177 }
178
179 $res = $dbr->select( 'page',
180 array_merge(
181 [ 'page_namespace', 'page_title' ],
182 LinkCache::getSelectFields()
183 ),
184 $conds,
185 __METHOD__,
186 [
187 'ORDER BY' => 'page_title',
188 'LIMIT' => $this->maxPerPage + 1,
189 'USE INDEX' => 'name_title',
190 ]
191 );
192
193 // @todo FIXME: Side link to previous
194
195 if ( $res->numRows() > 0 ) {
196 $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
197 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
198
199 $prefixLength = strlen( $prefix );
200 foreach ( $res as $row ) {
201 if ( $n >= $this->maxPerPage ) {
202 $nextRow = $row;
203 break;
204 }
205 $title = Title::newFromRow( $row );
206 // Make sure it gets into LinkCache
207 $linkCache->addGoodLinkObjFromRow( $title, $row );
208 $displayed = $title->getText();
209 // Try not to generate unclickable links
210 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
211 $displayed = substr( $displayed, $prefixLength );
212 }
213 $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
214 $this->getLinkRenderer()->makeKnownLink(
215 $title,
216 $displayed
217 ) .
218 ( $title->isRedirect() ? '</div>' : '' );
219
220 $out .= "<li>$link</li>\n";
221 $n++;
222
223 }
224 $out .= Html::closeElement( 'ul' );
225
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-prefixindex-body"'s CSS column count is 3.
229 $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
230 }
231 } else {
232 $out = '';
233 }
234 }
235
236 $output = $this->getOutput();
237
238 if ( $this->including() ) {
239 // We don't show the nav-links and the form when included into other
240 // pages so let's just finish here.
241 $output->addHTML( $out );
242 return;
243 }
244
245 $topOut = $this->namespacePrefixForm( $namespace, $prefix );
246
247 if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
248 $query = [
249 'from' => $nextRow->page_title,
250 'prefix' => $prefix,
251 'hideredirects' => $this->hideRedirects,
252 'stripprefix' => $this->stripPrefix,
253 ];
254
255 if ( $namespace || $prefix == '' ) {
256 // Keep the namespace even if it's 0 for empty prefixes.
257 // This tells us we're not just a holdover from old links.
258 $query['namespace'] = $namespace;
259 }
260
261 $nextLink = $this->getLinkRenderer()->makeKnownLink(
262 $this->getPageTitle(),
263 $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(),
264 [],
265 $query
266 );
267
268 // Link shown at the top of the page below the form
269 $topOut .= Html::rawElement( 'div',
270 [ 'class' => 'mw-prefixindex-nav' ],
271 $nextLink
272 );
273
274 // Link shown at the footer
275 $out .= "\n" . Html::element( 'hr' ) .
276 Html::rawElement(
277 'div',
278 [ 'class' => 'mw-prefixindex-nav' ],
279 $nextLink
280 );
281
282 }
283
284 $output->addHTML( $topOut . $out );
285 }
286
287 /**
288 * Return an array of subpages beginning with $search that this special page will accept.
289 *
290 * @param string $search Prefix to search for
291 * @param int $limit Maximum number of results to return (usually 10)
292 * @param int $offset Number of results to skip (usually 0)
293 * @return string[] Matching subpages
294 */
295 public function prefixSearchSubpages( $search, $limit, $offset ) {
296 return $this->prefixSearchString( $search, $limit, $offset );
297 }
298
299 protected function getGroupName() {
300 return 'pages';
301 }
302 }