Passed
Push — master ( 12c67d...250c4f )
by Sam
02:03
created
examples/humans.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 $datesOfBirth = $princeCharles->getDatesOfBirth();
18 18
 echo "  Date of birth: ".$datesOfBirth[0]->getDateTime()->format( 'j F, Y' )." ";
19 19
 foreach ( $datesOfBirth[0]->getReferences() as $ref ) {
20
-	if ( $ref->statedIn() ) {
21
-		echo "[$refNum]";
22
-		$references[$refNum] = $ref;
23
-	}
20
+    if ( $ref->statedIn() ) {
21
+        echo "[$refNum]";
22
+        $references[$refNum] = $ref;
23
+    }
24 24
 }
25 25
 echo "\n";
26 26
 
@@ -28,13 +28,13 @@  discard block
 block discarded – undo
28 28
 $fathers = $princeCharles->fathers();
29 29
 echo "  Father: ".$fathers[0]->getItem()->getLabel() . " ";
30 30
 foreach ( $fathers[0]->getReferences() as $ref ) {
31
-	if ( $ref->statedIn() ) {
32
-		echo "[$refNum]";
33
-		$references[$refNum] = $ref;
34
-	}
31
+    if ( $ref->statedIn() ) {
32
+        echo "[$refNum]";
33
+        $references[$refNum] = $ref;
34
+    }
35 35
 }
36 36
 echo "\n";
37 37
 
38 38
 foreach ( $references as $refNum => $ref ) {
39
-	echo "  [$refNum] - " . $ref->statedIn()->getLabel()."\n";
39
+    echo "  [$refNum] - " . $ref->statedIn()->getLabel()."\n";
40 40
 }
Please login to merge, or discard this patch.
examples/query.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -11,11 +11,11 @@
 block discarded – undo
11 11
 $query = new \Samwilson\SimpleWikidata\Query( $sparql, 'en', $cache );
12 12
 $hills = $query->getItems();
13 13
 foreach ( $hills as $hill ) {
14
-	$heights = $hill->getPropertyOfTypeQuantity( 'P2044' );
15
-	if ( !$heights ) {
16
-		echo "No heights found for ".$hill->getLabel()."\n";
17
-		continue;
18
-	}
19
-	$height = array_shift( $heights );
20
-	echo $hill->getLabel()." is ".$height['amount']." ".$height['unit']->getLabel()." high.\n";
14
+    $heights = $hill->getPropertyOfTypeQuantity( 'P2044' );
15
+    if ( !$heights ) {
16
+        echo "No heights found for ".$hill->getLabel()."\n";
17
+        continue;
18
+    }
19
+    $height = array_shift( $heights );
20
+    echo $hill->getLabel()." is ".$height['amount']." ".$height['unit']->getLabel()." high.\n";
21 21
 }
Please login to merge, or discard this patch.
src/Properties/Time.php 1 patch
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -27,48 +27,48 @@
 block discarded – undo
27 27
  */
28 28
 class Time extends Property {
29 29
 
30
-	/**
31
-	 * @return DateTime
32
-	 */
33
-	public function getDateTime() {
34
-		return new DateTime( $this->claim['mainsnak']['datavalue']['value']['time'] );
35
-	}
30
+    /**
31
+     * @return DateTime
32
+     */
33
+    public function getDateTime() {
34
+        return new DateTime( $this->claim['mainsnak']['datavalue']['value']['time'] );
35
+    }
36 36
 
37
-	/**
38
-	 * @return DateTimeZone
39
-	 */
40
-	public function getTimezone() {
41
-		return new DateTimeZone( $this->claim['mainsnak']['datavalue']['value']['timezone'] );
42
-	}
37
+    /**
38
+     * @return DateTimeZone
39
+     */
40
+    public function getTimezone() {
41
+        return new DateTimeZone( $this->claim['mainsnak']['datavalue']['value']['timezone'] );
42
+    }
43 43
 
44
-	/**
45
-	 * @return mixed
46
-	 */
47
-	public function getBefore() {
48
-		return $this->claim['mainsnak']['datavalue']['value']['before'];
49
-	}
44
+    /**
45
+     * @return mixed
46
+     */
47
+    public function getBefore() {
48
+        return $this->claim['mainsnak']['datavalue']['value']['before'];
49
+    }
50 50
 
51
-	/**
52
-	 * @return mixed
53
-	 */
54
-	public function getAfter() {
55
-		return $this->claim['mainsnak']['datavalue']['value']['time'];
56
-	}
51
+    /**
52
+     * @return mixed
53
+     */
54
+    public function getAfter() {
55
+        return $this->claim['mainsnak']['datavalue']['value']['time'];
56
+    }
57 57
 
58
-	/**
59
-	 * @return int The numbers have the following meanings: 0 - billion years,
60
-	 * 1 - hundred million years, ...,
61
-	 * 6 - millennium, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour,
62
-	 * 13 - minute, 14 - second.
63
-	 */
64
-	public function getPrecision() {
65
-		return $this->claim['mainsnak']['datavalue']['value']['precision'];
66
-	}
58
+    /**
59
+     * @return int The numbers have the following meanings: 0 - billion years,
60
+     * 1 - hundred million years, ...,
61
+     * 6 - millennium, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour,
62
+     * 13 - minute, 14 - second.
63
+     */
64
+    public function getPrecision() {
65
+        return $this->claim['mainsnak']['datavalue']['value']['precision'];
66
+    }
67 67
 
68
-	/**
69
-	 * @return mixed
70
-	 */
71
-	public function getCalendarModel() {
72
-		return $this->claim['mainsnak']['datavalue']['value']['calendarmodel'];
73
-	}
68
+    /**
69
+     * @return mixed
70
+     */
71
+    public function getCalendarModel() {
72
+        return $this->claim['mainsnak']['datavalue']['value']['calendarmodel'];
73
+    }
74 74
 }
Please login to merge, or discard this patch.
src/Properties/Item.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -6,11 +6,11 @@
 block discarded – undo
6 6
 
7 7
 class Item extends Property {
8 8
 
9
-	/**
10
-	 * @return \Samwilson\SimpleWikidata\Item
11
-	 */
12
-	public function getItem() {
13
-		$itemId = $this->claim['mainsnak']['datavalue']['value']['id'];
14
-		return \Samwilson\SimpleWikidata\Item::factory( $itemId, $this->lang, $this->cache );
15
-	}
9
+    /**
10
+     * @return \Samwilson\SimpleWikidata\Item
11
+     */
12
+    public function getItem() {
13
+        $itemId = $this->claim['mainsnak']['datavalue']['value']['id'];
14
+        return \Samwilson\SimpleWikidata\Item::factory( $itemId, $this->lang, $this->cache );
15
+    }
16 16
 }
Please login to merge, or discard this patch.
src/Property.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -6,32 +6,32 @@
 block discarded – undo
6 6
 
