Namespace Iterator

Change-Id: I87fa703da9e7725370d72c58fc40eb204a309e67
This commit is contained in:
Reedy 2022-11-02 14:26:48 -06:00
parent 3609edf153
commit 0f0c6c4fa6
8 changed files with 55 additions and 25 deletions

View file

@ -1077,7 +1077,8 @@
"EchoArrayList": "includes/EchoArrayList.php",
"EchoAttributeManager": "includes/AttributeManager.php",
"EchoCachedList": "includes/EchoCachedList.php",
"EchoCallbackIterator": "includes/iterator/CallbackIterator.php",
"EchoCallbackIterator": "includes/Iterator/CallbackIterator.php",
"MediaWiki\\Extension\\Notifications\\Iterator\\CallbackIterator": "includes/Iterator/CallbackIterator.php",
"EchoContainmentList": "includes/EchoContainmentList.php",
"EchoContainmentSet": "includes/EchoContainmentSet.php",
"EchoDataOutputFormatter": "includes/DataOutputFormatter.php",
@ -1093,15 +1094,12 @@
"EchoEventMapper": "includes/mapper/EventMapper.php",
"EchoEventPresentationModel": "includes/Formatters/EchoEventPresentationModel.php",
"MediaWiki\\Extension\\Notifications\\Formatters\\EchoEventPresentationModel": "includes/Formatters/EchoEventPresentationModel.php",
"EchoFilteredSequentialIterator": "includes/iterator/FilteredSequentialIterator.php",
"EchoForeignNotifications": "includes/ForeignNotifications.php",
"EchoForeignWikiRequest": "includes/ForeignWikiRequest.php",
"EchoMentionPresentationModel": "includes/Formatters/EchoMentionPresentationModel.php",
"MediaWiki\\Extension\\Notifications\\Formatters\\EchoMentionPresentationModel": "includes/Formatters/EchoMentionPresentationModel.php",
"EchoMentionStatusPresentationModel": "includes/Formatters/EchoMentionStatusPresentationModel.php",
"MediaWiki\\Extension\\Notifications\\Formatters\\EchoMentionStatusPresentationModel": "includes/Formatters/EchoMentionStatusPresentationModel.php",
"EchoMultipleIterator": "includes/iterator/MultipleIterator.php",
"EchoNotRecursiveIterator": "includes/iterator/NotRecursiveIterator.php",
"EchoNotification": "includes/model/Notification.php",
"EchoNotificationMapper": "includes/mapper/NotificationMapper.php",
"EchoNotifier": "includes/Notifier.php",

View file

