| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\NodeData; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 7 |
|
| 8 |
class DataBag { |
| 9 |
/** |
| 10 |
* @var NodeData[] A map of node data-object-id ids to data objects. |
| 11 |
* This map is used during DOM processing to avoid having to repeatedly |
| 12 |
* json-parse/json-serialize data-parsoid and data-mw attributes. |
| 13 |
* This map is initialized when a DOM is created/parsed/refreshed. |
| 14 |
*/ |
| 15 |
private array $dataObject = []; |
| 16 |
|
| 17 |
/** @var int An id counter for this document used for the dataObject map */ |
| 18 |
private int $nodeId = 0; |
| 19 |
|
| 20 |
/** @var PageBundle the page bundle object into which all data-parsoid and data-mw |
| 21 |
* attributes will be extracted to for pagebundle API requests. |
| 22 |
*/ |
| 23 |
private $pageBundle; |
| 24 |
|
| 25 |
/** |
| 26 |
* FIXME: Figure out a decent interface for updating these depths |
| 27 |
* without needing to import the various util files. |
| 28 |
* |
| 29 |
* Map of start/end meta tag tree depths keyed by about id |
| 30 |
*/ |
| 31 |
public array $transclusionMetaTagDepthMap = []; |
| 32 |
|
| 33 |
public function __construct() { |
| 34 |
$this->pageBundle = new PageBundle( |
| 35 |
'', |
| 36 |
[ "counter" => -1, "ids" => [] ], |
| 37 |
[ "ids" => [] ] |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Return this document's pagebundle object |
| 43 |
* @return PageBundle |
| 44 |
*/ |
| 45 |
public function getPageBundle(): PageBundle { |
| 46 |
return $this->pageBundle; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the data object for the node with data-object-id 'nodeId'. |
| 51 |
* This will return null if a non-existent nodeId is provided. |
| 52 |
* |
| 53 |
* @param int $nodeId |
| 54 |
* @return NodeData|null |
| 55 |
*/ |
| 56 |
public function getObject( int $nodeId ): ?NodeData { |
| 57 |
return $this->dataObject[$nodeId] ?? null; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Stash the data and return an id for retrieving it later |
| 62 |
* @param NodeData $data |
| 63 |
* @return int |
| 64 |
*/ |
| 65 |
public function stashObject( NodeData $data ): int { |
| 66 |
$nodeId = $this->nodeId++; |
| 67 |
$this->dataObject[$nodeId] = $data; |
| 68 |
return $nodeId; |
| 69 |
} |
| 70 |
} |