MediaWiki master
MultiUsernameFilter.php
Go to the documentation of this file.
1<?php
8
12
13class MultiUsernameFilter implements Filter {
17 private $lookup;
19 private $authorityOrAudience;
20
25 public function __construct(
26 ?CentralIdLookup $lookup = null,
27 $authorityOrAudience = CentralIdLookup::AUDIENCE_PUBLIC
28 ) {
29 $this->lookup = $lookup;
30 $this->authorityOrAudience = $authorityOrAudience;
31 }
32
36 public function filterFromForm( $names ) {
37 $names = trim( $names );
38 if ( $names !== '' ) {
39 $names = preg_split( '/\n/', $names, -1, PREG_SPLIT_NO_EMPTY );
40 $ids = $this->getLookup()->centralIdsFromNames( $names, $this->authorityOrAudience );
41 if ( $ids ) {
42 return implode( "\n", $ids );
43 }
44 }
45 // If the user list is empty, it should be null (don't save) rather than an empty string
46 return null;
47 }
48
52 public function filterForForm( $value ) {
53 $ids = is_string( $value ) ? self::splitIds( $value ) : [];
54 $names = $ids ? $this->getLookup()->namesFromCentralIds( $ids, $this->authorityOrAudience ) : [];
55 return implode( "\n", $names );
56 }
57
64 public static function splitIds( $str ) {
65 return array_map( 'intval', preg_split( '/\n/', $str, -1, PREG_SPLIT_NO_EMPTY ) );
66 }
67
71 private function getLookup() {
72 $this->lookup ??= MediaWikiServices::getInstance()->getCentralIdLookup();
73 return $this->lookup;
74 }
75}
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
static splitIds( $str)
Splits a newline separated list of user ids into an array.
__construct(?CentralIdLookup $lookup=null, $authorityOrAudience=CentralIdLookup::AUDIENCE_PUBLIC)
Find central user IDs associated with local user IDs, e.g.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23
Base interface for user preference filters that work as a middleware between storage and interface.
Definition Filter.php:13