Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ParametersHelper
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 getTargetTitleIfSpecialMyLanguage
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 isPageExistingAndViewable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MediaWiki\EditPage;
4
5use MediaWiki\Context\RequestContext;
6use MediaWiki\Page\PageIdentity;
7use MediaWiki\Permissions\Authority;
8use MediaWiki\SpecialPage\SpecialPageFactory;
9use MediaWiki\Specials\SpecialMyLanguage;
10use MediaWiki\Title\Title;
11
12/**
13 * Helper methods for resolving EditPage parameters that deal with page titles.
14 *
15 * @internal
16 * @property SpecialPageFactory $specialPageFactory
17 */
18trait ParametersHelper {
19
20    /**
21     * If the given Title is Special:MyLanguage/Foo, resolve the language chain for the
22     * actual target title desired.
23     *
24     * @param ?Title $title
25     * @return ?Title
26     */
27    private function getTargetTitleIfSpecialMyLanguage( ?Title $title ): ?Title {
28        if ( $title && $title->isSpecialPage() ) {
29            [ $spName, $spParam ] = $this->specialPageFactory->resolveAlias( $title->getText() );
30            if ( $spName ) {
31                $specialPage = $this->specialPageFactory->getPage( $spName );
32                if ( $specialPage instanceof SpecialMyLanguage ) {
33                    // TODO This should pass a language as a parameter, instead of the whole context
34                    $specialPage->setContext( RequestContext::getMain() );
35                    $title = $specialPage->findTitleForTransclusion( $spParam );
36                }
37            }
38        }
39
40        return $title;
41    }
42
43    /**
44     * Verify if a given title exists and the given user is allowed to view it
45     *
46     * @param PageIdentity|null $page
47     * @param Authority $performer
48     * @return bool
49     * @phan-assert-true-condition $page
50     */
51    private function isPageExistingAndViewable( ?PageIdentity $page, Authority $performer ): bool {
52        return $page && $page->exists() && $performer->authorizeRead( 'read', $page );
53    }
54}