Completed
Pull Request — master (#582)
by
unknown
05:23
created

OutlineResultPrinter::getIntroTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SRF\Outline;
4
5
use SMWResultPrinter as ResultPrinter;
6
use SMWQueryResult as QueryResult;
7
8
/**
9
 * A class to print query results in an outline format, along with some
10
 * helper classes to handle the aggregation
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.4.3
14
 *
15
 * @author Yaron Koren
16
 */
17
class OutlineResultPrinter extends ResultPrinter {
18
19
	/**
20
	 * @see ResultPrinter::getName
21
	 *
22
	 * {@inheritDoc}
23
	 */
24
	public function getName() {
25
		return wfMessage( 'srf_printername_outline' )->text();
26
	}
27
28
	/**
29
	 * @see SMWResultPrinter::getParamDefinitions
30
	 *
31
	 * @since 1.8
32
	 *
33
	 * {@inheritDoc}
34
	 */
35 2
	public function getParamDefinitions( array $definitions ) {
36 2
		$params = parent::getParamDefinitions( $definitions );
37
38 2
		$params['outlineproperties'] = [
39
			'islist' => true,
40
			'default' => [],
41
			'message' => 'srf_paramdesc_outlineproperties',
42
		];
43
44 2
		$params[] = [
45
			'name' => 'template',
46
			'message' => 'smw-paramdesc-template',
47
			'default' => '',
48
		];
49
50 2
		$params[] = [
51
			'name' => 'introtemplate',
52
			'message' => 'smw-paramdesc-introtemplate',
53
			'default' => ''
54
		];
55
56 2
		$params[] = [
57
			'name' => 'outrotemplate',
58
			'message' => 'smw-paramdesc-outrotemplate',
59
			'default' => ''
60
		];
61
62 2
		$params[] = [
63
			'name' => 'userparam',
64
			'message' => 'smw-paramdesc-userparam',
65
			'default' => '',
66
		];
67
68 2
		$params[] = [
69
			'name' => 'named args',
70
			'type' => 'boolean',
71
			'message' => 'smw-paramdesc-named_args',
72
			'default' => true,
73
		];
74
75 2
		return $params;
76
	}
77
78
	/**
79
	 * @see ResultPrinter::getResultText
80
	 *
81
	 * {@inheritDoc}
82
	 */
83 3
	protected function getResultText( QueryResult $res, $outputMode ) {
84
85
		// for each result row, create an array of the row itself
86
		// and all its sorted-on fields, and add it to the initial
87
		// 'tree'
88 3
		$outlineTree = new OutlineTree();
89
90 3
		while ( $row = $res->getNext() ) {
91 2
			$outlineTree->addItem( $this->newOutlineItem( $row ) );
92
		}
93
94
		// now, cycle through the outline properties, creating the
95
		// tree
96 3
		foreach ( $this->params['outlineproperties'] as $property ) {
97 2
			$outlineTree->addProperty( $property );
98
		}
99
100 3
		if ( $this->params['template'] !== '' ) {
101 1
			$this->hasTemplates = true;
102 1
			$templateBuilder = new TemplateBuilder(
103 1
				$this->params
104
			);
105
106 1
			$templateBuilder->setLinker( $this->mLinker );
107 1
			$result = $templateBuilder->build( $outlineTree );
108
		} else {
109 2
			$listTreeBuilder = new ListTreeBuilder(
110 2
				$this->params + [ 'showHeaders' => $this->mShowHeaders ]
111
			);
112
113 2
			$listTreeBuilder->setLinker( $this->mLinker );
114 2
			$result = $listTreeBuilder->build( $outlineTree );
115
		}
116
117 3
		if ( $this->linkFurtherResults( $res ) ) {
118
			$link = $this->getFurtherResultsLink(
119
				$res,
120
				$outputMode
121
			);
122
123
			$result .= $link->getText( $outputMode, $this->mLinker ) . "\n";
124
		}
125
126 3
		if( $this->params['introtemplate'] !== '' || $this->params['introtemplate'] !== ''){
127 2
			$this->hasTemplates = true;
128
		}
129
130 3
		$result = $this->getIntroTemplate() . $result;
131
132 3
		$result .= $this->getOutroTemplate();
133
134 3
		return $result;
135
	}
136
137 2
	private function newOutlineItem( $row ) {
138
139 2
		$outlineItem = new OutlineItem( $row );
140
141 2
		foreach ( $row as $field ) {
142 2
			$name = $field->getPrintRequest()->getText( SMW_OUTPUT_HTML );
143
144 2
			if ( !in_array( $name, $this->params['outlineproperties'] ) ) {
145 2
				continue;
146
			}
147
148 2
			while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
149 2
				$outlineItem->addFieldValue(
150 2
					$name,
151 2
					$dataValue->getLongWikiText( $this->getLinker() )
152
				);
153
			}
154
		}
155
156 2
		return $outlineItem;
157
	}
158
159 3
	function getIntroTemplate(): string {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160 3
		if ( $this->params['introtemplate'] !== '' ) {
161 2
			return "{{" . $this->params['introtemplate'] . "}}";
162
		}
163 3
		return "";
164
	}
165
166 3
	function getOutroTemplate(): string {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
167 3
		if ( $this->params['outrotemplate'] !== '' ) {
168 2
			return "{{" . $this->params['outrotemplate'] . "}}";
169
		}
170 3
		return "";
171
	}
172
}
173