Completed
Branch master (771964)
by
unknown
26:13
created

WatchedItem   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
lcom 1
cbo 3
dl 0
loc 221
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getUser() 0 3 1
A getLinkTarget() 0 3 1
B getNotificationTimestamp() 0 17 5
A getTitle() 0 10 3
A fromUserTitle() 0 4 1
A resetNotificationTimestamp() 0 12 3
B batchAddWatch() 0 28 6
A addWatch() 0 5 1
A removeWatch() 0 8 3
A isWatched() 0 4 1
A duplicateEntries() 0 5 1
1
<?php
2
/**
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 * @ingroup Watchlist
20
 */
21
22
/**
23
 * Representation of a pair of user and title for watchlist entries.
24
 *
25
 * @author Tim Starling
26
 * @author Addshore
27
 *
28
 * @ingroup Watchlist
29
 */
30
class WatchedItem {
31
32
	/**
33
	 * @deprecated since 1.27, see User::IGNORE_USER_RIGHTS
34
	 */
35
	const IGNORE_USER_RIGHTS = User::IGNORE_USER_RIGHTS;
36
37
	/**
38
	 * @deprecated since 1.27, see User::CHECK_USER_RIGHTS
39
	 */
40
	const CHECK_USER_RIGHTS = User::CHECK_USER_RIGHTS;
41
42
	/**
43
	 * @deprecated Internal class use only
44
	 */
45
	const DEPRECATED_USAGE_TIMESTAMP = -100;
46
47
	/**
48
	 * @var bool
49
	 * @deprecated Internal class use only
50
	 */
51
	public $checkRights = User::CHECK_USER_RIGHTS;
52
53
	/**
54
	 * @var Title
55
	 * @deprecated Internal class use only
56
	 */
57
	private $title;
58
59
	/**
60
	 * @var LinkTarget
61
	 */
62
	private $linkTarget;
63
64
	/**
65
	 * @var User
66
	 */
67
	private $user;
68
69
	/**
70
	 * @var null|string the value of the wl_notificationtimestamp field
71
	 */
72
	private $notificationTimestamp;
73
74
	/**
75
	 * @param User $user
76
	 * @param LinkTarget $linkTarget
77
	 * @param null|string $notificationTimestamp the value of the wl_notificationtimestamp field
78
	 * @param bool|null $checkRights DO NOT USE - used internally for backward compatibility
79
	 */
80
	public function __construct(
81
		User $user,
82
		LinkTarget $linkTarget,
83
		$notificationTimestamp,
84
		$checkRights = null
85
	) {
86
		$this->user = $user;
87
		$this->linkTarget = $linkTarget;
88
		$this->notificationTimestamp = $notificationTimestamp;
89
		if ( $checkRights !== null ) {
90
			$this->checkRights = $checkRights;
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
91
		}
92
	}
93
94
	/**
95
	 * @return User
96
	 */
97
	public function getUser() {
98
		return $this->user;
99
	}
100
101
	/**
102
	 * @return LinkTarget
103
	 */
104
	public function getLinkTarget() {
105
		return $this->linkTarget;
106
	}
107
108
	/**
109
	 * Get the notification timestamp of this entry.
110
	 *
111
	 * @return bool|null|string
112
	 */
113
	public function getNotificationTimestamp() {
114
		// Back compat for objects constructed using self::fromUserTitle
115
		if ( $this->notificationTimestamp === self::DEPRECATED_USAGE_TIMESTAMP ) {
0 ignored issues
show
Deprecated Code introduced by
The constant WatchedItem::DEPRECATED_USAGE_TIMESTAMP has been deprecated with message: Internal class use only

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
116
			// wfDeprecated( __METHOD__, '1.27' );
117
			if ( $this->checkRights && !$this->user->isAllowed( 'viewmywatchlist' ) ) {
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
118
				return false;
119
			}
120
			$item = WatchedItemStore::getDefaultInstance()
121
				->loadWatchedItem( $this->user, $this->linkTarget );
122
			if ( $item ) {
123
				$this->notificationTimestamp = $item->getNotificationTimestamp();
0 ignored issues
show
Documentation Bug introduced by
It seems like $item->getNotificationTimestamp() can also be of type boolean. However, the property $notificationTimestamp is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
124
			} else {
125
				$this->notificationTimestamp = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type null|string of property $notificationTimestamp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
126
			}
127
		}
128
		return $this->notificationTimestamp;
129
	}
130
131
	/**
132
	 * Back compat pre 1.27 with the WatchedItemStore introduction
133
	 * @todo remove in 1.28/9
134
	 * -------------------------------------------------
135
	 */
136
137
	/**
138
	 * @return Title
139
	 * @deprecated Internal class use only
140
	 */
141
	public function getTitle() {
142
		if ( !$this->title ) {
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$title has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
143
			if ( $this->linkTarget instanceof Title ) {
144
				$this->title = $this->linkTarget;
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$title has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
145
			} else {
146
				$this->title = Title::newFromLinkTarget( $this->linkTarget );
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$title has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
147
			}
148
		}
149
		return $this->title;
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$title has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
150
	}
151
152
	/**
153
	 * @deprecated since 1.27 Use the constructor, WatchedItemStore::getWatchedItem()
154
	 *             or WatchedItemStore::loadWatchedItem()
155
	 */
156
	public static function fromUserTitle( $user, $title, $checkRights = User::CHECK_USER_RIGHTS ) {
157
		// wfDeprecated( __METHOD__, '1.27' );
158
		return new self( $user, $title, self::DEPRECATED_USAGE_TIMESTAMP, (bool)$checkRights );
0 ignored issues
show
Deprecated Code introduced by
The constant WatchedItem::DEPRECATED_USAGE_TIMESTAMP has been deprecated with message: Internal class use only

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
159
	}
160
161
	/**
162
	 * @deprecated since 1.27 Use WatchedItemStore::resetNotificationTimestamp()
163
	 */
164
	public function resetNotificationTimestamp( $force = '', $oldid = 0 ) {
165
		// wfDeprecated( __METHOD__, '1.27' );
166
		if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
167
			return;
168
		}
169
		WatchedItemStore::getDefaultInstance()->resetNotificationTimestamp(
170
			$this->user,
171
			$this->getTitle(),
0 ignored issues
show
Deprecated Code introduced by
The method WatchedItem::getTitle() has been deprecated with message: Internal class use only

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
172
			$force,
173
			$oldid
174
		);
175
	}
