Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FRUserActivity
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 numUsersWatchingPage
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3use MediaWiki\MediaWikiServices;
4use MediaWiki\Title\Title;
5use Wikimedia\Rdbms\Database;
6
7/**
8 * Class of utility functions for getting/tracking user activity
9 */
10class FRUserActivity {
11    /**
12     * Get number of active users watching a page
13     * @param Title $title
14     * @return int
15     */
16    public static function numUsersWatchingPage( Title $title ) {
17        $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
18        $fname = __METHOD__;
19
20        return $cache->getWithSetCallback(
21            $cache->makeKey( 'flaggedrevs-users-watching', $title->getArticleID() ),
22            $cache::TTL_MINUTE * 5,
23            static function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache, $title, $fname ) {
24                global $wgActiveUserDays;
25
26                $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
27
28                $setOpts += Database::getCacheSetOptions( $dbr );
29                // Get number of active editors watching this page...
30                $count = (int)$dbr->newSelectQueryBuilder()
31                    ->select( 'COUNT(*)' )
32                    ->from( 'watchlist' )
33                    ->join( 'user', null, 'wl_user = user_id' )
34                    ->where( [
35                        'wl_namespace' => $title->getNamespace(),
36                        'wl_title' => $title->getDBkey(),
37                        'EXISTS(' . $dbr->newSelectQueryBuilder()
38                            ->select( '1' )
39                            ->from( 'recentchanges' )
40                            ->join( 'actor', null, 'actor_id=rc_actor' )
41                            ->where( [
42                                'actor_name=user_name',
43                                $dbr->expr( 'rc_timestamp', '>',
44                                    $dbr->timestamp( time() - 86400 * $wgActiveUserDays ) )
45                            ] )
46                            ->caller( $fname )
47                            ->getSQL() .
48                        ')'
49                    ] )
50                    ->caller( $fname )
51                    ->fetchField();
52
53                if ( $count > 100 ) {
54                    // More aggresive caching for larger counts
55                    $ttl = $cache::TTL_MINUTE * 30;
56                }
57
58                return $count;
59            }
60        );
61    }
62
63}