2015-08-19 20:22:45 +00:00
|
|
|
<?php
|
2018-08-13 07:29:32 +00:00
|
|
|
|
2022-11-01 22:01:23 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Formatters;
|
|
|
|
|
|
|
|
use Language;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2016-07-20 14:53:36 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-11-01 22:01:23 +00:00
|
|
|
use Message;
|
2015-08-19 20:22:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class that each "formatter" should implement.
|
|
|
|
*
|
|
|
|
* A formatter is an output type, example formatters would be:
|
|
|
|
* * Special:Notifications
|
|
|
|
* * HTML email
|
|
|
|
* * plaintext email
|
|
|
|
*
|
|
|
|
* The formatter does not maintain any state except for the
|
|
|
|
* arguments passed in the constructor (user and language)
|
|
|
|
*/
|
|
|
|
abstract class EchoEventFormatter {
|
2019-01-04 14:03:05 +00:00
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/** @var Language */
|
|
|
|
protected $language;
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
public function __construct( User $user, Language $language ) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->language = $language;
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2020-06-01 02:17:13 +00:00
|
|
|
* @param string $distributionType 'web' or 'email'
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return string[]|string|false Output format depends on implementation, false if it cannot be formatted
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
final public function format( Event $event, string $distributionType = "web" ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
// Deleted events should have been filtered out before getting there.
|
|
|
|
// This is just to be sure.
|
|
|
|
if ( $event->isDeleted() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-20 14:53:36 +00:00
|
|
|
if ( !EchoEventPresentationModel::supportsPresentationModel( $event->getType() ) ) {
|
2018-07-03 16:49:20 +00:00
|
|
|
LoggerFactory::getInstance( 'Echo' )->debug(
|
|
|
|
"No presentation model found for event type \"{type}\"",
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2016-07-20 14:53:36 +00:00
|
|
|
'type' => $event->getType(),
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
2016-07-20 14:53:36 +00:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-01 02:17:13 +00:00
|
|
|
$model = EchoEventPresentationModel::factory( $event, $this->language, $this->user, $distributionType );
|
2015-10-29 21:13:10 +00:00
|
|
|
if ( !$model->canRender() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->formatModel( $model );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EchoEventPresentationModel $model
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return string[]|string
|
2015-10-29 21:13:10 +00:00
|
|
|
*/
|
|
|
|
abstract protected function formatModel( EchoEventPresentationModel $model );
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|