176
177
	/**
178
	 * @deprecated since 1.27 Use WatchedItemStore::addWatchBatch()
179
	 */
180
	public static function batchAddWatch( array $items ) {
181
		// wfDeprecated( __METHOD__, '1.27' );
182
		if ( !$items ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
183
			return false;
184
		}
185
186
		$targets = [];
187
		$users = [];
188
		/** @var WatchedItem $watchedItem */
189
		foreach ( $items as $watchedItem ) {
190
			$user = $watchedItem->getUser();
191
			if ( $watchedItem->checkRights && !$user->isAllowed( 'editmywatchlist' ) ) {
192
				continue;
193
			}
194
			$userId = $user->getId();
195
			$users[$userId] = $user;
196
			$targets[$userId][] = $watchedItem->getTitle()->getSubjectPage();
197
			$targets[$userId][] = $watchedItem->getTitle()->getTalkPage();
198
		}
199
200
		$store = WatchedItemStore::getDefaultInstance();
201
		$success = true;
202
		foreach ( $users as $userId => $user ) {
203
			$success &= $store->addWatchBatchForUser( $user, $targets[$userId] );
204
		}
205
206
		return $success;
207
	}
208
209
	/**
210
	 * @deprecated since 1.27 Use User::addWatch()
211
	 * @return bool
212
	 */
213
	public function addWatch() {
214
		// wfDeprecated( __METHOD__, '1.27' );
215
		$this->user->addWatch( $this->getTitle(), $this->checkRights );
0 ignored issues
show
Deprecated Code introduced by
The method WatchedItem::getTitle() has been deprecated with message: Internal class use only

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
216
		return true;
217
	}
218
219
	/**
220
	 * @deprecated since 1.27 Use User::removeWatch()
221
	 * @return bool
222
	 */
223
	public function removeWatch() {
224
		// wfDeprecated( __METHOD__, '1.27' );
225
		if ( $this->checkRights && !$this->user->isAllowed( 'editmywatchlist' ) ) {
0 ignored issues
show
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
226
			return false;
227
		}
228
		$this->user->removeWatch( $this->getTitle(), $this->checkRights );
0 ignored issues
show
Deprecated Code introduced by
The method WatchedItem::getTitle() has been deprecated with message: Internal class use only

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
229
		return true;
230
	}
231
232
	/**
233
	 * @deprecated since 1.27 Use User::isWatched()
234
	 * @return bool
235
	 */
236
	public function isWatched() {
237
		// wfDeprecated( __METHOD__, '1.27' );
238
		return $this->user->isWatched( $this->getTitle(), $this->checkRights );
0 ignored issues
show
Deprecated Code introduced by
The method WatchedItem::getTitle() has been deprecated with message: Internal class use only

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The property WatchedItem::$checkRights has been deprecated with message: Internal class use only

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
239
	}
240
241
	/**
242
	 * @deprecated since 1.27 Use WatchedItemStore::duplicateAllAssociatedEntries()
243
	 */
244
	public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
245
		// wfDeprecated( __METHOD__, '1.27' );
246
		$store = WatchedItemStore::getDefaultInstance();
247
		$store->duplicateAllAssociatedEntries( $oldTitle, $newTitle );
248
	}
249
250
}
251