7 7
 abstract class Property {
8 8
 
9
-		/** @var string[] */
10
-	protected $claim;
11
-
12
-		/** @var string */
13
-	protected $lang;
14
-
15
-		/** @var CacheItemPoolInterface */
16
-	protected $cache;
17
-
18
-		public function __construct( $claim, $lang, $cache ) {
19
-		$this->claim = $claim;
20
-		$this->lang = $lang;
21
-		$this->cache = $cache;
22
-	 }
23
-
24
-	/**
25
-	 * @return Reference[]
26
-	 */
27
-	public function getReferences() {
28
-		$references = [];
29
-		if ( !isset( $this->claim['references'] ) ) {
30
-			return $references;
31
-		}
32
-		foreach ( $this->claim['references'] as $ref ) {
33
-			$references[] = new Reference( $ref, $this->lang, $this->cache );
34
-		}
35
-		return $references;
36
-	}
9
+        /** @var string[] */
10
+    protected $claim;
11
+
12
+        /** @var string */
13
+    protected $lang;
14
+
15
+        /** @var CacheItemPoolInterface */
16
+    protected $cache;
17
+
18
+        public function __construct( $claim, $lang, $cache ) {
19
+        $this->claim = $claim;
20
+        $this->lang = $lang;
21
+        $this->cache = $cache;
22
+        }
23
+
24
+    /**
25
+     * @return Reference[]
26
+     */
27
+    public function getReferences() {
28
+        $references = [];
29
+        if ( !isset( $this->claim['references'] ) ) {
30
+            return $references;
31
+        }
32
+        foreach ( $this->claim['references'] as $ref ) {
33
+            $references[] = new Reference( $ref, $this->lang, $this->cache );
34
+        }
35
+        return $references;
36
+    }
37 37
 }
Please login to merge, or discard this patch.
src/Query.php 1 patch
Indentation   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -8,78 +8,78 @@
 block discarded – undo
8 8
 
9 9
 class Query {
10 10
 
11
-	/** @var string */
12
-	protected $query;
11
+    /** @var string */
12
+    protected $query;
13 13
 
14
-	/** @var string */
15
-	protected $lang;
14
+    /** @var string */
15
+    protected $lang;
16 16
 
17
-	/** @var CacheItemPoolInterface */
18
-	protected $cache;
17
+    /** @var CacheItemPoolInterface */
18
+    protected $cache;
19 19
 
20
-	/**
21
-	 * Query constructor.
22
-	 * @param string $query The Sparql query.
23
-	 * @param string $lang The language.
24
-	 * @param CacheItemPoolInterface $cache The cache.
25
-	 */
26
-	public function __construct( $query, $lang, CacheItemPoolInterface $cache ) {
27
-		$this->query = $query;
28
-		$this->lang = $lang;
29
-		$this->cache = $cache;
30
-	}
20
+    /**
21
+     * Query constructor.
22
+     * @param string $query The Sparql query.
23
+     * @param string $lang The language.
24
+     * @param CacheItemPoolInterface $cache The cache.
25
+     */
26
+    public function __construct( $query, $lang, CacheItemPoolInterface $cache ) {
27
+        $this->query = $query;
28
+        $this->lang = $lang;
29
+        $this->cache = $cache;
30
+    }
31 31
 
32
-	/**
33
-	 * Get the items.
34
-	 * @return Item[] The results.
35
-	 */
36
-	public function getItems() {
37
-		$xml = $this->getXml( $this->query );
38
-		$results = [];
39
-		foreach ( $xml->results->result as $res ) {
40
-			$result = $this->getBindings( $res );
41
-			$id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 );
42
-			$item = Item::factory( $id, $this->lang, $this->cache );
43
-			$results[] = $item;
44
-		}
45
-		return $results;
46
-	}
32
+    /**
33
+     * Get the items.
34
+     * @return Item[] The results.
35
+     */
36
+    public function getItems() {
37
+        $xml = $this->getXml( $this->query );
38
+        $results = [];
39
+        foreach ( $xml->results->result as $res ) {
40
+            $result = $this->getBindings( $res );
41
+            $id = substr( $result['item'], strrpos( $result['item'], '/' ) + 1 );
42
+            $item = Item::factory( $id, $this->lang, $this->cache );
43
+            $results[] = $item;
44
+        }
45
+        return $results;
46
+    }
47 47
 
48
-	/**
49
-	 * @param string $query The Sparql query.
50
-	 * @return SimpleXmlElement
51
-	 * @throws Exception
52
-	 */
53
-	protected function getXml( $query ) {
54
-		$url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=" . urlencode( $query );
55
-		try {
56
-			$result = file_get_contents( $url );
57
-		} catch ( Exception $e ) {
58
-			throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 );
59
-		}
60
-		if ( empty( $result ) ) {
61
-			$msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>";
62
-			throw new Exception( $msg, 500 );
63
-		}
64
-		$xml = new SimpleXmlElement( $result );
65
-		return $xml;
66
-	}
48
+    /**
49
+     * @param string $query The Sparql query.
50
+     * @return SimpleXmlElement
51
+     * @throws Exception
52
+     */
53
+    protected function getXml( $query ) {
54
+        $url = "https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=" . urlencode( $query );
55
+        try {
56
+            $result = file_get_contents( $url );
57
+        } catch ( Exception $e ) {
58
+            throw new Exception( "Unable to run query: <pre>" . htmlspecialchars( $query ) . "</pre>", 500 );
59
+        }
60
+        if ( empty( $result ) ) {
61
+            $msg = "No result from query: <pre>" . htmlspecialchars( $query ) . "</pre>";
62
+            throw new Exception( $msg, 500 );
63
+        }
64
+        $xml = new SimpleXmlElement( $result );
65
+        return $xml;
66
+    }
67 67
 
68
-	/**
69
-	 * @param SimpleXmlElement $xml The query result XML.
70
-	 * @return array
71
-	 */
72
-	protected function getBindings( $xml ) {
73
-		$out = [];
74
-		foreach ( $xml->binding as $binding ) {
75
-			if ( isset( $binding->literal ) ) {
76
-				$out[(string)$binding['name']] = (string)$binding->literal;
77
-			}
78
-			if ( isset( $binding->uri ) ) {
79
-				$out[(string)$binding['name']] = (string)$binding->uri;
80
-			}
81
-		}
82
-		return $out;
83
-	}
68
+    /**
69
+     * @param SimpleXmlElement $xml The query result XML.
70
+     * @return array
71
+     */
72
+    protected function getBindings( $xml ) {
73
+        $out = [];
74
+        foreach ( $xml->binding as $binding ) {
75
+            if ( isset( $binding->literal ) ) {
76
+                $out[(string)$binding['name']] = (string)$binding->literal;
77
+            }
78
+            if ( isset( $binding->uri ) ) {
79
+                $out[(string)$binding['name']] = (string)$binding->uri;
80
+            }
81
+        }
82
+        return $out;
83
+    }
84 84
 
85 85
 }
Please login to merge, or discard this patch.
src/Reference.php 1 patch
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -6,37 +6,37 @@
 block discarded – undo
