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;
|
|
|
|
|
2023-06-09 00:21:09 +00:00
|
|
|
use BadMethodCallException;
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\DiscussionParser;
|
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\Parser\Parser;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-19 04:14:59 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\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
|
|
|
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;
|
|
|
|
}
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->parsedSectionTitle = DiscussionParser::getTextSnippet(
|
2016-09-27 23:04:44 +00:00
|
|
|
$rawSectionTitle,
|
|
|
|
$this->language,
|
2022-11-12 07:19:00 +00:00
|
|
|
DiscussionParser::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 ) {
|
2023-06-09 00:21:09 +00:00
|
|
|
throw new BadMethodCallException( 'Event #' . $this->event->getId() . ' with no title' );
|
2019-12-21 05:45:14 +00:00
|
|
|
}
|
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.
|
2024-10-24 21:53:48 +00:00
|
|
|
* @return string|false
|
2019-08-02 10:23:07 +00:00
|
|
|
*/
|
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' );
|