| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\Status\Status; |
| 23 |
use MediaWiki\Storage\BlobStore; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class MetadataStorageHelper { |
| 32 |
|
| 33 |
private $repo; |
| 34 |
|
| 35 |
public function __construct( LocalRepo $repo ) { |
| 36 |
$this->repo = $repo; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function getJsonMetadata( $file, $envelope ) { |
| 48 |
|
| 49 |
$s = $this->jsonEncode( $envelope ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if ( !$this->repo->isSplitMetadataEnabled() |
| 54 |
|| !$file->getHandler() |
| 55 |
|| !$file->getHandler()->useSplitMetadata() |
| 56 |
) { |
| 57 |
return [ $s, [] ]; |
| 58 |
} |
| 59 |
$threshold = $this->repo->getSplitMetadataThreshold(); |
| 60 |
if ( !$threshold || strlen( $s ) <= $threshold ) { |
| 61 |
return [ $s, [] ]; |
| 62 |
} |
| 63 |
$blobStore = $this->repo->getBlobStore(); |
| 64 |
if ( !$blobStore ) { |
| 65 |
return [ $s, [] ]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
$blobAddresses = []; |
| 71 |
foreach ( $envelope['data'] as $name => $value ) { |
| 72 |
$encoded = $this->jsonEncode( $value ); |
| 73 |
if ( strlen( $encoded ) > $threshold ) { |
| 74 |
$blobAddresses[$name] = $blobStore->storeBlob( |
| 75 |
$encoded, |
| 76 |
[ BlobStore::IMAGE_HINT => $file->getName() ] |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$envelope['data'] = array_diff_key( $envelope['data'], $blobAddresses ); |
| 82 |
$envelope['blobs'] = $blobAddresses; |
| 83 |
$s = $this->jsonEncode( $envelope ); |
| 84 |
|
| 85 |
return [ $s, $blobAddresses ]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function jsonEncode( $data ): string { |
| 95 |
$s = json_encode( $data, |
| 96 |
JSON_INVALID_UTF8_IGNORE | |
| 97 |
JSON_UNESCAPED_SLASHES | |
| 98 |
JSON_UNESCAPED_UNICODE ); |
| 99 |
if ( $s === false ) { |
| 100 |
throw new InvalidArgumentException( __METHOD__ . ': metadata is not JSON-serializable ' ); |
| 101 |
} |
| 102 |
return $s; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function getMetadataFromBlobStore( array $addresses ): array { |
| 110 |
$result = []; |
| 111 |
if ( $addresses ) { |
| 112 |
$blobStore = $this->repo->getBlobStore(); |
| 113 |
if ( !$blobStore ) { |
| 114 |
LoggerFactory::getInstance( 'LocalFile' )->warning( |
| 115 |
"Unable to load metadata: repo has no blob store" ); |
| 116 |
return $result; |
| 117 |
} |
| 118 |
$status = $blobStore->getBlobBatch( $addresses ); |
| 119 |
if ( !$status->isGood() ) { |
| 120 |
$msg = Status::wrap( $status )->getWikiText( |
| 121 |
false, false, 'en' ); |
| 122 |
LoggerFactory::getInstance( 'LocalFile' )->warning( |
| 123 |
"Error loading metadata from BlobStore: $msg" ); |
| 124 |
} |
| 125 |
foreach ( $addresses as $itemName => $address ) { |
| 126 |
$json = $status->getValue()[$address] ?? null; |
| 127 |
if ( $json !== null ) { |
| 128 |
$value = $this->jsonDecode( $json ); |
| 129 |
$result[$itemName] = $value; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
return $result; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function jsonDecode( string $s ) { |
| 148 |
|
| 149 |
return @json_decode( $s, true, 512, JSON_INVALID_UTF8_IGNORE ); |
| 150 |
} |
| 151 |
|
| 152 |
} |