Merge "Revert "Toolbar: Only show on WikiText pages""
[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( 'form', array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) ) );
106 $out .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
107 $out .= Xml::openElement( 'fieldset' );
108 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
109 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 $out .= "<tr>
111 <td class='mw-label'>" .
112 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
113 "</td>
114 <td class='mw-input'>" .
115 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
116 "</td>
117 </tr>
118 <tr>
119 <td class='mw-label'>" .
120 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
121 "</td>
122 <td class='mw-input'>" .
123 Html::namespaceSelector( array(
124 'selected' => $namespace,
125 ), array(
126 'name' => 'namespace',
127 'id' => 'namespace',
128 'class' => 'namespaceselector',
129 ) ) .
130 Xml::checkLabel(
131 $this->msg( 'allpages-hide-redirects' )->text(),
132 'hideredirects',
133 'hideredirects',
134 $this->hideRedirects
135 ) . ' ' .
136 Xml::checkLabel(
137 $this->msg( 'prefixindex-strip' )->text(),
138 'stripprefix',
139 'stripprefix',
140 $this->stripPrefix
141 ) . ' ' .
142 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
143 "</td>
144 </tr>";
145 $out .= Xml::closeElement( 'table' );
146 $out .= Xml::closeElement( 'fieldset' );
147 $out .= Xml::closeElement( 'form' );
148 $out .= Xml::closeElement( 'div' );
149
150 return $out;
151 }
152
153 /**
154 * @param int $namespace Default NS_MAIN
155 * @param string $prefix
156 * @param string $from List all pages from this name (default false)
157 */
158 protected function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
159 global $wgContLang;
160
161 if ( $from === null ) {
162 $from = $prefix;
163 }
164
165 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
166 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
167 $namespaces = $wgContLang->getNamespaces();
168 $res = null;
169
170 if ( !$prefixList || !$fromList ) {
171 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
172 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
173 // Show errormessage and reset to NS_MAIN
174 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
175 $namespace = NS_MAIN;
176 } else {
177 list( $namespace, $prefixKey, $prefix ) = $prefixList;
178 list( /* $fromNS */, $fromKey, ) = $fromList;
179
180 ### @todo FIXME: Should complain if $fromNs != $namespace
181
182 $dbr = wfGetDB( DB_SLAVE );
183
184 $conds = array(
185 'page_namespace' => $namespace,
186 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
187 'page_title >= ' . $dbr->addQuotes( $fromKey ),
188 );
189
190 if ( $this->hideRedirects ) {
191 $conds['page_is_redirect'] = 0;
192 }
193
194 $res = $dbr->select( 'page',
195 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
196 $conds,
197 __METHOD__,
198 array(
199 'ORDER BY' => 'page_title',
200 'LIMIT' => $this->maxPerPage + 1,
201 'USE INDEX' => 'name_title',
202 )
203 );
204
205 ### @todo FIXME: Side link to previous
206
207 $n = 0;
208 if ( $res->numRows() > 0 ) {
209 $out = Xml::openElement( 'table', array( 'class' => 'mw-prefixindex-list-table' ) );
210
211 $prefixLength = strlen( $prefix );
212 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
213 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
214 if ( $t ) {
215 $displayed = $t->getText();
216 // Try not to generate unclickable links
217 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
218 $displayed = substr( $displayed, $prefixLength );
219 }
220 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
221 Linker::linkKnown(
222 $t,
223 htmlspecialchars( $displayed ),
224 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
225 ) .
226 ( $s->page_is_redirect ? '</div>' : '' );
227 } else {
228 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
229 }
230 if ( $n % $this->columns == 0 ) {
231 $out .= '<tr>';
232 }
233 $out .= "<td>$link</td>";
234 $n++;
235 if ( $n % $this->columns == 0 ) {
236 $out .= '</tr>';
237 }
238 }
239
240 if ( $n % $this->columns != 0 ) {
241 $out .= '</tr>';
242 }
243
244 $out .= Xml::closeElement( 'table' );
245 } else {
246 $out = '';
247 }
248 }
249
250 $footer = '';
251 if ( $this->including() ) {
252 $out2 = '';
253 } else {
254 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
255 $self = $this->getPageTitle();
256 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
257 '<tr>
258 <td>' .
259 $nsForm .
260 '</td>
261 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
262
263 if ( $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
264 $query = array(
265 'from' => $s->page_title,
266 'prefix' => $prefix,
267 'hideredirects' => $this->hideRedirects,
268 'stripprefix' => $this->stripPrefix,
269 'columns' => $this->columns,
270 );
271
272 if ( $namespace || $prefix == '' ) {
273 // Keep the namespace even if it's 0 for empty prefixes.
274 // This tells us we're not just a holdover from old links.
275 $query['namespace'] = $namespace;
276 }
277
278 $nextLink = Linker::linkKnown(
279 $self,
280 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
281 array(),
282 $query
283 );
284
285 $out2 .= $nextLink;
286
287 $footer = "\n" . Html::element( 'hr' ) .
288 Html::rawElement(
289 'div',
290 array( 'class' => 'mw-prefixindex-nav' ),
291 $nextLink
292 );
293 }
294 $out2 .= "</td></tr>" .
295 Xml::closeElement( 'table' );
296 }
297
298 $this->getOutput()->addHTML( $out2 . $out . $footer );
299 }
300
301 protected function getGroupName() {
302 return 'pages';
303 }
304 }