revert r106095, fix apparently not this simple
[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
56 $namespaces = $wgContLang->getNamespaces();
57 $out->setPageTitle(
58 ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
59 ? $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
60 : $this->msg( 'prefixindex' )
61 );
62
63 $showme = '';
64 if( isset( $par ) ) {
65 $showme = $par;
66 } elseif( $prefix != '' ) {
67 $showme = $prefix;
68 } elseif( $from != '' && $ns === null ) {
69 // For back-compat with Special:Allpages
70 // Don't do this if namespace is passed, so paging works when doing NS views.
71 $showme = $from;
72 }
73
74 // Bug 27864: if transcluded, show all pages instead of the form.
75 if ( $this->including() || $showme != '' || $ns !== null ) {
76 $this->showPrefixChunk( $namespace, $showme, $from );
77 } else {
78 $out->addHTML( $this->namespacePrefixForm( $namespace, null ) );
79 }
80 }
81
82 /**
83 * HTML for the top form
84 * @param $namespace Integer: a namespace constant (default NS_MAIN).
85 * @param $from String: dbKey we are starting listing at.
86 */
87 function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
88 global $wgScript;
89
90 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
91 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
92 $out .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
93 $out .= Xml::openElement( 'fieldset' );
94 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
95 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
96 $out .= "<tr>
97 <td class='mw-label'>" .
98 Xml::label( wfMsg( 'allpagesprefix' ), 'nsfrom' ) .
99 "</td>
100 <td class='mw-input'>" .
101 Xml::input( 'prefix', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
102 "</td>
103 </tr>
104 <tr>
105 <td class='mw-label'>" .
106 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
107 "</td>
108 <td class='mw-input'>" .
109 Xml::namespaceSelector( $namespace, null ) . ' ' .
110 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
111 "</td>
112 </tr>";
113 $out .= Xml::closeElement( 'table' );
114 $out .= Xml::closeElement( 'fieldset' );
115 $out .= Xml::closeElement( 'form' );
116 $out .= Xml::closeElement( 'div' );
117 return $out;
118 }
119
120 /**
121 * @param $namespace Integer, default NS_MAIN
122 * @param $prefix String
123 * @param $from String: list all pages from this name (default FALSE)
124 */
125 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
126 global $wgContLang;
127
128 if ( $from === null ) {
129 $from = $prefix;
130 }
131
132 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
133 $prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
134 $namespaces = $wgContLang->getNamespaces();
135
136 if ( !$prefixList || !$fromList ) {
137 $out = wfMsgExt( 'allpagesbadtitle', 'parse' );
138 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
139 // Show errormessage and reset to NS_MAIN
140 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
141 $namespace = NS_MAIN;
142 } else {
143 list( $namespace, $prefixKey, $prefix ) = $prefixList;
144 list( /* $fromNS */, $fromKey, ) = $fromList;
145
146 ### @todo FIXME: Should complain if $fromNs != $namespace
147
148 $dbr = wfGetDB( DB_SLAVE );
149
150 $res = $dbr->select( 'page',
151 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
152 array(
153 'page_namespace' => $namespace,
154 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
155 'page_title >= ' . $dbr->addQuotes( $fromKey ),
156 ),
157 __METHOD__,
158 array(
159 'ORDER BY' => 'page_title',
160 'LIMIT' => $this->maxPerPage + 1,
161 'USE INDEX' => 'name_title',
162 )
163 );
164
165 ### @todo FIXME: Side link to previous
166
167 $n = 0;
168 if( $res->numRows() > 0 ) {
169 $out = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-list-table' ) );
170
171 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
172 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
173 if( $t ) {
174 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
175 Linker::linkKnown(
176 $t,
177 htmlspecialchars( $t->getText() )
178 ) .
179 ($s->page_is_redirect ? '</div>' : '' );
180 } else {
181 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
182 }
183 if( $n % 3 == 0 ) {
184 $out .= '<tr>';
185 }
186 $out .= "<td>$link</td>";
187 $n++;
188 if( $n % 3 == 0 ) {
189 $out .= '</tr>';
190 }
191 }
192 if( ($n % 3) != 0 ) {
193 $out .= '</tr>';
194 }
195 $out .= Xml::closeElement( 'table' );
196 } else {
197 $out = '';
198 }
199 }
200
201 $footer = '';
202 if ( $this->including() ) {
203 $out2 = '';
204 } else {
205 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
206 $self = $this->getTitle();
207 $out2 = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-nav-table' ) ) .
208 '<tr>
209 <td>' .
210 $nsForm .
211 '</td>
212 <td id="mw-prefixindex-nav-form" class="mw-prefixindex-nav">';
213
214 if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
215 $query = array(
216 'from' => $s->page_title,
217 'prefix' => $prefix
218 );
219
220 if( $namespace || ($prefix == '')) {
221 // Keep the namespace even if it's 0 for empty prefixes.
222 // This tells us we're not just a holdover from old links.
223 $query['namespace'] = $namespace;
224 }
225 $nextLink = Linker::linkKnown(
226 $self,
227 wfMsgHtml( 'nextpage', str_replace( '_',' ', htmlspecialchars( $s->page_title ) ) ),
228 array(),
229 $query
230 );
231 $out2 .= $nextLink;
232
233 $footer = "\n" . Html::element( "hr" )
234 . Html::rawElement( "div", array( "class" => "mw-prefixindex-nav" ), $nextLink );
235 }
236 $out2 .= "</td></tr>" .
237 Xml::closeElement( 'table' );
238 }
239
240 $this->getOutput()->addHTML( $out2 . $out . $footer );
241 }
242 }