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\Api; |
||
4 | |||
5 | use ApiBase; |
||
6 | use ApiUsageException; |
||
7 | use LogicException; |
||
8 | use Wikibase\DataModel\Entity\EntityDocument; |
||
9 | use Wikibase\DataModel\Entity\EntityId; |
||
10 | use Wikibase\DataModel\Entity\EntityIdParser; |
||
11 | use Wikibase\DataModel\Entity\EntityIdParsingException; |
||
12 | use Wikibase\Lib\Store\BadRevisionException; |
||
13 | use Wikibase\Lib\Store\EntityByLinkedTitleLookup; |
||
14 | use Wikibase\Lib\Store\EntityRevision; |
||
15 | use Wikibase\Lib\Store\EntityRevisionLookup; |
||
16 | use Wikibase\Lib\Store\LookupConstants; |
||
17 | use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException; |
||
18 | use Wikibase\Lib\Store\StorageException; |
||
19 | use Wikimedia\Assert\Assert; |
||
20 | |||
21 | /** |
||
22 | * Helper class for api modules to load entities. |
||
23 | * |
||
24 | * @license GPL-2.0-or-later |
||
25 | * @author Addshore |
||
26 | * @author Daniel Kinzler |
||
27 | */ |
||
28 | class EntityLoadingHelper { |
||
29 | |||
30 | /** |
||
31 | * @var ApiBase |
||
32 | */ |
||
33 | protected $apiModule; |
||
34 | |||
35 | /** |
||
36 | * @var EntityIdParser |
||
37 | */ |
||
38 | private $idParser; |
||
39 | |||
40 | /** |
||
41 | * @var EntityRevisionLookup |
||
42 | */ |
||
43 | protected $entityRevisionLookup; |
||
44 | |||
45 | /** |
||
46 | * @var ApiErrorReporter |
||
47 | */ |
||
48 | protected $errorReporter; |
||
49 | |||
50 | /** |
||
51 | * @var string See the LATEST_XXX constants defined in EntityRevisionLookup |
||
52 | */ |
||
53 | protected $defaultRetrievalMode = LookupConstants::LATEST_FROM_REPLICA; |
||
54 | |||
55 | /** |
||
56 | * @var EntityByLinkedTitleLookup|null |
||
57 | */ |
||
58 | private $entityByLinkedTitleLookup = null; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | private $entityIdParam = 'entity'; |
||
64 | |||
65 | public function __construct( |
||
66 | ApiBase $apiModule, |
||
67 | EntityIdParser $idParser, |
||
68 | EntityRevisionLookup $entityRevisionLookup, |
||
69 | ApiErrorReporter $errorReporter |
||
70 | ) { |
||
71 | $this->apiModule = $apiModule; |
||
72 | $this->idParser = $idParser; |
||
73 | $this->entityRevisionLookup = $entityRevisionLookup; |
||
74 | $this->errorReporter = $errorReporter; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns the name of the request parameter expected to contain the ID of the entity to load. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getEntityIdParam() { |
||
83 | return $this->entityIdParam; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Sets the name of the request parameter expected to contain the ID of the entity to load. |
||
88 | * |
||
89 | * @param string $entityIdParam |
||
90 | */ |
||
91 | public function setEntityIdParam( $entityIdParam ) { |
||
92 | $this->entityIdParam = $entityIdParam; |
||
93 | } |
||
94 | |||
95 | public function setEntityByLinkedTitleLookup( EntityByLinkedTitleLookup $lookup ) { |
||
96 | $this->entityByLinkedTitleLookup = $lookup; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getDefaultRetrievalMode() { |
||
103 | return $this->defaultRetrievalMode; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param string $defaultRetrievalMode Use the LATEST_XXX constants defined |
||
108 | * in EntityRevisionLookup |
||
109 | */ |
||
110 | public function setDefaultRetrievalMode( $defaultRetrievalMode ) { |
||
111 | Assert::parameterType( 'string', $defaultRetrievalMode, '$defaultRetrievalMode' ); |
||
112 | $this->defaultRetrievalMode = $defaultRetrievalMode; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Load the entity content of the given revision. |
||
117 | * |
||
118 | * Will fail by calling dieException() $this->errorReporter if the revision |
||
119 | * cannot be found or cannot be loaded. |
||
120 | * |
||
121 | * @param EntityId $entityId EntityId of the page to load the revision for |
||
122 | * @param int $revId The desired revision id, or 0 for the latest revision. |
||
123 | * @param string|null $mode LATEST_FROM_REPLICA, LATEST_FROM_REPLICA_WITH_FALLBACK or |
||
124 | * LATEST_FROM_MASTER (from EntityRevisionLookup). Null for the default. |
||
125 | * |
||
126 | * @throws ApiUsageException |
||
127 | * @throws LogicException |
||
128 | * @return EntityRevision|null |
||
129 | */ |
||
130 | protected function loadEntityRevision( |
||
131 | EntityId $entityId, |
||
132 | $revId = 0, |
||
133 | $mode = null |
||
134 | ) { |
||
135 | if ( $revId === null ) { |
||
136 | $revId = 0; |
||
137 | } |
||
138 | if ( $mode === null ) { |
||
139 | $mode = $this->defaultRetrievalMode; |
||
140 | } |
||
141 | |||
142 | try { |
||
143 | $revision = $this->entityRevisionLookup->getEntityRevision( $entityId, $revId, $mode ); |
||
144 | return $revision; |
||
145 | } catch ( RevisionedUnresolvedRedirectException $ex ) { |
||
146 | $this->errorReporter->dieException( $ex, 'unresolved-redirect' ); |
||
147 | } catch ( BadRevisionException $ex ) { |
||
148 | $this->errorReporter->dieException( $ex, 'nosuchrevid' ); |
||
149 | } catch ( StorageException $ex ) { |
||
150 | $this->errorReporter->dieException( $ex, 'cant-load-entity-content' ); |
||
151 | } |
||
152 | |||
153 | throw new LogicException( 'ApiErrorReporter::dieException did not throw an ApiUsageException' ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param EntityId|null $entityId ID of the entity to load. If not given, the ID is taken |
||
158 | * from the request parameters. If $entityId is given, it must be consistent with |
||
159 | * the 'baserevid' parameter. |
||
160 | * |
||
161 | * @return EntityDocument |
||
162 | */ |
||
163 | public function loadEntity( EntityId $entityId = null ) { |
||
164 | if ( !$entityId ) { |
||
165 | $params = $this->apiModule->extractRequestParams(); |
||
166 | $entityId = $this->getEntityIdFromParams( $params ); |
||
167 | } |
||
168 | |||
169 | if ( !$entityId ) { |
||
170 | $this->errorReporter->dieError( |
||
0 ignored issues
–
show
|
|||
171 | 'No entity ID provided', |
||
172 | 'no-entity-id' ); |
||
173 | } |
||
174 | |||
175 | $entityRevision = $this->loadEntityRevision( $entityId ); |
||
0 ignored issues
–
show
It seems like
$entityId can be null ; however, loadEntityRevision() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
176 | |||
177 | if ( !$entityRevision ) { |
||
178 | $this->errorReporter->dieWithError( [ 'no-such-entity', $entityId ], |
||
0 ignored issues
–
show
array('no-such-entity', $entityId) is of type array<integer,string|nul...l\\Entity\\EntityId>"}> , but the function expects a string|array<integer,str...bject<MessageSpecifier> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
179 | 'no-such-entity' ); |
||
180 | } |
||
181 | |||
182 | return $entityRevision->getEntity(); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string[] $params |
||
187 | * |
||
188 | * @return EntityId|null |
||
189 | */ |
||
190 | public function getEntityIdFromParams( array $params ) { |
||
191 | if ( isset( $params[$this->entityIdParam] ) ) { |
||
192 | return $this->getEntityIdFromString( $params[$this->entityIdParam] ); |
||
193 | } elseif ( isset( $params['site'] ) && isset( $params['title'] ) ) { |
||
194 | return $this->getEntityIdFromSiteTitleCombination( |
||
195 | $params['site'], |
||
196 | $params['title'] |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | return null; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Returns an EntityId object based on the given $id, |
||
205 | * or throws a usage exception if the ID is invalid. |
||
206 | * |
||
207 | * @param string $id |
||
208 | * |
||
209 | * @throws ApiUsageException |
||
210 | * @return EntityId |
||
211 | */ |
||
212 | private function getEntityIdFromString( $id ) { |
||
213 | try { |
||
214 | return $this->idParser->parse( $id ); |
||
215 | } catch ( EntityIdParsingException $ex ) { |
||
216 | $this->errorReporter->dieException( $ex, 'invalid-entity-id' ); |
||
217 | } |
||
218 | |||
219 | return null; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param string $site |
||
224 | * @param string $title |
||
225 | * |
||
226 | * @throws ApiUsageException If no such entity is found. |
||
227 | * @return EntityId The ID of the entity connected to $title on $site. |
||
228 | */ |
||
229 | private function getEntityIdFromSiteTitleCombination( $site, $title ) { |
||
230 | if ( $this->entityByLinkedTitleLookup ) { |
||
231 | // FIXME: Normalization missing, see T47282. Use EntityByTitleHelper! |
||
232 | $entityId = $this->entityByLinkedTitleLookup->getEntityIdForLinkedTitle( $site, $title ); |
||
233 | } else { |
||
234 | $entityId = null; |
||
235 | } |
||
236 | |||
237 | if ( $entityId === null ) { |
||
238 | $this->errorReporter->dieError( |
||
0 ignored issues
–
show
The method
Wikibase\Repo\Api\ApiErrorReporter::dieError() has been deprecated with message: Use dieWithError() instead.
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead. ![]() |
|||
239 | 'No entity found matching site link ' . $site . ':' . $title, |
||
240 | 'no-such-entity-link' |
||
241 | ); |
||
242 | } |
||
243 | |||
244 | return $entityId; |
||
245 | } |
||
246 | |||
247 | } |
||
248 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.