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