Merge "Some improvements to Special:MergeHistory"
[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 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 int $namespace Default NS_MAIN
157 * @param string $prefix
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 $res = null;
171
172 if ( !$prefixList || !$fromList ) {
173 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
174 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
175 // Show errormessage and reset to NS_MAIN
176 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
177 $namespace = NS_MAIN;
178 } else {
179 list( $namespace, $prefixKey, $prefix ) = $prefixList;
180 list( /* $fromNS */, $fromKey, ) = $fromList;
181
182 ### @todo FIXME: Should complain if $fromNs != $namespace
183
184 $dbr = wfGetDB( DB_SLAVE );
185
186 $conds = array(
187 'page_namespace' => $namespace,
188 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
189 'page_title >= ' . $dbr->addQuotes( $fromKey ),
190 );
191
192 if ( $this->hideRedirects ) {
193 $conds['page_is_redirect'] = 0;
194 }
195
196 $res = $dbr->select( 'page',
197 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
198 $conds,
199 __METHOD__,
200 array(
201 'ORDER BY' => 'page_title',
202 'LIMIT' => $this->maxPerPage + 1,
203 'USE INDEX' => 'name_title',
204 )
205 );
206
207 ### @todo FIXME: Side link to previous
208
209 $n = 0;
210 if ( $res->numRows() > 0 ) {
211 $out = Xml::openElement( 'table', array( 'class' => 'mw-prefixindex-list-table' ) );
212
213 $prefixLength = strlen( $prefix );
214 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
215 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
216 if ( $t ) {
217 $displayed = $t->getText();
218 // Try not to generate unclickable links
219 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
220 $displayed = substr( $displayed, $prefixLength );
221 }
222 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
223 Linker::linkKnown(
224 $t,
225 htmlspecialchars( $displayed ),
226 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
227 ) .
228 ( $s->page_is_redirect ? '</div>' : '' );
229 } else {
230 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
231 }
232 if ( $n % $this->columns == 0 ) {
233 $out .= '<tr>';
234 }
235 $out .= "<td>$link</td>";
236 $n++;
237 if ( $n % $this->columns == 0 ) {
238 $out .= '</tr>';
239 }
240 }
241
242 if ( $n % $this->columns != 0 ) {
243 $out .= '</tr>';
244 }
245
246 $out .= Xml::closeElement( 'table' );
247 } else {
248 $out = '';
249 }
250 }
251
252 $footer = '';
253 if ( $this->including() ) {
254 $out2 = '';
255 } else {
256 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
257 $self = $this->getPageTitle();
258 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
259 '<tr>
260 <td>' .
261 $nsForm .
262 '</td>
263 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
264
265 if ( $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
266 $query = array(
267 'from' => $s->page_title,
268 'prefix' => $prefix,
269 'hideredirects' => $this->hideRedirects,
270 'stripprefix' => $this->stripPrefix,
271 'columns' => $this->columns,
272 );
273
274 if ( $namespace || $prefix == '' ) {
275 // Keep the namespace even if it's 0 for empty prefixes.
276 // This tells us we're not just a holdover from old links.
277 $query['namespace'] = $namespace;
278 }
279
280 $nextLink = Linker::linkKnown(
281 $self,
282 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
283 array(),
284 $query
285 );
286
287 $out2 .= $nextLink;
288
289 $footer = "\n" . Html::element( 'hr' ) .
290 Html::rawElement(
291 'div',
292 array( 'class' => 'mw-prefixindex-nav' ),
293 $nextLink
294 );
295 }
296 $out2 .= "</td></tr>" .
297 Xml::closeElement( 'table' );
298 }
299
300 $this->getOutput()->addHTML( $out2 . $out . $footer );
301 }
302
303 protected function getGroupName() {
304 return 'pages';
305 }
306 }