3 namespace MediaWiki\Skin
;
5 use MediaWiki\HookContainer\HookRunner
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Permissions\Authority
;
8 use MediaWiki\Request\WebRequest
;
9 use MediaWiki\SpecialPage\SpecialPage
;
10 use MediaWiki\Title\Title
;
12 class SkinComponentUtils
{
14 * Adds a class to the existing class value, supporting it as a string
17 * @param string|array $class to update.
18 * @param string $newClass to add.
19 * @return string|array classes.
22 public static function addClassToClassList( $class, string $newClass ) {
23 if ( is_array( $class ) ) {
26 $class .= ' ' . $newClass;
27 $class = trim( $class );
33 * Builds query params for the page to return to, used when building links
37 * @param WebRequest $request
38 * @param Authority $authority
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.
47 $title->isSpecial( 'Userlogin' )
48 ||
$title->isSpecial( 'CreateAccount' )
49 ||
$title->isSpecial( 'Userlogout' )
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' ),
59 ( new HookRunner( MediaWikiServices
::getInstance()->getHookContainer() ) )
60 ->onAuthPreserveQueryParams( $params, [ 'reset' => true ] );
61 return array_filter( $params, static fn ( $val ) => $val !== null );
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' ) ) {
71 $page = Title
::newFromText( $request->getVal( 'title', '' ) );
75 if ( !$request->wasPosted() ) {
76 $query = $request->getQueryValues();
77 unset( $query['title'] );
82 $params['returnto'] = $page->getPrefixedText();
84 $params['returntoquery'] = wfArrayToCgi( $query );
92 * Make a URL for a Special Page using the given query and protocol.
94 * If $proto is set to null, make a local URL. Otherwise, make a full
95 * URL with the protocol specified.
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
102 public static function makeSpecialUrl( $name, $urlaction = '', $proto = null ) {
103 $title = SpecialPage
::getSafeTitleFor( $name );
104 if ( $proto === null ) {
105 return $title->getLocalURL( $urlaction );
107 return $title->getFullURL( $urlaction, false, $proto );
112 * @param string $name
113 * @param string|bool $subpage false for no subpage
114 * @param string|array $urlaction
117 public static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
118 $title = SpecialPage
::getSafeTitleFor( $name, $subpage );
119 return $title->getLocalURL( $urlaction );