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