Debian Codegraph @

extensions/Echo/includes/Formatters/EchoPresentationModelSection.php

raw symbols SCIP
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 * Component that represents a section of a page to be used from EchoEventPresentationModel subclass.
16 */
17 class EchoPresentationModelSection {
18
19 /**
20 * @var string|false|null
21 */
22 private $rawSectionTitle = null;
23
24 /**
25 * @var string|false|null
26 */
27 private $parsedSectionTitle = null;
28
29 /**
30 * @var Event
31 */
32 protected $event;
33
34 /**
35 * @var User
36 */
37 protected $user;
38
39 /**
40 * @var Language
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 * Get the raw (unparsed) section title
52 * @return string|false Section title
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 // Check permissions
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 * Get the section title parsed to plain text
75 * @return string|false Section title (plain text)
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 // linestart=false, because this wikitext was inside a heading like `== … ==`,
92 // so start-of-line markup like `*` should not be parsed (T299572)
93 false
94 );
95 return $this->parsedSectionTitle;
96 }
97
98 /**
99 * Check if there is a section.
100 *
101 * This also returns false if the revision is deleted,
102 * even if there is a section, because the section can't
103 * be viewed in that case.
104 * @return bool Whether there is a section
105 */
106 public function exists() {
107 return (bool)$this->getRawSectionTitle();
108 }
109
110 /**
111 * Get a Title pointing to the section, if available.
112 * @return Title
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 * Get truncated section title, according to user's language.
129 * You should only call this if EchoPresentationModelSection::exists returns true.
130 * @return string
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' );