6 6
 
7 7
 class Reference {
8 8
 
9
-	const STATED_IN = 'P248';
10
-
11
-	/** @var array */
12
-	protected $data;
13
-
14
-	/** @var string */
15
-	protected $lang;
16
-
17
-	/** @var CacheItemPoolInterface */
18
-	protected $cache;
19
-
20
-	/**
21
-	 * @param string[] $data The data.
22
-	 * @param string $lang ISO639 language code.
23
-	 * @param CacheItemPoolInterface $cache The cache.
24
-	 */
25
-	public function __construct( $data, $lang, $cache ) {
26
-		$this->data = $data;
27
-		$this->lang = $lang;
28
-		$this->cache = $cache;
29
-	}
30
-
31
-	/**
32
-	 * @return Item|bool The item, or false if there isn't one.
33
-	 */
34
-	public function statedIn() {
35
-		if ( !isset( $this->data['snaks'][self::STATED_IN] ) ) {
36
-			return false;
37
-		}
38
-		foreach ( $this->data['snaks'][self::STATED_IN] as $snak ) {
39
-			return Item::factory( $snak['datavalue']['value']['id'], $this->lang, $this->cache );
40
-		}
41
-	}
9
+    const STATED_IN = 'P248';
10
+
11
+    /** @var array */
12
+    protected $data;
13
+
14
+    /** @var string */
15
+    protected $lang;
16
+
17
+    /** @var CacheItemPoolInterface */
18
+    protected $cache;
19
+
20
+    /**
21
+     * @param string[] $data The data.
22
+     * @param string $lang ISO639 language code.
23
+     * @param CacheItemPoolInterface $cache The cache.
24
+     */
25
+    public function __construct( $data, $lang, $cache ) {
26
+        $this->data = $data;
27
+        $this->lang = $lang;
28
+        $this->cache = $cache;
29
+    }
30
+
31
+    /**
32
+     * @return Item|bool The item, or false if there isn't one.
33
+     */
34
+    public function statedIn() {
35
+        if ( !isset( $this->data['snaks'][self::STATED_IN] ) ) {
36
+            return false;
37
+        }
38
+        foreach ( $this->data['snaks'][self::STATED_IN] as $snak ) {
39
+            return Item::factory( $snak['datavalue']['value']['id'], $this->lang, $this->cache );
40
+        }
41
+    }
42 42
 }
Please login to merge, or discard this patch.
src/Item.php 1 patch
Indentation   +423 added lines, -423 removed lines patch added patch discarded remove patch
@@ -13,150 +13,150 @@  discard block
 block discarded – undo
13 13
 
