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( |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.