1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SCI\DataValues; |
4
|
|
|
|
5
|
|
|
use SCI\CitationReferencePositionJournal; |
6
|
|
|
use SMWStringValue as StringValue; |
7
|
|
|
use SMWDIBlob as DIBlob; |
8
|
|
|
use Html; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class CitationReferenceValue extends StringValue { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var integer |
20
|
|
|
*/ |
21
|
|
|
private $captionFormat; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CitationReferencePositionJournal |
25
|
|
|
*/ |
26
|
|
|
private $citationReferencePositionJournal; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $reference; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* To display something like [[CiteRef::Foo, 1970|:50-52]] as [1]:50-52 for |
35
|
|
|
* SCI_CITEREF_NUM |
36
|
|
|
* |
37
|
|
|
* @var boolean |
38
|
|
|
*/ |
39
|
|
|
private $usesShortFormCaption = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $typeid |
43
|
|
|
*/ |
44
|
10 |
|
public function __construct( $typeid = '' ) { |
|
|
|
|
45
|
10 |
|
parent::__construct( '_sci_ref' ); |
46
|
|
|
|
47
|
|
|
// Currently there is no good way to inject the setting |
48
|
10 |
|
$this->captionFormat = $GLOBALS['scigCitationReferenceCaptionFormat']; |
49
|
10 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @since 1.0 |
53
|
|
|
* |
54
|
|
|
* @param integer $captionFormat |
55
|
|
|
*/ |
56
|
|
|
public function setCaptionFormat( $captionFormat ) { |
57
|
|
|
$this->captionFormat = $captionFormat; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @see StringValue::parseUserValue |
62
|
|
|
*/ |
63
|
9 |
|
protected function parseUserValue( $value ) { |
64
|
|
|
|
65
|
9 |
|
if ( method_exists( $this, 'getCallable' ) ) { |
66
|
9 |
|
$citationReferencePositionJournal = $this->getCallable( 'sci.citationreferencepositionjournal' ); |
67
|
9 |
|
$this->citationReferencePositionJournal = $citationReferencePositionJournal(); |
68
|
9 |
|
} else { |
69
|
|
|
$this->citationReferencePositionJournal = $this->getExtraneousFunctionFor( '\SCI\CitationReferencePositionJournal' ); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
$value = trim( $value ); |
73
|
|
|
|
74
|
9 |
|
if ( $value === '' ) { |
75
|
1 |
|
$this->addError( wfMessage( 'sci-datavalue-empty-reference' )->inContentLanguage()->text() ); |
76
|
1 |
|
$this->m_dataitem = new DIBlob( 'ERROR' ); |
77
|
1 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
if ( $this->m_contextPage === null ) { |
81
|
|
|
parent::parseUserValue( $value ); |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
$this->reference = $value; |
86
|
|
|
|
87
|
8 |
|
if ( $this->m_caption && $this->captionFormat === SCI_CITEREF_NUM ) { |
88
|
1 |
|
$this->usesShortFormCaption = true; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
8 |
|
if ( !$this->m_caption ) { |
92
|
7 |
|
$this->m_caption = $this->reference; |
93
|
7 |
|
} |
94
|
|
|
|
95
|
|
|
// This is where the magic happens, compute the position of |
96
|
|
|
// a reference relative to previous CiteRef annotations |
97
|
8 |
|
$this->citationReferencePositionJournal->addJournalEntryFor( |
98
|
8 |
|
$this->m_contextPage, |
99
|
8 |
|
$this->reference |
100
|
8 |
|
); |
101
|
|
|
|
102
|
8 |
|
parent::parseUserValue( $this->reference ); |
103
|
8 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @see StringValue::parseUserValue |
107
|
|
|
*/ |
108
|
7 |
|
public function getShortWikiText( $linked = null ) { |
109
|
|
|
|
110
|
7 |
|
if ( $this->citationReferencePositionJournal === null ) { |
111
|
|
|
if ( method_exists( $this, 'getCallable' ) ) { |
112
|
|
|
$citationReferencePositionJournal = $this->getCallable( 'sci.citationreferencepositionjournal' ); |
113
|
|
|
$this->citationReferencePositionJournal = $citationReferencePositionJournal(); |
114
|
|
|
} else { |
115
|
|
|
$this->citationReferencePositionJournal = $this->getExtraneousFunctionFor( '\SCI\CitationReferencePositionJournal' ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// We want the last entry here to get the major/minor |
120
|
|
|
// number that was internally recorded |
121
|
7 |
|
$referencePosition = $this->citationReferencePositionJournal->findLastReferencePositionEntryFor( |
122
|
7 |
|
$this->m_contextPage, |
123
|
7 |
|
$this->reference |
124
|
7 |
|
); |
125
|
|
|
|
126
|
7 |
|
if ( $referencePosition === null || $this->m_caption === false ) { |
127
|
|
|
return ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
$referenceHash = md5( $this->reference ); |
131
|
|
|
|
132
|
7 |
|
if ( $this->captionFormat === SCI_CITEREF_NUM ) { |
133
|
4 |
|
list( $major, $minor ) = explode( '-', $referencePosition ); |
|
|
|
|
134
|
4 |
|
$caption = $major; |
135
|
4 |
|
$captionClass = 'number'; |
136
|
|
|
|
137
|
|
|
// [[CiteRef::Foo, 1970|:50-52]] will add the caption to the outside |
|
|
|
|
138
|
4 |
|
$shortFormCaption = $this->usesShortFormCaption ? $this->m_caption : ''; |
139
|
4 |
|
} else { |
140
|
3 |
|
$captionClass = 'key'; |
141
|
3 |
|
$caption = $this->m_caption; |
142
|
3 |
|
$shortFormCaption = ''; // Never has a short form |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
if ( $shortFormCaption !== '' ) { |
146
|
1 |
|
$shortFormCaption = Html::rawElement( |
147
|
1 |
|
'span', |
148
|
1 |
|
[ 'class' => 'scite-citeref-shortcaption' ], |
149
|
|
|
$shortFormCaption |
150
|
1 |
|
); |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
// Build element with back and forth link anchor |
154
|
7 |
|
$html = Html::rawElement( |
155
|
7 |
|
'span', |
156
|
|
|
[ |
157
|
7 |
|
'id' => 'scite-ref-'. $referenceHash . '-' . $referencePosition, |
158
|
7 |
|
'class' => 'scite-citeref-' . $captionClass, |
159
|
7 |
|
'data-reference' => $this->reference |
160
|
7 |
|
], |
161
|
7 |
|
'[[' .'#scite-' . $referenceHash . '|' . $caption . ']]' |
162
|
7 |
|
) . $shortFormCaption; |
163
|
|
|
|
164
|
7 |
|
return $html; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: