Debian Codegraph @

extensions/Echo/includes/ContainmentSet.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use BadMethodCallException;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Preferences\MultiUsernameFilter;
8 use MediaWiki\User\User;
9 use Wikimedia\ObjectCache\WANObjectCache;
10
11 /**
12 * Utilizes ContainmentList interface to provide a fluent interface to whitelist/blacklist
13 * from multiple sources like global variables, wiki pages, etc.
14 *
15 * Initialize:
16 * $cache = ObjectCacheFactory::getLocalClusterInstance();
17 * $set = new ContainmentSet;
18 * $set->addArray( $wgSomeGlobalParameter );
19 * $set->addOnWiki( NS_USER, 'Foo/bar-baz', $cache, 'some_user_specific_cache_key' );
20 *
21 * Usage:
22 * if ( $set->contains( 'SomeUser' ) ) {
23 * ...
24 * }
25 */
26 class ContainmentSet {
27 /**
28 * @var ContainmentList[]
29 */
30 protected $lists = [];
31
32 /**
33 * @var User
34 */
35 protected $recipient;
36
37 public function __construct( User $recipient ) {
38 $this->recipient = $recipient;
39 }
40
41 /**
42 * Add an ContainmentList to the set of lists checked by self::contains()
43 *
44 * @param ContainmentList $list
45 */
46 public function add( ContainmentList $list ) {
47 $this->lists[] = $list;
48 }
49
50 /**
51 * Add a php array to the set of lists checked by self::contains()
52 *
53 * @param array $list
54 */
55 public function addArray( array $list ) {
56 $this->add( new ArrayList( $list ) );
57 }
58
59 /**
60 * Add a list from a user preference to the set of lists checked by self::contains().
61 *
62 * @param string $preferenceName
63 */
64 public function addFromUserOption( string $preferenceName ) {
65 $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
66 $preference = $userOptionsLookup->getOption( $this->recipient, $preferenceName, [] );
67 if ( $preference ) {
68 $ids = MultiUsernameFilter::splitIds( $preference );
69 $names = MediaWikiServices::getInstance()
70 ->getCentralIdLookup()
71 ->namesFromCentralIds( $ids, $this->recipient );
72 $this->addArray( $names );
73 }
74 }
75
76 /**
77 * Add a list of title IDs from a user preference to the set of lists
78 * checked by self::contains().
79 *
80 * @param string $preferenceName
81 */
82 public function addTitleIDsFromUserOption( string $preferenceName ): void {
83 $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
84 $preference = $userOptionsLookup->getOption( $this->recipient, $preferenceName, [] );
85 if ( !is_string( $preference ) ) {
86 // We expect the preference data to be saved as a string via the
87 // preferences form; if the user modified their data so it's no
88 // longer a string, ignore it.
89 return;
90 }
91 $titleIDs = preg_split( '/\n/', $preference, -1, PREG_SPLIT_NO_EMPTY );
92 $this->addArray( $titleIDs );
93 }
94
95 /**
96 * Add a list from a wiki page to the set of lists checked by self::contains(). Data
97 * from wiki pages is cached via the BagOStuff. Caching is disabled when passing a null
98 * $cache object.
99 *
100 * @param int $namespace An NS_* constant representing the mediawiki namespace of the page containing the list.
101 * @param string $title The title of the page containing the list.
102 * @param WANObjectCache|null $cache An object to cache the page with or null for no cache.
103 * @param string $cacheKeyPrefix A prefix to be combined with the pages latest revision id and used as a cache key.
104 */
105 public function addOnWiki(
106 $namespace, $title, ?WANObjectCache $cache = null, $cacheKeyPrefix = ''
107 ) {
108 $list = new OnWikiList( $namespace, $title );
109 if ( $cache ) {
110 if ( $cacheKeyPrefix === '' ) {
111 throw new BadMethodCallException( 'Cache requires providing a cache key prefix.' );
112 }
113 $list = new CachedList( $cache, $cacheKeyPrefix, $list );
114 }
115 $this->add( $list );
116 }
117
118 /**
119 * Test the wrapped lists for existence of $value
120 *
121 * @param mixed $value The value to look for
122 * @return bool True when the set contains the provided value
123 */
124 public function contains( $value ) {
125 foreach ( $this->lists as $list ) {
126 // Use strict comparison to prevent the number 0 from matching all strings (T177825)
127 if ( in_array( $value, $list->getValues(), true ) ) {
128 return true;
129 }
130 }
131
132 return false;
133 }
134 }