14 14
 class Item {
15 15
 
16
-	const PROP_INSTANCE_OF = 'P31';
17
-	const PROP_TITLE = 'P1476';
18
-	const PROP_IMAGE = 'P18';
19
-	const PROP_AUTHOR = 'P50';
20
-
21
-	/** @var string */
22
-	protected $id;
23
-
24
-	/** @var MediawikiApi */
25
-	protected $wdApi;
26
-
27
-	/** @var string */
28
-	protected $lang;
29
-
30
-	/** @var CacheItemPoolInterface */
31
-	protected $cache;
32
-
33
-	/** @var string The base URL of Wikidata, with trailing slash. */
34
-	protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/';
35
-
36
-		private function __construct( $id, $lang, CacheItemPoolInterface $cache ) {
37
-		if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) {
38
-			throw new Exception( "Not a valid ID: " . var_export( $id, true ) );
39
-		}
40
-		$this->id = $id;
41
-		$this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' );
42
-		$this->entities = [];
43
-		$this->lang = $lang;
44
-		$this->cache = $cache;
45
-	 }
46
-
47
-	/**
48
-	 * Create a new Item object with class based on the item's 'instance of' statement.
49
-	 *
50
-	 * @param string $id
51
-	 * @param string $lang
52
-	 *
53
-	 * @return Item
54
-	 */
55
-	public static function factory( $id, $lang, CacheItemPoolInterface $cache ) {
56
-		$item = new Item( $id, $lang, $cache );
57
-		foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) {
58
-			// Try to find a class mating the 'instance of' name.
59
-			$possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase( $instanceOf->getItem()->getLabel() );
60
-			if ( class_exists( $possibleClassName ) ) {
61
-				// This won't re-request the metadata, because that's cached.
62
-				$specificItem = new $possibleClassName( $id, $lang, $cache );
63
-				return $specificItem;
64
-			}
65
-		}
66
-
67
-		// If we're here, just leave it as a basic Item.
68
-		$item->setCache( $cache );
69
-		return $item;
70
-	}
71
-
72
-	public function setCache( CacheItemPoolInterface $cache_item_pool ) {
73
-		$this->cache = $cache_item_pool;
74
-	}
75
-
76
-	/**
77
-	 * Get the ID (Q-number) of this item.
78
-	 * @return string|bool The ID or false if it couldn't be determined.
79
-	 */
80
-	public function getId() {
81
-		$entity = $this->getEntity( $this->id );
82
-		return isset( $entity['id'] ) ? $entity['id'] : false;
83
-	}
84
-
85
-	/**
86
-	 * Get this item's label.
87
-	 * @return string
88
-	 */
89
-	public function getLabel() {
90
-		$entity = $this->getEntity( $this->id );
91
-		if ( ! empty( $entity['labels'][ $this->lang ]['value'] ) ) {
92
-			// Use the label if there is one.
93
-			return $entity['labels'][ $this->lang ]['value'];
94
-		}
95
-		// Or just use the ID.
96
-		return $entity['id'];
97
-	}
98
-
99
-	public function getWikidataUrl() {
100
-		return $this->wikidataUrlBase.$this->id;
101
-	}
102
-
103
-	/**
104
-	 * Wikiprojects list their properties like this:
105
-	 *
106
-	 *     {{List of properties/Header}}
107
-	 *     {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}}
108
-	 *     </table>
109
-	 *
110
-	 * @param string $wikiProject
111
-	 * @param string $type
112
-	 * @return array
113
-	 */
114
-	public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) {
115
-		if ( $type !== 'work' ) {
116
-			$type = 'edition';
117
-		}
118
-		$cacheKey = $type . '_item_property_IDs';
119
-		if ( $this->cache->hasItem( $cacheKey ) ) {
120
-			$propIds = $this->cache->getItem( $cacheKey )->get();
121
-		} else {
122
-			$domCrawler = new Crawler();
123
-			$wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject;
124
-			$domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) );
125
-			$propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']/../following-sibling::table[1]//td[2]/a";
126
-			$propCells = $domCrawler->filterXPath( $propAncors );
127
-			$propIds = [];
128
-			$propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) {
129
-				$propId = $node->text();
130
-				$propIds[] = $propId;
131
-			} );
132
-			$cacheItem = $this->cache->getItem( $cacheKey )
133
-				->expiresAfter( new DateInterval( 'PT1H' ) )
134
-				->set( $propIds );
135
-			$this->cache->save( $cacheItem );
136
-		}
137
-		$workProperties = [];
138
-		foreach ( $propIds as $propId ) {
139
-			$workProperties[] = self::factory( $propId, $this->lang, $this->cache );
140
-		}
141
-
142
-		return $workProperties;
143
-	}
144
-
145
-	/**
146
-	 * @param string $propertyId
147
-	 * @return bool|Time[]
148
-	 */
149
-	protected function getPropertyOfTypeTime( $propertyId ) {
150
-		$times = [];
151
-		$entity = $this->getEntity();
152
-		if ( !isset( $entity['claims'][$propertyId] ) ) {
153
-			// No statements for this property.
154
-			return $times;
155
-		}
156
-		// print_r($entity['claims'][$propertyId]);exit();
157
-		foreach ( $entity['claims'][$propertyId] as $claim ) {
158
-			// print_r($claim);
159
-			$times[] = new Time( $claim, $this->lang, $this->cache );
16
+    const PROP_INSTANCE_OF = 'P31';
17
+    const PROP_TITLE = 'P1476';
18
+    const PROP_IMAGE = 'P18';
19
+    const PROP_AUTHOR = 'P50';
20
+
21
+    /** @var string */
22
+    protected $id;
23
+
24
+    /** @var MediawikiApi */
25
+    protected $wdApi;
26
+
27
+    /** @var string */
28
+    protected $lang;
29
+
30
+    /** @var CacheItemPoolInterface */
31
+    protected $cache;
32
+
33
+    /** @var string The base URL of Wikidata, with trailing slash. */
34
+    protected $wikidataUrlBase = 'https://www.wikidata.org/wiki/';
35
+
36
+        private function __construct( $id, $lang, CacheItemPoolInterface $cache ) {
37
+        if ( !is_string( $id ) || preg_match( '/[QP][0-9]*/i', $id ) !== 1 ) {
38
+            throw new Exception( "Not a valid ID: " . var_export( $id, true ) );
39
+        }
40
+        $this->id = $id;
41
+        $this->wdApi = new MediawikiApi( 'https://www.wikidata.org/w/api.php' );
42
+        $this->entities = [];
43
+        $this->lang = $lang;
44
+        $this->cache = $cache;
45
+        }
46
+
47
+    /**
48
+     * Create a new Item object with class based on the item's 'instance of' statement.
49
+     *
50
+     * @param string $id
51
+     * @param string $lang
52
+     *
53
+     * @return Item
54
+     */
55
+    public static function factory( $id, $lang, CacheItemPoolInterface $cache ) {
56
+        $item = new Item( $id, $lang, $cache );
57
+        foreach ( $item->getPropertyOfTypeItem( self::PROP_INSTANCE_OF ) as $instanceOf ) {
58
+            // Try to find a class mating the 'instance of' name.
59
+            $possibleClassName = __NAMESPACE__ . '\\Items\\' . Str::toCamelCase( $instanceOf->getItem()->getLabel() );
60
+            if ( class_exists( $possibleClassName ) ) {
61
+                // This won't re-request the metadata, because that's cached.
62
+                $specificItem = new $possibleClassName( $id, $lang, $cache );
63
+                return $specificItem;
64
+            }
65
+        }
66
+
67
+        // If we're here, just leave it as a basic Item.
68
+        $item->setCache( $cache );
69
+        return $item;
70
+    }
71
+
72
+    public function setCache( CacheItemPoolInterface $cache_item_pool ) {
73
+        $this->cache = $cache_item_pool;
74
+    }
75
+
76
+    /**
77
+     * Get the ID (Q-number) of this item.
78
+     * @return string|bool The ID or false if it couldn't be determined.
79
+     */
80
+    public function getId() {
81
+        $entity = $this->getEntity( $this->id );
82
+        return isset( $entity['id'] ) ? $entity['id'] : false;
83
+    }
84
+
85
+    /**
86
+     * Get this item's label.
87
+     * @return string
88
+     */
89
+    public function getLabel() {
90
+        $entity = $this->getEntity( $this->id );
91
+        if ( ! empty( $entity['labels'][ $this->lang ]['value'] ) ) {
92
+            // Use the label if there is one.
93
+            return $entity['labels'][ $this->lang ]['value'];
94
+        }
95
+        // Or just use the ID.
96
+        return $entity['id'];
97
+    }
98
+
99
+    public function getWikidataUrl() {
100
+        return $this->wikidataUrlBase.$this->id;
101
+    }
102
+
103
+    /**
104
+     * Wikiprojects list their properties like this:
105
+     *
106
+     *     {{List of properties/Header}}
107
+     *     {{List of properties/Row|id=31|example-subject=Q923767|example-object=Q3331189}}
108
+     *     </table>
109
+     *
110
+     * @param string $wikiProject
111
+     * @param string $type
112
+     * @return array
113
+     */
114
+    public function getStandardProperties( $wikiProject = 'WikiProject_Books', $type = 'work' ) {
115
+        if ( $type !== 'work' ) {
116
+            $type = 'edition';
117
+        }
118
+        $cacheKey = $type . '_item_property_IDs';
119
+        if ( $this->cache->hasItem( $cacheKey ) ) {
120
+            $propIds = $this->cache->getItem( $cacheKey )->get();
121
+        } else {
122
+            $domCrawler = new Crawler();
123
+            $wikiProjectUrl = 'https://www.wikidata.org/wiki/Wikidata:' . $wikiProject;
124
+            $domCrawler->addHtmlContent( file_get_contents( $wikiProjectUrl ) );
125
+            $propAncors = "//h3/span[@id='" . ucfirst( $type ) . "_item_properties']/../following-sibling::table[1]//td[2]/a";
126
+            $propCells = $domCrawler->filterXPath( $propAncors );
127
+            $propIds = [];
128
+            $propCells->each( function ( Crawler $node, $i ) use ( &$propIds ) {
129
+                $propId = $node->text();
130
+                $propIds[] = $propId;
131
+            } );
132
+            $cacheItem = $this->cache->getItem( $cacheKey )
133
+                ->expiresAfter( new DateInterval( 'PT1H' ) )
134
+                ->set( $propIds );
135
+            $this->cache->save( $cacheItem );
136
+        }
137
+        $workProperties = [];
138
+        foreach ( $propIds as $propId ) {
139
+            $workProperties[] = self::factory( $propId, $this->lang, $this->cache );
140
+        }
141
+
142
+        return $workProperties;
143
+    }
144
+
145
+    /**
146
+     * @param string $propertyId
147
+     * @return bool|Time[]
148
+     */
149
+    protected function getPropertyOfTypeTime( $propertyId ) {
150
+        $times = [];
151
+        $entity = $this->getEntity();
152
+        if ( !isset( $entity['claims'][$propertyId] ) ) {
153
+            // No statements for this property.
154
+            return $times;
155
+        }
156
+        // print_r($entity['claims'][$propertyId]);exit();
157
+        foreach ( $entity['claims'][$propertyId] as $claim ) {
158
+            // print_r($claim);
159
+            $times[] = new Time( $claim, $this->lang, $this->cache );
160 160
 //
161 161
 // $timeValue = $claim['datavalue']['value']['time'];
162 162
 // // Ugly workaround for imprecise dates. :-(
@@ -166,283 +166,283 @@  discard block
 block discarded – undo
166 166
 // }
167 167
 // $time = strtotime($timeValue);
168 168
 // return date($dateFormat, $time);
169
-			// }
170
-		}
171
-		return $times;
172
-	}
173
-
174
-	/**
175
-	 * Get the Item that is referred to by the specified item's property.
176
-	 *
177
-	 * @param string $propertyId
178
-	 *
179
-	 * @return \Samwilson\SimpleWikidata\Properties\Item[]
180
-	 */
181
-	protected function getPropertyOfTypeItem( $propertyId ) {
182
-		$entity = $this->getEntity( $this->id );
183
-		if ( !isset( $entity['claims'][$propertyId] ) ) {
184
-			return [];
185
-		}
186
-		$items = [];
187
-		foreach ( $entity['claims'][$propertyId] as $claim ) {
188
-			$items[] = new Properties\Item( $claim, $this->lang, $this->cache );
189
-		}
190
-
191
-		return $items;
192
-	}
193
-
194
-	public function setPropertyOfTypeItem( $property, $itemId ) {
195
-		$itemIdNumeric = substr( $itemId, 1 );
196
-
197
-		// First see if this property already exists, and that it is different from what's being set.
198
-		$entity = $this->getEntity( $this->id );
199
-		if ( !empty( $entity['claims'][$property] ) ) {
200
-			// Get the first claim, and update it if necessary.
201
-			$claim = array_shift( $entity['claims'][$property] );
202
-			if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) {
203
-				// Already is the required value, no need to change.
204
-				return;
205
-			}
206
-			$claim['mainsnak']['datavalue']['value']['id'] = $itemId;
207
-			$claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric;
208
-			$apiParams = [
209
-				'action' => 'wbsetclaim',
210
-				'claim' => json_encode( $claim ),
211
-			];
212
-		}
213
-
214
-		// If no claim was found (and modified) above, create a new claim.
215
-		if ( !isset( $apiParams ) ) {
216
-			$apiParams = [
217
-				'action' => 'wbcreateclaim',
218
-				'entity' => $this->getId(),
219
-				'property' => $property,
220
-				'snaktype' => 'value',
221
-				'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ),
222
-			];
223
-		}
224
-
225
-		// Save the property.
226
-		$wdWpOauth = new WdOauth();
227
-		$wdWpOauth->makeCall( $apiParams, true );
228
-
229
-		// Clear the cache.
230
-		$this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) );
231
-	}
232
-
233
-	public function getPropertyOfTypeUrl( $entityId, $propertyId ) {
234
-		$entity = $this->getEntity( $entityId );
235
-		if ( !isset( $entity['claims'][$propertyId] ) ) {
236
-			return false;
237
-		}
238
-		$urls = [];
239
-		foreach ( $entity['claims'][$propertyId] as $claim ) {
240
-			$urls[] = $claim['mainsnak']['datavalue']['value'];
241
-		}
242
-
243
-		return $urls;
244
-	}
245
-
246
-	public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) {
247
-		$entity = $this->getEntity( $entityId );
248
-		if ( !isset( $entity['claims'][$propertyId] ) ) {
249
-			return false;
250
-		}
251
-		$idents = [];
252
-		foreach ( $entity['claims'][$propertyId] as $claim ) {
253
-			$qualifiers = [];
254
-			if ( !isset( $claim['qualifiers'] ) ) {
255
-				continue;
256
-			}
257
-			foreach ( $claim['qualifiers'] as $qualsInfo ) {
258
-				foreach ( $qualsInfo as $qualInfo ) {
259
-					$qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache );
260
-					$propLabel = $qualProp->getLabel();
261
-					if ( !isset( $qualifiers[$propLabel] ) ) {
262
-						$qualifiers[$propLabel] = [];
263
-					}
264
-					$qualifiers[$propLabel][] = $qualInfo['datavalue']['value'];
265
-				}
266
-			}
267
-			$idents[] = [
268
-				'qualifiers' => $qualifiers,
269
-				'value' => $claim['mainsnak']['datavalue']['value'],
270
-			];
271
-		}
272
-
273
-		return $idents;
274
-	}
275
-
276
-	/**
277
-	 * Get a single-valued text property.
278
-	 * @param string $property One of the PROP_* constants.
279
-	 * @return string|bool The value, or false if it can't be found.
280
-	 */
281
-	public function getPropertyOfTypeText( $property ) {
282
-		$entity = $this->getEntity( $this->id );
283
-		if ( isset( $entity['claims'][$property] ) ) {
284
-			// Use the first title.
285
-			foreach ( $entity['claims'][$property] as $t ) {
286
-				if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) {
287
-					var_dump( $t['mainsnak']['datavalue']['value'] );
288
-					exit();
289
-				}
290
-				if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang
291
-					&& !empty( $t['mainsnak']['datavalue']['value']['text'] )
292
-				) {
293
-					return $t['mainsnak']['datavalue']['value']['text'];
294
-				}
295
-			}
296
-		}
297
-		return false;
298
-	}
299
-
300
-	/**
301
-	 * Literal data field for a quantity that relates to some kind of well-defined unit. The actual unit goes in the data values that is entered.
302
-	 *   - amount – implicit part of the string (mapping of unit prefix is unclear)
303
-	 *   - unit – implicit part of the string that defaults to "1" (mapping to standardizing body is unclear)
304
-	 *   - upperbound - quantity's upper bound
305
-	 *   - lowerbound - quantity's lower bound
306
-	 * @param $property
307
-	 * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc.
308
-	 */
309
-	public function getPropertyOfTypeQuantity( $property ) {
310
-		$quantities = [];
311
-		$entity = $this->getEntity( $this->id );
312
-		if ( !isset( $entity['claims'][$property] ) ) {
313
-			return false;
314
-		}
315
-		foreach ( $entity['claims'][$property] as $t ) {
316
-			$quantity = $t['mainsnak']['datavalue']['value'];
317
-			$unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 );
318
-			$quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache );
319
-			$quantities[] = $quantity;
320
-		}
321
-		return $quantities;
322
-	}
323
-
324
-	/**
325
-	 * Set a single-valued text property.
326
-	 * @param string $property One of the PROP_* constants.
327
-	 * @param string $value The value.
328
-	 */
329
-	public function setPropertyOfTypeText( $property, $value ) {
330
-		// First see if this property already exists, and that it is different from what's being set.
331
-		$entity = $this->getEntity( $this->id );
332
-		if ( !empty( $entity['claims'][$property] ) ) {
333
-			// Find this language's claim (if there is one).
334
-			foreach ( $entity['claims'][$property] as $claim ) {
335
-				if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) {
336
-					// Modify this claim's text value.
337
-					$titleClaim = $claim;
338
-					$titleClaim['mainsnak']['datavalue']['value']['text'] = $value;
339
-					$setTitleParams = [
340
-						'action' => 'wbsetclaim',
341
-						'claim' => \GuzzleHttp\json_encode( $titleClaim ),
342
-					];
343
-					continue;
344
-				}
345
-			}
346
-		}
347
-
348
-		// If no claim was found (and modified) above, create a new claim.
349
-		if ( !isset( $setTitleParams ) ) {
350
-			$setTitleParams = [
351
-				'action' => 'wbcreateclaim',
352
-				'entity' => $this->getId(),
353
-				'property' => $property,
354
-				'snaktype' => 'value',
355
-				'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ),
356
-			];
357
-		}
358
-
359
-		// Save the property.
360
-		$wdWpOauth = new WdWpOauth();
361
-		$wdWpOauth->makeCall( $setTitleParams, true );
362
-
363
-		// Clear the cache.
364
-		$this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) );
365
-	}
366
-
367
-	public function getInstanceOf() {
368
-		$instancesOf = $this->getPropertyOfTypeItem( $this->getId(), self::PROP_INSTANCE_OF );
369
-		return array_shift( $instancesOf );
370
-	}
371
-
372
-	/**
373
-	 * Does this item exist?
374
-	 * @return bool
375
-	 */
376
-	public function exists() {
377
-		return $this->getId() !== false;
378
-	}
379
-
380
-	public function getWikipediaIntro() {
381
-		$cacheKey = 'wikipedia-intro-' . $this->id . $this->lang;
382
-		if ( $this->cache->hasItem( $cacheKey ) ) {
383
-			return $this->cache->getItem( $cacheKey )->get();
384
-		}
385
-		$entity = $this->getEntity( $this->id );
386
-		if ( !isset( $entity['sitelinks'] ) ) {
387
-			return [];
388
-		}
389
-		foreach ( $entity['sitelinks'] as $sitelink ) {
390
-			if ( $sitelink['site'] == $this->lang . 'wiki' ) {
391
-				$api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' );
392
-				$req = new SimpleRequest( 'query', [
393
-					'prop' => 'extracts',
394
-					'exintro' => true,
395
-					'titles' => $sitelink['title'],
396
-				] );
397
-				$response = $api->getRequest( $req );
398
-				$page = array_shift( $response['query']['pages'] );
399
-				$out = [
400
-					'title' => $page['title'],
401
-					'html' => $page['extract'],
402
-				];
403
-				$cacheItem = $this->cache->getItem( $cacheKey )
404
-					->expiresAfter( new DateInterval( 'P1D' ) )
405
-					->set( $out );
406
-				$this->cache->save( $cacheItem );
407
-
408
-				return $out;
409
-			}
410
-		}
411
-
412
-		return [];
413
-	}
414
-
415
-	/**
416
-	 * Get the raw entity data from the 'wbgetentities' API call.
417
-	 * @param string $id
418
-	 * @param bool $ignoreCache
419
-	 * @return bool
420
-	 */
421
-	public function getEntity( $id = null, $ignoreCache = false ) {
422
-		$idActual = $id ?: $this->id;
423
-		$cacheKey = $this->getEntityCacheKey( $idActual );
424
-		if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) {
425
-			return $this->cache->getItem( $cacheKey )->get();
426
-		}
427
-		$metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] );
428
-		$itemResult = $this->wdApi->getRequest( $metadataRequest );
429
-		if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) {
430
-			return false;
431
-		}
432
-		$metadata = $itemResult['entities'][$idActual];
433
-		$cacheItem = $this->cache->getItem( $cacheKey )
434
-			->expiresAfter( new DateInterval( 'PT10M' ) )
435
-			->set( $metadata );
436
-		$this->cache->save( $cacheItem );
437
-		return $metadata;
438
-	}
439
-
440
-	/**
441
-	 * @param $id
442
-	 *
443
-	 * @return string
444
-	 */
445
-	protected function getEntityCacheKey( $id ) {
446
-		return 'entities' . $id;
447
-	}
169
+            // }
170
+        }
171
+        return $times;
172
+    }
173
+
174
+    /**
175
+     * Get the Item that is referred to by the specified item's property.
176
+     *
177
+     * @param string $propertyId
178
+     *
179
+     * @return \Samwilson\SimpleWikidata\Properties\Item[]
180
+     */
181
+    protected function getPropertyOfTypeItem( $propertyId ) {
182
+        $entity = $this->getEntity( $this->id );
183
+        if ( !isset( $entity['claims'][$propertyId] ) ) {
184
+            return [];
185
+        }
186
+        $items = [];
187
+        foreach ( $entity['claims'][$propertyId] as $claim ) {
188
+            $items[] = new Properties\Item( $claim, $this->lang, $this->cache );
189
+        }
190
+
191
+        return $items;
192
+    }
193
+
194
+    public function setPropertyOfTypeItem( $property, $itemId ) {
195
+        $itemIdNumeric = substr( $itemId, 1 );
196
+
197
+        // First see if this property already exists, and that it is different from what's being set.
198
+        $entity = $this->getEntity( $this->id );
199
+        if ( !empty( $entity['claims'][$property] ) ) {
200
+            // Get the first claim, and update it if necessary.
201
+            $claim = array_shift( $entity['claims'][$property] );
202
+            if ( $claim['mainsnak']['datavalue']['value']['id'] == $itemId ) {
203
+                // Already is the required value, no need to change.
204
+                return;
205
+            }
206
+            $claim['mainsnak']['datavalue']['value']['id'] = $itemId;
207
+            $claim['mainsnak']['datavalue']['value']['numeric-id'] = $itemIdNumeric;
208
+            $apiParams = [
209
+                'action' => 'wbsetclaim',
210
+                'claim' => json_encode( $claim ),
211
+            ];
212
+        }
213
+
214
+        // If no claim was found (and modified) above, create a new claim.
215
+        if ( !isset( $apiParams ) ) {
216
+            $apiParams = [
217
+                'action' => 'wbcreateclaim',
218
+                'entity' => $this->getId(),
219
+                'property' => $property,
220
+                'snaktype' => 'value',
221
+                'value' => json_encode( [ 'entity-type' => 'item', 'numeric-id' => $itemIdNumeric ] ),
222
+            ];
223
+        }
224
+
225
+        // Save the property.
226
+        $wdWpOauth = new WdOauth();
227
+        $wdWpOauth->makeCall( $apiParams, true );
228
+
229
+        // Clear the cache.
230
+        $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) );
231
+    }
232
+
233
+    public function getPropertyOfTypeUrl( $entityId, $propertyId ) {
234
+        $entity = $this->getEntity( $entityId );
235
+        if ( !isset( $entity['claims'][$propertyId] ) ) {
236
+            return false;
237
+        }
238
+        $urls = [];
239
+        foreach ( $entity['claims'][$propertyId] as $claim ) {
240
+            $urls[] = $claim['mainsnak']['datavalue']['value'];
241
+        }
242
+
243
+        return $urls;
244
+    }
245
+
246
+    public function getPropertyOfTypeExternalIdentifier( $entityId, $propertyId ) {
247
+        $entity = $this->getEntity( $entityId );
248
+        if ( !isset( $entity['claims'][$propertyId] ) ) {
249
+            return false;
250
+        }
251
+        $idents = [];
252
+        foreach ( $entity['claims'][$propertyId] as $claim ) {
253
+            $qualifiers = [];
254
+            if ( !isset( $claim['qualifiers'] ) ) {
255
+                continue;
256
+            }
257
+            foreach ( $claim['qualifiers'] as $qualsInfo ) {
258
+                foreach ( $qualsInfo as $qualInfo ) {
259
+                    $qualProp = self::factory( $qualInfo['property'], $this->lang, $this->cache );
260
+                    $propLabel = $qualProp->getLabel();
261
+                    if ( !isset( $qualifiers[$propLabel] ) ) {
262
+                        $qualifiers[$propLabel] = [];
263
+                    }
264
+                    $qualifiers[$propLabel][] = $qualInfo['datavalue']['value'];
265
+                }
266
+            }
267
+            $idents[] = [
268
+                'qualifiers' => $qualifiers,
269
+                'value' => $claim['mainsnak']['datavalue']['value'],
270
+            ];
271
+        }
272
+
273
+        return $idents;
274
+    }
275
+
276
+    /**
277
+     * Get a single-valued text property.
278
+     * @param string $property One of the PROP_* constants.
279
+     * @return string|bool The value, or false if it can't be found.
280
+     */
281
+    public function getPropertyOfTypeText( $property ) {
282
+        $entity = $this->getEntity( $this->id );
283
+        if ( isset( $entity['claims'][$property] ) ) {
284
+            // Use the first title.
285
+            foreach ( $entity['claims'][$property] as $t ) {
286
+                if ( !isset( $t['mainsnak']['datavalue']['value']['language'] ) ) {
287
+                    var_dump( $t['mainsnak']['datavalue']['value'] );
288
+                    exit();
289
+                }
290
+                if ( $t['mainsnak']['datavalue']['value']['language'] == $this->lang
291
+                    && !empty( $t['mainsnak']['datavalue']['value']['text'] )
292
+                ) {
293
+                    return $t['mainsnak']['datavalue']['value']['text'];
294
+                }
295
+            }
296
+        }
297
+        return false;
298
+    }
299
+
300
+    /**
301
+     * Literal data field for a quantity that relates to some kind of well-defined unit. The actual unit goes in the data values that is entered.
302
+     *   - amount – implicit part of the string (mapping of unit prefix is unclear)
303
+     *   - unit – implicit part of the string that defaults to "1" (mapping to standardizing body is unclear)
304
+     *   - upperbound - quantity's upper bound
305
+     *   - lowerbound - quantity's lower bound
306
+     * @param $property
307
+     * @return mixed[]|bool If it's not false it's an array with 'amount', 'unit', etc.
308
+     */
309
+    public function getPropertyOfTypeQuantity( $property ) {
310
+        $quantities = [];
311
+        $entity = $this->getEntity( $this->id );
312
+        if ( !isset( $entity['claims'][$property] ) ) {
313
+            return false;
314
+        }
315
+        foreach ( $entity['claims'][$property] as $t ) {
316
+            $quantity = $t['mainsnak']['datavalue']['value'];
317
+            $unitId = substr( $quantity['unit'], strlen( $this->wikidataUrlBase ) + 1 );
318
+            $quantity['unit'] = self::factory( $unitId, $this->lang, $this->cache );
319
+            $quantities[] = $quantity;
320
+        }
321
+        return $quantities;
322
+    }
323
+
324
+    /**
325
+     * Set a single-valued text property.
326
+     * @param string $property One of the PROP_* constants.
327
+     * @param string $value The value.
328
+     */
329
+    public function setPropertyOfTypeText( $property, $value ) {
330
+        // First see if this property already exists, and that it is different from what's being set.
331
+        $entity = $this->getEntity( $this->id );
332
+        if ( !empty( $entity['claims'][$property] ) ) {
333
+            // Find this language's claim (if there is one).
334
+            foreach ( $entity['claims'][$property] as $claim ) {
335
+                if ( $claim['mainsnak']['datavalue']['value']['language'] == $this->lang ) {
336
+                    // Modify this claim's text value.
337
+                    $titleClaim = $claim;
338
+                    $titleClaim['mainsnak']['datavalue']['value']['text'] = $value;
339
+                    $setTitleParams = [
340
+                        'action' => 'wbsetclaim',
341
+                        'claim' => \GuzzleHttp\json_encode( $titleClaim ),
342
+                    ];
343
+                    continue;
344
+                }
345
+            }
346
+        }
347
+
348
+        // If no claim was found (and modified) above, create a new claim.
349
+        if ( !isset( $setTitleParams ) ) {
350
+            $setTitleParams = [
351
+                'action' => 'wbcreateclaim',
352
+                'entity' => $this->getId(),
353
+                'property' => $property,
354
+                'snaktype' => 'value',
355
+                'value' => \GuzzleHttp\json_encode( [ 'text' => $value, 'language' => $this->lang ] ),
356
+            ];
357
+        }
358
+
359
+        // Save the property.
360
+        $wdWpOauth = new WdWpOauth();
361
+        $wdWpOauth->makeCall( $setTitleParams, true );
362
+
363
+        // Clear the cache.
364
+        $this->cache->deleteItem( $this->getEntityCacheKey( $this->id ) );
365
+    }
366
+
367
+    public function getInstanceOf() {
368
+        $instancesOf = $this->getPropertyOfTypeItem( $this->getId(), self::PROP_INSTANCE_OF );
369
+        return array_shift( $instancesOf );
370
+    }
371
+
372
+    /**
373
+     * Does this item exist?
374
+     * @return bool
375
+     */
376
+    public function exists() {
377
+        return $this->getId() !== false;
378
+    }
379
+
380
+    public function getWikipediaIntro() {
381
+        $cacheKey = 'wikipedia-intro-' . $this->id . $this->lang;
382
+        if ( $this->cache->hasItem( $cacheKey ) ) {
383
+            return $this->cache->getItem( $cacheKey )->get();
384
+        }
385
+        $entity = $this->getEntity( $this->id );
386
+        if ( !isset( $entity['sitelinks'] ) ) {
387
+            return [];
388
+        }
389
+        foreach ( $entity['sitelinks'] as $sitelink ) {
390
+            if ( $sitelink['site'] == $this->lang . 'wiki' ) {
391
+                $api = new MediawikiApi( 'https://' . $this->lang . '.wikipedia.org/w/api.php' );
392
+                $req = new SimpleRequest( 'query', [
393
+                    'prop' => 'extracts',
394
+                    'exintro' => true,
395
+                    'titles' => $sitelink['title'],
396
+                ] );
397
+                $response = $api->getRequest( $req );
398
+                $page = array_shift( $response['query']['pages'] );
399
+                $out = [
400
+                    'title' => $page['title'],
401
+                    'html' => $page['extract'],
402
+                ];
403
+                $cacheItem = $this->cache->getItem( $cacheKey )
404
+                    ->expiresAfter( new DateInterval( 'P1D' ) )
405
+                    ->set( $out );
406
+                $this->cache->save( $cacheItem );
407
+
408
+                return $out;
409
+            }
410
+        }
411
+
412
+        return [];
413
+    }
414
+
415
+    /**
416
+     * Get the raw entity data from the 'wbgetentities' API call.
417
+     * @param string $id
418
+     * @param bool $ignoreCache
419
+     * @return bool
420
+     */
421
+    public function getEntity( $id = null, $ignoreCache = false ) {
422
+        $idActual = $id ?: $this->id;
423
+        $cacheKey = $this->getEntityCacheKey( $idActual );
424
+        if ( !$ignoreCache && $this->cache->hasItem( $cacheKey ) ) {
425
+            return $this->cache->getItem( $cacheKey )->get();
426
+        }
427
+        $metadataRequest = new SimpleRequest( 'wbgetentities', [ 'ids' => $idActual ] );
428
+        $itemResult = $this->wdApi->getRequest( $metadataRequest );
429
+        if ( !isset( $itemResult['success'] ) || !isset( $itemResult['entities'][$id] ) ) {
430
+            return false;
431
+        }
432
+        $metadata = $itemResult['entities'][$idActual];
433
+        $cacheItem = $this->cache->getItem( $cacheKey )
434
+            ->expiresAfter( new DateInterval( 'PT10M' ) )
435
+            ->set( $metadata );
436
+        $this->cache->save( $cacheItem );
437
+        return $metadata;
438
+    }
439
+
440
+    /**
441
+     * @param $id
442
+     *
443
+     * @return string
444
+     */
445
+    protected function getEntityCacheKey( $id ) {
446
+        return 'entities' . $id;
447
+    }
448 448
 }
