Add columns option to Special:PrefixIndex
[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 && in_array( $namespace, array_keys( $namespaces ) ) )
74 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
75 : $this->msg( 'prefixindex' )
76 );
77
78 $showme = '';
79 if ( isset( $par ) ) {
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 $namespace Integer: 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 global $wgScript;
105
106 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
107 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
108 $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
109 $out .= Xml::openElement( 'fieldset' );
110 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
111 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
112 $out .= "<tr>
113 <td class='mw-label'>" .
114 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
115 "</td>
116 <td class='mw-input'>" .
117 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
118 "</td>
119 </tr>
120 <tr>
121 <td class='mw-label'>" .
122 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
123 "</td>
124 <td class='mw-input'>" .
125 Html::namespaceSelector( array(
126 'selected' => $namespace,
127 ), array(
128 'name' => 'namespace',
129 'id' => 'namespace',
130 'class' => 'namespaceselector',
131 ) ) .
132 Xml::checkLabel(
133 $this->msg( 'allpages-hide-redirects' )->text(),
134 'hideredirects',
135 'hideredirects',
136 $this->hideRedirects
137 ) . ' ' .
138 Xml::checkLabel(
139 $this->msg( 'prefixindex-strip' )->text(),
140 'stripprefix',
141 'stripprefix',
142 $this->stripPrefix
143 ) . ' ' .
144 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
145 "</td>
146 </tr>";
147 $out .= Xml::closeElement( 'table' );
148 $out .= Xml::closeElement( 'fieldset' );
149 $out .= Xml::closeElement( 'form' );
150 $out .= Xml::closeElement( 'div' );
151
152 return $out;
153 }
154
155 /**
156 * @param $namespace Integer, default NS_MAIN
157 * @param $prefix String
158 * @param string $from list all pages from this name (default FALSE)
159 */
160 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
161 global $wgContLang;
162
163 if ( $from === null ) {
164 $from = $prefix;
165 }
166
167 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
168 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
169 $namespaces = $wgContLang->getNamespaces();
170
171 if ( !$prefixList || !$fromList ) {
172 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
173 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
174 // Show errormessage and reset to NS_MAIN
175 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
176 $namespace = NS_MAIN;
177 } else {
178 list( $namespace, $prefixKey, $prefix ) = $prefixList;
179 list( /* $fromNS */, $fromKey, ) = $fromList;
180
181 ### @todo FIXME: Should complain if $fromNs != $namespace
182
183 $dbr = wfGetDB( DB_SLAVE );
184
185 $conds = array(
186 'page_namespace' => $namespace,
187 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
188 'page_title >= ' . $dbr->addQuotes( $fromKey ),
189 );
190
191 if ( $this->hideRedirects ) {
192 $conds['page_is_redirect'] = 0;
193 }
194
195 $res = $dbr->select( 'page',
196 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
197 $conds,
198 __METHOD__,
199 array(
200 'ORDER BY' => 'page_title',
201 'LIMIT' => $this->maxPerPage + 1,
202 'USE INDEX' => 'name_title',
203 )
204 );
205
206 ### @todo FIXME: Side link to previous
207
208 $n = 0;
209 if ( $res->numRows() > 0 ) {
210 $out = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-list-table' ) );
211
212 $prefixLength = strlen( $prefix );
213 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
214 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
215 if ( $t ) {
216 $displayed = $t->getText();
217 // Try not to generate unclickable links
218 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
219 $displayed = substr( $displayed, $prefixLength );
220 }
221 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
222 Linker::linkKnown(
223 $t,
224 htmlspecialchars( $displayed ),
225 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
226 ) .
227 ( $s->page_is_redirect ? '</div>' : '' );
228 } else {
229 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
230 }
231 if ( $n % $this->columns == 0 ) {
232 $out .= '<tr>';
233 }
234 $out .= "<td>$link</td>";
235 $n++;
236 if ( $n % $this->columns == 0 ) {
237 $out .= '</tr>';
238 }
239 }
240
241 if ( $n % $this->columns != 0 ) {
242 $out .= '</tr>';
243 }
244
245 $out .= Xml::closeElement( 'table' );
246 } else {
247 $out = '';
248 }
249 }
250
251 $footer = '';
252 if ( $this->including() ) {
253 $out2 = '';
254 } else {
255 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
256 $self = $this->getPageTitle();
257 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
258 '<tr>
259 <td>' .
260 $nsForm .
261 '</td>
262 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
263
264 if ( isset( $res ) && $res && ( $n == $this->maxPerPage ) &&
265 ( $s = $res->fetchObject() )
266 ) {
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 }