wiki.techinc.nl/includes/editpage/IntroMessageList.php
Bartosz Dziewoński 6a20dc29ae editpage: Split off producing edit intro messages and preloaded content
Introduce two new classes, containing code split off from EditPage:
* IntroMessageBuilder (edit notices and other intro messages)
* PreloadedContentBuilder (initial text of new pages / sections)

I'm doing both of these features in one change, because they share a
lot of code. They are meant to be used by alternative editors to
support all of the features of the MediaWiki edit form. This isn't
everything you need yet (we should at least do this for the edit
checkboxes too), but it's a step.

Bug: T201613
Change-Id: If0b05710cb52a977bf4e85947d72d68683a0a29e
2023-05-16 20:51:00 +02:00

66 lines
1.8 KiB
PHP

<?php
namespace MediaWiki\EditPage;
use InvalidArgumentException;
use 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() ) {
return;
}
if ( isset( $this->skip[ $msg->getKey() ] ) ) {
return;
}
$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 ] ) ) {
return;
}
$this->list[ $key ] = $this->wrap( $html, $wrap );
}
public function getList(): array {
return $this->list;
}
}