|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
|
4
|
|
|
|
|
5
|
|
|
// Any PSR6 cache can be used. |
|
6
|
|
|
$cache = new Stash\Pool( new \Stash\Driver\FileSystem() ); |
|
7
|
|
|
|
|
8
|
|
|
// @codingStandardsIgnoreStart |
|
9
|
|
|
/** |
|
10
|
|
|
* We want to be able to easily retrieve information |
|
11
|
|
|
* about things that are instances of railway station (Q55488), |
|
12
|
|
|
* so we register a new class that defines its INSTANCE_OF constant. |
|
13
|
|
|
* @link https://www.wikidata.org/wiki/Wikidata:WikiProject_Railways |
|
14
|
|
|
*/ |
|
15
|
|
|
class RailwayStation extends \Samwilson\SimpleWikidata\Item { |
|
16
|
|
|
// @codingStandardsIgnoreEnd |
|
17
|
|
|
/** |
|
18
|
|
|
* Every subclass of Item should define its 'instance of' ID. |
|
19
|
|
|
*/ |
|
20
|
|
|
const INSTANCE_OF = 'Q55488'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* It can then contain any type-specific methods that are required. |
|
24
|
|
|
* @return RailwayStation[] |
|
25
|
|
|
*/ |
|
26
|
|
|
public function hasAdjacentStations() { |
|
27
|
|
|
return $this->getPropertyOfTypeItem( 'P197' ); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Including queries. |
|
32
|
|
|
* @return RailwayStation[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public function isAdjacentTo() { |
|
35
|
|
|
$sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }"; |
|
36
|
|
|
$query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache ); |
|
37
|
|
|
// Each query result will also be a RailwayStation |
|
38
|
|
|
// (if they're correctly recorded as such on Wikidata). |
|
39
|
|
|
return $query->getItems(); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Then the class must register itself. |
|
44
|
|
|
RailwayStation::register(); |
|
45
|
|
|
|
|
46
|
|
|
// Then we can create items with the factory and use them. |
|
47
|
|
|
/** @var RailwayStation $eustonStation */ |
|
48
|
|
|
$eustonStation = \Samwilson\SimpleWikidata\Item::factory( 'Q800751', 'en', $cache ); |
|
49
|
|
|
echo $eustonStation->getLabel() |
|
50
|
|
|
. " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations" |
|
51
|
|
|
. " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n"; |
|
52
|
|
|
|