Debian Codegraph @

includes/skins/components/SkinComponentUtils.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Skin;
4
5 use MediaWiki\Permissions\Authority;
6 use MediaWiki\Request\WebRequest;
7 use MediaWiki\SpecialPage\SpecialPage;
8 use MediaWiki\Title\Title;
9
10 class SkinComponentUtils {
11 /**
12 * Adds a class to the existing class value, supporting it as a string
13 * or array.
14 *
15 * @param string|array $class to update.
16 * @param string $newClass to add.
17 * @return string|array classes.
18 * @internal
19 */
20 public static function addClassToClassList( $class, string $newClass ) {
21 if ( is_array( $class ) ) {
22 $class[] = $newClass;
23 } else {
24 $class .= ' ' . $newClass;
25 $class = trim( $class );
26 }
27 return $class;
28 }
29
30 /**
31 * Builds query params for the page to return to, used when building links
32 * @unstable
33 *
34 * @param Title $title
35 * @param WebRequest $request
36 * @param Authority $authority
37 * @return string[]
38 */
39 public static function getReturnToParam( $title, $request, $authority ) {
40 # Due to T34276, if a user does not have read permissions,
41 # $this->getTitle() will just give Special:Badtitle, which is
42 # not especially useful as a returnto parameter. Use the title
43 # from the request instead, if there was one.
44 if ( $authority->isAllowed( 'read' ) ) {
45 $page = $title;
46 } else {
47 $page = Title::newFromText( $request->getVal( 'title', '' ) );
48 }
49 $page = $request->getVal( 'returnto', $page );
50
51 $query = [];
52 if ( !$request->wasPosted() ) {
53 $query = $request->getValues();
54 unset( $query['title'] );
55 unset( $query['returnto'] );
56 unset( $query['returntoquery'] );
57 }
58
59 $thisquery = wfArrayToCgi( $query );
60
61 $returnto = [];
62 if ( strval( $page ) !== '' ) {
63 $returnto['returnto'] = $page;
64 $query = $request->getVal( 'returntoquery', $thisquery );
65 $paramsArray = wfCgiToArray( $query );
66 $query = wfArrayToCgi( $paramsArray );
67 if ( $query != '' ) {
68 $returnto['returntoquery'] = $query;
69 }
70 }
71
72 return $returnto;
73 }
74
75 /**
76 * Make a URL for a Special Page using the given query and protocol.
77 *
78 * If $proto is set to null, make a local URL. Otherwise, make a full
79 * URL with the protocol specified.
80 *
81 * @param string $name Name of the Special page
82 * @param string|array $urlaction Query to append
83 * @param string|null $proto Protocol to use or null for a local URL
84 * @return string
85 */
86 public static function makeSpecialUrl( $name, $urlaction = '', $proto = null ) {
87 $title = SpecialPage::getSafeTitleFor( $name );
88 if ( $proto === null ) {
89 return $title->getLocalURL( $urlaction );
90 } else {
91 return $title->getFullURL( $urlaction, false, $proto );
92 }
93 }
94
95 /**
96 * @param string $name
97 * @param string|bool $subpage false for no subpage
98 * @param string|array $urlaction
99 * @return string
100 */
101 public static function makeSpecialUrlSubpage( $name, $subpage, $urlaction = '' ) {
102 $title = SpecialPage::getSafeTitleFor( $name, $subpage );
103 return $title->getLocalURL( $urlaction );
104 }
105 }