Replace in_array( array_keys( ... ) ) with array_key_exists( ... )
[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 // Inherit $maxPerPage
40
41 function __construct() {
42 parent::__construct( 'Prefixindex' );
43 }
44
45 /**
46 * Entry point : initialise variables and call subfunctions.
47 * @param string $par becomes "FOO" when called like Special:Prefixindex/FOO (default null)
48 */
49 function execute( $par ) {
50 global $wgContLang;
51
52 $this->setHeaders();
53 $this->outputHeader();
54
55 $out = $this->getOutput();
56 $out->addModuleStyles( 'mediawiki.special' );
57
58 # GET values
59 $request = $this->getRequest();
60 $from = $request->getVal( 'from', '' );
61 $prefix = $request->getVal( 'prefix', '' );
62 $ns = $request->getIntOrNull( 'namespace' );
63 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
64 $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
65 $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
66
67 $namespaces = $wgContLang->getNamespaces();
68 $out->setPageTitle(
69 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
70 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
71 : $this->msg( 'prefixindex' )
72 );
73
74 $showme = '';
75 if ( isset( $par ) ) {
76 $showme = $par;
77 } elseif ( $prefix != '' ) {
78 $showme = $prefix;
79 } elseif ( $from != '' && $ns === null ) {
80 // For back-compat with Special:Allpages
81 // Don't do this if namespace is passed, so paging works when doing NS views.
82 $showme = $from;
83 }
84
85 // Bug 27864: if transcluded, show all pages instead of the form.
86 if ( $this->including() || $showme != '' || $ns !== null ) {
87 $this->showPrefixChunk( $namespace, $showme, $from );
88 } else {
89 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
90 }
91 }
92
93 /**
94 * HTML for the top form
95 * @param $namespace Integer: a namespace constant (default NS_MAIN).
96 * @param string $from dbKey we are starting listing at.
97 * @return string
98 */
99 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
100 global $wgScript;
101
102 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
103 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
104 $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
105 $out .= Xml::openElement( 'fieldset' );
106 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
107 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
108 $out .= "<tr>
109 <td class='mw-label'>" .
110 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
111 "</td>
112 <td class='mw-input'>" .
113 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
114 "</td>
115 </tr>
116 <tr>
117 <td class='mw-label'>" .
118 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
119 "</td>
120 <td class='mw-input'>" .
121 Html::namespaceSelector( array(
122 'selected' => $namespace,
123 ), array(
124 'name' => 'namespace',
125 'id' => 'namespace',
126 'class' => 'namespaceselector',
127 ) ) .
128 Xml::checkLabel(
129 $this->msg( 'allpages-hide-redirects' )->text(),
130 'hideredirects',
131 'hideredirects',
132 $this->hideRedirects
133 ) . ' ' .
134 Xml::checkLabel(
135 $this->msg( 'prefixindex-strip' )->text(),
136 'stripprefix',
137 'stripprefix',
138 $this->stripPrefix
139 ) . ' ' .
140 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
141 "</td>
142 </tr>";
143 $out .= Xml::closeElement( 'table' );
144 $out .= Xml::closeElement( 'fieldset' );
145 $out .= Xml::closeElement( 'form' );
146 $out .= Xml::closeElement( 'div' );
147
148 return $out;
149 }
150
151 /**
152 * @param $namespace Integer, default NS_MAIN
153 * @param $prefix String
154 * @param string $from list all pages from this name (default FALSE)
155 */
156 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
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 ( !array_key_exists( $namespace, $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 ( $this->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( 'class' => '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 // Try not to generate unclickable links
214 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
215 $displayed = substr( $displayed, $prefixLength );
216 }
217 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
218 Linker::linkKnown(
219 $t,
220 htmlspecialchars( $displayed ),
221 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
222 ) .
223 ( $s->page_is_redirect ? '</div>' : '' );
224 } else {
225 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
226 }
227 if ( $n % 3 == 0 ) {
228 $out .= '<tr>';
229 }
230 $out .= "<td>$link</td>";
231 $n++;
232 if ( $n % 3 == 0 ) {
233 $out .= '</tr>';
234 }
235 }
236
237 if ( $n % 3 != 0 ) {
238 $out .= '</tr>';
239 }
240
241 $out .= Xml::closeElement( 'table' );
242 } else {
243 $out = '';
244 }
245 }
246
247 $footer = '';
248 if ( $this->including() ) {
249 $out2 = '';
250 } else {
251 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
252 $self = $this->getPageTitle();
253 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
254 '<tr>
255 <td>' .
256 $nsForm .
257 '</td>
258 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
259
260 if ( isset( $res ) && $res && ( $n == $this->maxPerPage ) &&
261 ( $s = $res->fetchObject() )
262 ) {
263 $query = array(
264 'from' => $s->page_title,
265 'prefix' => $prefix,
266 'hideredirects' => $this->hideRedirects,
267 'stripprefix' => $this->stripPrefix,
268 );
269
270 if ( $namespace || $prefix == '' ) {
271 // Keep the namespace even if it's 0 for empty prefixes.
272 // This tells us we're not just a holdover from old links.
273 $query['namespace'] = $namespace;
274 }
275
276 $nextLink = Linker::linkKnown(
277 $self,
278 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
279 array(),
280 $query
281 );
282
283 $out2 .= $nextLink;
284
285 $footer = "\n" . Html::element( 'hr' ) .
286 Html::rawElement(
287 'div',
288 array( 'class' => 'mw-prefixindex-nav' ),
289 $nextLink
290 );
291 }
292 $out2 .= "</td></tr>" .
293 Xml::closeElement( 'table' );
294 }
295
296 $this->getOutput()->addHTML( $out2 . $out . $footer );
297 }
298
299 protected function getGroupName() {
300 return 'pages';
301 }
302 }