MediaWiki master
MetadataStorageHelper.php
Go to the documentation of this file.
1<?php
24
33 private $repo;
34
35 public function __construct( LocalRepo $repo ) {
36 $this->repo = $repo;
37 }
38
47 public function getJsonMetadata( $file, $envelope ) {
48 // Try encoding
49 $s = $this->jsonEncode( $envelope );
50
51 // Decide whether to try splitting the metadata.
52 // Return early if it's not going to happen.
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 // The data as a whole is above the item threshold. Look for
69 // large items that can be split out.
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 // Remove any items that were split out
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
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
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
147 public function jsonDecode( string $s ) {
148 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
149 return @json_decode( $s, true, 512, JSON_INVALID_UTF8_IGNORE );
150 }
151
152}
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
Create PSR-3 logger objects.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Helper for storage of metadata.
jsonEncode( $data)
Do JSON encoding with local flags.
getJsonMetadata( $file, $envelope)
Get metadata in JSON format ready for DB insertion, optionally splitting items out to BlobStore.
jsonDecode(string $s)
Do JSON decoding with local flags.
getMetadataFromBlobStore(array $addresses)
Service for loading and storing data blobs.
Definition BlobStore.php:33