Please login to merge, or discard this patch.
src/Items/Edition.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -6,66 +6,66 @@
 block discarded – undo
6 6
 
7 7
 class Edition extends Item {
8 8
 
9
-	const PROP_EDITION_OR_TRANSLATION_OF = 'P629';
10
-	const PROP_WIKISOURCE_INDEX_PAGE = 'P1957';
11
-	const PROP_SCANNED_FILE_ON_COMMONS = 'P996';
12
-	const PROP_INTERNET_ARCHIVE_ID = 'P724';
13
-	const PROP_PUBLICATION_DATE = 'P577';
14
-	const PROP_PUBLISHER = 'P123';
9
+    const PROP_EDITION_OR_TRANSLATION_OF = 'P629';
10
+    const PROP_WIKISOURCE_INDEX_PAGE = 'P1957';
11
+    const PROP_SCANNED_FILE_ON_COMMONS = 'P996';
12
+    const PROP_INTERNET_ARCHIVE_ID = 'P724';
13
+    const PROP_PUBLICATION_DATE = 'P577';
14
+    const PROP_PUBLISHER = 'P123';
15 15
 
16
-	/**
17
-	 * @return string
18
-	 */
19
-	public function getPublicationYear() {
20
-		$publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE );
21
-		return $publicationYears[0]->getDateTime()->format( 'Y' );
22
-	}
16
+    /**
17
+     * @return string
18
+     */
19
+    public function getPublicationYear() {
20
+        $publicationYears = $this->getPropertyOfTypeTime( static::PROP_PUBLICATION_DATE );
21
+        return $publicationYears[0]->getDateTime()->format( 'Y' );
22
+    }
23 23
 
