| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\NodeData; |
| 5 |
|
| 6 |
use stdClass; |
| 7 |
use Wikimedia\JsonCodec\Hint; |
| 8 |
use Wikimedia\JsonCodec\JsonCodec; |
| 9 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 10 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 11 |
use Wikimedia\Parsoid\Tokens\SourceRange; |
| 12 |
|
| 13 |
/** |
| 14 |
* Editing data for a DOM node. Managed by DOMDataUtils::get/setDataMw(). |
| 15 |
* |
| 16 |
* To reduce memory usage, most of the properties need to be dynamic, but |
| 17 |
* we use the property declarations below to allow type checking. |
| 18 |
* |
| 19 |
* @property list<string|TemplateInfo> $parts |
| 20 |
* @property string $name |
| 21 |
* @property string $extPrefix |
| 22 |
* @property string $extSuffix |
| 23 |
* @property list<DataMwAttrib> $attribs Extended attributes of an HTML tag |
| 24 |
* @property string $src |
| 25 |
* @property string $caption |
| 26 |
* @property string $thumb |
| 27 |
* @property bool $autoGenerated |
| 28 |
* @property list<DataMwError> $errors |
| 29 |
* @property stdClass $body |
| 30 |
* @property mixed $html |
| 31 |
* @property float $scale |
| 32 |
* @property string $starttime |
| 33 |
* @property string $endtime |
| 34 |
* @property string $thumbtime |
| 35 |
* @property string $page |
| 36 |
* == Annotations == |
| 37 |
* @property string $rangeId |
| 38 |
* @property SourceRange $wtOffsets |
| 39 |
* @property bool $extendedRange |
| 40 |
* @property stdClass $attrs Attributes for an extension tag or annotation (T367616 should be renamed) |
| 41 |
*/ |
| 42 |
#[\AllowDynamicProperties] |
| 43 |
class DataMw implements JsonCodecable { |
| 44 |
use JsonCodecableTrait; |
| 45 |
|
| 46 |
public function __construct( array $initialVals = [] ) { |
| 47 |
foreach ( $initialVals as $k => $v ) { |
| 48 |
// @phan-suppress-next-line PhanNoopSwitchCases |
| 49 |
switch ( $k ) { |
| 50 |
// Add cases here for components which should be instantiated |
| 51 |
// as proper classes. |
| 52 |
default: |
| 53 |
$this->$k = $v; |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** Returns true iff there are no dynamic properties of this object. */ |
| 60 |
public function isEmpty(): bool { |
| 61 |
return ( (array)$this ) === []; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Deeply clone this object |
| 66 |
* |
| 67 |
* @return DataMw |
| 68 |
*/ |
| 69 |
public function clone(): self { |
| 70 |
$codec = new JsonCodec; |
| 71 |
return $codec->newFromJsonArray( $codec->toJsonArray( $this ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** @inheritDoc */ |
| 75 |
public static function jsonClassHintFor( string $keyname ) { |
| 76 |
static $hints = null; |
| 77 |
if ( $hints === null ) { |
| 78 |
$hints = [ |
| 79 |
'attribs' => Hint::build( DataMwAttrib::class, Hint::USE_SQUARE, Hint::LIST ), |
| 80 |
// T367616: 'attrs' should be renamed to 'extAttrs' |
| 81 |
'attrs' => Hint::build( stdClass::class, Hint::ALLOW_OBJECT ), |
| 82 |
'body' => Hint::build( stdClass::class, Hint::ALLOW_OBJECT ), |
| 83 |
'wtOffsets' => Hint::build( SourceRange::class, Hint::USE_SQUARE ), |
| 84 |
'parts' => Hint::build( TemplateInfo::class, Hint::STDCLASS, Hint::LIST ), |
| 85 |
'errors' => Hint::build( DataMwError::class, Hint::LIST ), |
| 86 |
]; |
| 87 |
} |
| 88 |
return $hints[$keyname] ?? null; |
| 89 |
} |
| 90 |
|
| 91 |
/** @inheritDoc */ |
| 92 |
public function toJsonArray(): array { |
| 93 |
$result = (array)$this; |
| 94 |
// T367141: Third party clients (eg Cite) create arrays instead of |
| 95 |
// error objects. We should convert them to proper DataMwError |
| 96 |
// objects once those exist. |
| 97 |
if ( isset( $result['errors'] ) ) { |
| 98 |
$result['errors'] = array_map( |
| 99 |
fn ( $e ) => is_array( $e ) ? DataMwError::newFromJsonArray( $e ) : |
| 100 |
( $e instanceof DataMwError ? $e : DataMwError::newFromJsonArray( (array)$e ) ), |
| 101 |
$result['errors'] |
| 102 |
); |
| 103 |
} |
| 104 |
// Legacy encoding of parts. |
| 105 |
if ( isset( $result['parts'] ) ) { |
| 106 |
$result['parts'] = array_map( static function ( $p ) { |
| 107 |
if ( $p instanceof TemplateInfo ) { |
| 108 |
$type = $p->type ?? 'template'; |
| 109 |
if ( $type === 'parserfunction' ) { |
| 110 |
$type = 'template'; |
| 111 |
} |
| 112 |
$pp = (object)[]; |
| 113 |
$pp->$type = $p; |
| 114 |
return $pp; |
| 115 |
} |
| 116 |
return $p; |
| 117 |
}, $result['parts'] ); |
| 118 |
} |
| 119 |
return $result; |
| 120 |
} |
| 121 |
|
| 122 |
/** @inheritDoc */ |
| 123 |
public static function newFromJsonArray( array $json ): DataMw { |
| 124 |
// Decode legacy encoding of parts. |
| 125 |
if ( isset( $json['parts'] ) ) { |
| 126 |
$json['parts'] = array_map( static function ( $p ) { |
| 127 |
if ( is_object( $p ) ) { |
| 128 |
$type = 'template'; |
| 129 |
if ( isset( $p->templatearg ) ) { |
| 130 |
$type = 'templatearg'; |
| 131 |
} |
| 132 |
$p = $p->$type; |
| 133 |
if ( isset( $p->func ) ) { |
| 134 |
$type = 'parserfunction'; |
| 135 |
} |
| 136 |
$p->type = $type; |
| 137 |
} |
| 138 |
return $p; |
| 139 |
}, $json['parts'] ); |
| 140 |
} |
| 141 |
return new DataMw( $json ); |
| 142 |
} |
| 143 |
} |