2015-10-29 21:14:35 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-01 22:01:23 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Formatters;
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2024-10-19 22:55:03 +00:00
|
|
|
use MediaWiki\Language\Language;
|
2024-06-12 18:31:47 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-11-01 22:01:23 +00:00
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* Abstract class for formatters that process multiple events.
|
|
|
|
*
|
|
|
|
* The formatter does not maintain any state except for the
|
|
|
|
* arguments passed in the constructor (user and language)
|
|
|
|
*/
|
|
|
|
abstract class EchoEventDigestFormatter {
|
2019-01-04 14:03:05 +00:00
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/** @var Language */
|
|
|
|
protected $language;
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
public function __construct( User $user, Language $language ) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->language = $language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to IContextSource::msg for the current
|
|
|
|
* language
|
|
|
|
*
|
2022-10-09 03:40:45 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param mixed ...$params
|
2015-10-29 21:14:35 +00:00
|
|
|
* @return Message
|
|
|
|
*/
|
2022-10-09 03:40:45 +00:00
|
|
|
protected function msg( string $key, ...$params ) {
|
|
|
|
$msg = wfMessage( $key, ...$params );
|
2015-10-29 21:14:35 +00:00
|
|
|
$msg->inLanguage( $this->language );
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event[] $events
|
2015-10-29 21:14:35 +00:00
|
|
|
* @param string $distributionType 'web' or 'email'
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return string[]|false Output format depends on implementation, false if it cannot be formatted
|
2015-10-29 21:14:35 +00:00
|
|
|
*/
|
|
|
|
final public function format( array $events, $distributionType ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$models = [];
|
2015-10-29 21:14:35 +00:00
|
|
|
foreach ( $events as $event ) {
|
|
|
|
$model = EchoEventPresentationModel::factory( $event, $this->language, $this->user, $distributionType );
|
|
|
|
if ( $model->canRender() ) {
|
|
|
|
$models[] = $model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:19:01 +00:00
|
|
|
return $models ? $this->formatModels( $models ) : false;
|
2015-10-29 21:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EchoEventPresentationModel[] $models
|
2018-08-13 07:33:57 +00:00
|
|
|
* @return string[]|string
|
2015-10-29 21:14:35 +00:00
|
|
|
*/
|
|
|
|
abstract protected function formatModels( array $models );
|
|
|
|
}
|