From: Thalia Date: Mon, 17 Jun 2019 09:56:49 +0000 (+0100) Subject: Filter out blocks with duplicate IDs when checking for blocks X-Git-Tag: 1.34.0-rc.0~1380^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=f32ef0627c1a85ad1ebff75334078afa722431f3;p=lhc%2Fweb%2Fwiklou.git Filter out blocks with duplicate IDs when checking for blocks Bug: T225919 Change-Id: I76549072d53083e6057f4fd8fe963e8989daa25c --- diff --git a/includes/block/BlockManager.php b/includes/block/BlockManager.php index b11e76f330..41ff893d57 100644 --- a/includes/block/BlockManager.php +++ b/includes/block/BlockManager.php @@ -202,6 +202,9 @@ class BlockManager { ] ); } + // Filter out any duplicated blocks, e.g. from the cookie + $blocks = $this->getUniqueBlocks( $blocks ); + if ( count( $blocks ) > 0 ) { if ( count( $blocks ) === 1 ) { $block = $blocks[ 0 ]; @@ -219,6 +222,28 @@ class BlockManager { return null; } + /** + * Given a list of blocks, return a list blocks where each block either has a + * unique ID or has ID null. + * + * @param AbstractBlock[] $blocks + * @return AbstractBlock[] + */ + private function getUniqueBlocks( $blocks ) { + $blockIds = []; + $uniqueBlocks = []; + foreach ( $blocks as $block ) { + $id = $block->getId(); + if ( $id === null ) { + $uniqueBlocks[] = $block; + } elseif ( !isset( $blockIds[$id] ) ) { + $uniqueBlocks[] = $block; + $blockIds[$block->getId()] = true; + } + } + return $uniqueBlocks; + } + /** * Try to load a block from an ID given in a cookie value. *