summaryrefslogtreecommitdiff
blob: 5db4771f294640b7ef5b948fd4101fa54379d871 (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
<?php

/**
 * Static class for handling all kinds of event logging
 */
class MWEchoEventLogging {

	private static $revisionIds = [
		'Echo' => 7731316,
		'EchoMail' => 5467650,
		// Keep in sync with client-side revision
		// in extension.json
		'EchoInteraction' => 15823738
	];

	/**
	 * This is the only function that interacts with EventLogging
	 *
	 * Adds common fields, and logs if logging is enabled for the given $schema.
	 *
	 * @param string $schema
	 * @param array $data
	 */
	protected static function logEvent( $schema, array $data ) {
		global $wgEchoEventLoggingSchemas, $wgEchoEventLoggingVersion;

		$schemaConfig = $wgEchoEventLoggingSchemas[$schema];
		if ( !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' )
			|| !$schemaConfig['enabled']
		) {
			// If logging for this schema is disabled, it's a no-op.
			return;
		}

		$revision = self::$revisionIds[$schema];
		$data['version'] = $wgEchoEventLoggingVersion;

		EventLogging::logEvent( $schema, $revision, $data );
	}

	/**
	 * Function for logging the event for Schema:Echo
	 * @param User $user User being notified.
	 * @param EchoEvent $event Event to log detail about.
	 * @param string $deliveryMethod 'web' or 'email'
	 */
	public static function logSchemaEcho( User $user, EchoEvent $event, $deliveryMethod ) {
		global $wgEchoNotifications;

		// Notifications under system category should have -1 as sender id
		if ( $event->getCategory() === 'system' ) {
			$sender = -1;
		} else {
			$agent = $event->getAgent();
			if ( $agent ) {
				$sender = $agent->isAnon() ? $agent->getName() : $agent->getId();
			} else {
				$sender = -1;
			}
		}

		if ( isset( $wgEchoNotifications[$event->getType()]['group'] ) ) {
			$group = $wgEchoNotifications[$event->getType()]['group'];
		} else {
			$group = 'neutral';
		}
		$data = [
			'eventId' => (int)$event->getId(),
			'notificationType' => $event->getType(),
			'notificationGroup' => $group,
			'sender' => (string)$sender,
			'recipientUserId' => $user->getId(),
			'recipientEditCount' => (int)$user->getEditCount()
		];
		// Add the source if it exists. (This is mostly for the Thanks extension.)
		$extra = $event->getExtra();
		if ( isset( $extra['source'] ) ) {
			$data['eventSource'] = (string)$extra['source'];
		}
		if ( $deliveryMethod === 'email' ) {
			$data['deliveryMethod'] = 'email';
		} else {
			// whitelist valid delivery methods so it is always valid
			$data['deliveryMethod'] = 'web';
		}
		// Add revision ID if it exists
		$rev = $event->getRevision();
		if ( $rev ) {
			$data['revisionId'] = $rev->getId();
		}

		self::logEvent( 'Echo', $data );
	}

	/**
	 * Function for logging the event for Schema:EchoEmail
	 * @param User $user
	 * @param string $emailDeliveryMode 'single' (default), 'daily_digest', or 'weekly_digest'
	 */
	public static function logSchemaEchoMail( User $user, $emailDeliveryMode = 'single' ) {
		$data = [
			'recipientUserId' => $user->getId(),
			'emailDeliveryMode' => $emailDeliveryMode
		];

		self::logEvent( 'EchoMail', $data );
	}

	/**
	 * @param User $user
	 * @param string $skinName
	 */
	public static function logSpecialPageVisit( User $user, $skinName ) {
		self::logEvent(
			'EchoInteraction',
			[
				'context' => 'archive',
				'action' => 'special-page-visit',
				'userId' => (int)$user->getId(),
				'editCount' => (int)$user->getEditCount(),
				'notifWiki' => wfWikiID(),
				// Hack: Figure out if we are in the mobile skin
				'mobile' => $skinName === 'minerva',
			]
		);
	}

}