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