aboutsummaryrefslogtreecommitdiffstats
path: root/includes/editpage/IntroMessageList.php
blob: 1d170d387b20b0c695a0589f10e273a90d991770 (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
<?php

namespace MediaWiki\EditPage;

use InvalidArgumentException;
use MediaWiki\Message\Message;

/**
 * Encapsulates a list of edit form intro messages (as HTML) with their identifiers.
 *
 * @internal
 */
class IntroMessageList {
	/** @var array<string,string> */
	public array $list = [];
	/** @var int IntroMessageBuilder::MORE_FRAMES or IntroMessageBuilder::LESS_FRAMES */
	private int $frames;
	/** @var array<string,true> */
	private array $skip;

	/**
	 * @param int $frames Some intro messages come with optional wrapper frames.
	 *   Pass IntroMessageBuilder::MORE_FRAMES to include the frames whenever possible,
	 *   or IntroMessageBuilder::LESS_FRAMES to omit them whenever possible.
	 * @param string[] $skip Identifiers of messages not to generate
	 */
	public function __construct( int $frames, array $skip = [] ) {
		if ( !in_array( $frames, [ IntroMessageBuilder::MORE_FRAMES, IntroMessageBuilder::LESS_FRAMES ], true ) ) {
			throw new InvalidArgumentException( "Expected MORE_FRAMES or LESS_FRAMES" );
		}
		$this->frames = $frames;
		$this->skip = array_fill_keys( $skip, true );
	}

	private function wrap( string $html, string $wrap ): string {
		if ( $this->frames === IntroMessageBuilder::LESS_FRAMES ) {
			return $html;
		}
		return str_replace( '$1', $html, $wrap );
	}

	public function add( Message $msg, string $wrap = '$1' ): void {
		if ( !$msg->isDisabled() && !isset( $this->skip[ $msg->getKey() ] ) ) {
			$this->addWithKey( $msg->getKey(), $msg->parse(), $wrap );
		}
	}

	public function addWithKey( string $key, string $html, string $wrap = '$1' ): void {
		if ( $html === '' ) {
			// Remove empty notices (T265798)
			return;
		}
		if ( !isset( $this->skip[$key] ) ) {
			$this->list[$key] = $this->wrap( $html, $wrap );
		}
	}

	public function getList(): array {
		return $this->list;
	}
}