Edition::getWikisourceLink()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 16
ccs 0
cts 16
cp 0
crap 20
rs 9.9
1
<?php
2
3
namespace Samwilson\SimpleWikidata\Items;
4
5
use Samwilson\SimpleWikidata\Item;
6
7
class Edition extends Item {
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';
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
	}
23
24
	/**
25
	 * @return \Samwilson\SimpleWikidata\Properties\Item[]
26
	 */
27
	public function getPublishers() {
28
		return $this->getPropertyOfTypeItem( static::PROP_PUBLISHER );
29
	}
30
31
	/**
32
	 * @return array|bool
33
	 */
34
	public function getWikisourceIndexPages() {
35
		return $this->getPropertyOfTypeUrl( $this->getId(), static::PROP_WIKISOURCE_INDEX_PAGE );
36
	}
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
	}
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
	}
70
71
}
72