Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 160 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
SpecialPrefixIndex | |
0.00% |
0 / 159 |
|
0.00% |
0 / 5 |
1056 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
110 | |||
namespacePrefixForm | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
2 | |||
showPrefixChunk | |
0.00% |
0 / 90 |
|
0.00% |
0 / 1 |
380 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Specials; |
22 | |
23 | use MediaWiki\Cache\LinkCache; |
24 | use MediaWiki\Html\Html; |
25 | use MediaWiki\HTMLForm\Field\HTMLCheckField; |
26 | use MediaWiki\HTMLForm\HTMLForm; |
27 | use MediaWiki\Title\Title; |
28 | use Wikimedia\Rdbms\IConnectionProvider; |
29 | use Wikimedia\Rdbms\IExpression; |
30 | use Wikimedia\Rdbms\LikeValue; |
31 | |
32 | /** |
33 | * Implements Special:Prefixindex |
34 | * |
35 | * @ingroup SpecialPage |
36 | * @ingroup Search |
37 | */ |
38 | class SpecialPrefixIndex extends SpecialAllPages { |
39 | |
40 | /** |
41 | * Whether to remove the searched prefix from the displayed link. Useful |
42 | * for inclusion of a set of subpages in a root page. |
43 | * @var bool |
44 | */ |
45 | protected $stripPrefix = false; |
46 | |
47 | /** @var bool */ |
48 | protected $hideRedirects = false; |
49 | |
50 | // Inherit $maxPerPage |
51 | |
52 | // phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.WrongStyle |
53 | private IConnectionProvider $dbProvider; |
54 | private LinkCache $linkCache; |
55 | |
56 | public function __construct( |
57 | IConnectionProvider $dbProvider, |
58 | LinkCache $linkCache |
59 | ) { |
60 | parent::__construct( $dbProvider ); |
61 | $this->mName = 'Prefixindex'; |
62 | $this->dbProvider = $dbProvider; |
63 | $this->linkCache = $linkCache; |
64 | } |
65 | |
66 | /** |
67 | * Entry point: initialise variables and call subfunctions. |
68 | * @param string|null $par Becomes "FOO" when called like Special:Prefixindex/FOO |
69 | */ |
70 | public function execute( $par ) { |
71 | $this->setHeaders(); |
72 | $this->outputHeader(); |
73 | |
74 | $out = $this->getOutput(); |
75 | $out->addModuleStyles( 'mediawiki.special' ); |
76 | |
77 | # GET values |
78 | $request = $this->getRequest(); |
79 | $from = $request->getVal( 'from', '' ); |
80 | $prefix = $request->getVal( 'prefix', '' ); |
81 | $ns = $request->getIntOrNull( 'namespace' ); |
82 | $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN). |
83 | $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects ); |
84 | $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix ); |
85 | |
86 | $namespaces = $this->getContentLanguage()->getNamespaces(); |
87 | $out->setPageTitleMsg( |
88 | ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) |
89 | ? $this->msg( 'prefixindex-namespace' )->plaintextParams( |
90 | str_replace( '_', ' ', $namespaces[$namespace] ) |
91 | ) |
92 | : $this->msg( 'prefixindex' ) |
93 | ); |
94 | |
95 | $showme = ''; |
96 | if ( $par !== null ) { |
97 | $showme = $par; |
98 | } elseif ( $prefix != '' ) { |
99 | $showme = $prefix; |
100 | } elseif ( $from != '' && $ns === null ) { |
101 | // For back-compat with Special:Allpages |
102 | // Don't do this if namespace is passed, so paging works when doing NS views. |
103 | $showme = $from; |
104 | } |
105 | |
106 | // T29864: if transcluded, show all pages instead of the form. |
107 | if ( $this->including() || $showme != '' || $ns !== null ) { |
108 | $this->showPrefixChunk( $namespace, $showme, $from ); |
109 | } else { |
110 | $out->addHTML( $this->namespacePrefixForm( $namespace, '' )->getHTML( false ) ); |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Prepared HTMLForm object for the top form |
116 | * @param int $namespace A namespace constant (default NS_MAIN). |
117 | * @param string $from DbKey we are starting listing at. |
118 | * @return HTMLForm |
119 | */ |
120 | protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ): HTMLForm { |
121 | $formDescriptor = [ |
122 | 'prefix' => [ |
123 | 'label-message' => 'allpagesprefix', |
124 | 'name' => 'prefix', |
125 | 'id' => 'nsfrom', |
126 | 'type' => 'text', |
127 | 'size' => '30', |
128 | 'default' => str_replace( '_', ' ', $from ), |
129 | ], |
130 | 'namespace' => [ |
131 | 'type' => 'namespaceselect', |
132 | 'name' => 'namespace', |
133 | 'id' => 'namespace', |
134 | 'label-message' => 'namespace', |
135 | 'all' => null, |
136 | 'default' => $namespace, |
137 | ], |
138 | 'hidedirects' => [ |
139 | 'class' => HTMLCheckField::class, |
140 | 'name' => 'hideredirects', |
141 | 'label-message' => 'allpages-hide-redirects', |
142 | ], |
143 | 'stripprefix' => [ |
144 | 'class' => HTMLCheckField::class, |
145 | 'name' => 'stripprefix', |
146 | 'label-message' => 'prefixindex-strip', |
147 | ], |
148 | ]; |
149 | |
150 | $this->getHookRunner()->onSpecialPrefixIndexGetFormFilters( $this->getContext(), $formDescriptor ); |
151 | |
152 | $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
153 | ->setMethod( 'get' ) |
154 | ->setTitle( $this->getPageTitle() ) // Remove subpage |
155 | ->setWrapperLegendMsg( 'prefixindex' ) |
156 | ->setSubmitTextMsg( 'prefixindex-submit' ); |
157 | |
158 | return $htmlForm->prepareForm(); |
159 | } |
160 | |
161 | /** |
162 | * @param int $namespace |
163 | * @param string $prefix |
164 | * @param string|null $from List all pages from this name (default false) |
165 | */ |
166 | protected function showPrefixChunk( $namespace, $prefix, $from = null ) { |
167 | $from ??= $prefix; |
168 | |
169 | $fromList = $this->getNamespaceKeyAndText( $namespace, $from ); |
170 | $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix ); |
171 | $namespaces = $this->getContentLanguage()->getNamespaces(); |
172 | $res = null; |
173 | $n = 0; |
174 | $nextRow = null; |
175 | $preparedHtmlForm = $this->namespacePrefixForm( $namespace, $prefix ); |
176 | |
177 | if ( !$prefixList || !$fromList ) { |
178 | $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock(); |
179 | } elseif ( !array_key_exists( $namespace, $namespaces ) ) { |
180 | // Show errormessage and reset to NS_MAIN |
181 | $out = $this->msg( 'allpages-bad-ns', $namespace )->parse(); |
182 | $namespace = NS_MAIN; |
183 | } else { |
184 | [ $namespace, $prefixKey, $prefix ] = $prefixList; |
185 | [ /* $fromNS */, $fromKey, ] = $fromList; |
186 | |
187 | # ## @todo FIXME: Should complain if $fromNs != $namespace |
188 | |
189 | $dbr = $this->dbProvider->getReplicaDatabase(); |
190 | $queryBuiler = $dbr->newSelectQueryBuilder() |
191 | ->select( LinkCache::getSelectFields() ) |
192 | ->from( 'page' ) |
193 | ->where( [ |
194 | 'page_namespace' => $namespace, |
195 | $dbr->expr( |
196 | 'page_title', |
197 | IExpression::LIKE, |
198 | new LikeValue( $prefixKey, $dbr->anyString() ) |
199 | ), |
200 | $dbr->expr( 'page_title', '>=', $fromKey ), |
201 | ] ) |
202 | ->orderBy( 'page_title' ) |
203 | ->limit( $this->maxPerPage + 1 ) |
204 | ->useIndex( 'page_name_title' ); |
205 | |
206 | if ( $this->hideRedirects ) { |
207 | $queryBuiler->andWhere( [ 'page_is_redirect' => 0 ] ); |
208 | } |
209 | |
210 | $this->getHookRunner()->onSpecialPrefixIndexQuery( $preparedHtmlForm->mFieldData, $queryBuiler ); |
211 | |
212 | $res = $queryBuiler->caller( __METHOD__ )->fetchResultSet(); |
213 | |
214 | // @todo FIXME: Side link to previous |
215 | |
216 | if ( $res->numRows() > 0 ) { |
217 | $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] ); |
218 | |
219 | $prefixLength = strlen( $prefix ); |
220 | foreach ( $res as $row ) { |
221 | if ( $n >= $this->maxPerPage ) { |
222 | $nextRow = $row; |
223 | break; |
224 | } |
225 | $title = Title::newFromRow( $row ); |
226 | // Make sure it gets into LinkCache |
227 | $this->linkCache->addGoodLinkObjFromRow( $title, $row ); |
228 | $displayed = $title->getText(); |
229 | // Try not to generate unclickable links |
230 | if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) { |
231 | $displayed = substr( $displayed, $prefixLength ); |
232 | } |
233 | $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) . |
234 | $this->getLinkRenderer()->makeKnownLink( |
235 | $title, |
236 | $displayed |
237 | ) . |
238 | ( $title->isRedirect() ? '</div>' : '' ); |
239 | |
240 | $out .= "<li>$link</li>\n"; |
241 | $n++; |
242 | |
243 | } |
244 | $out .= Html::closeElement( 'ul' ); |
245 | |
246 | if ( $res->numRows() > 2 ) { |
247 | // Only apply CSS column styles if there are more than 2 entries. |
248 | // Otherwise, rendering is broken as "mw-prefixindex-body"'s CSS column count is 3. |
249 | $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out ); |
250 | } |
251 | } else { |
252 | $out = ''; |
253 | } |
254 | } |
255 | |
256 | $output = $this->getOutput(); |
257 | |
258 | if ( $this->including() ) { |
259 | // We don't show the nav-links and the form when included in other |
260 | // pages, so let's just finish here. |
261 | $output->addHTML( $out ); |
262 | return; |
263 | } |
264 | |
265 | $topOut = $preparedHtmlForm->getHTML( false ); |
266 | |
267 | if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) { |
268 | $query = [ |
269 | 'from' => $nextRow->page_title, |
270 | 'prefix' => $prefix, |
271 | 'hideredirects' => $this->hideRedirects, |
272 | 'stripprefix' => $this->stripPrefix, |
273 | ]; |
274 | |
275 | if ( $namespace || $prefix == '' ) { |
276 | // Keep the namespace even if it's 0 for empty prefixes. |
277 | // This tells us we're not just a holdover from old links. |
278 | $query['namespace'] = $namespace; |
279 | } |
280 | |
281 | $nextLink = $this->getLinkRenderer()->makeKnownLink( |
282 | $this->getPageTitle(), |
283 | $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(), |
284 | [], |
285 | $query |
286 | ); |
287 | |
288 | // Link shown at the top of the page below the form |
289 | $topOut .= Html::rawElement( 'div', |
290 | [ 'class' => 'mw-prefixindex-nav' ], |
291 | $nextLink |
292 | ); |
293 | |
294 | // Link shown at the footer |
295 | $out .= "\n" . Html::element( 'hr' ) . |
296 | Html::rawElement( |
297 | 'div', |
298 | [ 'class' => 'mw-prefixindex-nav' ], |
299 | $nextLink |
300 | ); |
301 | |
302 | } |
303 | |
304 | $output->addHTML( $topOut . $out ); |
305 | } |
306 | |
307 | protected function getGroupName() { |
308 | return 'pages'; |
309 | } |
310 | } |
311 | |
312 | /** |
313 | * Retain the old class name for backwards compatibility. |
314 | * @deprecated since 1.41 |
315 | */ |
316 | class_alias( SpecialPrefixIndex::class, 'SpecialPrefixindex' ); |