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
|
|
|
* It can then contain any type-specific methods that are required. |
23
|
|
|
* @return RailwayStation[] |
24
|
|
|
*/ |
25
|
|
|
public function hasAdjacentStations() { |
26
|
|
|
return $this->getPropertyOfTypeItem( 'P197' ); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
/** |
29
|
|
|
* Including queries. |
30
|
|
|
* @return RailwayStation[] |
31
|
|
|
*/ |
32
|
|
|
public function isAdjacentTo() { |
33
|
|
|
$sparql = "SELECT ?item WHERE { ?item wdt:P197 wd:" . $this->getId() . " }"; |
34
|
|
|
$query = new \Samwilson\SimpleWikidata\Query( $sparql, $this->lang, $this->cache ); |
35
|
|
|
// Each query result will also be a RailwayStation |
36
|
|
|
// (if they're correctly recorded as such on Wikidata). |
37
|
|
|
return $query->getItems(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Then the class must register itself. |
42
|
|
|
RailwayStation::register(); |
43
|
|
|
|
44
|
|
|
// Then we can create items with the factory and use them. |
45
|
|
|
/** @var RailwayStation $eustonStation */ |
46
|
|
|
$eustonStation = \Samwilson\SimpleWikidata\Item::factory( 'Q800751', 'en', $cache ); |
47
|
|
|
echo $eustonStation->getLabel() |
48
|
|
|
. " has " . count( $eustonStation->hasAdjacentStations() ) . " adjacent stations" |
49
|
|
|
. " and is adjacent to " . count( $eustonStation->isAdjacentTo() ) . " stations.\n"; |
50
|
|
|
|