|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SCI\Specials\CitableMetadata; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
|
6
|
|
|
use SCI\CitationResourceMatchFinder; |
|
7
|
|
|
use SMW\MediaWiki\Renderer\HtmlFormRenderer; |
|
8
|
|
|
use SMW\MediaWiki\Renderer\HtmlColumnListRenderer; |
|
9
|
|
|
use SCI\FilteredMetadata\HttpResponseParserFactory; |
|
10
|
|
|
use SCI\FilteredMetadata\BibliographicFilteredRecord; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @license GNU GPL v2+ |
|
14
|
|
|
* @since 1.0 |
|
15
|
|
|
* |
|
16
|
|
|
* @author mwjames |
|
17
|
|
|
*/ |
|
18
|
|
|
class PageBuilder { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var HtmlFormRenderer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $htmlFormRenderer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var HtmlColumnListRenderer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $htmlColumnListRenderer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var CitationResourceMatchFinder |
|
32
|
|
|
*/ |
|
33
|
|
|
private $citationResourceMatchFinder; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var HttpResponseParserFactory |
|
37
|
|
|
*/ |
|
38
|
|
|
private $httpResponseParserFactory; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
private $isReadOnly = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @since 1.0 |
|
47
|
|
|
* |
|
48
|
|
|
* @param HtmlFormRenderer $htmlFormRenderer |
|
49
|
|
|
* @param HtmlColumnListRenderer $htmlColumnListRenderer |
|
50
|
|
|
* @param CitationResourceMatchFinder $citationResourceMatchFinder |
|
51
|
|
|
* @param HttpResponseParserFactory $httpResponseParserFactory |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function __construct( HtmlFormRenderer $htmlFormRenderer, HtmlColumnListRenderer $htmlColumnListRenderer, CitationResourceMatchFinder $citationResourceMatchFinder, HttpResponseParserFactory $httpResponseParserFactory ) { |
|
54
|
3 |
|
$this->htmlFormRenderer = $htmlFormRenderer; |
|
55
|
3 |
|
$this->htmlColumnListRenderer = $htmlColumnListRenderer; |
|
56
|
3 |
|
$this->citationResourceMatchFinder = $citationResourceMatchFinder; |
|
57
|
3 |
|
$this->httpResponseParserFactory = $httpResponseParserFactory; |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @since 1.4 |
|
62
|
|
|
* |
|
63
|
|
|
* @param boolean $isReadOnly |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isReadOnly( $isReadOnly ) { |
|
66
|
|
|
$this->isReadOnly = (bool)$isReadOnly; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @since 1.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $type |
|
73
|
|
|
* @param string $id |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getRawResponseFor( $type, $id ) { |
|
78
|
|
|
|
|
79
|
1 |
|
$responseParser = $this->httpResponseParserFactory->newResponseParserForType( |
|
80
|
|
|
$type |
|
81
|
1 |
|
); |
|
82
|
|
|
|
|
83
|
1 |
|
$htmlResponseParserRenderer = new HtmlResponseParserRenderer( |
|
84
|
|
|
$responseParser |
|
85
|
1 |
|
); |
|
86
|
|
|
|
|
87
|
1 |
|
return $htmlResponseParserRenderer->getRawResponse( $id ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @since 1.0 |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
* @param string $id |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getHtmlFor( $type, $id ) { |
|
99
|
|
|
|
|
100
|
1 |
|
$text = ''; |
|
101
|
1 |
|
$success = true; |
|
102
|
1 |
|
$log = ''; |
|
103
|
|
|
|
|
104
|
1 |
|
if ( $type !== '' && $id !== '' ) { |
|
105
|
|
|
|
|
106
|
1 |
|
$responseParser = $this->httpResponseParserFactory->newResponseParserForType( |
|
107
|
|
|
$type |
|
108
|
1 |
|
); |
|
109
|
|
|
|
|
110
|
1 |
|
$htmlResponseParserRenderer = new HtmlResponseParserRenderer( |
|
111
|
|
|
$responseParser |
|
112
|
1 |
|
); |
|
113
|
|
|
|
|
114
|
1 |
|
$htmlResponseParserRenderer->isReadOnly( |
|
115
|
1 |
|
$this->isReadOnly |
|
116
|
1 |
|
); |
|
117
|
|
|
|
|
118
|
1 |
|
$text = $htmlResponseParserRenderer->renderTextFor( $id ); |
|
119
|
|
|
|
|
120
|
1 |
|
$matches = $this->tryToFindCitationResourceMatches( |
|
121
|
1 |
|
$responseParser->getFilteredRecord() |
|
|
|
|
|
|
122
|
1 |
|
); |
|
123
|
|
|
|
|
124
|
1 |
|
$success = $responseParser->getMessages() === array(); |
|
125
|
|
|
|
|
126
|
1 |
|
$log = $this->prepareLog( |
|
127
|
1 |
|
$responseParser->getMessages(), |
|
128
|
1 |
|
$matches, |
|
129
|
1 |
|
$responseParser->usesCache() |
|
130
|
1 |
|
); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
return $this->doRenderHtml( $type, $id, $success, $text, $log ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
private function doRenderHtml( $type, $id, $success, $text, $log ) { |
|
137
|
|
|
|
|
138
|
1 |
|
$htmlFormRenderer = $this->htmlFormRenderer; |
|
139
|
1 |
|
$messageBuilder = $this->htmlFormRenderer->getMessageBuilder(); |
|
140
|
|
|
|
|
141
|
|
|
$types = array( |
|
142
|
1 |
|
'pubmed' => 'PMID', |
|
143
|
1 |
|
'pmc' => 'PMCID', |
|
144
|
1 |
|
'doi' => 'DOI', |
|
145
|
1 |
|
'oclc' => 'OCLC', |
|
146
|
1 |
|
'viaf' => 'VIAF', |
|
147
|
1 |
|
'ol' => 'OLID', |
|
148
|
1 |
|
); |
|
149
|
|
|
|
|
150
|
1 |
|
$html = $htmlFormRenderer->setName( 'sci-metadata-search-form' ) |
|
151
|
1 |
|
->withFieldset() |
|
152
|
1 |
|
->setMethod( 'get' ) |
|
153
|
1 |
|
->addParagraph( $messageBuilder->getMessage( 'sci-metadata-search-intro' )->parse() ) |
|
154
|
1 |
|
->addParagraph( $this->getTypeIdIntroText( $messageBuilder ) ) |
|
155
|
1 |
|
->addHorizontalRule() |
|
156
|
1 |
|
->addOptionSelectList( |
|
157
|
1 |
|
$messageBuilder->getMessage( 'sci-metadata-search-select-label' )->text(), |
|
158
|
1 |
|
'type', |
|
159
|
1 |
|
$type, |
|
160
|
1 |
|
$types ) |
|
161
|
1 |
|
->addInputField( |
|
162
|
1 |
|
'', |
|
163
|
1 |
|
'id', |
|
164
|
1 |
|
$id, |
|
165
|
1 |
|
'id', |
|
166
|
1 |
|
40 ) |
|
167
|
1 |
|
->addNonBreakingSpace() |
|
168
|
1 |
|
->addSubmitButton( $messageBuilder->getMessage( 'sci-metadata-search-form-submit' )->text() ) |
|
169
|
1 |
|
->addNonBreakingSpace() |
|
170
|
1 |
|
->addCheckbox( 'Raw', 'format', 'raw' ) |
|
171
|
1 |
|
->getForm(); |
|
172
|
|
|
|
|
173
|
1 |
|
if ( $text !== '' && $success ) { |
|
174
|
|
|
$htmlFormRenderer |
|
175
|
1 |
|
->addHeader( 'h2', $messageBuilder->getMessage( 'sci-metadata-search-header-result' )->text() ) |
|
176
|
1 |
|
->addParagraph( $text ); |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
if ( $log !== '' ) { |
|
180
|
|
|
$htmlFormRenderer |
|
181
|
|
|
->setName( 'metadata-match' ) |
|
182
|
|
|
->addHeader( 'h2', $messageBuilder->getMessage( 'sci-metadata-search-header-log' )->text() ) |
|
183
|
|
|
->addParagraph( $log ); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
1 |
|
return $html . $htmlFormRenderer->getForm(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
private function prepareLog( $messages, $matches, $usesCache ) { |
|
190
|
|
|
|
|
191
|
1 |
|
$messageBuilder = $this->htmlFormRenderer->getMessageBuilder(); |
|
192
|
|
|
|
|
193
|
1 |
|
$log = array(); |
|
194
|
|
|
|
|
195
|
1 |
|
foreach ( $messages as $m ) { |
|
196
|
|
|
|
|
197
|
|
|
if ( call_user_func_array( 'wfMessage', $m )->exists() ) { |
|
198
|
|
|
$m = call_user_func_array( 'wfMessage', $m )->parse(); |
|
199
|
|
|
} else { |
|
200
|
|
|
$m = current( $m ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$log[] = $m; |
|
204
|
1 |
|
} |
|
205
|
|
|
|
|
206
|
1 |
|
if ( $matches !== '' ) { |
|
207
|
|
|
$log[] = $messageBuilder->getMessage( 'sci-metadata-search-has-match', $matches )->text(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
1 |
|
if ( $usesCache ) { |
|
211
|
|
|
$log[] = $messageBuilder->getMessage( 'sci-metadata-search-cached' )->text(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
if ( $this->isReadOnly ) { |
|
215
|
|
|
$log[] = $messageBuilder->getMessage( 'sci-metadata-search-read-only' )->text(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
if ( $log === array() ) { |
|
219
|
1 |
|
return ''; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$this->htmlColumnListRenderer->addContentsByNoIndex( $log ); |
|
223
|
|
|
$this->htmlColumnListRenderer->setNumberOfColumns( 1 ); |
|
224
|
|
|
|
|
225
|
|
|
return $this->htmlColumnListRenderer->getHtml(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
1 |
|
private function getTypeIdIntroText( $messageBuilder ) { |
|
229
|
|
|
|
|
230
|
1 |
|
$explain = array(); |
|
231
|
|
|
|
|
232
|
1 |
|
foreach ( array( 'doi', 'oclc', 'pubmed', 'ol', 'viaf' ) as $value ) { |
|
233
|
1 |
|
$explain[] = $messageBuilder->getMessage( 'sci-metadata-search-intro-'. $value )->parse(); |
|
234
|
1 |
|
} |
|
235
|
|
|
|
|
236
|
1 |
|
$this->htmlColumnListRenderer->setColumnListClass( 'scite-metadata-search-types' ); |
|
237
|
1 |
|
$this->htmlColumnListRenderer->addContentsByNoIndex( $explain ); |
|
238
|
1 |
|
$this->htmlColumnListRenderer->setNumberOfColumns( 2 ); |
|
239
|
|
|
|
|
240
|
1 |
|
return $this->htmlColumnListRenderer->getHtml(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
1 |
|
private function tryToFindCitationResourceMatches( BibliographicFilteredRecord $bibliographicFilteredRecord ) { |
|
244
|
|
|
|
|
245
|
1 |
|
$html = array(); |
|
246
|
|
|
|
|
247
|
1 |
|
foreach ( array( 'doi', 'oclc', 'viaf', 'olid', 'pubmed', 'pmc' ) as $type ) { |
|
248
|
1 |
|
$subjects = $this->citationResourceMatchFinder->findMatchForResourceIdentifierTypeToValue( |
|
249
|
1 |
|
$type, |
|
250
|
1 |
|
$bibliographicFilteredRecord->getSearchMatchSetValueFor( $type ) |
|
251
|
1 |
|
); |
|
252
|
|
|
|
|
253
|
1 |
|
if ( $subjects !== array() ) { |
|
254
|
|
|
$html = array_merge( |
|
255
|
|
|
$html, |
|
256
|
|
|
$this->citationResourceMatchFinder->findCitationResourceLinks( $subjects, '', strtoupper( $type ) ) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
1 |
|
} |
|
260
|
|
|
|
|
261
|
1 |
|
return $html !== array() ? '<strong>' . implode( ', ', $html ) . '</strong>' : ''; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.