WatchedItemStoreInterface.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. use MediaWiki\Linker\LinkTarget;
  22. use MediaWiki\User\UserIdentity;
  23. use Wikimedia\Rdbms\DBUnexpectedError;
  24. /**
  25. * @author Addshore
  26. * @since 1.31 interface created. WatchedItemStore implementation available since 1.27
  27. */
  28. interface WatchedItemStoreInterface {
  29. /**
  30. * @since 1.31
  31. */
  32. const SORT_ASC = 'ASC';
  33. /**
  34. * @since 1.31
  35. */
  36. const SORT_DESC = 'DESC';
  37. /**
  38. * Count the number of individual items that are watched by the user.
  39. * If a subject and corresponding talk page are watched this will return 2.
  40. *
  41. * @since 1.31
  42. *
  43. * @param UserIdentity $user
  44. *
  45. * @return int
  46. */
  47. public function countWatchedItems( UserIdentity $user );
  48. /**
  49. * @since 1.31
  50. *
  51. * @param LinkTarget $target
  52. *
  53. * @return int
  54. */
  55. public function countWatchers( LinkTarget $target );
  56. /**
  57. * Number of page watchers who also visited a "recent" edit
  58. *
  59. * @since 1.31
  60. *
  61. * @param LinkTarget $target
  62. * @param mixed $threshold timestamp accepted by wfTimestamp
  63. *
  64. * @return int
  65. * @throws DBUnexpectedError
  66. * @throws MWException
  67. */
  68. public function countVisitingWatchers( LinkTarget $target, $threshold );
  69. /**
  70. * @since 1.31
  71. *
  72. * @param LinkTarget[] $targets
  73. * @param array $options Allowed keys:
  74. * 'minimumWatchers' => int
  75. *
  76. * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
  77. * All targets will be present in the result. 0 either means no watchers or the number
  78. * of watchers was below the minimumWatchers option if passed.
  79. */
  80. public function countWatchersMultiple( array $targets, array $options = [] );
  81. /**
  82. * Number of watchers of each page who have visited recent edits to that page
  83. *
  84. * @since 1.31
  85. *
  86. * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed
  87. * $threshold),
  88. * $threshold is:
  89. * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
  90. * - null if $target doesn't exist
  91. * @param int|null $minimumWatchers
  92. *
  93. * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
  94. * where $watchers is an int:
  95. * - if the page exists, number of users watching who have visited the page recently
  96. * - if the page doesn't exist, number of users that have the page on their watchlist
  97. * - 0 means there are no visiting watchers or their number is below the
  98. * minimumWatchers
  99. * option (if passed).
  100. */
  101. public function countVisitingWatchersMultiple(
  102. array $targetsWithVisitThresholds,
  103. $minimumWatchers = null
  104. );
  105. /**
  106. * Get an item (may be cached)
  107. *
  108. * @since 1.31
  109. *
  110. * @param UserIdentity $user
  111. * @param LinkTarget $target
  112. *
  113. * @return WatchedItem|false
  114. */
  115. public function getWatchedItem( UserIdentity $user, LinkTarget $target );
  116. /**
  117. * Loads an item from the db
  118. *
  119. * @since 1.31
  120. *
  121. * @param UserIdentity $user
  122. * @param LinkTarget $target
  123. *
  124. * @return WatchedItem|false
  125. */
  126. public function loadWatchedItem( UserIdentity $user, LinkTarget $target );
  127. /**
  128. * @since 1.31
  129. *
  130. * @param UserIdentity $user
  131. * @param array $options Allowed keys:
  132. * 'forWrite' => bool defaults to false
  133. * 'sort' => string optional sorting by namespace ID and title
  134. * one of the self::SORT_* constants
  135. *
  136. * @return WatchedItem[]
  137. */
  138. public function getWatchedItemsForUser( UserIdentity $user, array $options = [] );
  139. /**
  140. * Must be called separately for Subject & Talk namespaces
  141. *
  142. * @since 1.31
  143. *
  144. * @param UserIdentity $user
  145. * @param LinkTarget $target
  146. *
  147. * @return bool
  148. */
  149. public function isWatched( UserIdentity $user, LinkTarget $target );
  150. /**
  151. * @since 1.31
  152. *
  153. * @param UserIdentity $user
  154. * @param LinkTarget[] $targets
  155. *
  156. * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
  157. * where $timestamp is:
  158. * - string|null value of wl_notificationtimestamp,
  159. * - false if $target is not watched by $user.
  160. */
  161. public function getNotificationTimestampsBatch( UserIdentity $user, array $targets );
  162. /**
  163. * Must be called separately for Subject & Talk namespaces
  164. *
  165. * @since 1.31
  166. *
  167. * @param UserIdentity $user
  168. * @param LinkTarget $target
  169. */
  170. public function addWatch( UserIdentity $user, LinkTarget $target );
  171. /**
  172. * @since 1.31
  173. *
  174. * @param UserIdentity $user
  175. * @param LinkTarget[] $targets
  176. *
  177. * @return bool success
  178. */
  179. public function addWatchBatchForUser( UserIdentity $user, array $targets );
  180. /**
  181. * Removes an entry for the UserIdentity watching the LinkTarget
  182. * Must be called separately for Subject & Talk namespaces
  183. *
  184. * @since 1.31
  185. *
  186. * @param UserIdentity $user
  187. * @param LinkTarget $target
  188. *
  189. * @return bool success
  190. * @throws DBUnexpectedError
  191. * @throws MWException
  192. */
  193. public function removeWatch( UserIdentity $user, LinkTarget $target );
  194. /**
  195. * @since 1.31
  196. *
  197. * @param UserIdentity $user The user to set the timestamps for
  198. * @param string|null $timestamp Set the update timestamp to this value
  199. * @param LinkTarget[] $targets List of targets to update. Default to all targets
  200. *
  201. * @return bool success
  202. */
  203. public function setNotificationTimestampsForUser(
  204. UserIdentity $user,
  205. $timestamp,
  206. array $targets = []
  207. );
  208. /**
  209. * Reset all watchlist notificaton timestamps for a user using the job queue
  210. *
  211. * @since 1.31
  212. *
  213. * @param UserIdentity $user The user to reset the timestamps for
  214. */
  215. public function resetAllNotificationTimestampsForUser( UserIdentity $user );
  216. /**
  217. * @since 1.31
  218. *
  219. * @param UserIdentity $editor The editor that triggered the update. Their notification
  220. * timestamp will not be updated(they have already seen it)
  221. * @param LinkTarget $target The target to update timestamps for
  222. * @param string $timestamp Set the update (first unseen revision) timestamp to this value
  223. *
  224. * @return int[] Array of user IDs the timestamp has been updated for
  225. */
  226. public function updateNotificationTimestamp(
  227. UserIdentity $editor, LinkTarget $target, $timestamp );
  228. /**
  229. * Reset the notification timestamp of this entry
  230. *
  231. * @since 1.31
  232. *
  233. * @param UserIdentity $user
  234. * @param LinkTarget $title
  235. * @param string $force Whether to force the write query to be executed even if the
  236. * page is not watched or the notification timestamp is already NULL.
  237. * 'force' in order to force
  238. * @param int $oldid The revision id being viewed. If not given or 0, latest revision is
  239. * assumed.
  240. *
  241. * @return bool success Whether a job was enqueued
  242. */
  243. public function resetNotificationTimestamp(
  244. UserIdentity $user, LinkTarget $title, $force = '', $oldid = 0 );
  245. /**
  246. * @since 1.31
  247. *
  248. * @param UserIdentity $user
  249. * @param int|null $unreadLimit
  250. *
  251. * @return int|bool The number of unread notifications
  252. * true if greater than or equal to $unreadLimit
  253. */
  254. public function countUnreadNotifications( UserIdentity $user, $unreadLimit = null );
  255. /**
  256. * Check if the given title already is watched by the user, and if so
  257. * add a watch for the new title.
  258. *
  259. * To be used for page renames and such.
  260. *
  261. * @since 1.31
  262. *
  263. * @param LinkTarget $oldTarget
  264. * @param LinkTarget $newTarget
  265. */
  266. public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget );
  267. /**
  268. * Check if the given title already is watched by the user, and if so
  269. * add a watch for the new title.
  270. *
  271. * To be used for page renames and such.
  272. * This must be called separately for Subject and Talk pages
  273. *
  274. * @since 1.31
  275. *
  276. * @param LinkTarget $oldTarget
  277. * @param LinkTarget $newTarget
  278. */
  279. public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget );
  280. /**
  281. * Queues a job that will clear the users watchlist using the Job Queue.
  282. *
  283. * @since 1.31
  284. *
  285. * @param UserIdentity $user
  286. */
  287. public function clearUserWatchedItems( UserIdentity $user );
  288. /**
  289. * Queues a job that will clear the users watchlist using the Job Queue.
  290. *
  291. * @since 1.31
  292. *
  293. * @param UserIdentity $user
  294. */
  295. public function clearUserWatchedItemsUsingJobQueue( UserIdentity $user );
  296. /**
  297. * @since 1.32
  298. *
  299. * @param UserIdentity $user
  300. * @param LinkTarget[] $targets
  301. *
  302. * @return bool success
  303. */
  304. public function removeWatchBatchForUser( UserIdentity $user, array $targets );
  305. /**
  306. * Convert $timestamp to TS_MW or return null if the page was visited since then by $user
  307. *
  308. * Use this only on single-user methods (having higher read-after-write expectations)
  309. * and not in places involving arbitrary batches of different users
  310. *
  311. * Usage of this method should be limited to WatchedItem* classes
  312. *
  313. * @param string|null $timestamp Value of wl_notificationtimestamp from the DB
  314. * @param UserIdentity $user
  315. * @param LinkTarget $target
  316. * @return string|null TS_MW timestamp of first unseen revision or null if there isn't one
  317. */
  318. public function getLatestNotificationTimestamp(
  319. $timestamp, UserIdentity $user, LinkTarget $target );
  320. }