Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
14 / 15 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UserNameListener | |
93.33% |
14 / 15 |
|
66.67% |
2 / 3 |
9.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| onAfterClear | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterLoad | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Provide usernames filtered by per-wiki ipblocks. Batches together |
| 4 | * database requests for multiple usernames when possible. |
| 5 | */ |
| 6 | namespace Flow\Data\Listener; |
| 7 | |
| 8 | use Flow\Repository\UserNameBatch; |
| 9 | use MediaWiki\WikiMap\WikiMap; |
| 10 | |
| 11 | /** |
| 12 | * Listen for loaded objects and pre-load their user id fields into |
| 13 | * a batch username loader. |
| 14 | */ |
| 15 | class UserNameListener extends AbstractListener { |
| 16 | /** @var UserNameBatch */ |
| 17 | protected $batch; |
| 18 | /** @var array */ |
| 19 | protected $keys; |
| 20 | /** @var string */ |
| 21 | protected $wiki; |
| 22 | |
| 23 | /** |
| 24 | * @param UserNameBatch $batch |
| 25 | * @param array $keys key - a list of keys from storage that contain user ids, value - the wiki for the |
| 26 | * user id lookup, default to $wiki if null |
| 27 | * @param string|null $wiki The wikiid to use when $wikiKey is null. If both are null WikiMap::getCurrentWikiId() is used |
| 28 | */ |
| 29 | public function __construct( UserNameBatch $batch, array $keys, $wiki = null ) { |
| 30 | $this->batch = $batch; |
| 31 | $this->keys = $keys; |
| 32 | |
| 33 | if ( $wiki === null ) { |
| 34 | $this->wiki = WikiMap::getCurrentWikiId(); |
| 35 | } else { |
| 36 | $this->wiki = $wiki; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function onAfterClear() { |
| 41 | $this->batch->clear(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Load any user ids in $row into the username batch |
| 46 | * @param object $object |
| 47 | * @param array $row |
| 48 | */ |
| 49 | public function onAfterLoad( $object, array $row ) { |
| 50 | foreach ( $this->keys as $userKey => $wikiKey ) { |
| 51 | // check if the user id key exists in the data array and |
| 52 | // make sure it has a non-zero value |
| 53 | if ( isset( $row[$userKey] ) && $row[$userKey] != 0 ) { |
| 54 | // the wiki for the user id lookup is specified, |
| 55 | // check if it exists in the data array |
| 56 | if ( $wikiKey ) { |
| 57 | if ( !isset( $row[$wikiKey] ) ) { |
| 58 | wfDebugLog( 'Flow', __METHOD__ . ": could not detect wiki with " . $wikiKey ); |
| 59 | continue; |
| 60 | } |
| 61 | $wiki = $row[$wikiKey]; |
| 62 | // no wiki lookup is specified, default to $this->wiki |
| 63 | } else { |
| 64 | $wiki = $this->wiki; |
| 65 | } |
| 66 | $this->batch->add( $wiki, $row[$userKey] ); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |