MediaWiki 1.41.2
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
96 public function jsonEncode( $data ): string {
97 $s = json_encode( $data,
98 JSON_INVALID_UTF8_IGNORE |
99 JSON_UNESCAPED_SLASHES |
100 JSON_UNESCAPED_UNICODE );
101 if ( $s === false ) {
102 throw new MWException( __METHOD__ . ': metadata is not JSON-serializable ' );
103 }
104 return $s;
105 }
106
111 public function getMetadataFromBlobStore( array $addresses ): array {
112 $result = [];
113 if ( $addresses ) {
114 $blobStore = $this->repo->getBlobStore();
115 if ( !$blobStore ) {
116 LoggerFactory::getInstance( 'LocalFile' )->warning(
117 "Unable to load metadata: repo has no blob store" );
118 return $result;
119 }
120 $status = $blobStore->getBlobBatch( $addresses );
121 if ( !$status->isGood() ) {
122 $msg = Status::wrap( $status )->getWikiText(
123 false, false, 'en' );
124 LoggerFactory::getInstance( 'LocalFile' )->warning(
125 "Error loading metadata from BlobStore: $msg" );
126 }
127 foreach ( $addresses as $itemName => $address ) {
128 $json = $status->getValue()[$address] ?? null;
129 if ( $json !== null ) {
130 $value = $this->jsonDecode( $json );
131 $result[$itemName] = $value;
132 }
133 }
134 }
135 return $result;
136 }
137
149 public function jsonDecode( string $s ) {
150 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
151 return @json_decode( $s, true, 512, JSON_INVALID_UTF8_IGNORE );
152 }
153
154}
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
MediaWiki exception.
Create PSR-3 logger objects.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:58
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
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42