Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialFilepath
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRedirect
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 prefixSearchSubpages
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Implements Special:Filepath
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
24namespace MediaWiki\Specials;
25
26use MediaWiki\SpecialPage\RedirectSpecialPage;
27use MediaWiki\SpecialPage\SpecialPage;
28use MediaWiki\Title\Title;
29use SearchEngineFactory;
30
31/**
32 * A special page that redirects to the URL of a given file
33 *
34 * @ingroup SpecialPage
35 */
36class SpecialFilepath extends RedirectSpecialPage {
37
38    private SearchEngineFactory $searchEngineFactory;
39
40    /**
41     * @param SearchEngineFactory $searchEngineFactory
42     */
43    public function __construct(
44        SearchEngineFactory $searchEngineFactory
45    ) {
46        parent::__construct( 'Filepath' );
47        $this->mAllowedRedirectParams = [ 'width', 'height' ];
48        $this->searchEngineFactory = $searchEngineFactory;
49    }
50
51    /**
52     * Implement by redirecting through Special:Redirect/file.
53     *
54     * @param string|null $par
55     * @return Title
56     */
57    public function getRedirect( $par ) {
58        $file = $par ?: $this->getRequest()->getText( 'file' );
59
60        $redirect = null;
61        if ( $file ) {
62            $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
63        }
64        if ( $redirect === null ) {
65            // The user input is empty or an invalid title,
66            // redirect to form of Special:Redirect with the invalid value prefilled
67            $this->mAddedRedirectParams['wpvalue'] = $file;
68            $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
69        }
70        // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
71        return $redirect;
72    }
73
74    /**
75     * Return an array of subpages beginning with $search that this special page will accept.
76     *
77     * @param string $search Prefix to search for
78     * @param int $limit Maximum number of results to return (usually 10)
79     * @param int $offset Number of results to skip (usually 0)
80     * @return string[] Matching subpages
81     */
82    public function prefixSearchSubpages( $search, $limit, $offset ) {
83        $title = Title::newFromText( $search, NS_FILE );
84        if ( !$title || $title->getNamespace() !== NS_FILE ) {
85            // No prefix suggestion outside of file namespace
86            return [];
87        }
88        $searchEngine = $this->searchEngineFactory->create();
89        $searchEngine->setLimitOffset( $limit, $offset );
90        // Autocomplete subpage the same as a normal search, but just for files
91        $searchEngine->setNamespaces( [ NS_FILE ] );
92        $result = $searchEngine->defaultPrefixSearch( $search );
93
94        return array_map( static function ( Title $t ) {
95            // Remove namespace in search suggestion
96            return $t->getText();
97        }, $result );
98    }
99
100    protected function getGroupName() {
101        return 'media';
102    }
103}
104
105/** @deprecated class alias since 1.41 */
106class_alias( SpecialFilepath::class, 'SpecialFilepath' );