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