2016-04-22 02:33:35 +00:00
|
|
|
<?php
|
2019-04-17 15:46:06 +00:00
|
|
|
|
2022-11-01 22:01:23 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Formatters;
|
|
|
|
|
|
|
|
use EchoDiscussionParser;
|
|
|
|
use Language;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2022-11-01 22:01:23 +00:00
|
|
|
use MWException;
|
|
|
|
use Parser;
|
|
|
|
use Title;
|
|
|
|
use User;
|
2019-04-17 15:46:06 +00:00
|
|
|
|
2016-04-22 02:33:35 +00:00
|
|
|
/**
|
2019-04-05 20:50:03 +00:00
|
|
|
* Component that represents a section of a page to be used from EchoEventPresentationModel subclass.
|
2016-04-22 02:33:35 +00:00
|
|
|
*/
|
2019-04-05 20:50:03 +00:00
|
|
|
class EchoPresentationModelSection {
|
|
|
|
|
|
|
|
/**
|
2019-08-02 10:23:07 +00:00
|
|
|
* @var string|false|null
|
2019-04-05 20:50:03 +00:00
|
|
|
*/
|
2016-09-27 23:04:44 +00:00
|
|
|
private $rawSectionTitle = null;
|
2019-04-05 20:50:03 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-02 10:23:07 +00:00
|
|
|
* @var string|false|null
|
2019-04-05 20:50:03 +00:00
|
|
|
*/
|
2016-09-27 23:04:44 +00:00
|
|
|
private $parsedSectionTitle = null;
|
2016-04-22 02:33:35 +00:00
|
|
|
|
2019-04-05 20:50:03 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @var Event
|
2019-04-05 20:50:03 +00:00
|
|
|
*/
|
2022-01-20 22:29:39 +00:00
|
|
|
protected $event;
|
2019-04-05 20:50:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
2022-01-20 22:29:39 +00:00
|
|
|
protected $user;
|
2019-04-05 20:50:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Language
|
|
|
|
*/
|
2022-01-20 22:29:39 +00:00
|
|
|
protected $language;
|
2019-04-05 20:50:03 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2019-04-05 20:50:03 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param Language $language
|
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public function __construct( Event $event, User $user, Language $language ) {
|
2019-04-05 20:50:03 +00:00
|
|
|
$this->event = $event;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->language = $language;
|
|
|
|
}
|
|
|
|
|
2016-04-22 02:33:35 +00:00
|
|
|
/**
|
2016-09-27 23:04:44 +00:00
|
|
|
* Get the raw (unparsed) section title
|
2019-08-02 10:23:07 +00:00
|
|
|
* @return string|false Section title
|
2016-04-22 02:33:35 +00:00
|
|
|
*/
|
2022-01-20 22:29:39 +00:00
|
|
|
protected function getRawSectionTitle() {
|
2016-09-27 23:04:44 +00:00
|
|
|
if ( $this->rawSectionTitle !== null ) {
|
|
|
|
return $this->rawSectionTitle;
|
2016-04-22 02:33:35 +00:00
|
|
|
}
|
|
|
|
$sectionTitle = $this->event->getExtraParam( 'section-title' );
|
|
|
|
if ( !$sectionTitle ) {
|
2016-09-27 23:04:44 +00:00
|
|
|
$this->rawSectionTitle = false;
|
2016-04-22 02:33:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check permissions
|
2019-04-17 15:46:06 +00:00
|
|
|
if ( !$this->event->userCan( RevisionRecord::DELETED_TEXT, $this->user ) ) {
|
2016-09-27 23:04:44 +00:00
|
|
|
$this->rawSectionTitle = false;
|
2016-04-22 02:33:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-27 23:04:44 +00:00
|
|
|
$this->rawSectionTitle = $sectionTitle;
|
|
|
|
return $this->rawSectionTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the section title parsed to plain text
|
2019-08-02 10:23:07 +00:00
|
|
|
* @return string|false Section title (plain text)
|
2016-09-27 23:04:44 +00:00
|
|
|
*/
|
2022-01-20 22:29:39 +00:00
|
|
|
protected function getParsedSectionTitle() {
|
2016-09-27 23:04:44 +00:00
|
|
|
if ( $this->parsedSectionTitle !== null ) {
|
|
|
|
return $this->parsedSectionTitle;
|
|
|
|
}
|
|
|
|
$rawSectionTitle = $this->getRawSectionTitle();
|
|
|
|
if ( !$rawSectionTitle ) {
|
|
|
|
$this->parsedSectionTitle = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->parsedSectionTitle = EchoDiscussionParser::getTextSnippet(
|
|
|
|
$rawSectionTitle,
|
|
|
|
$this->language,
|
2020-07-22 22:03:44 +00:00
|
|
|
EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH,
|
2022-01-20 22:52:01 +00:00
|
|
|
$this->event->getTitle(),
|
|
|
|
// linestart=false, because this wikitext was inside a heading like `== … ==`,
|
|
|
|
// so start-of-line markup like `*` should not be parsed (T299572)
|
|
|
|
false
|
2016-09-27 23:04:44 +00:00
|
|
|
);
|
|
|
|
return $this->parsedSectionTitle;
|
2016-04-22 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there is a section.
|
|
|
|
*
|
|
|
|
* This also returns false if the revision is deleted,
|
|
|
|
* even if there is a section, because the section can't
|
|
|
|
* be viewed in that case.
|
2017-07-26 19:34:44 +00:00
|
|
|
* @return bool Whether there is a section
|
2016-04-22 02:33:35 +00:00
|
|
|
*/
|
2019-04-05 20:50:03 +00:00
|
|
|
public function exists() {
|
2016-09-27 23:04:44 +00:00
|
|
|
return (bool)$this->getRawSectionTitle();
|
2016-04-22 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Title pointing to the section, if available.
|
|
|
|
* @return Title
|
|
|
|
*/
|
2019-04-05 20:50:03 +00:00
|
|
|
public function getTitleWithSection() {
|
2016-04-22 02:33:35 +00:00
|
|
|
$title = $this->event->getTitle();
|
2019-12-21 05:45:14 +00:00
|
|
|
if ( $title === null ) {
|
|
|
|
throw new MWException( 'Event #' . $this->event->getId() . ' with no title' );
|
|
|
|
}
|
2016-09-27 23:04:44 +00:00
|
|
|
$section = $this->getParsedSectionTitle();
|
2016-04-22 02:33:35 +00:00
|
|
|
if ( $section ) {
|
2019-08-02 10:23:07 +00:00
|
|
|
$fragment = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 );
|
|
|
|
$title = $title->createFragmentTarget( $fragment );
|
2016-04-22 02:33:35 +00:00
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
2016-09-27 23:04:44 +00:00
|
|
|
|
2019-08-02 10:23:07 +00:00
|
|
|
/**
|
|
|
|
* Get truncated section title, according to user's language.
|
|
|
|
* You should only call this if EchoPresentationModelSection::exists returns true.
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-04-05 20:50:03 +00:00
|
|
|
public function getTruncatedSectionTitle() {
|
2018-06-15 20:01:16 +00:00
|
|
|
return $this->language->embedBidi( $this->language->truncateForVisual(
|
2016-09-27 23:04:44 +00:00
|
|
|
$this->getParsedSectionTitle(),
|
2019-04-05 20:50:03 +00:00
|
|
|
EchoEventPresentationModel::SECTION_TITLE_RECOMMENDED_LENGTH,
|
2016-09-27 23:04:44 +00:00
|
|
|
'...',
|
|
|
|
false
|
|
|
|
) );
|
|
|
|
}
|
2016-04-22 02:33:35 +00:00
|
|
|
}
|
2022-11-01 22:01:23 +00:00
|
|
|
|
|
|
|
class_alias( EchoPresentationModelSection::class, 'EchoPresentationModelSection' );
|