Completed
Push — master ( 195f4f...8c0d0a )
by Jeroen De
05:54
created

OutlineResultPrinter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 5
dl 0
loc 134
ccs 38
cts 43
cp 0.8837
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getParamDefinitions() 0 42 1
B getResultText() 0 45 5
A newOutlineItem() 0 21 4
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
		return $result;
127
	}
128
129 2
	private function newOutlineItem( $row ) {
130
131 2
		$outlineItem = new OutlineItem( $row );
132
133 2
		foreach ( $row as $field ) {
134 2
			$name = $field->getPrintRequest()->getText( SMW_OUTPUT_HTML );
135
136 2
			if ( !in_array( $name, $this->params['outlineproperties'] ) ) {
137 2
				continue;
138
			}
139
140 2
			while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
141 2
				$outlineItem->addFieldValue(
142 2
					$name,
143 2
					$dataValue->getLongWikiText( $this->getLinker() )
144
				);
145
			}
146
		}
147
148 2
		return $outlineItem;
149
	}
150
}
151