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