| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Preferences; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Permissions\Authority; |
| 25 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 26 |
|
| 27 |
class MultiUsernameFilter implements Filter { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private $lookup; |
| 32 |
|
| 33 |
private $authorityOrAudience; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
?CentralIdLookup $lookup = null, |
| 41 |
$authorityOrAudience = CentralIdLookup::AUDIENCE_PUBLIC |
| 42 |
) { |
| 43 |
$this->lookup = $lookup; |
| 44 |
$this->authorityOrAudience = $authorityOrAudience; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function filterFromForm( $names ) { |
| 51 |
$names = trim( $names ); |
| 52 |
if ( $names !== '' ) { |
| 53 |
$names = preg_split( '/\n/', $names, -1, PREG_SPLIT_NO_EMPTY ); |
| 54 |
$ids = $this->getLookup()->centralIdsFromNames( $names, $this->authorityOrAudience ); |
| 55 |
if ( $ids ) { |
| 56 |
return implode( "\n", $ids ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function filterForForm( $value ) { |
| 67 |
$ids = is_string( $value ) ? self::splitIds( $value ) : []; |
| 68 |
$names = $ids ? $this->getLookup()->namesFromCentralIds( $ids, $this->authorityOrAudience ) : []; |
| 69 |
return implode( "\n", $names ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static function splitIds( $str ) { |
| 79 |
return array_map( 'intval', preg_split( '/\n/', $str, -1, PREG_SPLIT_NO_EMPTY ) ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private function getLookup() { |
| 86 |
$this->lookup ??= MediaWikiServices::getInstance()->getCentralIdLookup(); |
| 87 |
return $this->lookup; |
| 88 |
} |
| 89 |
} |