Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
10 / 15
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiUsernameFilter
66.67% covered (warning)
66.67%
10 / 15
40.00% covered (danger)
40.00%
2 / 5
10.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 filterFromForm
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 filterForForm
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 splitIds
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLookup
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Preferences;
22
23use MediaWiki\MediaWikiServices;
24use MediaWiki\Permissions\Authority;
25use MediaWiki\User\CentralId\CentralIdLookup;
26
27class MultiUsernameFilter implements Filter {
28    /**
29     * @var CentralIdLookup|null
30     */
31    private $lookup;
32    /** @var Authority|int User querying central usernames or one of the audience constants */
33    private $authorityOrAudience;
34
35    /**
36     * @param CentralIdLookup|null $lookup
37     * @param Authority|int $authorityOrAudience
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     * @inheritDoc
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        // If the user list is empty, it should be null (don't save) rather than an empty string
60        return null;
61    }
62
63    /**
64     * @inheritDoc
65     */
66    public function filterForForm( $value ) {
67        $ids = self::splitIds( $value );
68        $names = $ids ? $this->getLookup()->namesFromCentralIds( $ids, $this->authorityOrAudience ) : [];
69        return implode( "\n", $names );
70    }
71
72    /**
73     * Splits a newline separated list of user ids into an array.
74     *
75     * @param string $str
76     * @return int[]
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     * @return CentralIdLookup
84     */
85    private function getLookup() {
86        $this->lookup ??= MediaWikiServices::getInstance()->getCentralIdLookup();
87        return $this->lookup;
88    }
89}