Completed
Pull Request — master (#77)
by mw
02:46 queued 01:34
created

CitationResourceMatchFinder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 12
dl 0
loc 180
ccs 66
cts 68
cp 0.9706
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findCitationResourceLinks() 0 22 2
A findMatchForResourceIdentifierTypeToValue() 0 36 5
A findCitationTextFor() 0 30 5
A findMatchForCitationReference() 0 31 2
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 = '', $caption = '' ) {
53
54 1
		$citationResourceLinks = [];
55
56 1
		foreach ( $subjects as $subject ) {
57
58 1
			$dataValue = $this->dataValueFactory->newDataItemValue(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newDataItemValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByItem

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59 1
				$subject,
60 1
				null
61
			);
62
63 1
			$browselink = \SMWInfolink::newBrowsingLink(
64 1
				$caption,
65 1
				$dataValue->getWikiValue(),
66 1
				$linkClass
67
			);
68
69 1
			$citationResourceLinks[] = $browselink->getHTML();
70
		}
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 [];
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
		);
101
102 6
		$query = new Query(
103 6
			$description,
104 6
			false,
105 6
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
106
		);
107
108 6
		$query->querymode = Query::MODE_INSTANCES;
109 6
		$query->setLimit( 10 );
110
111 6
		if ( defined( 'SMWQuery::NO_CACHE' ) ) {
112 6
			$query->setOption( Query::NO_CACHE, true );
113
		}
114
115 6
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
116 6
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
117
		}
118
119 6
		return $this->store->getQueryResult( $query )->getResults();
120
	}
121
122
	/**
123
	 * @since 1.0
124
	 *
125
	 * @param string $citationReference
126
	 *
127
	 * @return array
128
	 */
129 8
	public function findCitationTextFor( $citationReference ) {
130
131 8
		$text = '';
132 8
		$subjects = [];
133
134 8
		$queryResult = $this->findMatchForCitationReference(
135 8
			$citationReference
136
		);
137
138 8
		if ( !$queryResult instanceof \SMWQueryResult ) {
139
			return [ $subjects, $text ];
140
		}
141
142 8
		while ( $resultArray = $queryResult->getNext() ) {
143 8
			foreach ( $resultArray as $result ) {
144
145
				// Collect all subjects for the same reference because it can happen
146
				// that the same reference key is used for different citation
147
				// resources therefore only return one (the last) valid citation
148
				// text but return all subjects to make it easier to find them later
149 8
				$subjects[] = $result->getResultSubject();
150
151 8
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
152 8
					$text = $dataValue->getShortWikiText();
153
				}
154
			}
155
		}
156
157 8
		return [ $subjects, $text ];
158
	}
159
160
	/**
161
	 * Find match for [[Citation key::SomeKey]]|?Citation text
162
	 *
163
	 * @since 1.0
164
	 *
165
	 * @param string $citationReference
166
	 *
167
	 * @return QueryResult
168
	 */
169 8
	public function findMatchForCitationReference( $citationReference ) {
170
171 8
		$description = new SomeProperty(
172 8
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
173 8
			new ValueDescription( new DIBlob( $citationReference ) )
174
		);
175
176 8
		$propertyValue = $this->dataValueFactory->newDataValueByType( '__pro' );
177 8
		$propertyValue->setDataItem(
178 8
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
179
		);
180
181 8
		$description->addPrintRequest(
182 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
183
		);
184
185 8
		$query = new Query(
186 8
			$description,
187 8
			false,
188 8
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
189
		);
190
191 8
		$query->querymode = Query::MODE_INSTANCES;
192 8
		$query->setLimit( 10 );
193
194 8
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
195 8
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
196
		}
197
198 8
		return $this->store->getQueryResult( $query );
199
	}
200
201
}
202