Completed
Push — master ( 0591cb...33c12c )
by
unknown
07:17
created

ReferenceListParserFunction::doProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 13
cts 13
cp 1
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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();
0 ignored issues
show
Bug introduced by
The property dataValueFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The method SMW\ParserData::pushSemanticDataToParserOutput() has been deprecated with message: since 3.0, use copyToParserOutput

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.

Loading history...
127 4
		}
128
129 4
		return json_encode( $values );
130
	}
131
132
}
133