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