| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
namespace MediaWiki\Content; |
| 12 |
|
| 13 |
use MediaWiki\Html\Html; |
| 14 |
use MediaWiki\Json\FormatJson; |
| 15 |
use MediaWiki\Status\Status; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class JsonContent extends TextContent { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected $jsonParse; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) { |
| 39 |
parent::__construct( $text, $modelId ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function getData() { |
| 51 |
$this->jsonParse ??= FormatJson::parse( $this->getText() ); |
| 52 |
return $this->jsonParse; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function isValid() { |
| 59 |
return $this->getData()->isGood(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function beautifyJSON() { |
| 70 |
return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function rootValueTable( $val ) { |
| 82 |
if ( is_object( $val ) ) { |
| 83 |
$table = $this->objectTable( $val ); |
| 84 |
|
| 85 |
} elseif ( is_array( $val ) ) { |
| 86 |
|
| 87 |
|
| 88 |
$table = $this->arrayTable( [ $val ] ); |
| 89 |
|
| 90 |
} else { |
| 91 |
$table = Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ], |
| 92 |
Html::rawElement( 'tbody', [], |
| 93 |
Html::rawElement( 'tr', [], |
| 94 |
Html::element( 'td', [], $this->primitiveValue( $val ) ) |
| 95 |
) |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected function objectTable( $mapping ) { |
| 110 |
$rows = []; |
| 111 |
$empty = true; |
| 112 |
|
| 113 |
foreach ( $mapping as $key => $val ) { |
| 114 |
$rows[] = $this->objectRow( $key, $val ); |
| 115 |
$empty = false; |
| 116 |
} |
| 117 |
if ( $empty ) { |
| 118 |
$rows[] = Html::rawElement( 'tr', [], |
| 119 |
Html::element( 'td', [ 'class' => 'mw-json-empty' ], |
| 120 |
wfMessage( 'content-json-empty-object' )->text() |
| 121 |
) |
| 122 |
); |
| 123 |
} |
| 124 |
return Html::rawElement( 'table', [ 'class' => 'mw-json' ], |
| 125 |
Html::rawElement( 'tbody', [], implode( '', $rows ) ) |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
protected function objectRow( $key, $val ) { |
| 137 |
$thContent = Html::element( 'span', [], $key ); |
| 138 |
$th = Html::rawElement( 'th', [], $thContent ); |
| 139 |
$td = $this->valueCell( $val ); |
| 140 |
return Html::rawElement( 'tr', [], $th . $td ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected function arrayTable( $mapping ) { |
| 150 |
$rows = []; |
| 151 |
$empty = true; |
| 152 |
|
| 153 |
foreach ( $mapping as $val ) { |
| 154 |
$rows[] = $this->arrayRow( $val ); |
| 155 |
$empty = false; |
| 156 |
} |
| 157 |
if ( $empty ) { |
| 158 |
$rows[] = Html::rawElement( 'tr', [], |
| 159 |
Html::element( 'td', [ 'class' => 'mw-json-empty' ], |
| 160 |
wfMessage( 'content-json-empty-array' )->text() |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
return Html::rawElement( 'table', [ 'class' => 'mw-json' ], |
| 165 |
Html::rawElement( 'tbody', [], implode( "\n", $rows ) ) |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected function arrayRow( $val ) { |
| 176 |
$td = $this->valueCell( $val ); |
| 177 |
return Html::rawElement( 'tr', [], $td ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function valueCell( $val ) { |
| 187 |
if ( is_object( $val ) ) { |
| 188 |
return Html::rawElement( 'td', [], $this->objectTable( $val ) ); |
| 189 |
} |
| 190 |
|
| 191 |
if ( is_array( $val ) ) { |
| 192 |
return Html::rawElement( 'td', [], $this->arrayTable( $val ) ); |
| 193 |
} |
| 194 |
|
| 195 |
return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
protected function primitiveValue( $val ) { |
| 205 |
if ( is_string( $val ) ) { |
| 206 |
|
| 207 |
|
| 208 |
return '"' . $val . '"'; |
| 209 |
} |
| 210 |
return FormatJson::encode( $val ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
class_alias( JsonContent::class, 'JsonContent' ); |