aboutsummaryrefslogtreecommitdiffstats
path: root/includes/skins/components/SkinComponentUtils.php
blob: e137a2861878c5f84ae85dfb7e201314afaa2a96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace MediaWiki\Skin;

use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\Authority;
use MediaWiki\Request\WebRequest;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\Title;

class SkinComponentUtils {
	/**
	 * Adds a class to the existing class value, supporting it as a string
	 * or array.
	 *
	 * @param string|array $class to update.
	 * @param string $newClass to add.
	 * @return string|array classes.
	 * @internal
	 */
	public static function addClassToClassList( $class, string $newClass ) {
		if ( is_array( $class ) ) {
			$class[] = $newClass;
		} else {
			$class .= ' ' . $newClass;
			$class = trim( $class );
		}
		return $class;
	}

	/**
	 * Builds query params for the page to return to, used when building links
	 * @unstable
	 *
	 * @param Title $title
	 * @param WebRequest $request
	 * @param Authority $authority
	 * @return string[]
	 */
	public static function getReturnToParam( $title, $request, $authority ) {
		// T379295/T381216: Preserve authentication query params so they don't get lost
		// during switching between Login/Logout or CreateAccount pages where we need them.
		// See AuthManagerSpecialPage/LoginSignupSpecialPage::getPreservedParams().
		// This special case also avoids "nesting" returnto values on these pages.
		if (
			$title->isSpecial( 'Userlogin' )
			|| $title->isSpecial( 'CreateAccount' )
			|| $title->isSpecial( 'Userlogout' )
		) {
			$params = [
				'uselang' => $request->getVal( 'uselang' ),
				'variant' => $request->getVal( 'variant' ),
				'display' => $request->getVal( 'display' ),
				'returnto' => $request->getVal( 'returnto' ),
				'returntoquery' => $request->getVal( 'returntoquery' ),
				'returntoanchor' => $request->getVal( 'returntoanchor' ),
			];
			( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
				->onAuthPreserveQueryParams( $params, [ 'request' => $request, 'reset' => true ] );
			return array_filter( $params, static fn ( $val ) => $val !== null );
		}

		# Due to T34276, if a user does not have read permissions,
		# $this->getTitle() will just give Special:Badtitle, which is
		# not especially useful as a returnto parameter. Use the title
		# from the request instead, if there was one.
		if ( $authority->isAllowed( 'read' ) ) {
			$page = $title;
		} else {
			$page = Title::newFromText( $request->getVal( 'title', '' ) );
		}

		$query = [];
		if ( !$request->wasPosted() ) {
			$query = $request->getQueryValues();
			unset( $query['title'] );
		}

		$params = [];
		if ( $page ) {
			$params['returnto'] = $page->getPrefixedText();
			if ( $query ) {
				$params['returntoquery'] = wfArrayToCgi( $query );
			}
		}

		return $params;
	}

	/**
	 * Make a URL for a Special Page using the given query and protocol.
	 *
	 * If $proto is set to null, make a local URL. Otherwise, make a full
	 * URL with the protocol specified.
	 *
	 * @param string $name Name of the Special page
	 * @param string|array $urlaction Query to append
	 * @param string|null $proto Protocol to use or null for a local URL
	 * @return string
	 */
	public static function makeSpecialUrl( $name, $urlaction = '', $proto = null ) {
		$title = SpecialPage::getSafeTitleFor( $name );
		if ( $proto === null ) {
			return $title->getLocalURL( $urlaction );
		} else {
			return $title->getFullURL( $urlaction, false, $proto );
		}
	}

	/**
	 * @param string $name
	 * @param string|bool $subpage false for no subpage
	 * @param string|array $urlaction
	 * @return string
	 */
	public static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
		$title = SpecialPage::getSafeTitleFor( $name, $subpage );
		return $title->getLocalURL( $urlaction );
	}
}