Add SpecialPage::getGroupName and use it
[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 // Inherit $maxPerPage
31
32 function __construct() {
33 parent::__construct( 'Prefixindex' );
34 }
35
36 /**
37 * Entry point : initialise variables and call subfunctions.
38 * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default null)
39 */
40 function execute( $par ) {
41 global $wgContLang;
42
43 $this->setHeaders();
44 $this->outputHeader();
45
46 $out = $this->getOutput();
47 $out->addModuleStyles( 'mediawiki.special' );
48
49 # GET values
50 $request = $this->getRequest();
51 $from = $request->getVal( 'from', '' );
52 $prefix = $request->getVal( 'prefix', '' );
53 $ns = $request->getIntOrNull( 'namespace' );
54 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
55 $hideredirects = $request->getBool( 'hideredirects', false );
56
57 $namespaces = $wgContLang->getNamespaces();
58 $out->setPageTitle(
59 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
60 ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
61 : $this->msg( 'prefixindex' )
62 );
63
64 $showme = '';
65 if( isset( $par ) ) {
66 $showme = $par;
67 } elseif( $prefix != '' ) {
68 $showme = $prefix;
69 } elseif( $from != '' && $ns === null ) {
70 // For back-compat with Special:Allpages
71 // Don't do this if namespace is passed, so paging works when doing NS views.
72 $showme = $from;
73 }
74
75 // Bug 27864: if transcluded, show all pages instead of the form.
76 if ( $this->including() || $showme != '' || $ns !== null ) {
77 $this->showPrefixChunk( $namespace, $showme, $from, $hideredirects );
78 } else {
79 $out->addHTML( $this->namespacePrefixForm( $namespace, null, $hideredirects ) );
80 }
81 }
82
83 /**
84 * HTML for the top form
85 * @param $namespace Integer: a namespace constant (default NS_MAIN).
86 * @param $from String: dbKey we are starting listing at.
87 * @param $hideredirects Bool: hide redirects (default FALSE)
88 * @return string
89 */
90 function namespacePrefixForm( $namespace = NS_MAIN, $from = '', $hideredirects = false ) {
91 global $wgScript;
92
93 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
94 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
95 $out .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
96 $out .= Xml::openElement( 'fieldset' );
97 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
98 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
99 $out .= "<tr>
100 <td class='mw-label'>" .
101 Xml::label( $this->msg( 'allpagesprefix' )->text(), 'nsfrom' ) .
102 "</td>
103 <td class='mw-input'>" .
104 Xml::input( 'prefix', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
105 "</td>
106 </tr>
107 <tr>
108 <td class='mw-label'>" .
109 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
110 "</td>
111 <td class='mw-input'>" .
112 Html::namespaceSelector( array(
113 'selected' => $namespace,
114 ), array(
115 'name' => 'namespace',
116 'id' => 'namespace',
117 'class' => 'namespaceselector',
118 ) ) .
119 Xml::checkLabel(
120 $this->msg( 'allpages-hide-redirects' )->text(),
121 'hideredirects',
122 'hideredirects',
123 $hideredirects
124 ) . ' ' .
125 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
126 "</td>
127 </tr>";
128 $out .= Xml::closeElement( 'table' );
129 $out .= Xml::closeElement( 'fieldset' );
130 $out .= Xml::closeElement( 'form' );
131 $out .= Xml::closeElement( 'div' );
132 return $out;
133 }
134
135 /**
136 * @param $namespace Integer, default NS_MAIN
137 * @param $prefix String
138 * @param $from String: list all pages from this name (default FALSE)
139 * @param $hideredirects Bool: hide redirects (default FALSE)
140 */
141 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null, $hideredirects = false ) {
142 global $wgContLang;
143
144 if ( $from === null ) {
145 $from = $prefix;
146 }
147
148 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
149 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
150 $namespaces = $wgContLang->getNamespaces();
151
152 if ( !$prefixList || !$fromList ) {
153 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
154 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
155 // Show errormessage and reset to NS_MAIN
156 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
157 $namespace = NS_MAIN;
158 } else {
159 list( $namespace, $prefixKey, $prefix ) = $prefixList;
160 list( /* $fromNS */, $fromKey, ) = $fromList;
161
162 ### @todo FIXME: Should complain if $fromNs != $namespace
163
164 $dbr = wfGetDB( DB_SLAVE );
165
166 $conds = array(
167 'page_namespace' => $namespace,
168 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
169 'page_title >= ' . $dbr->addQuotes( $fromKey ),
170 );
171
172 if ( $hideredirects ) {
173 $conds['page_is_redirect'] = 0;
174 }
175
176 $res = $dbr->select( 'page',
177 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
178 $conds,
179 __METHOD__,
180 array(
181 'ORDER BY' => 'page_title',
182 'LIMIT' => $this->maxPerPage + 1,
183 'USE INDEX' => 'name_title',
184 )
185 );
186
187 ### @todo FIXME: Side link to previous
188
189 $n = 0;
190 if( $res->numRows() > 0 ) {
191 $out = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-list-table' ) );
192
193 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
194 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
195 if( $t ) {
196 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
197 Linker::linkKnown(
198 $t,
199 htmlspecialchars( $t->getText() ),
200 $s->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array()
201 ) .
202 ($s->page_is_redirect ? '</div>' : '' );
203 } else {
204 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
205 }
206 if( $n % 3 == 0 ) {
207 $out .= '<tr>';
208 }
209 $out .= "<td>$link</td>";
210 $n++;
211 if( $n % 3 == 0 ) {
212 $out .= '</tr>';
213 }
214 }
215 if( ($n % 3) != 0 ) {
216 $out .= '</tr>';
217 }
218 $out .= Xml::closeElement( 'table' );
219 } else {
220 $out = '';
221 }
222 }
223
224 $footer = '';
225 if ( $this->including() ) {
226 $out2 = '';
227 } else {
228 $nsForm = $this->namespacePrefixForm( $namespace, $prefix, $hideredirects );
229 $self = $this->getTitle();
230 $out2 = Xml::openElement( 'table', array( 'id' => 'mw-prefixindex-nav-table' ) ) .
231 '<tr>
232 <td>' .
233 $nsForm .
234 '</td>
235 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
236
237 if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
238 $query = array(
239 'from' => $s->page_title,
240 'prefix' => $prefix,
241 'hideredirects' => $hideredirects,
242 );
243
244 if( $namespace || $prefix == '' ) {
245 // Keep the namespace even if it's 0 for empty prefixes.
246 // This tells us we're not just a holdover from old links.
247 $query['namespace'] = $namespace;
248 }
249 $nextLink = Linker::linkKnown(
250 $self,
251 $this->msg( 'nextpage', str_replace( '_', ' ', $s->page_title ) )->escaped(),
252 array(),
253 $query
254 );
255 $out2 .= $nextLink;
256
257 $footer = "\n" . Html::element( "hr" )
258 . Html::rawElement( "div", array( "class" => "mw-prefixindex-nav" ), $nextLink );
259 }
260 $out2 .= "</td></tr>" .
261 Xml::closeElement( 'table' );
262 }
263
264 $this->getOutput()->addHTML( $out2 . $out . $footer );
265 }
266
267 protected function getGroupName() {
268 return 'pages';
269 }
270 }