Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SkinComponentUtils
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 addClassToClassList
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getReturnToParam
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
72
 makeSpecialUrl
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 makeSpecialUrlSubpage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Skin;
4
5use MediaWiki\HookContainer\HookRunner;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Permissions\Authority;
8use MediaWiki\Request\WebRequest;
9use MediaWiki\SpecialPage\SpecialPage;
10use MediaWiki\Title\Title;
11
12class SkinComponentUtils {
13    /**
14     * Adds a class to the existing class value, supporting it as a string
15     * or array.
16     *
17     * @param string|array $class to update.
18     * @param string $newClass to add.
19     * @return string|array classes.
20     * @internal
21     */
22    public static function addClassToClassList( $class, string $newClass ) {
23        if ( is_array( $class ) ) {
24            $class[] = $newClass;
25        } else {
26            $class .= ' ' . $newClass;
27            $class = trim( $class );
28        }
29        return $class;
30    }
31
32    /**
33     * Builds query params for the page to return to, used when building links
34     * @unstable
35     *
36     * @param Title $title
37     * @param WebRequest $request
38     * @param Authority $authority
39     * @return string[]
40     */
41    public static function getReturnToParam( $title, $request, $authority ) {
42        // T379295/T381216: Preserve authentication query params so they don't get lost
43        // during switching between Login/Logout or CreateAccount pages where we need them.
44        // See AuthManagerSpecialPage/LoginSignupSpecialPage::getPreservedParams().
45        // This special case also avoids "nesting" returnto values on these pages.
46        if (
47            $title->isSpecial( 'Userlogin' )
48            || $title->isSpecial( 'CreateAccount' )
49            || $title->isSpecial( 'Userlogout' )
50        ) {
51            $params = [
52                'uselang' => $request->getVal( 'uselang' ),
53                'variant' => $request->getVal( 'variant' ),
54                'display' => $request->getVal( 'display' ),
55                'returnto' => $request->getVal( 'returnto' ),
56                'returntoquery' => $request->getVal( 'returntoquery' ),
57                'returntoanchor' => $request->getVal( 'returntoanchor' ),
58            ];
59            ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
60                ->onAuthPreserveQueryParams( $params, [ 'request' => $request, 'reset' => true ] );
61            return array_filter( $params, static fn ( $val ) => $val !== null );
62        }
63
64        # Due to T34276, if a user does not have read permissions,
65        # $this->getTitle() will just give Special:Badtitle, which is
66        # not especially useful as a returnto parameter. Use the title
67        # from the request instead, if there was one.
68        if ( $authority->isAllowed( 'read' ) ) {
69            $page = $title;
70        } else {
71            $page = Title::newFromText( $request->getVal( 'title', '' ) );
72        }
73
74        $query = [];
75        if ( !$request->wasPosted() ) {
76            $query = $request->getQueryValues();
77            unset( $query['title'] );
78        }
79
80        $params = [];
81        if ( $page ) {
82            $params['returnto'] = $page->getPrefixedText();
83            if ( $query ) {
84                $params['returntoquery'] = wfArrayToCgi( $query );
85            }
86        }
87
88        return $params;
89    }
90
91    /**
92     * Make a URL for a Special Page using the given query and protocol.
93     *
94     * If $proto is set to null, make a local URL. Otherwise, make a full
95     * URL with the protocol specified.
96     *
97     * @param string $name Name of the Special page
98     * @param string|array $urlaction Query to append
99     * @param string|null $proto Protocol to use or null for a local URL
100     * @return string
101     */
102    public static function makeSpecialUrl( $name, $urlaction = '', $proto = null ) {
103        $title = SpecialPage::getSafeTitleFor( $name );
104        if ( $proto === null ) {
105            return $title->getLocalURL( $urlaction );
106        } else {
107            return $title->getFullURL( $urlaction, false, $proto );
108        }
109    }
110
111    /**
112     * @param string $name
113     * @param string|bool $subpage false for no subpage
114     * @param string|array $urlaction
115     * @return string
116     */
117    public static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
118        $title = SpecialPage::getSafeTitleFor( $name, $subpage );
119        return $title->getLocalURL( $urlaction );
120    }
121}