Switch some HTMLForms in special pages to OOUI
[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
24 /**
25 * Implements Special:Prefixindex
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialPrefixindex extends SpecialAllPages {
30
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.
34 */
35 protected $stripPrefix = false;
36
37 protected $hideRedirects = false;
38
39 // number of columns in output table
40 protected $columns = 3;
41
42 // Inherit $maxPerPage
43
44 function __construct() {
45 parent::__construct( 'Prefixindex' );
46 }
47
48 /**
49 * Entry point : initialise variables and call subfunctions.
50 * @param string $par Becomes "FOO" when called like Special:Prefixindex/FOO (default null)
51 */
52 function execute( $par ) {
53 global $wgContLang;
54
55 $this->setHeaders();
56 $this->outputHeader();
57
58 $out = $this->getOutput();
59 $out->addModuleStyles( 'mediawiki.special' );
60
61 # GET values
62 $request = $this->getRequest();
63 $from = $request->getVal( 'from', '' );
64 $prefix = $request->getVal( 'prefix', '' );
65 $ns = $request->getIntOrNull( 'namespace' );
66 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
67 $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
68 $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
69 $this->columns = $request->getInt( 'columns', $this->columns );
70
71 $namespaces = $wgContLang->getNamespaces();
72 $out->setPageTitle(
73 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
74 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
75 : $this->msg( 'prefixindex' )
76 );
77
78 $showme = '';
79 if ( $par !== null ) {
80 $showme = $par;
81 } elseif ( $prefix != '' ) {
82 $showme = $prefix;
83 } elseif ( $from != '' && $ns === null ) {
84 // For back-compat with Special:Allpages
85 // Don't do this if namespace is passed, so paging works when doing NS views.
86 $showme = $from;
87 }
88
89 // Bug 27864: if transcluded, show all pages instead of the form.
90 if ( $this->including() || $showme != '' || $ns !== null ) {
91 $this->showPrefixChunk( $namespace, $showme, $from );
92 } else {
93 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
94 }
95 }
96
97 /**
98 * HTML for the top form
99 * @param int $namespace A namespace constant (default NS_MAIN).
100 * @param string $from DbKey we are starting listing at.
101 * @return string
102 */
103 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
104 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml::openElement(
106 'form',
107 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
108 );
109 $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
110 $out .= Xml::openElement( 'fieldset' );
111 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
112 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
113 $out .= "<tr>
114 <td class='mw-label'>" .
115 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
116 "</td>
117 <td class='mw-input'>" .
118 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
119 "</td>
120 </tr>
121 <tr>
122 <td class='mw-label'>" .
123 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
124 "</td>
125 <td class='mw-input'>" .
126 Html::namespaceSelector( array(
127 'selected' => $namespace,
128 ), array(
129 'name' => 'namespace',
130 'id' => 'namespace',
131 'class' => 'namespaceselector',
132 ) ) .
133 Xml::checkLabel(
134 $this->msg( 'allpages-hide-redirects' )->text(),
135 'hideredirects',
136 'hideredirects',
137 $this->hideRedirects
138 ) . ' ' .
139 Xml::checkLabel(
140 $this->msg( 'prefixindex-strip' )->text(),
141 'stripprefix',
142 'stripprefix',
143 $this->stripPrefix
144 ) . ' ' .
145 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
146 "</td>
147 </tr>";
148 $out .= Xml::closeElement( 'table' );
149 $out .= Xml::closeElement( 'fieldset' );
150 $out .= Xml::closeElement( 'form' );
151 $out .= Xml::closeElement( 'div' );
152
153 return $out;
154 }
155
156 /**
157 * @param int $namespace Default NS_MAIN
158 * @param string $prefix
159 * @param string $from List all pages from this name (default false)
160 */
161 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
162 global $wgContLang;
163
164 if ( $from === null ) {
165 $from = $prefix;
166 }
167
168 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
169 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
170 $namespaces = $wgContLang->getNamespaces();
171 $res = null;
172
173 if ( !$prefixList || !$fromList ) {
174 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
175 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
176 // Show errormessage and reset to NS_MAIN
177 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
178 $namespace = NS_MAIN;
179 } else {
180 list( $namespace, $prefixKey, $prefix ) = $prefixList;
181 list( /* $fromNS */, $fromKey, ) = $fromList;
182
183 ### @todo FIXME: Should complain if $fromNs != $namespace
184
185 $dbr = wfGetDB( DB_SLAVE );
186
187 $conds = array(
188 'page_namespace' => $namespace,
189 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
190 'page_title >= ' . $dbr->addQuotes( $fromKey ),
191 );
192
193 if ( $this->hideRedirects ) {
194 $conds['page_is_redirect'] = 0;
195 }
196
197 $res = $dbr->select( 'page',
198 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
199 $conds,
200 __METHOD__,
201 array(
202 'ORDER BY' => 'page_title',
203 'LIMIT' => $this->maxPerPage + 1,
204 'USE INDEX' => 'name_title',
205 )
206 );
207
208 ### @todo FIXME: Side link to previous
209
210 $n = 0;
211 if ( $res->numRows() > 0 ) {
212 $out = Xml::openElement( 'table', array( 'class' => 'mw-prefixindex-list-table' ) );
213
214 $prefixLength = strlen( $prefix );
215 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
216 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
217 if ( $t ) {
218 $displayed = $t->getText();
219 // Try not to generate unclickable links
220 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
221 $displayed = substr( $displayed, $prefixLength );
222 }
223 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
224 Linker::linkKnown(
225 $t,
226 htmlspecialchars( $displayed ),
227 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
228 ) .
229 ( $s->page_is_redirect ? '</div>' : '' );
230 } else {
231 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
232 }
233 if ( $n % $this->columns == 0 ) {
234 $out .= '<tr>';
235 }
236 $out .= "<td>$link</td>";
237 $n++;
238 if ( $n % $this->columns == 0 ) {
239 $out .= '</tr>';
240 }
241 }
242
243 if ( $n % $this->columns != 0 ) {
244 $out .= '</tr>';
245 }
246
247 $out .= Xml::closeElement( 'table' );
248 } else {
249 $out = '';
250 }
251 }
252
253 $footer = '';
254 if ( $this->including() ) {
255 $out2 = '';
256 } else {
257 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
258 $self = $this->getPageTitle();
259 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
260 '<tr>
261 <td>' .
262 $nsForm .
263 '</td>
264 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
265
266 if ( $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
267 $query = array(
268 'from' => $s->page_title,
269 'prefix' => $prefix,
270 'hideredirects' => $this->hideRedirects,
271 'stripprefix' => $this->stripPrefix,
272 'columns' => $this->columns,
273 );
274
275 if ( $namespace || $prefix == '' ) {
276 // Keep the namespace even if it's 0 for empty prefixes.
277 // This tells us we're not just a holdover from old links.
278 $query['namespace'] = $namespace;
279 }
280
281 $nextLink = Linker::linkKnown(
282 $self,
283 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
284 array(),
285 $query
286 );
287
288 $out2 .= $nextLink;
289
290 $footer = "\n" . Html::element( 'hr' ) .
291 Html::rawElement(
292 'div',
293 array( 'class' => 'mw-prefixindex-nav' ),
294 $nextLink
295 );
296 }
297 $out2 .= "</td></tr>" .
298 Xml::closeElement( 'table' );
299 }
300
301 $this->getOutput()->addHTML( $out2 . $out . $footer );
302 }
303
304 protected function getGroupName() {
305 return 'pages';
306 }
307 }