Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / specials / SpecialPrefixindex.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * implements Special:Prefixindex
22 * @ingroup SpecialPage
23 */
24 class SpecialPrefixindex extends SpecialAllpages {
25 // Inherit $maxPerPage
26
27 function __construct(){
28 parent::__construct( 'Prefixindex' );
29 }
30
31 /**
32 * Entry point : initialise variables and call subfunctions.
33 * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default null)
34 */
35 function execute( $par ) {
36 global $wgRequest, $wgOut, $wgContLang;
37
38 $this->setHeaders();
39 $this->outputHeader();
40
41 # GET values
42 $from = $wgRequest->getVal( 'from' );
43 $prefix = $wgRequest->getVal( 'prefix', '' );
44 $namespace = $wgRequest->getInt( 'namespace' );
45 $namespaces = $wgContLang->getNamespaces();
46
47 $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
48 ? wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
49 : wfMsg( 'prefixindex' )
50 );
51
52 if( isset( $par ) ){
53 $this->showPrefixChunk( $namespace, $par, $from );
54 } elseif( isset( $prefix ) ){
55 $this->showPrefixChunk( $namespace, $prefix, $from );
56 } elseif( isset( $from ) ){
57 $this->showPrefixChunk( $namespace, $from, $from );
58 } else {
59 $wgOut->addHTML( $this->namespacePrefixForm( $namespace, null ) );
60 }
61 }
62
63 /**
64 * HTML for the top form
65 * @param $namespace Integer: a namespace constant (default NS_MAIN).
66 * @param $from String: dbKey we are starting listing at.
67 */
68 function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
69 global $wgScript;
70 $t = $this->getTitle();
71
72 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
73 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
74 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
75 $out .= Xml::openElement( 'fieldset' );
76 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
77 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
78 $out .= "<tr>
79 <td class='mw-label'>" .
80 Xml::label( wfMsg( 'allpagesprefix' ), 'nsfrom' ) .
81 "</td>
82 <td class='mw-input'>" .
83 Xml::input( 'prefix', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
84 "</td>
85 </tr>
86 <tr>
87 <td class='mw-label'>" .
88 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
89 "</td>
90 <td class='mw-input'>" .
91 Xml::namespaceSelector( $namespace, null ) . ' ' .
92 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
93 "</td>
94 </tr>";
95 $out .= Xml::closeElement( 'table' );
96 $out .= Xml::closeElement( 'fieldset' );
97 $out .= Xml::closeElement( 'form' );
98 $out .= Xml::closeElement( 'div' );
99 return $out;
100 }
101
102 /**
103 * @param $namespace Integer, default NS_MAIN
104 * @param $prefix String
105 * @param $from String: list all pages from this name (default FALSE)
106 */
107 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
108 global $wgOut, $wgUser, $wgContLang, $wgLang;
109
110 $sk = $wgUser->getSkin();
111
112 if (!isset($from)) $from = $prefix;
113
114 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
115 $prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
116 $namespaces = $wgContLang->getNamespaces();
117
118 if ( !$prefixList || !$fromList ) {
119 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
120 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
121 // Show errormessage and reset to NS_MAIN
122 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
123 $namespace = NS_MAIN;
124 } else {
125 list( $namespace, $prefixKey, $prefix ) = $prefixList;
126 list( /* $fromNs */, $fromKey, $from ) = $fromList;
127
128 ### FIXME: should complain if $fromNs != $namespace
129
130 $dbr = wfGetDB( DB_SLAVE );
131
132 $res = $dbr->select( 'page',
133 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
134 array(
135 'page_namespace' => $namespace,
136 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
137 'page_title >= ' . $dbr->addQuotes( $fromKey ),
138 ),
139 __METHOD__,
140 array(
141 'ORDER BY' => 'page_title',
142 'LIMIT' => $this->maxPerPage + 1,
143 'USE INDEX' => 'name_title',
144 )
145 );
146
147 ### FIXME: side link to previous
148
149 $n = 0;
150 if( $res->numRows() > 0 ) {
151 $out = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-list-table' ) );
152
153 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
154 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
155 if( $t ) {
156 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
157 $sk->linkKnown(
158 $t,
159 htmlspecialchars( $t->getText() )
160 ) .
161 ($s->page_is_redirect ? '</div>' : '' );
162 } else {
163 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
164 }
165 if( $n % 3 == 0 ) {
166 $out .= '<tr>';
167 }
168 $out .= "<td>$link</td>";
169 $n++;
170 if( $n % 3 == 0 ) {
171 $out .= '</tr>';
172 }
173 }
174 if( ($n % 3) != 0 ) {
175 $out .= '</tr>';
176 }
177 $out .= Xml::closeElement( 'table' );
178 } else {
179 $out = '';
180 }
181 }
182
183 if ( $this->including() ) {
184 $out2 = '';
185 } else {
186 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
187 $self = $this->getTitle();
188 $out2 = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-nav-table' ) ) .
189 '<tr>
190 <td>' .
191 $nsForm .
192 '</td>
193 <td id="mw-prefixindex-nav-form">' .
194 $sk->linkKnown( $self, wfMsgHtml( 'allpages' ) );
195
196 if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
197 $query = array(
198 'from' => $s->page_title,
199 'prefix' => $prefix
200 );
201
202 if( $namespace ) {
203 $query['namespace'] = $namespace;
204 }
205
206 $out2 = $wgLang->pipeList( array(
207 $out2,
208 $sk->linkKnown(
209 $self,
210 wfMsgHtml( 'nextpage', str_replace( '_',' ', htmlspecialchars( $s->page_title ) ) ),
211 array(),
212 $query
213 )
214 ) );
215 }
216 $out2 .= "</td></tr>" .
217 Xml::closeElement( 'table' );
218 }
219
220 $wgOut->addHTML( $out2 . $out );
221 }
222 }