mediawiki-extensions-Echo/includes/Bundler.php
Stephane Bisson 24caf50ff6 Dynamic bundles
To allow individual notifications to be
marked as read/unread or moderated,
bundles are created by grouping associated
notifications when they are fetched for display
instead of when they are created.

From a product perspective, this change doesn't
introduce moderation or expandable bundles but
it counts each individual notifications.
For instance, the bundled notification
"3 new topics on PageA" now counts as 3
notifications.

Bug: T93673
Bug: T120153
Change-Id: Iacd098573efd92bb1e3fcd7da4cd40cea9522f15
2016-06-27 09:49:13 -04:00

49 lines
1.2 KiB
PHP

<?php
class Bundler {
private function sort( &$array ) {
// We have to ignore the error here (use @usort)
// otherwise this code fails when executed by unit tests
// See: https://bugs.php.net/bug.php?id=50688
// @codingStandardsIgnoreStart
@usort( $array, function( Bundleable $a, Bundleable $b ) {
return strcmp( $b->getSortingKey(), $a->getSortingKey() );
} );
// @codingStandardsIgnoreEnd
}
/**
* Bundle bundleable elements that can be bundled by their bundling keys
*
* @param Bundleable[] $bundleables
* @return Bundleable[] Grouped notifications sorted by timestamp DESC
*/
public function bundle( $bundleables ) {
$groups = array();
$bundled = array();
/** @var Bundleable $element */
foreach ( $bundleables as $element ) {
if ( $element->canBeBundled() && $element->getBundlingKey() ) {
$groups[ $element->getBundlingKey() ][] = $element;
} else {
$bundled[] = $element;
}
}
foreach ( $groups as $bundlingKey => $group ) {
$this->sort( $group );
/** @var Bundleable $base */
$base = array_shift( $group );
$base->setBundledElements( $group );
$bundled[] = $base;
}
$this->sort( $bundled );
return $bundled;
}
}