24
-	/**
25
-	 * @return \Samwilson\SimpleWikidata\Properties\Item[]
26
-	 */
27
-	public function getPublishers() {
28
-		return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER );
29
-	}
24
+    /**
25
+     * @return \Samwilson\SimpleWikidata\Properties\Item[]
26
+     */
27
+    public function getPublishers() {
28
+        return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER );
29
+    }
30 30
 
31
-	/**
32
-	 * @return array|bool
33
-	 */
34
-	public function getWikisourceIndexPages() {
35
-		return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE );
36
-	}
31
+    /**
32
+     * @return array|bool
33
+     */
34
+    public function getWikisourceIndexPages() {
35
+        return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE );
36
+    }
37 37
 
38
-	/**
39
-	 * @return array
40
-	 */
41
-	public function internetArchiveIds() {
42
-		return $this->getPropertyOfTypeExternalIdentifier(
43
-			$this->getId(),
44
-			self::PROP_INTERNET_ARCHIVE_ID
45
-		);
46
-	}
38
+    /**
39
+     * @return array
40
+     */
41
+    public function internetArchiveIds() {
42
+        return $this->getPropertyOfTypeExternalIdentifier(
43
+            $this->getId(),
44
+            self::PROP_INTERNET_ARCHIVE_ID
45
+        );
46
+    }
47 47
 
