MediaWiki  master
MultiUsernameFilter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Preferences;
22 
23 use CentralIdLookup;
24 
25 class MultiUsernameFilter implements Filter {
29  private $lookup;
31  private $userOrAudience;
32 
37  public function __construct( CentralIdLookup $lookup = null,
39  ) {
40  $this->lookup = $lookup;
41  $this->userOrAudience = $userOrAudience;
42  }
43 
47  public function filterFromForm( $names ) {
48  $names = trim( $names );
49  if ( $names !== '' ) {
50  $names = preg_split( '/\n/', $names, -1, PREG_SPLIT_NO_EMPTY );
51  $ids = $this->getLookup()->centralIdsFromNames( $names, $this->userOrAudience );
52  if ( $ids ) {
53  return implode( "\n", $ids );
54  }
55  }
56  // If the user list is empty, it should be null (don't save) rather than an empty string
57  return null;
58  }
59 
63  public function filterForForm( $value ) {
64  $ids = self::splitIds( $value );
65  $names = $ids ? $this->getLookup()->namesFromCentralIds( $ids, $this->userOrAudience ) : [];
66  return implode( "\n", $names );
67  }
68 
75  public static function splitIds( $str ) {
76  return array_map( 'intval', preg_split( '/\n/', $str, -1, PREG_SPLIT_NO_EMPTY ) );
77  }
78 
82  private function getLookup() {
83  $this->lookup = $this->lookup ?? CentralIdLookup::factory();
84  return $this->lookup;
85  }
86 }
MediaWiki\Preferences\MultiUsernameFilter\$userOrAudience
CentralIdLookup int $userOrAudience
User querying central usernames or one of the audience constants.
Definition: MultiUsernameFilter.php:31
MediaWiki\Preferences\MultiUsernameFilter\splitIds
static splitIds( $str)
Splits a newline separated list of user ids into an array.
Definition: MultiUsernameFilter.php:75
MediaWiki\Preferences\MultiUsernameFilter\getLookup
getLookup()
Definition: MultiUsernameFilter.php:82
MediaWiki\Preferences\MultiUsernameFilter\__construct
__construct(CentralIdLookup $lookup=null, $userOrAudience=CentralIdLookup::AUDIENCE_PUBLIC)
Definition: MultiUsernameFilter.php:37
MediaWiki\Preferences\MultiUsernameFilter\filterForForm
filterForForm( $value)
mixed
Definition: MultiUsernameFilter.php:63
MediaWiki\Preferences
Definition: DefaultPreferencesFactory.php:21
MediaWiki\Preferences\MultiUsernameFilter\$lookup
CentralIdLookup null $lookup
Definition: MultiUsernameFilter.php:29
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:33
MediaWiki\Preferences\MultiUsernameFilter\filterFromForm
filterFromForm( $names)
mixed
Definition: MultiUsernameFilter.php:47
MediaWiki\Preferences\MultiUsernameFilter
Definition: MultiUsernameFilter.php:25
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:31
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:47
MediaWiki\Preferences\Filter
Base interface for user preference filters that work as a middleware between storage and interface.
Definition: Filter.php:27