| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Formatters; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use MediaWiki\Extension\Notifications\DiscussionParser; |
| 7 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 8 |
use MediaWiki\Language\Language; |
| 9 |
use MediaWiki\Parser\Parser; |
| 10 |
use MediaWiki\Revision\RevisionRecord; |
| 11 |
use MediaWiki\Title\Title; |
| 12 |
use MediaWiki\User\User; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class EchoPresentationModelSection { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private $rawSectionTitle = null; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private $parsedSectionTitle = null; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected $event; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $user; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected $language; |
| 43 |
|
| 44 |
public function __construct( Event $event, User $user, Language $language ) { |
| 45 |
$this->event = $event; |
| 46 |
$this->user = $user; |
| 47 |
$this->language = $language; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected function getRawSectionTitle() { |
| 55 |
if ( $this->rawSectionTitle !== null ) { |
| 56 |
return $this->rawSectionTitle; |
| 57 |
} |
| 58 |
$sectionTitle = $this->event->getExtraParam( 'section-title' ); |
| 59 |
if ( !$sectionTitle ) { |
| 60 |
$this->rawSectionTitle = false; |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
if ( !$this->event->userCan( RevisionRecord::DELETED_TEXT, $this->user ) ) { |
| 65 |
$this->rawSectionTitle = false; |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
$this->rawSectionTitle = $sectionTitle; |
| 70 |
return $this->rawSectionTitle; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function getParsedSectionTitle() { |
| 78 |
if ( $this->parsedSectionTitle !== null ) { |
| 79 |
return $this->parsedSectionTitle; |
| 80 |
} |
| 81 |
$rawSectionTitle = $this->getRawSectionTitle(); |
| 82 |
if ( !$rawSectionTitle ) { |
| 83 |
$this->parsedSectionTitle = false; |
| 84 |
return false; |
| 85 |
} |
| 86 |
$this->parsedSectionTitle = DiscussionParser::getTextSnippet( |
| 87 |
$rawSectionTitle, |
| 88 |
$this->language, |
| 89 |
DiscussionParser::DEFAULT_SNIPPET_LENGTH, |
| 90 |
$this->event->getTitle(), |
| 91 |
`, |
| 92 |
|
| 93 |
false |
| 94 |
); |
| 95 |
return $this->parsedSectionTitle; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function exists() { |
| 107 |
return (bool)$this->getRawSectionTitle(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function getTitleWithSection() { |
| 115 |
$title = $this->event->getTitle(); |
| 116 |
if ( $title === null ) { |
| 117 |
throw new BadMethodCallException( 'Event #' . $this->event->getId() . ' with no title' ); |
| 118 |
} |
| 119 |
$section = $this->getParsedSectionTitle(); |
| 120 |
if ( $section ) { |
| 121 |
$fragment = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 ); |
| 122 |
$title = $title->createFragmentTarget( $fragment ); |
| 123 |
} |
| 124 |
return $title; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function getTruncatedSectionTitle() { |
| 133 |
return $this->language->embedBidi( $this->language->truncateForVisual( |
| 134 |
$this->getParsedSectionTitle(), |
| 135 |
EchoEventPresentationModel::SECTION_TITLE_RECOMMENDED_LENGTH, |
| 136 |
'...', |
| 137 |
false |
| 138 |
) ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
class_alias( EchoPresentationModelSection::class, 'EchoPresentationModelSection' ); |