Completed
Push — master ( 3f8ea9...57a28f )
by mw
04:55
created

CitationResourceMatchFinder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 14

Test Coverage

Coverage 97.14%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 12
c 7
b 0
f 3
lcom 2
cbo 14
dl 0
loc 168
ccs 68
cts 70
cp 0.9714
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findCitationResourceLinks() 0 22 2
B findCitationTextFor() 0 30 5
B findMatchForCitationReference() 0 27 1
B findMatchForResourceIdentifierTypeToValue() 0 28 3
1
<?php
2
3
namespace SCI;
4
5
use SMW\Query\Language\SomeProperty;
6
use SMW\Query\Language\ValueDescription;
7
use SMW\Query\PrintRequest;
8
use SMW\Store;
9
use SMW\DIProperty;
10
use SMWPropertyValue as PropertyValue;
11
use SMWQuery as Query;
12
use SMWDIBlob as DIBlob;
13
use SMW\DataValueFactory;
14
use SCI\DataValues\ResourceIdentifierFactory;
15
16
/**
17
 * @license GNU GPL v2+
18
 * @since 2.2
19
 *
20
 * @author mwjames
21
 */
22
class CitationResourceMatchFinder {
23
24
	/**
25
	 * @var Store
26
	 */
27
	private $store;
28
29
	/**
30
	 * @var DataValueFactory
31
	 */
32
	private $dataValueFactory;
33
34
	/**
35
	 * @since 1.0
36
	 *
37
	 * @param Store $store
38
	 */
39 16
	public function __construct( Store $store ) {
40 16
		$this->store = $store;
41 16
		$this->dataValueFactory = DataValueFactory::getInstance();
42 16
	}
43
44
	/**
45
	 * @since 1.0
46
	 *
47
	 * @param array $subjects
48
	 * @param string $linkClass
49
	 *
50
	 * @return array
51
	 */
52 1
	public function findCitationResourceLinks( array $subjects, $linkClass = '' ) {
53
54 1
		$citationResourceLinks = array();
55
56 1
		foreach ( $subjects as $subject ) {
57
58 1
			$dataValue = $this->dataValueFactory->newDataItemValue(
59 1
				$subject,
60
				null
61 1
			);
62
63 1
			$browselink = \SMWInfolink::newBrowsingLink(
64 1
				'&#8593;', // ↑; $reference,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65 1
				$dataValue->getWikiValue(),
66
				$linkClass
67 1
			);
68
69 1
			$citationResourceLinks[] = $browselink->getHTML();
70 1
		}
71
72 1
		return $citationResourceLinks;
73
	}
74
75
	/**
76
	 * Find match for [[OCLC::SomeOclcKey]]
77
	 *
78
	 * @since 1.0
79
	 *
80
	 * @param string $type
81
	 * @param string|null $id
82
	 *
83
	 * @return array
84
	 */
85 6
	public function findMatchForResourceIdentifierTypeToValue( $type, $id = null ) {
86
87 6
		if ( $id === null || $id === '' ) {
88
			return array();
89
		}
90
91 6
		$resourceIdentifierFactory = new ResourceIdentifierFactory();
92
93 6
		$resourceIdentifierStringValue = $resourceIdentifierFactory->newResourceIdentifierStringValueForType( $type );
94 6
		$resourceIdentifierStringValue->setUserValue( $id );
95 6
		$id = $resourceIdentifierStringValue->getWikiValue();
96
97 6
		$description = new SomeProperty(
98 6
			$resourceIdentifierStringValue->getProperty(),
99 6
			new ValueDescription( new DIBlob( $id ) )
100 6
		);
101
102 6
		$query = new Query(
103 6
			$description,
104 6
			false,
105
			false
106 6
		);
107
108 6
		$query->querymode = Query::MODE_INSTANCES;
109 6
		$query->setLimit( 10 );
110
111 6
		return $this->store->getQueryResult( $query )->getResults();
112
	}
113
114
	/**
115
	 * @since 1.0
116
	 *
117
	 * @param string $citationReference
118
	 *
119
	 * @return array
120
	 */
121 8
	public function findCitationTextFor( $citationReference ) {
122
123 8
		$text = '';
124 8
		$subjects = array();
125
126 8
		$queryResult = $this->findMatchForCitationReference(
127
			$citationReference
128 8
		);
129
130 8
		if ( !$queryResult instanceof \SMWQueryResult ) {
131
			return array( $subjects, $text );
132
		}
133
134 8
		while ( $resultArray = $queryResult->getNext() ) {
135 8
			foreach ( $resultArray as $result ) {
136
137
				// Collect all subjects for the same reference because it can happen
138
				// that the same reference key is used for different citation
139
				// resources therefore only return one (the last) valid citation
140
				// text but return all subjects to make it easier to find them later
141 8
				$subjects[] = $result->getResultSubject();
142
143 8
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
144 8
					$text = $dataValue->getShortWikiText();
145 8
				}
146 8
			}
147 8
		}
148
149 8
		return array( $subjects, $text );
150
	}
151
152
	/**
153
	 * Find match for [[Citation key::SomeKey]]|?Citation text
154
	 *
155
	 * @since 1.0
156
	 *
157
	 * @param string $citationReference
158
	 *
159
	 * @return QueryResult
160
	 */
161 8
	public function findMatchForCitationReference( $citationReference ) {
162
163 8
		$description = new SomeProperty(
164 8
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
165 8
			new ValueDescription( new DIBlob( $citationReference ) )
166 8
		);
167
168 8
		$propertyValue = new PropertyValue( '__pro' );
169 8
		$propertyValue->setDataItem(
170 8
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
171 8
		);
172
173 8
		$description->addPrintRequest(
174 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
175 8
		);
176
177 8
		$query = new Query(
178 8
			$description,
179 8
			false,
180
			false
181 8
		);
182
183 8
		$query->querymode = Query::MODE_INSTANCES;
184 8
		$query->setLimit( 10 );
185
186 8
		return $this->store->getQueryResult( $query );
187
	}
188
189
}
190