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