Merge "Revert "Allow auto suggestion for subpages of Special:BotPasswords""
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
1 <?php
2
3 /**
4 * Storage layer class for WatchedItems.
5 * Database interaction
6 *
7 * @author Addshore
8 *
9 * @since 1.27
10 */
11 class WatchedItemStore {
12
13 /**
14 * @var LoadBalancer
15 */
16 private $loadBalancer;
17
18 public function __construct( LoadBalancer $loadBalancer ) {
19 $this->loadBalancer = $loadBalancer;
20 }
21
22 /**
23 * @return self
24 */
25 public static function getDefaultInstance() {
26 static $instance;
27 if ( !$instance ) {
28 $instance = new self( wfGetLB() );
29 }
30 return $instance;
31 }
32
33 /**
34 * Check if the given title already is watched by the user, and if so
35 * add a watch for the new title.
36 *
37 * To be used for page renames and such.
38 * This must be called separately for Subject and Talk pages
39 *
40 * @param LinkTarget $oldTarget
41 * @param LinkTarget $newTarget
42 */
43 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
44 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
45
46 $result = $dbw->select(
47 'watchlist',
48 [ 'wl_user', 'wl_notificationtimestamp' ],
49 [
50 'wl_namespace' => $oldTarget->getNamespace(),
51 'wl_title' => $oldTarget->getDBkey(),
52 ],
53 __METHOD__,
54 [ 'FOR UPDATE' ]
55 );
56
57 $newNamespace = $newTarget->getNamespace();
58 $newDBkey = $newTarget->getDBkey();
59
60 # Construct array to replace into the watchlist
61 $values = [];
62 foreach ( $result as $row ) {
63 $values[] = [
64 'wl_user' => $row->wl_user,
65 'wl_namespace' => $newNamespace,
66 'wl_title' => $newDBkey,
67 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
68 ];
69 }
70
71 if ( !empty( $values ) ) {
72 # Perform replace
73 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
74 # some other DBMSes, mostly due to poor simulation by us
75 $dbw->replace(
76 'watchlist',
77 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
78 $values,
79 __METHOD__
80 );
81 }
82
83 $this->loadBalancer->reuseConnection( $dbw );
84 }
85
86 }