|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SCI; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\ParserParameterProcessor; |
|
6
|
|
|
use SMW\DIProperty; |
|
7
|
|
|
use SMW\ParserData; |
|
8
|
|
|
use SMW\DataValueFactory; |
|
9
|
|
|
use Html; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @license GNU GPL v2+ |
|
13
|
|
|
* @since 1.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @author mwjames |
|
16
|
|
|
*/ |
|
17
|
|
|
class ReferenceListParserFunction { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ParserData |
|
21
|
|
|
*/ |
|
22
|
|
|
private $parserData; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @param ParserData $parserData |
|
28
|
|
|
*/ |
|
29
|
10 |
|
public function __construct( ParserData $parserData ) { |
|
30
|
10 |
|
$this->parserData = $parserData; |
|
31
|
10 |
|
$this->dataValueFactory = DataValueFactory::getInstance(); |
|
|
|
|
|
|
32
|
10 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {{#referencelist: |
|
36
|
|
|
* |columns=2 |
|
37
|
|
|
* |listtype="ol" |
|
38
|
|
|
* |browselinks=true |
|
39
|
|
|
* }} |
|
40
|
|
|
* |
|
41
|
|
|
* @since 1.0 |
|
42
|
|
|
* |
|
43
|
|
|
* @param ParserParameterProcessor $parserParameterProcessor |
|
44
|
|
|
*/ |
|
45
|
9 |
|
public function doProcess( ParserParameterProcessor $parserParameterProcessor ) { |
|
46
|
|
|
|
|
47
|
9 |
|
list( $header, $headerElement, $attributes ) = $this->getElementsForHtml( |
|
48
|
9 |
|
$parserParameterProcessor->toArray() |
|
49
|
9 |
|
); |
|
50
|
|
|
|
|
51
|
|
|
// Only the Parser can add a section/toc entry therefore the default reference |
|
52
|
|
|
// list is "too" late for the parser to process/add a toc section therefore only |
|
53
|
|
|
// the #referencelist can create a placeholder so that by the time the reference |
|
54
|
|
|
// list is generated the header is recognized. |
|
55
|
|
|
|
|
56
|
|
|
// The parser will set the headerTocId and is later fetched by the |
|
57
|
|
|
// CachedReferenceListOutputRenderer when replacing the placeholder. This |
|
58
|
|
|
// also takes care of any encoded title with non-Latin characters |
|
59
|
9 |
|
$header = Html::element( |
|
60
|
9 |
|
$headerElement, |
|
61
|
9 |
|
[], |
|
62
|
|
|
$header |
|
63
|
9 |
|
); |
|
64
|
|
|
|
|
65
|
9 |
|
$html = Html::rawElement( |
|
66
|
9 |
|
'div', |
|
67
|
9 |
|
$attributes, |
|
68
|
|
|
$header |
|
69
|
9 |
|
); |
|
70
|
|
|
|
|
71
|
9 |
|
return $html . "<!-- end marker -->\n"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
private function getElementsForHtml( $parameters ) { |
|
75
|
|
|
|
|
76
|
9 |
|
$header = wfMessage( 'sci-referencelist-header' )->text(); |
|
77
|
|
|
|
|
78
|
|
|
// The span placeholder will hide the header from the TOC by default |
|
79
|
9 |
|
$headerElement = 'span'; |
|
80
|
|
|
|
|
81
|
|
|
$attributes = [ |
|
82
|
|
|
'id' => 'scite-custom-referencelist' |
|
83
|
9 |
|
]; |
|
84
|
|
|
|
|
85
|
9 |
|
foreach ( $parameters as $key => $values ) { |
|
86
|
|
|
|
|
87
|
7 |
|
if ( $key === 'references' ) { |
|
88
|
4 |
|
$attributes['data-references'] = $this->doProcessReferenceValues( $values ); |
|
89
|
4 |
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
foreach ( $values as $value ) { |
|
93
|
|
|
|
|
94
|
5 |
|
if ( $key === 'header' ) { |
|
95
|
3 |
|
$header = $value; |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
if ( $key === 'toc' && filter_var( $value, FILTER_VALIDATE_BOOLEAN ) ) { |
|
99
|
1 |
|
$headerElement = 'h2'; |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
5 |
|
$attributes['data-'. $key] = $value; |
|
103
|
5 |
|
} |
|
104
|
9 |
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
return [ $header, $headerElement, $attributes ]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
4 |
|
private function doProcessReferenceValues( array $values ) { |
|
110
|
|
|
|
|
111
|
4 |
|
$subject = $this->parserData->getSubject(); |
|
112
|
|
|
|
|
113
|
4 |
|
foreach ( $values as $value ) { |
|
114
|
4 |
|
$dataValue = $this->dataValueFactory->newPropertyValue( |
|
115
|
4 |
|
new DIProperty( PropertyRegistry::SCI_CITE_REFERENCE ), |
|
116
|
4 |
|
trim( $value ), |
|
117
|
4 |
|
false, |
|
118
|
|
|
$subject |
|
119
|
4 |
|
); |
|
120
|
|
|
|
|
121
|
4 |
|
$this->parserData->addDataValue( $dataValue ); |
|
122
|
4 |
|
} |
|
123
|
|
|
|
|
124
|
4 |
|
if ( !$this->parserData->getSemanticData()->isEmpty() ) { |
|
125
|
4 |
|
$this->parserData->getSubject()->setContextReference( 'referencelistp:' . uniqid() ); |
|
126
|
4 |
|
$this->parserData->pushSemanticDataToParserOutput(); |
|
|
|
|
|
|
127
|
4 |
|
} |
|
128
|
|
|
|
|
129
|
4 |
|
return json_encode( $values ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: