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