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 SiteList; |
||
6 | use Wikibase\DataModel\Entity\EntityDocument; |
||
7 | use Wikibase\DataModel\Entity\Item; |
||
8 | use Wikibase\DataModel\SiteLink; |
||
9 | use Wikimedia\Purtle\RdfWriter; |
||
10 | |||
11 | /** |
||
12 | * RDF mapping for entity SiteLinks. |
||
13 | * |
||
14 | * @license GPL-2.0-or-later |
||
15 | */ |
||
16 | class SiteLinksRdfBuilder implements EntityRdfBuilder { |
||
17 | |||
18 | /** |
||
19 | * @var RdfVocabulary |
||
20 | */ |
||
21 | private $vocabulary; |
||
22 | |||
23 | /** |
||
24 | * @var RdfWriter |
||
25 | */ |
||
26 | private $writer; |
||
27 | |||
28 | /** |
||
29 | * @var SiteList |
||
30 | */ |
||
31 | private $siteLookup; |
||
32 | |||
33 | /** |
||
34 | * @var string[]|null a list of desired sites, or null for all sites. |
||
35 | */ |
||
36 | private $sites; |
||
37 | |||
38 | /** |
||
39 | * @var DedupeBag |
||
40 | */ |
||
41 | private $dedupeBag; |
||
42 | |||
43 | /** |
||
44 | * @param RdfVocabulary $vocabulary |
||
45 | * @param RdfWriter $writer |
||
46 | * @param SiteList $siteLookup |
||
47 | * @param string[]|null $sites |
||
48 | */ |
||
49 | public function __construct( RdfVocabulary $vocabulary, RdfWriter $writer, SiteList $siteLookup, array $sites = null ) { |
||
50 | $this->vocabulary = $vocabulary; |
||
51 | $this->writer = $writer; |
||
52 | $this->siteLookup = $siteLookup; |
||
53 | $this->sites = $sites === null ? null : array_flip( $sites ); |
||
54 | $this->dedupeBag = new NullDedupeBag(); |
||
55 | } |
||
56 | |||
57 | public function setDedupeBag( DedupeBag $dedupeBag ) { |
||
58 | $this->dedupeBag = $dedupeBag; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Site filter |
||
63 | * |
||
64 | * @param string $lang |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function isSiteIncluded( $lang ) { |
||
69 | return $this->sites === null || isset( $this->sites[$lang] ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Adds the site links of the given item to the RDF graph. |
||
74 | * |
||
75 | * @param Item $item |
||
76 | */ |
||
77 | public function addSiteLinks( Item $item ) { |
||
78 | $id = $item->getId(); |
||
79 | $entityLName = $this->vocabulary->getEntityLName( $id ); |
||
0 ignored issues
–
show
|
|||
80 | $entityRepoName = $this->vocabulary->getEntityRepositoryName( $id ); |
||
0 ignored issues
–
show
It seems like
$id defined by $item->getId() on line 78 can be null ; however, Wikibase\Repo\Rdf\RdfVoc...tEntityRepositoryName() 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);
}
}
![]() |
|||
81 | |||
82 | /** @var SiteLink $siteLink */ |
||
83 | foreach ( $item->getSiteLinkList() as $siteLink ) { |
||
84 | if ( !$this->isSiteIncluded( $siteLink->getSiteId() ) ) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | // FIXME: we should check the site exists using hasGlobalId here before asuming it does |
||
89 | $site = $this->siteLookup->getSite( $siteLink->getSiteId() ); |
||
90 | if ( !$site ) { |
||
91 | // Somehow we've got site that we don't know about - skip |
||
92 | continue; |
||
93 | } |
||
94 | $baseUrl = str_replace( '$1', wfUrlencode( str_replace( ' ', '_', $siteLink->getPageName() ) ), |
||
95 | $site->getLinkPath() ); |
||
96 | // XXX: ideally, we'd use https if the target site supports it. |
||
97 | if ( !parse_url( $baseUrl, PHP_URL_SCHEME ) ) { |
||
98 | $url = "http:" . $baseUrl; |
||
99 | } else { |
||
100 | $url = $baseUrl; |
||
101 | } |
||
102 | |||
103 | $group = $site->getGroup(); |
||
104 | $siteUrl = parse_url( $url, PHP_URL_SCHEME ) . '://' . parse_url( $url, PHP_URL_HOST ) . "/"; |
||
105 | $lang = $this->vocabulary->getCanonicalLanguageCode( $site->getLanguageCode() ); |
||
106 | |||
107 | $this->writer->about( $url ) |
||
108 | ->a( RdfVocabulary::NS_SCHEMA_ORG, 'Article' ) |
||
109 | ->say( RdfVocabulary::NS_SCHEMA_ORG, 'about' ) |
||
110 | ->is( $this->vocabulary->entityNamespaceNames[$entityRepoName], $entityLName ) |
||
111 | ->say( RdfVocabulary::NS_SCHEMA_ORG, 'inLanguage' )->text( $lang ) |
||
112 | ->say( RdfVocabulary::NS_SCHEMA_ORG, 'isPartOf' )->is( $siteUrl ) |
||
113 | ->say( RdfVocabulary::NS_SCHEMA_ORG, 'name' )->text( $siteLink->getPageName(), $lang ); |
||
114 | |||
115 | foreach ( $siteLink->getBadges() as $badge ) { |
||
116 | $badgeRepoName = $this->vocabulary->getEntityRepositoryName( $badge ); |
||
117 | $this->writer |
||
118 | ->say( RdfVocabulary::NS_ONTOLOGY, 'badge' ) |
||
119 | ->is( |
||
120 | $this->vocabulary->entityNamespaceNames[$badgeRepoName], |
||
121 | $this->vocabulary->getEntityLName( $badge ) |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | /* Write group of the site only once. |
||
126 | * We are using URL as namespace to ensure it is not cut off. |
||
127 | * Since we do not have too may distinct sites, memory cost is small. |
||
128 | */ |
||
129 | if ( !$this->dedupeBag->alreadySeen( $group, $siteUrl ) ) { |
||
130 | $this->writer->about( $siteUrl ) |
||
131 | ->say( RdfVocabulary::NS_ONTOLOGY, 'wikiGroup' )->text( $group ); |
||
132 | } |
||
133 | |||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Add the entity's sitelinks to the RDF graph. |
||
139 | * |
||
140 | * @param EntityDocument $entity the entity to output. |
||
141 | */ |
||
142 | public function addEntity( EntityDocument $entity ) { |
||
143 | if ( $entity instanceof Item ) { |
||
144 | $this->addSiteLinks( $entity ); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Does nothing, since SiteLinks should not be part of entity stubs. |
||
150 | * |
||
151 | * @see EntityRdfBuilder::addEntityStub |
||
152 | * |
||
153 | * @param EntityDocument $entity the entity to output. |
||
154 | */ |
||
155 | public function addEntityStub( EntityDocument $entity ) { |
||
156 | // noop |
||
157 | } |
||
158 | |||
159 | } |
||
160 |
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: