Completed
Pull Request — master (#22)
by mw
03:56
created

findMatchForCitationReference()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 35
ccs 26
cts 26
cp 1
rs 8.8571
cc 1
eloc 21
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SCI;
4
5
use SMW\Query\Language\SomeProperty;
6
use SMW\Query\Language\ValueDescription;
7
use SMW\Query\Language\Conjunction;
8
use SMW\Query\Language\ThingDescription;
9
use SMW\Query\PrintRequest;
10
use SMW\Store;
11
use SMW\DIProperty;
12
use SMWPropertyValue as PropertyValue;
13
use SMWQuery as Query;
14
use SMWDIBlob as DIBlob;
15
use SMW\DataValueFactory;
16
use SCI\DataValues\ResourceIdentifierFactory;
17
18
/**
19
 * @license GNU GPL v2+
20
 * @since 2.2
21
 *
22
 * @author mwjames
23
 */
24
class CitationResourceMatchFinder {
25
26
	/**
27
	 * @var Store
28
	 */
29
	private $store;
30
31
	/**
32
	 * @var DataValueFactory
33
	 */
34
	private $dataValueFactory;
35
36
	/**
37
	 * @since 1.0
38
	 *
39
	 * @param Store $store
40
	 */
41 16
	public function __construct( Store $store ) {
42 16
		$this->store = $store;
43 16
		$this->dataValueFactory = DataValueFactory::getInstance();
44 16
	}
45
46
	/**
47
	 * @since 1.0
48
	 *
49
	 * @param array $subjects
50
	 * @param string $linkClass
51
	 *
52
	 * @return array
53
	 */
54 1
	public function findCitationResourceLinks( array $subjects, $linkClass = '' ) {
55
56 1
		$citationResourceLinks = array();
57
58 1
		foreach ( $subjects as $subject ) {
59
60 1
			$dataValue = $this->dataValueFactory->newDataItemValue(
61 1
				$subject,
62
				null
63 1
			);
64
65 1
			$browselink = \SMWInfolink::newBrowsingLink(
66 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...
67 1
				$dataValue->getWikiValue(),
68
				$linkClass
69 1
			);
70
71 1
			$citationResourceLinks[] = $browselink->getHTML();
72 1
		}
73
74 1
		return $citationResourceLinks;
75
	}
76
77
	/**
78
	 * Find match for [[OCLC::SomeOclcKey]]
79
	 *
80
	 * @since 1.0
81
	 *
82
	 * @param string $type
83
	 * @param string|null $id
84
	 *
85
	 * @return array
86
	 */
87 7
	public function findMatchForResourceIdentifierTypeToValue( $type, $id = null ) {
88
89 6
		if ( $id === null || $id === '' ) {
90
			return array();
91
		}
92
93 6
		$resourceIdentifierFactory = new ResourceIdentifierFactory();
94
95 6
		$resourceIdentifierStringValue = $resourceIdentifierFactory->newResourceIdentifierStringValueForType( $type );
96 6
		$resourceIdentifierStringValue->setUserValue( $id );
97 6
		$id = $resourceIdentifierStringValue->getWikiValue();
98
99 6
		$description = new SomeProperty(
100 6
			$resourceIdentifierStringValue->getProperty(),
101 6
			new ValueDescription( new DIBlob( $id ) )
102 6
		);
103
104 6
		$query = new Query(
105 6
			$description,
106 6
			false,
107
			false
108 6
		);
109
110 6
		$query->querymode = Query::MODE_INSTANCES;
111 7
		$query->setLimit( 10 );
112
113 6
		return $this->store->getQueryResult( $query )->getResults();
114
	}
115
116
	/**
117
	 * @since 1.0
118
	 *
119
	 * @param string $citationReference
120
	 *
121
	 * @return array
122
	 */
123 8
	public function findCitationTextFor( $citationReference ) {
124
125 8
		$text = '';
126 8
		$subjects = array();
127
128 8
		$queryResult = $this->findMatchForCitationReference(
129
			$citationReference
130 8
		);
131
132 8
		if ( !$queryResult instanceof \SMWQueryResult ) {
133
			return array( $subjects, $text );
134
		}
135
136 8
		while ( $resultArray = $queryResult->getNext() ) {
137 8
			foreach ( $resultArray as $result ) {
138
139
				// Collect all subjects for the same reference because it can happen
140
				// that the same reference key is used for different citation
141
				// resources therefore only return one (the last) valid citation
142
				// text but return all subjects to make it easier to find them later
143 8
				$subjects[] = $result->getResultSubject();
144
145 8
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
146 8
					$text = $dataValue->getShortWikiText();
147 8
				}
148 8
			}
149 8
		}
150
151 8
		return array( $subjects, $text );
152
	}
153
154
	/**
155
	 * Find match for [[Citation key::SomeKey]]|?Citation text
156
	 *
157
	 * @since 1.0
158
	 *
159
	 * @param string $citationReference
160
	 *
161
	 * @return QueryResult
162
	 */
163 8
	public function findMatchForCitationReference( $citationReference ) {
164
165 8
		$description = new SomeProperty(
166 8
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
167 8
			new ValueDescription( new DIBlob( $citationReference ) )
168 8
		);
169
170 8
		$description = new Conjunction( array( $description ) );
171 8
		$description->addDescription(
172 8
			new SomeProperty(
173 8
				new DIProperty( PropertyRegistry::SCI_CITE_TEXT ),
174 8
				new ThingDescription()
175 8
			)
176 8
		);
177
178 8
		$propertyValue = new PropertyValue( '__pro' );
179 8
		$propertyValue->setDataItem(
180 8
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
181 8
		);
182
183 8
		$description->addPrintRequest(
184 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
185 8
		);
186
187 8
		$query = new Query(
188 8
			$description,
189 8
			false,
190
			false
191 8
		);
192
193 8
		$query->querymode = Query::MODE_INSTANCES;
194 8
		$query->setLimit( 10 );
195
196 8
		return $this->store->getQueryResult( $query );
197
	}
198
199
}
200