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 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\Search\SearchEngineFactory;
10use MediaWiki\SpecialPage\RedirectSpecialPage;
11use MediaWiki\SpecialPage\SpecialPage;
12use MediaWiki\Title\Title;
13
14/**
15 * Redirects to the URL of a thumbnail for the given file.
16 *
17 * @ingroup SpecialPage
18 */
19class SpecialFilepath extends RedirectSpecialPage {
20
21    private SearchEngineFactory $searchEngineFactory;
22
23    public function __construct(
24        SearchEngineFactory $searchEngineFactory
25    ) {
26        parent::__construct( 'Filepath' );
27        $this->mAllowedRedirectParams = [ 'width', 'height' ];
28        $this->searchEngineFactory = $searchEngineFactory;
29    }
30
31    /**
32     * Implement by redirecting through Special:Redirect/file.
33     *
34     * @param string|null $par
35     * @return Title
36     */
37    public function getRedirect( $par ) {
38        $file = $par ?: $this->getRequest()->getText( 'file' );
39
40        $redirect = null;
41        if ( $file ) {
42            $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
43        }
44        if ( $redirect === null ) {
45            // The user input is empty or an invalid title,
46            // redirect to form of Special:Redirect with the invalid value prefilled
47            $this->mAddedRedirectParams['wpvalue'] = $file;
48            $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
49        }
50        // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
51        return $redirect;
52    }
53
54    /**
55     * Return an array of subpages beginning with $search that this special page will accept.
56     *
57     * @param string $search Prefix to search for
58     * @param int $limit Maximum number of results to return (usually 10)
59     * @param int $offset Number of results to skip (usually 0)
60     * @return string[] Matching subpages
61     */
62    public function prefixSearchSubpages( $search, $limit, $offset ) {
63        $title = Title::newFromText( $search, NS_FILE );
64        if ( !$title || $title->getNamespace() !== NS_FILE ) {
65            // No prefix suggestion outside of file namespace
66            return [];
67        }
68        $searchEngine = $this->searchEngineFactory->create();
69        $searchEngine->setLimitOffset( $limit, $offset );
70        // Autocomplete subpage the same as a normal search, but just for files
71        $searchEngine->setNamespaces( [ NS_FILE ] );
72        $result = $searchEngine->defaultPrefixSearch( $search );
73
74        return array_map( static function ( Title $t ) {
75            // Remove namespace in search suggestion
76            return $t->getText();
77        }, $result );
78    }
79
80    /** @inheritDoc */
81    protected function getGroupName() {
82        return 'media';
83    }
84}
85
86/** @deprecated class alias since 1.41 */
87class_alias( SpecialFilepath::class, 'SpecialFilepath' );