| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class ContainmentSet { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected $lists = []; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $recipient; |
| 36 |
|
| 37 |
public function __construct( User $recipient ) { |
| 38 |
$this->recipient = $recipient; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function add( ContainmentList $list ) { |
| 47 |
$this->lists[] = $list; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function addArray( array $list ) { |
| 56 |
$this->add( new ArrayList( $list ) ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
return; |
| 90 |
} |
| 91 |
$titleIDs = preg_split( '/\n/', $preference, -1, PREG_SPLIT_NO_EMPTY ); |
| 92 |
$this->addArray( $titleIDs ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function contains( $value ) { |
| 125 |
foreach ( $this->lists as $list ) { |
| 126 |
|
| 127 |
if ( in_array( $value, $list->getValues(), true ) ) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return false; |
| 133 |
} |
| 134 |
} |