Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[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 'id' => 'nsfrom',
106 'type' => 'text',
107 'size' => '30',
108 'default' => str_replace( '_', ' ', $from ),
109 ],
110 'namespace' => [
111 'type' => 'namespaceselect',
112 'name' => 'namespace',
113 'id' => 'namespace',
114 'label-message' => 'namespace',
115 'all' => null,
116 'default' => $namespace,
117 ],
118 'hidedirects' => [
119 'class' => 'HTMLCheckField',
120 'name' => 'hideredirects',
121 'label-message' => 'allpages-hide-redirects',
122 ],
123 'stripprefix' => [
124 'class' => 'HTMLCheckField',
125 'name' => 'stripprefix',
126 'label-message' => 'prefixindex-strip',
127 ],
128 ];
129 $context = new DerivativeContext( $this->getContext() );
130 $context->setTitle( $this->getPageTitle() ); // Remove subpage
131 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $context );
132 $htmlForm
133 ->setMethod( 'get' )
134 ->setWrapperLegendMsg( 'prefixindex' )
135 ->setSubmitTextMsg( 'prefixindex-submit' );
136
137 return $htmlForm->prepareForm()->getHTML( false );
138 }
139
140 /**
141 * @param int $namespace Default NS_MAIN
142 * @param string $prefix
143 * @param string $from List all pages from this name (default false)
144 */
145 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
146 global $wgContLang;
147
148 if ( $from === null ) {
149 $from = $prefix;
150 }
151
152 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
153 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
154 $namespaces = $wgContLang->getNamespaces();
155 $res = null;
156 $n = 0;
157 $nextRow = null;
158
159 if ( !$prefixList || !$fromList ) {
160 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
161 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
162 // Show errormessage and reset to NS_MAIN
163 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
164 $namespace = NS_MAIN;
165 } else {
166 list( $namespace, $prefixKey, $prefix ) = $prefixList;
167 list( /* $fromNS */, $fromKey, ) = $fromList;
168
169 # ## @todo FIXME: Should complain if $fromNs != $namespace
170
171 $dbr = wfGetDB( DB_REPLICA );
172
173 $conds = [
174 'page_namespace' => $namespace,
175 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
176 'page_title >= ' . $dbr->addQuotes( $fromKey ),
177 ];
178
179 if ( $this->hideRedirects ) {
180 $conds['page_is_redirect'] = 0;
181 }
182
183 $res = $dbr->select( 'page',
184 array_merge(
185 [ 'page_namespace', 'page_title' ],
186 LinkCache::getSelectFields()
187 ),
188 $conds,
189 __METHOD__,
190 [
191 'ORDER BY' => 'page_title',
192 'LIMIT' => $this->maxPerPage + 1,
193 'USE INDEX' => 'name_title',
194 ]
195 );
196
197 // @todo FIXME: Side link to previous
198
199 if ( $res->numRows() > 0 ) {
200 $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
201 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
202
203 $prefixLength = strlen( $prefix );
204 foreach ( $res as $row ) {
205 if ( $n >= $this->maxPerPage ) {
206 $nextRow = $row;
207 break;
208 }
209 $title = Title::newFromRow( $row );
210 // Make sure it gets into LinkCache
211 $linkCache->addGoodLinkObjFromRow( $title, $row );
212 $displayed = $title->getText();
213 // Try not to generate unclickable links
214 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
215 $displayed = substr( $displayed, $prefixLength );
216 }
217 $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
218 $this->getLinkRenderer()->makeKnownLink(
219 $title,
220 $displayed
221 ) .
222 ( $title->isRedirect() ? '</div>' : '' );
223
224 $out .= "<li>$link</li>\n";
225 $n++;
226
227 }
228 $out .= Html::closeElement( 'ul' );
229
230 if ( $res->numRows() > 2 ) {
231 // Only apply CSS column styles if there's more than 2 entries.
232 // Otherwise rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
233 $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
234 }
235 } else {
236 $out = '';
237 }
238 }
239
240 $output = $this->getOutput();
241
242 if ( $this->including() ) {
243 // We don't show the nav-links and the form when included into other
244 // pages so let's just finish here.
245 $output->addHTML( $out );
246 return;
247 }
248
249 $topOut = $this->namespacePrefixForm( $namespace, $prefix );
250
251 if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
252 $query = [
253 'from' => $nextRow->page_title,
254 'prefix' => $prefix,
255 'hideredirects' => $this->hideRedirects,
256 'stripprefix' => $this->stripPrefix,
257 ];
258
259 if ( $namespace || $prefix == '' ) {
260 // Keep the namespace even if it's 0 for empty prefixes.
261 // This tells us we're not just a holdover from old links.
262 $query['namespace'] = $namespace;
263 }
264
265 $nextLink = $this->getLinkRenderer()->makeKnownLink(
266 $this->getPageTitle(),
267 $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(),
268 [],
269 $query
270 );
271
272 // Link shown at the top of the page below the form
273 $topOut .= Html::rawElement( 'div',
274 [ 'class' => 'mw-prefixindex-nav' ],
275 $nextLink
276 );
277
278 // Link shown at the footer
279 $out .= "\n" . Html::element( 'hr' ) .
280 Html::rawElement(
281 'div',
282 [ 'class' => 'mw-prefixindex-nav' ],
283 $nextLink
284 );
285
286 }
287
288 $output->addHTML( $topOut . $out );
289 }
290
291 /**
292 * Return an array of subpages beginning with $search that this special page will accept.
293 *
294 * @param string $search Prefix to search for
295 * @param int $limit Maximum number of results to return (usually 10)
296 * @param int $offset Number of results to skip (usually 0)
297 * @return string[] Matching subpages
298 */
299 public function prefixSearchSubpages( $search, $limit, $offset ) {
300 return $this->prefixSearchString( $search, $limit, $offset );
301 }
302
303 protected function getGroupName() {
304 return 'pages';
305 }
306 }