(bug 20275) Fixed LIKE queries on SQLite backend
[lhc/web/wiklou.git] / includes / specials / SpecialPrefixindex.php
1 <?php
2
3 /**
4 * implements Special:Prefixindex
5 * @ingroup SpecialPage
6 */
7 class SpecialPrefixindex extends SpecialAllpages {
8 // Inherit $maxPerPage
9
10 function __construct(){
11 parent::__construct( 'Prefixindex' );
12 }
13
14 /**
15 * Entry point : initialise variables and call subfunctions.
16 * @param $par String: becomes "FOO" when called like Special:Prefixindex/FOO (default null)
17 */
18 function execute( $par ) {
19 global $wgRequest, $wgOut, $wgContLang;
20
21 $this->setHeaders();
22 $this->outputHeader();
23
24 # GET values
25 $from = $wgRequest->getVal( 'from' );
26 $prefix = $wgRequest->getVal( 'prefix', '' );
27 $namespace = $wgRequest->getInt( 'namespace' );
28 $namespaces = $wgContLang->getNamespaces();
29
30 $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) )
31 ? wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) )
32 : wfMsg( 'prefixindex' )
33 );
34
35 if( isset( $par ) ){
36 $this->showPrefixChunk( $namespace, $par, $from );
37 } elseif( isset( $prefix ) ){
38 $this->showPrefixChunk( $namespace, $prefix, $from );
39 } elseif( isset( $from ) ){
40 $this->showPrefixChunk( $namespace, $from, $from );
41 } else {
42 $wgOut->addHTML( $this->namespacePrefixForm( $namespace, null ) );
43 }
44 }
45
46 /**
47 * HTML for the top form
48 * @param integer $namespace A namespace constant (default NS_MAIN).
49 * @param string $from dbKey we are starting listing at.
50 */
51 function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ) {
52 global $wgScript;
53 $t = $this->getTitle();
54
55 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
56 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
57 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
58 $out .= Xml::openElement( 'fieldset' );
59 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
60 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
61 $out .= "<tr>
62 <td class='mw-label'>" .
63 Xml::label( wfMsg( 'allpagesprefix' ), 'nsfrom' ) .
64 "</td>
65 <td class='mw-input'>" .
66 Xml::input( 'prefix', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
67 "</td>
68 </tr>
69 <tr>
70 <td class='mw-label'>" .
71 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
72 "</td>
73 <td class='mw-input'>" .
74 Xml::namespaceSelector( $namespace, null ) . ' ' .
75 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
76 "</td>
77 </tr>";
78 $out .= Xml::closeElement( 'table' );
79 $out .= Xml::closeElement( 'fieldset' );
80 $out .= Xml::closeElement( 'form' );
81 $out .= Xml::closeElement( 'div' );
82 return $out;
83 }
84
85 /**
86 * @param integer $namespace (Default NS_MAIN)
87 * @param string $from list all pages from this name (default FALSE)
88 */
89 function showPrefixChunk( $namespace = NS_MAIN, $prefix, $from = null ) {
90 global $wgOut, $wgUser, $wgContLang, $wgLang;
91
92 $sk = $wgUser->getSkin();
93
94 if (!isset($from)) $from = $prefix;
95
96 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
97 $prefixList = $this->getNamespaceKeyAndText($namespace, $prefix);
98 $namespaces = $wgContLang->getNamespaces();
99
100 if ( !$prefixList || !$fromList ) {
101 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
102 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
103 // Show errormessage and reset to NS_MAIN
104 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
105 $namespace = NS_MAIN;
106 } else {
107 list( $namespace, $prefixKey, $prefix ) = $prefixList;
108 list( /* $fromNs */, $fromKey, $from ) = $fromList;
109
110 ### FIXME: should complain if $fromNs != $namespace
111
112 $dbr = wfGetDB( DB_SLAVE );
113
114 $res = $dbr->select( 'page',
115 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
116 array(
117 'page_namespace' => $namespace,
118 'page_title' . $dbr->buildLike( $prefixKey, $dbr->anyString() ),
119 'page_title >= ' . $dbr->addQuotes( $fromKey ),
120 ),
121 __METHOD__,
122 array(
123 'ORDER BY' => 'page_title',
124 'LIMIT' => $this->maxPerPage + 1,
125 'USE INDEX' => 'name_title',
126 )
127 );
128
129 ### FIXME: side link to previous
130
131 $n = 0;
132 if( $res->numRows() > 0 ) {
133 $out = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-list-table' ) );
134
135 while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
136 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
137 if( $t ) {
138 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
139 $sk->linkKnown(
140 $t,
141 htmlspecialchars( $t->getText() )
142 ) .
143 ($s->page_is_redirect ? '</div>' : '' );
144 } else {
145 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
146 }
147 if( $n % 3 == 0 ) {
148 $out .= '<tr>';
149 }
150 $out .= "<td>$link</td>";
151 $n++;
152 if( $n % 3 == 0 ) {
153 $out .= '</tr>';
154 }
155 }
156 if( ($n % 3) != 0 ) {
157 $out .= '</tr>';
158 }
159 $out .= Xml::closeElement( 'table' );
160 } else {
161 $out = '';
162 }
163 }
164
165 if ( $this->including() ) {
166 $out2 = '';
167 } else {
168 $nsForm = $this->namespacePrefixForm( $namespace, $prefix );
169 $self = $this->getTitle();
170 $out2 = Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-prefixindex-nav-table' ) ) .
171 '<tr>
172 <td>' .
173 $nsForm .
174 '</td>
175 <td id="mw-prefixindex-nav-form">' .
176 $sk->linkKnown( $self, wfMsgHtml( 'allpages' ) );
177
178 if( isset( $res ) && $res && ( $n == $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
179 $query = array(
180 'from' => $s->page_title,
181 'prefix' => $prefix
182 );
183
184 if( $namespace ) {
185 $query['namespace'] = $namespace;
186 }
187
188 $out2 = $wgLang->pipeList( array(
189 $out2,
190 $sk->linkKnown(
191 $self,
192 wfMsgHtml( 'nextpage', str_replace( '_',' ', htmlspecialchars( $s->page_title ) ) ),
193 array(),
194 $query
195 )
196 ) );
197 }
198 $out2 .= "</td></tr>" .
199 Xml::closeElement( 'table' );
200 }
201
202 $wgOut->addHTML( $out2 . $out );
203 }
204 }