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