Debian Codegraph @

extensions/Echo/includes/Model/TargetPage.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Model;
4
5 use InvalidArgumentException;
6 use MediaWiki\Title\Title;
7 use stdClass;
8
9 /**
10 * Map a title to an echo event so that we can mark a notification as read
11 * when visiting the page. This only supports titles with ids because majority
12 * of notifications have page_id and searching by namespace and title is slow
13 */
14 class TargetPage extends AbstractEntity {
15
16 /**
17 * @var Title|null|false False if not initialized yet
18 */
19 protected $title = false;
20
21 /**
22 * @var int
23 */
24 protected $pageId;
25
26 /**
27 * @var Event|null
28 */
29 protected $event;
30
31 /**
32 * @var int
33 */
34 protected $eventId;
35
36 /**
37 * @var string
38 */
39 protected $eventType;
40
41 /**
42 * Only allow creating instance internally
43 */
44 protected function __construct() {
45 }
46
47 /**
48 * Create a TargetPage instance from Title and Event
49 *
50 * @param Title $title
51 * @param Event $event
52 * @return TargetPage|null
53 */
54 public static function create( Title $title, Event $event ) {
55 // This only support title with a page_id
56 if ( !$title->getArticleID() ) {
57 return null;
58 }
59 $obj = new self();
60 $obj->event = $event;
61 $obj->eventId = $event->getId();
62 $obj->eventType = $event->getType();
63 $obj->title = $title;
64 $obj->pageId = $title->getArticleID();
65
66 return $obj;
67 }
68
69 /**
70 * Create a TargetPage instance from stdClass object
71 *
72 * @param stdClass $row
73 * @return TargetPage
74 */
75 public static function newFromRow( $row ) {
76 $requiredFields = [
77 'etp_page',
78 'etp_event'
79 ];
80 foreach ( $requiredFields as $field ) {
81 if ( !isset( $row->$field ) || !$row->$field ) {
82 throw new InvalidArgumentException( $field . ' is not set in the row!' );
83 }
84 }
85 $obj = new self();
86 $obj->pageId = $row->etp_page;
87 $obj->eventId = $row->etp_event;
88 if ( isset( $row->event_type ) ) {
89 $obj->eventType = $row->event_type;
90 }
91
92 return $obj;
93 }
94
95 /**
96 * @return Title|null
97 */
98 public function getTitle() {
99 if ( $this->title === false ) {
100 $this->title = Title::newFromID( $this->pageId );
101 }
102
103 return $this->title;
104 }
105
106 /**
107 * @return int
108 */
109 public function getPageId() {
110 return $this->pageId;
111 }
112
113 /**
114 * @return Event
115 */
116 public function getEvent() {
117 if ( !$this->event ) {
118 $this->event = Event::newFromID( $this->eventId );
119 }
120
121 return $this->event;
122 }
123
124 /**
125 * @return int
126 */
127 public function getEventId() {
128 return $this->eventId;
129 }
130
131 /**
132 * @return string
133 */
134 public function getEventType() {
135 if ( !$this->eventType ) {
136 $this->eventType = $this->getEvent()->getType();
137 }
138
139 return $this->eventType;
140 }
141
142 /**
143 * Convert the properties to a database row
144 * @return int[]
145 */
146 public function toDbArray() {
147 return [
148 'etp_page' => $this->pageId,
149 'etp_event' => $this->eventId
150 ];
151 }
152 }