SWLGroups::getMatchingWatchGroups()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * Static class with functions interact with watchlist groups.
5
 *
6
 * @since 0.1
7
 *
8
 * @file SWL_Groups.php
9
 * @ingroup SemanticWatchlist
10
 *
11
 * @licence GNU GPL v3 or later
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
final class SWLGroups {
15
16
	/**
17
	 * Cached list of all watchlist groups.
18
	 *
19
	 * @var array of SWLGroup
20
	 */
21
	private static $groups = false;
22
23
    /**
24
     * Returns all watchlist groups.
25
     *
26
     * @since 0.1
27
     *
28
     * @return array of SWLGroup
29
     */
30
	public static function getAll() {
31
		if ( self::$groups === false ) {
32
			self::$groups = array();
33
34
	        $dbr = wfGetDB( DB_REPLICA );
35
36
	        $groups = $dbr->select( 'swl_groups', array(
37
				'group_id',
38
				'group_name',
39
				'group_categories',
40
				'group_namespaces',
41
				'group_properties',
42
				'group_concepts',
43
				'group_custom_texts'
44
	        ) );
45
46
	        foreach ( $groups as $group ) {
47
	        	self::$groups[] = SWLGroup::newFromDBResult( $group );
48
	        }
49
		}
50
51
        return self::$groups;
52
	}
53
54
    /**
55
     * Returns all watchlist groups that watch the specified page.
56
     *
57
     * @since 0.1
58
     *
59
     * @param Title $title
60
     *
61
     * @return array of SWLGroup
62
     */
63
    public static function getMatchingWatchGroups( Title $title ) {
64
        $matchingGroups = array();
65
66
        foreach ( self::getAll() as /* SWLGroup */ $group ) {
67
            if ( $group->coversPage( $title ) ) {
68
                $matchingGroups[] = $group;
69
            }
70
        }
71
72
        return $matchingGroups;
73
    }
74
75
    /**
76
     * Returns all watchlist groups that are watched by the specified user.
77
     *
78
     * @since 0.1
79
     *
80
     * @param User $user
81
     *
82
     * @return array of SWLGroup
83
     */
84
	public static function getGroupsForUser( User $user ) {
85
        $matchingGroups = array();
86
87
        foreach ( self::getAll() as /* SWLGroup */ $group ) {
88
            if ( $group->isWatchedByUser( $user ) ) {
89
                $matchingGroups[] = $group;
90
            }
91
        }
92
93
        return $matchingGroups;
94
	}
95
96
}
97