@ -8,13 +8,13 @@ use EchoCachedList;
use EchoContainmentList;
use EchoContainmentSet;
use EchoEvent;
use EchoFilteredSequentialIterator;
use EchoOnWikiList;
use EchoServices;
use Hooks;
use Iterator;
use MapCacheLRU;
use MediaWiki\Extension\Notifications\Exception\CatchableFatalErrorException;
use MediaWiki\Extension\Notifications\Iterator\FilteredSequentialIterator;
use MediaWiki\Extension\Notifications\Jobs\NotificationDeleteJob;
use MediaWiki\Extension\Notifications\Jobs\NotificationJob;
use MediaWiki\Logger\LoggerFactory;
@ -481,7 +481,7 @@ class NotificationController {
* @return Iterator values are User objects
*/
public static function getUsersToNotifyForEvent( EchoEvent $event ) {
$notify = new EchoFilteredSequentialIterator;
$notify = new FilteredSequentialIterator;
foreach ( self::evaluateUserCallable( $event, EchoAttributeManager::ATTR_LOCATORS ) as $users ) {
$notify->add( $users );
}

View file

@ -1,9 +1,14 @@
<?php
namespace MediaWiki\Extension\Notifications\Iterator;
use Iterator;
use IteratorDecorator;
/**
* Applies a callback to all values returned from the iterator
*/
class EchoCallbackIterator extends IteratorDecorator {
class CallbackIterator extends IteratorDecorator {
/** @var callable */
protected $callable;
@ -16,3 +21,5 @@ class EchoCallbackIterator extends IteratorDecorator {
return call_user_func( $this->callable, $this->iterator->current() );
}
}
class_alias( CallbackIterator::class, 'EchoCallbackIterator' );

View file

@ -1,5 +1,15 @@
<?php
namespace MediaWiki\Extension\Notifications\Iterator;
use ArrayIterator;
use CallbackFilterIterator;
use EmptyIterator;
use Iterator;
use IteratorAggregate;
use MWException;
use RecursiveIteratorIterator;
/**
* Allows building a single iterator out of multiple iterators
* and filtering the results. Accepts plain arrays for the simple
@ -10,13 +20,13 @@
* them all in one giant query.
*
* Usage:
* $users = new EchoFilteredSequentialIterator;
* $users = new FilteredSequentialIterator;
* $users->add( array( $userA, $userB, $userC ) );
*
* $it = new BatchRowIterator( ... );
* ...
* $it = new RecursiveIteratorIterator( $it );
* $users->add( new EchoCallbackIterator( $it, function( $row ) {
* $users->add( new CallbackIterator( $it, function( $row ) {
* ...
* return $user;
* } ) );
@ -27,12 +37,12 @@
*
* By default the BatchRowIterator returns an array of rows, this class
* expects a stream of user objects. To bridge that gap the
* RecursiveIteratorIterator is used to flatten and the EchoCallbackIterator
* RecursiveIteratorIterator is used to flatten and the CallbackIterator
* is used to transform each database $row into a User object.
*
* @todo name?
*/
class EchoFilteredSequentialIterator implements IteratorAggregate {
class FilteredSequentialIterator implements IteratorAggregate {
/**
* @var Iterator[]
*/
@ -96,7 +106,7 @@ class EchoFilteredSequentialIterator implements IteratorAggregate {
return reset( $this->iterators );
default:
return new RecursiveIteratorIterator( new EchoMultipleIterator( $this->iterators ) );
return new RecursiveIteratorIterator( new MultipleIterator( $this->iterators ) );
}
}

View file

@ -1,5 +1,11 @@
<?php
namespace MediaWiki\Extension\Notifications\Iterator;
use ArrayIterator;
use Iterator;
use RecursiveIterator;
/**
* Presents a list of iterators as a single stream of results
* when wrapped with the RecursiveIteratorIterator.
@ -9,7 +15,7 @@
* * implements RecursiveIterator
* * Lots less features(e.g. simple!)
*/
class EchoMultipleIterator implements RecursiveIterator {
class MultipleIterator implements RecursiveIterator {
/** @var Iterator[] */
protected $active = [];
/** @var array */
@ -66,7 +72,7 @@ class EchoMultipleIterator implements RecursiveIterator {
public function getChildren(): ?RecursiveIterator {
// The NotRecursiveIterator is used rather than a RecursiveArrayIterator
// so that nested arrays dont get recursed.
return new EchoNotRecursiveIterator( new ArrayIterator( $this->current() ) );
// so that nested arrays don't get recursed.
return new NotRecursiveIterator( new ArrayIterator( $this->current() ) );
}
}

View file

@ -1,5 +1,10 @@
<?php
namespace MediaWiki\Extension\Notifications\Iterator;
use IteratorDecorator;
use RecursiveIterator;
/**
* Wraps a non-recursive iterator with methods to be recursive
* without children.
@ -7,7 +12,7 @@
* Alternatively wraps a recursive iterator to prevent recursing deeper
* than the wrapped iterator.
*/
class EchoNotRecursiveIterator extends IteratorDecorator implements RecursiveIterator {
class NotRecursiveIterator extends IteratorDecorator implements RecursiveIterator {
public function hasChildren(): bool {
return false;
}

View file

@ -1,5 +1,6 @@
<?php
use MediaWiki\Extension\Notifications\Iterator\CallbackIterator;
use MediaWiki\MediaWikiServices;
class EchoUserLocator {
@ -35,7 +36,7 @@ class EchoUserLocator {
$recursiveIt = new RecursiveIteratorIterator( $batchRowIt );
// add callback to convert user id to user objects
$echoCallbackIt = new EchoCallbackIterator( $recursiveIt, static function ( $row ) {
$echoCallbackIt = new CallbackIterator( $recursiveIt, static function ( $row ) {
return User::newFromId( $row->wl_user );
} );

View file

@ -1,13 +1,16 @@
<?php
use MediaWiki\Extension\Notifications\Iterator\CallbackIterator;
use MediaWiki\Extension\Notifications\Iterator\FilteredSequentialIterator;
/**
* @covers \EchoCallbackIterator
* @covers \EchoFilteredSequentialIterator
* @covers \MediaWiki\Extension\Notifications\Iterator\CallbackIterator
* @covers \MediaWiki\Extension\Notifications\Iterator\FilteredSequentialIterator
*/
class FilteredSequentialIteratorTest extends MediaWikiUnitTestCase {
public function testEchoCallbackIteratorDoesntBlowUp() {
$it = new EchoCallbackIterator(
public function testCallbackIteratorDoesntBlowUp() {
$it = new CallbackIterator(
new ArrayIterator( [ 1, 2, 3 ] ),
static function ( $num ) {
return "There were $num items";
@ -22,7 +25,7 @@ class FilteredSequentialIteratorTest extends MediaWikiUnitTestCase {
$this->assertEquals( $expected, $res, 'Basic iteration with callback applied' );
}
public static function echoFilteredSequentialIteratorProvider() {
public static function filteredSequentialIteratorProvider() {
$odd = static function ( $v ) {
return $v & 1;
};
@ -89,10 +92,10 @@ class FilteredSequentialIteratorTest extends MediaWikiUnitTestCase {
}
/**
* @dataProvider echoFilteredSequentialIteratorProvider
* @dataProvider filteredSequentialIteratorProvider
*/
public function testEchoFilteredSequentialIterator( $message, $expect, $userLists, $filters ) {
$notify = new EchoFilteredSequentialIterator;
public function testFilteredSequentialIterator( $message, $expect, $userLists, $filters ) {
$notify = new FilteredSequentialIterator;
foreach ( $userLists as $userList ) {
$notify->add( $userList );