summaryrefslogtreecommitdiff
blob: 4134f039bc8f9ec6892b42dc9f5d475af59970a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

/**
 * Map a title to an echo event so that we can mark a notification as read
 * when visiting the page. This only supports titles with ids because majority
 * of notifications have page_id and searching by namespace and title is slow
 */
class EchoTargetPage extends EchoAbstractEntity {

	/**
	 * @var Title|null|false False if not initialized yet
	 */
	protected $title = false;

	/**
	 * @var int
	 */
	protected $pageId;

	/**
	 * @var EchoEvent|null
	 */
	protected $event;

	/**
	 * @var int
	 */
	protected $eventId;

	/**
	 * @var string
	 */
	protected $eventType;

	/**
	 * Only allow creating instance internally
	 */
	protected function __construct() {
	}

	/**
	 * Create a EchoTargetPage instance from Title and EchoEvent
	 *
	 * @param Title $title
	 * @param EchoEvent $event
	 * @return EchoTargetPage|null
	 */
	public static function create( Title $title, EchoEvent $event ) {
		// This only support title with a page_id
		if ( !$title->getArticleID() ) {
			return null;
		}
		$obj = new self();
		$obj->event = $event;
		$obj->eventId = $event->getId();
		$obj->eventType = $event->getType();
		$obj->title = $title;
		$obj->pageId = $title->getArticleID();

		return $obj;
	}

	/**
	 * Create a EchoTargetPage instance from stdClass object
	 *
	 * @param stdClass $row
	 * @return EchoTargetPage
	 * @throws MWException
	 */
	public static function newFromRow( $row ) {
		$requiredFields = [
			'etp_page',
			'etp_event'
		];
		foreach ( $requiredFields as $field ) {
			if ( !isset( $row->$field ) || !$row->$field ) {
				throw new MWException( $field . ' is not set in the row!' );
			}
		}
		$obj = new self();
		$obj->pageId = $row->etp_page;
		$obj->eventId = $row->etp_event;
		if ( isset( $row->event_type ) ) {
			$obj->eventType = $row->event_type;
		}

		return $obj;
	}

	/**
	 * @return Title|null
	 */
	public function getTitle() {
		if ( $this->title === false ) {
			$this->title = Title::newFromID( $this->pageId );
		}

		return $this->title;
	}

	/**
	 * @return int
	 */
	public function getPageId() {
		return $this->pageId;
	}

	/**
	 * @return EchoEvent
	 */
	public function getEvent() {
		if ( !$this->event ) {
			$this->event = EchoEvent::newFromID( $this->eventId );
		}

		return $this->event;
	}

	/**
	 * @return int
	 */
	public function getEventId() {
		return $this->eventId;
	}

	/**
	 * @return string
	 */
	public function getEventType() {
		if ( !$this->eventType ) {
			$this->eventType = $this->getEvent()->getType();
		}

		return $this->eventType;
	}

	/**
	 * Convert the properties to a database row
	 * @return int[]
	 */
	public function toDbArray() {
		return [
			'etp_page' => $this->pageId,
			'etp_event' => $this->eventId
		];
	}
}