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\Rdf; |
||
| 4 | |||
| 5 | use Wikibase\DataModel\Entity\EntityDocument; |
||
| 6 | use Wikibase\DataModel\Entity\EntityId; |
||
| 7 | use Wikibase\DataModel\Reference; |
||
| 8 | use Wikibase\DataModel\Statement\Statement; |
||
| 9 | use Wikibase\DataModel\Statement\StatementList; |
||
| 10 | use Wikibase\DataModel\Statement\StatementListProvider; |
||
| 11 | use Wikimedia\Purtle\RdfWriter; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Fully reified RDF mapping for wikibase statements, including deprecated and non-"best" |
||
| 15 | * statements, ranks, qualifiers, and references. This modells statements as identifiable objects |
||
| 16 | * and does not output a direct property to value mapping as the TruthyStatementRdfBuilder does. If |
||
| 17 | * both forms (direct and full) are desired, use TruthyStatementRdfBuilder in addition to |
||
| 18 | * FullStatementRdfBuilder. |
||
| 19 | * |
||
| 20 | * @see TruthyStatementRdfBuilder |
||
| 21 | * |
||
| 22 | * @license GPL-2.0-or-later |
||
| 23 | * @author Daniel Kinzler |
||
| 24 | * @author Stas Malyshev |
||
| 25 | */ |
||
| 26 | class FullStatementRdfBuilder implements EntityRdfBuilder { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var DedupeBag |
||
| 30 | */ |
||
| 31 | private $dedupeBag; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | private $produceQualifiers = true; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | private $produceReferences = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var RdfVocabulary |
||
| 45 | */ |
||
| 46 | private $vocabulary; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var RdfWriter |
||
| 50 | */ |
||
| 51 | private $statementWriter; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var RdfWriter |
||
| 55 | */ |
||
| 56 | private $referenceWriter; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var SnakRdfBuilder |
||
| 60 | */ |
||
| 61 | private $snakBuilder; |
||
| 62 | |||
| 63 | public function __construct( RdfVocabulary $vocabulary, RdfWriter $writer, SnakRdfBuilder $snakBuilder ) { |
||
| 64 | $this->vocabulary = $vocabulary; |
||
| 65 | |||
| 66 | // Note: since we process references as nested structures, they need a separate |
||
| 67 | // rdf writer, so outputting references doesn't destroy the state of the statement writer. |
||
| 68 | $this->statementWriter = $writer; |
||
| 69 | $this->referenceWriter = $writer->sub(); |
||
| 70 | |||
| 71 | $this->snakBuilder = $snakBuilder; |
||
| 72 | |||
| 73 | $this->dedupeBag = new NullDedupeBag(); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function setDedupeBag( DedupeBag $dedupeBag ) { |
||
| 77 | $this->dedupeBag = $dedupeBag; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return boolean |
||
| 82 | */ |
||
| 83 | public function getProduceQualifiers() { |
||
| 84 | return $this->produceQualifiers; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param boolean $produceQualifiers |
||
| 89 | */ |
||
| 90 | public function setProduceQualifiers( $produceQualifiers ) { |
||
| 91 | $this->produceQualifiers = $produceQualifiers; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return boolean |
||
| 96 | */ |
||
| 97 | public function getProduceReferences() { |
||
| 98 | return $this->produceReferences; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param boolean $produceReferences |
||
| 103 | */ |
||
| 104 | public function setProduceReferences( $produceReferences ) { |
||
| 105 | $this->produceReferences = $produceReferences; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Adds Statements to the RDF graph. |
||
| 110 | * |
||
| 111 | * @param EntityId $entityId |
||
| 112 | * @param StatementList $statementList |
||
| 113 | */ |
||
| 114 | public function addStatements( EntityId $entityId, StatementList $statementList ) { |
||
| 115 | $bestList = []; |
||
| 116 | |||
| 117 | // FIXME: This is expensive, share the result with TruthyStatementRdfBuilder! |
||
| 118 | foreach ( $statementList->getPropertyIds() as $propertyId ) { |
||
| 119 | $bestStatements = $statementList->getByPropertyId( $propertyId )->getBestStatements(); |
||
| 120 | foreach ( $bestStatements->toArray() as $statement ) { |
||
| 121 | $bestList[$statement->getGuid()] = true; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach ( $statementList->toArray() as $statement ) { |
||
| 126 | $this->addStatement( $entityId, $statement, isset( $bestList[$statement->getGuid()] ) ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Adds the given Statement from the given Entity to the RDF graph. |
||
| 132 | * |
||
| 133 | * @param EntityId $entityId |
||
| 134 | * @param Statement $statement |
||
| 135 | * @param bool $isBest Is this best ranked statement? |
||
| 136 | */ |
||
| 137 | private function addStatement( EntityId $entityId, Statement $statement, $isBest ) { |
||
| 138 | $statementLName = $this->vocabulary->getStatementLName( $statement ); |
||
| 139 | |||
| 140 | $entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId ); |
||
| 141 | |||
| 142 | $this->addMainSnak( $entityId, $statementLName, $statement, $isBest ); |
||
| 143 | |||
| 144 | // XXX: separate builder for qualifiers? |
||
| 145 | if ( $this->produceQualifiers ) { |
||
| 146 | // this assumes statement was added by addMainSnak |
||
| 147 | foreach ( $statement->getQualifiers() as $q ) { |
||
| 148 | $propertyRepository = $this->vocabulary->getEntityRepositoryName( $q->getPropertyId() ); |
||
| 149 | $this->snakBuilder->addSnak( |
||
| 150 | $this->statementWriter, |
||
| 151 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE], |
||
| 152 | $q, |
||
| 153 | $this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_QUALIFIER], |
||
| 154 | $statementLName |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // XXX: separate builder for references? |
||
| 160 | if ( $this->produceReferences ) { |
||
| 161 | $entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId ); |
||
| 162 | /** @var Reference $reference */ |
||
| 163 | foreach ( $statement->getReferences() as $reference ) { //FIXME: split body into separate method |
||
| 164 | $hash = $reference->getSnaks()->getHash(); |
||
| 165 | $refLName = $hash; |
||
| 166 | |||
| 167 | $this->statementWriter->about( |
||
| 168 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT], |
||
| 169 | $statementLName |
||
| 170 | ) |
||
| 171 | ->say( RdfVocabulary::NS_PROV, 'wasDerivedFrom' )->is( |
||
| 172 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_REFERENCE], |
||
| 173 | $refLName |
||
| 174 | ); |
||
| 175 | if ( $this->dedupeBag->alreadySeen( $hash, 'R' ) !== false ) { |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->referenceWriter->about( |
||
| 180 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_REFERENCE], |
||
| 181 | $refLName |
||
| 182 | ) |
||
| 183 | ->a( RdfVocabulary::NS_ONTOLOGY, 'Reference' ); |
||
| 184 | |||
| 185 | foreach ( $reference->getSnaks() as $refSnak ) { |
||
| 186 | $propertyRepository = $this->vocabulary->getEntityRepositoryName( $refSnak->getPropertyId() ); |
||
| 187 | $this->snakBuilder->addSnak( |
||
| 188 | $this->referenceWriter, |
||
| 189 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE], |
||
| 190 | $refSnak, |
||
| 191 | $this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_REFERENCE], |
||
| 192 | $refLName |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Adds the given Statement's main Snak to the RDF graph. |
||
| 201 | * |
||
| 202 | * @param EntityId $entityId |
||
| 203 | * @param string $statementLName |
||
| 204 | * @param Statement $statement |
||
| 205 | * @param bool $isBest Is this best ranked statement? |
||
| 206 | */ |
||
| 207 | private function addMainSnak( EntityId $entityId, $statementLName, Statement $statement, $isBest ) { |
||
| 208 | $snak = $statement->getMainSnak(); |
||
| 209 | |||
| 210 | $entityLName = $this->vocabulary->getEntityLName( $entityId ); |
||
| 211 | $entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId ); |
||
| 212 | $propertyId = $snak->getPropertyId(); |
||
| 213 | $propertyLName = $this->vocabulary->getEntityLName( $propertyId ); |
||
| 214 | $propertyRepository = $this->vocabulary->getEntityRepositoryName( $propertyId ); |
||
| 215 | |||
| 216 | $this->statementWriter->about( |
||
| 217 | $this->vocabulary->entityNamespaceNames[$entityRepository], |
||
| 218 | $entityLName |
||
| 219 | ) |
||
| 220 | ->say( |
||
| 221 | $this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_CLAIM], |
||
| 222 | $propertyLName |
||
| 223 | ) |
||
| 224 | ->is( $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT], $statementLName ); |
||
| 225 | |||
| 226 | $this->statementWriter->about( |
||
| 227 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT], |
||
| 228 | $statementLName |
||
| 229 | ) |
||
| 230 | ->a( RdfVocabulary::NS_ONTOLOGY, 'Statement' ); |
||
| 231 | |||
| 232 | $rank = $statement->getRank(); |
||
| 233 | if ( isset( RdfVocabulary::$rankMap[$rank] ) ) { |
||
| 234 | if ( $isBest ) { |
||
| 235 | $this->statementWriter->a( RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::WIKIBASE_RANK_BEST ); |
||
| 236 | } |
||
| 237 | $this->statementWriter->about( |
||
| 238 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT], |
||
| 239 | $statementLName |
||
| 240 | ) |
||
| 241 | ->say( RdfVocabulary::NS_ONTOLOGY, 'rank' )->is( RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::$rankMap[$rank] ); |
||
| 242 | } else { |
||
| 243 | wfLogWarning( "Unknown rank $rank encountered for $entityId:{$statement->getGuid()}" ); |
||
| 244 | } |
||
| 245 | |||
| 246 | $this->snakBuilder->addSnak( |
||
| 247 | $this->statementWriter, |
||
| 248 | $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE], |
||
| 249 | $snak, |
||
| 250 | $this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_CLAIM_STATEMENT], |
||
| 251 | $statementLName |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Add fully reified statements for the given entity to the RDF graph. |
||
| 257 | * This may include qualifiers and references, depending on calls to |
||
| 258 | * setProduceQualifiers() resp. setProduceReferences(). |
||
| 259 | * |
||
| 260 | * @param EntityDocument $entity the entity to output. |
||
| 261 | */ |
||
| 262 | public function addEntity( EntityDocument $entity ) { |
||
| 263 | $entityId = $entity->getId(); |
||
| 264 | |||
| 265 | if ( $entity instanceof StatementListProvider ) { |
||
| 266 | $this->addStatements( $entityId, $entity->getStatements() ); |
||
|
0 ignored issues
–
show
|
|||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Does nothing, since Statements should not be part of entity stubs. |
||
| 272 | * |
||
| 273 | * @see EntityRdfBuilder::addEntityStub |
||
| 274 | * |
||
| 275 | * @param EntityDocument $entity the entity to output. |
||
| 276 | */ |
||
| 277 | public function addEntityStub( EntityDocument $entity ) { |
||
| 278 | // noop |
||
| 279 | } |
||
| 280 | |||
| 281 | } |
||
| 282 |
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: