This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Wikibase\Repo\Dumpers; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use MWContentSerializationException; |
||
7 | use MWException; |
||
8 | use Serializers\Serializer; |
||
9 | use Wikibase\DataModel\Entity\EntityId; |
||
10 | use Wikibase\DataModel\Services\Entity\EntityPrefetcher; |
||
11 | use Wikibase\DataModel\Services\Lookup\EntityLookupException; |
||
12 | use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; |
||
13 | use Wikibase\Lib\Serialization\CallbackFactory; |
||
14 | use Wikibase\Lib\Serialization\SerializationModifier; |
||
15 | use Wikibase\Lib\Store\EntityRevisionLookup; |
||
16 | use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
||
17 | use Wikibase\Lib\Store\StorageException; |
||
18 | |||
19 | /** |
||
20 | * JsonDumpGenerator generates an JSON dump of a given set of entities, excluding |
||
21 | * redirects. |
||
22 | * |
||
23 | * @license GPL-2.0-or-later |
||
24 | * @author Daniel Kinzler |
||
25 | */ |
||
26 | class JsonDumpGenerator extends DumpGenerator { |
||
27 | |||
28 | /** |
||
29 | * @var int flags to use with json_encode as a bit field, see PHP's JSON_XXX constants. |
||
30 | */ |
||
31 | private $jsonFlags = 0; |
||
32 | |||
33 | /** |
||
34 | * @var Serializer |
||
35 | */ |
||
36 | private $entitySerializer; |
||
37 | |||
38 | /** |
||
39 | * @var EntityRevisionLookup |
||
40 | */ |
||
41 | private $entityLookup; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $useSnippets = false; |
||
47 | |||
48 | /** |
||
49 | * @var JsonDataTypeInjector |
||
50 | */ |
||
51 | private $dataTypeInjector; |
||
52 | |||
53 | /** |
||
54 | * @param resource $out |
||
55 | * @param EntityRevisionLookup $lookup |
||
56 | * @param Serializer $entitySerializer |
||
57 | * @param EntityPrefetcher $entityPrefetcher |
||
58 | * @param PropertyDataTypeLookup $dataTypeLookup |
||
59 | * |
||
60 | * @throws InvalidArgumentException |
||
61 | */ |
||
62 | public function __construct( |
||
63 | $out, |
||
64 | EntityRevisionLookup $lookup, |
||
65 | Serializer $entitySerializer, |
||
66 | EntityPrefetcher $entityPrefetcher, |
||
67 | PropertyDataTypeLookup $dataTypeLookup |
||
68 | ) { |
||
69 | parent::__construct( $out, $entityPrefetcher ); |
||
70 | |||
71 | $this->entitySerializer = $entitySerializer; |
||
72 | $this->entityLookup = $lookup; |
||
73 | |||
74 | $this->dataTypeInjector = new JsonDataTypeInjector( |
||
75 | new SerializationModifier(), |
||
76 | new CallbackFactory(), |
||
77 | $dataTypeLookup |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Do something before dumping data |
||
83 | */ |
||
84 | protected function preDump() { |
||
85 | if ( !$this->useSnippets ) { |
||
86 | $this->writeToDump( "[\n" ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Do something after dumping data |
||
92 | */ |
||
93 | protected function postDump() { |
||
94 | if ( !$this->useSnippets ) { |
||
95 | $this->writeToDump( "\n]\n" ); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Do something before dumping entity |
||
101 | * |
||
102 | * @param int $dumpCount |
||
103 | */ |
||
104 | protected function preEntityDump( $dumpCount ) { |
||
105 | if ( $dumpCount > 0 ) { |
||
106 | $this->writeToDump( ",\n" ); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param EntityId $entityId |
||
112 | * |
||
113 | * @throws EntityLookupException |
||
114 | * @throws StorageException |
||
115 | * @return string|null |
||
116 | */ |
||
117 | protected function generateDumpForEntityId( EntityId $entityId ) { |
||
118 | try { |
||
119 | $revision = $this->entityLookup->getEntityRevision( $entityId ); |
||
120 | |||
121 | if ( !$revision ) { |
||
122 | throw new EntityLookupException( $entityId, 'Entity not found: ' . $entityId->getSerialization() ); |
||
123 | } |
||
124 | |||
125 | } catch ( MWContentSerializationException $ex ) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
126 | throw new StorageException( 'Deserialization error for ' . $entityId->getSerialization() ); |
||
127 | } catch ( RevisionedUnresolvedRedirectException $e ) { |
||
128 | // Redirects aren't supposed to be in the JSON dumps |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | $entity = $revision->getEntity(); |
||
133 | |||
134 | $data = $this->entitySerializer->serialize( $entity ); |
||
135 | |||
136 | $data = $this->dataTypeInjector->injectEntitySerializationWithDataTypes( $data ); |
||
137 | |||
138 | // HACK: replace empty arrays with objects at the first level of the array |
||
139 | foreach ( $data as &$element ) { |
||
140 | if ( empty( $element ) ) { |
||
141 | $element = (object)[]; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | $data['lastrevid'] = $revision->getRevisionId(); |
||
146 | |||
147 | $json = $this->encode( $data ); |
||
148 | |||
149 | return $json; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Encodes the given data as JSON |
||
154 | * |
||
155 | * @param mixed $data |
||
156 | * |
||
157 | * @return string |
||
158 | * @throws MWException |
||
159 | */ |
||
160 | public function encode( $data ) { |
||
161 | $json = json_encode( $data, $this->jsonFlags ); |
||
162 | |||
163 | if ( $json === false ) { |
||
164 | throw new StorageException( 'Failed to encode data structure.' ); |
||
165 | } |
||
166 | |||
167 | return $json; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param bool $useSnippets Whether to output valid json (false) or only comma separated entities |
||
172 | */ |
||
173 | public function setUseSnippets( $useSnippets ) { |
||
174 | $this->useSnippets = (bool)$useSnippets; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Flags to use with json_encode as a bit field, see PHP's JSON_XXX constants. |
||
179 | * |
||
180 | * @param int $jsonFlags |
||
181 | */ |
||
182 | public function setJsonFlags( $jsonFlags ) { |
||
183 | $this->jsonFlags = $jsonFlags; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return int |
||
188 | * |
||
189 | * @see setJsonFlags |
||
190 | */ |
||
191 | public function getJsonFlags() { |
||
192 | return $this->jsonFlags; |
||
193 | } |
||
194 | |||
195 | } |
||
196 |