48
-	/**
49
-	 * Get information about the Wikisource sitelink.
50
-	 * An edition should only ever be present on one Wikisource.
51
-	 * @return string[]
52
-	 */
53
-	public function getWikisourceLink() {
54
-		$entity = $this->getEntity( $this->id );
55
-		if ( !isset( $entity['sitelinks'] ) ) {
56
-			return [];
57
-		}
58
-		foreach ( $entity['sitelinks'] as $sitelink ) {
59
-			if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) {
60
-				$lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) );
61
-				return [
62
-					'title' => $sitelink['title'],
63
-					'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'],
64
-					'lang' => $lang,
65
-				];
66
-			}
67
-		}
68
-		return [];
69
-	}
48
+    /**
49
+     * Get information about the Wikisource sitelink.
50
+     * An edition should only ever be present on one Wikisource.
51
+     * @return string[]
52
+     */
53
+    public function getWikisourceLink() {
54
+        $entity = $this->getEntity( $this->id );
55
+        if ( !isset( $entity['sitelinks'] ) ) {
56
+            return [];
57
+        }
58
+        foreach ( $entity['sitelinks'] as $sitelink ) {
59
+            if ( strpos( $sitelink['site'], 'wikisource' ) !== false ) {
60
+                $lang = substr( $sitelink['site'], 0, strpos( $sitelink['site'], 'wikisource' ) );
61
+                return [
62
+                    'title' => $sitelink['title'],
63
+                    'url' => "https://$lang.wikisource.org/wiki/".$sitelink['title'],
64
+                    'lang' => $lang,
65
+                ];
66
+            }
67
+        }
68
+        return [];
69
+    }
70 70
 
71 71
 }
Please login to merge, or discard this patch.