MediaWiki REL1_35
MultiUsernameFilter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Preferences;
22
24
25class MultiUsernameFilter implements Filter {
29 private $lookup;
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}
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
static factory( $providerId=null)
Fetch a CentralIdLookup.
static splitIds( $str)
Splits a newline separated list of user ids into an array.
CentralIdLookup int $userOrAudience
User querying central usernames or one of the audience constants.
__construct(CentralIdLookup $lookup=null, $userOrAudience=CentralIdLookup::AUDIENCE_PUBLIC)
Base interface for user preference filters that work as a middleware between storage and interface.
Definition Filter.php:27