Assignees::getNewAssignees()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace ST;
4
5
use ParserOutput;
6
use SMW\ApplicationFactory;
7
use SMW\DIWikiPage;
8
use SMWDataItem;
9
use User;
10
use WikiPage;
11
12
/** @todo: rename TaskDiff something similar */
13
class Assignees {
14
15
	private $taskAssignees;
16
	private $taskStatus;
17
18
	/**
19
	 * Previously this was SemanticTasksMailer::findOldValues
20
	 *
21
	 * @param WikiPage $article
22
	 * @return bool
23
	 */
24
	public function saveAssignees( WikiPage &$article ) {
25
		$this->taskAssignees = $this->getCurrentAssignees( $article, null );
26
		$this->taskStatus = $this->getCurrentStatus( $article, null );
27
		return true;
28
	}
29
30
	public function getSavedStatus() {
31
		return $this->taskStatus;
32
	}
33
34
	public function getSavedAssignees() {
35
		return $this->taskAssignees;
36
	}
37
38
	/**
39
	 * @param WikiPage $article
40
	 * @param $revision
41
	 * @return array
42
	 */
43
	public function getCurrentAssignees( WikiPage &$article, $revision ) {
44
		global $stgPropertyAssignedTo;
45
		return $this->getProperties( $stgPropertyAssignedTo, $article, $revision );
46
	}
47
48
	public function getCurrentCarbonCopy( WikiPage &$article, $revision ) {
49
		global $stgPropertyCarbonCopy;
50
		return $this->getProperties( $stgPropertyCarbonCopy, $article, $revision );
51
	}
52
53
	/**
54
	 * @param WikiPage $article
55
	 * @param $revision
56
	 * @return string
57
	 */
58
	public function getCurrentStatus( WikiPage &$article, $revision ) {
59
		global $stgPropertyStatus;
60
		$status = $this->getProperties( $stgPropertyStatus, $article, $revision );
61
		$statusString = '';
62
		if ( count( $status ) > 0 ) {
63
			$statusString = $status[0];
64
		}
65
		return $statusString;
66
	}
67
68
	/**
69
	 * @param WikiPage $article
70
	 * @param $revision
71
	 * @return array
72
	 */
73
	public function getNewAssignees( WikiPage &$article, $revision ) {
74
		return array_diff( $this->getCurrentAssignees( $article, $revision ), $this->taskAssignees );
75
	}
76
77
	/**
78
	 * @param WikiPage $article
79
	 * @param $revision
80
	 * @return array
81
	 */
82
	public function getRemovedAssignees( WikiPage &$article, $revision ) {
83
		return array_diff( $this->taskAssignees, $this->getCurrentAssignees( $article, $revision ) );
84
	}
85
86
	/**
87
	 * Returns an array of assignees based on $query_word
88
	 *
89
	 * @param WikiPage $article
90
	 * @return array
91
	 */
92
	public function getGroupAssignees( WikiPage &$article ) {
93
		global $stgPropertyAssignedToGroup;
94
		global $stgPropertyHasAssignee;
95
96
		$query_word = $stgPropertyAssignedToGroup;
97
		$title_text = $article->getTitle()->getFullText();
98
99
		// Array of assignees to return
100
		$assignee_arr = array();
101
102
		// get the result of the query "[[$title]][[$query_word::+]]"
103
		$properties_to_display = array( $query_word );
104
		$results = Query::getQueryResults( "[[$title_text]][[$query_word::+]]", $properties_to_display,
105
			false );
106
107
		$group_assignees = null;
108
		// In theory, there is only one row
109
		while ( $row = $results->getNext() ) {
110
			$group_assignees = $row[0];
111
		}
112
113
		// If not any row, do nothing
114
		if ( !empty( $group_assignees ) ) {
115
			while ( $group_assignee = $group_assignees->getNextObject() ) {
116
				$group_assignee = $group_assignee->getTitle();
117
				$group_name = $group_assignee->getText();
118
				$query_word = $stgPropertyHasAssignee;
119
				$properties_to_display = array( $query_word );
120
				$results = Query::getQueryResults( "[[$group_name]][[$query_word::+]]", $properties_to_display,
121
					false );
122
123
				$task_assignees = null;
124
				// In theory, there is only one row
125
				while ( $row = $results->getNext() ) {
126
					$task_assignees = $row[0];
127
				}
128
129
				if ( !empty( $task_assignees ) ) {
130
					while ( $task_assignee = $task_assignees->getNextObject() ) {
131
						$assignee_name = $task_assignee->getTitle();
132
						$assignee_name = $assignee_name->getText();
133
						/** @todo Create User object */
134
						$assignee_name = explode( ":", $assignee_name );
135
						$assignee_name = $assignee_name[0];
136
137
						array_push( $assignee_arr, $assignee_name );
138
					}
139
				}
140
			}
141
		}
142
143
		return $assignee_arr;
144
	}
145
146
	/**
147
	 * Get the email addresses of all the assignees
148
	 *
149
	 * @param array $assignees
150
	 * @return array
151
	 */
152
	static public function getAssigneeAddresses( array $assignees ) {
153
		$assignee_arr = array();
154
		foreach ( $assignees as $assignee_name ) {
155
			$assignee = User::newFromName( $assignee_name );
156
			// if assignee is the current user, do nothing
157
			# if ( $assignee->getID() != $user->getID() ) {
158
			if (!$assignee) {
159
				continue;
160
			}
161
			$assignee_mail = new \MailAddress( $assignee->getEmail(), $assignee_name );
162
			array_push( $assignee_arr, $assignee_mail );
163
			# }
164
		}
165
166
		return $assignee_arr;
167
	}
168
169
	/**
170
	 * Returns an array of properties based on $query_word
171
	 *
172
	 * @param string $propertyString The property that designate the users to notify.
173
	 * @param WikiPage $article
174
	 * @param $revision
175
	 * @return array
176
	 */
177
	private function getProperties( $propertyString, $article, $revision ) {
178
		$smwFactory = ApplicationFactory::getInstance();
179
		$mwCollaboratorFactory = $smwFactory->newMwCollaboratorFactory();
180
		if ($revision === null) {
181
			$revision = $article->getRevision();
182
		}
183
		if ($revision === null) {
184
			return [];
185
		}
186
		if ( version_compare( SMW_VERSION, '3.1', '<' ) ) {
187
			$editInfo = $mwCollaboratorFactory->newEditInfoProvider(
188
				$article,
189
				$revision,
190
				null
191
			);
192
		} else {
193
			$editInfo = $mwCollaboratorFactory->newEditInfo(
194
				$article,
195
				$revision,
196
				null
197
			);
198
		}
199
		$editInfo->fetchEditInfo();
200
		$parserOutput = $editInfo->getOutput();
201
202
		if ( !$parserOutput instanceof ParserOutput ) {
0 ignored issues
show
Bug introduced by
The class ParserOutput does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
203
			return [];
204
		}
205
206
		$propertyStringUnderscores = str_replace( ' ', '_', $propertyString );
207
		$property = new \SMW\DIProperty( $propertyStringUnderscores, false );
208
209
		/** @var $semanticData \SMW\SemanticData */
210
		$semanticData = $parserOutput->getExtensionData( 'smwdata' );
211
		if ( $semanticData === null ) {
212
			return [];
213
		}
214
		$propValues = $semanticData->getPropertyValues( $property );
215
		$valueList = array_map(function( SMWDataItem $propVal ) {
216
			if ($propVal instanceof DIWikiPage) {
217
				return $propVal->getTitle()->getText();
218
			}
219
			return $propVal;
220
		}, $propValues);
221
222
		return $valueList;
223
	}
224
}
225
226