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