summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/src/Outline/OutlineResultPrinter.php
blob: 9bb3b04274341d9e940ab9453ec3e277d80b1413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace SRF\Outline;

use SMWResultPrinter as ResultPrinter;
use SMWQueryResult as QueryResult;

/**
 * A class to print query results in an outline format, along with some
 * helper classes to handle the aggregation
 *
 * @license GNU GPL v2+
 * @since 1.4.3
 *
 * @author Yaron Koren
 */
class OutlineResultPrinter extends ResultPrinter {

	/**
	 * @see ResultPrinter::getName
	 *
	 * {@inheritDoc}
	 */
	public function getName() {
		return wfMessage( 'srf_printername_outline' )->text();
	}

	/**
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params['outlineproperties'] = [
			'islist' => true,
			'default' => [],
			'message' => 'srf_paramdesc_outlineproperties',
		];

		$params[] = [
			'name' => 'template',
			'message' => 'smw-paramdesc-template',
			'default' => '',
		];

		$params[] = [
			'name' => 'userparam',
			'message' => 'smw-paramdesc-userparam',
			'default' => '',
		];

		$params[] = [
			'name' => 'named args',
			'type' => 'boolean',
			'message' => 'smw-paramdesc-named_args',
			'default' => true,
		];

		return $params;
	}

	/**
	 * @see ResultPrinter::getResultText
	 *
	 * {@inheritDoc}
	 */
	protected function getResultText( QueryResult $res, $outputMode ) {

		// for each result row, create an array of the row itself
		// and all its sorted-on fields, and add it to the initial
		// 'tree'
		$outlineTree = new OutlineTree();

		while ( $row = $res->getNext() ) {
			$outlineTree->addItem( $this->newOutlineItem( $row ) );
		}

		// now, cycle through the outline properties, creating the
		// tree
		foreach ( $this->params['outlineproperties'] as $property ) {
			$outlineTree->addProperty( $property );
		}

		if ( $this->params['template'] !== '' ) {
			$this->hasTemplates = true;
			$templateBuilder = new TemplateBuilder(
				$this->params
			);

			$templateBuilder->setLinker( $this->mLinker );
			$result = $templateBuilder->build( $outlineTree );
		} else {
			$listTreeBuilder = new ListTreeBuilder(
				$this->params + [ 'showHeaders' => $this->mShowHeaders ]
			);

			$listTreeBuilder->setLinker( $this->mLinker );
			$result = $listTreeBuilder->build( $outlineTree );
		}

		if ( $this->linkFurtherResults( $res ) ) {
			$link = $this->getFurtherResultsLink(
				$res,
				$outputMode
			);

			$result .= $link->getText( $outputMode, $this->mLinker ) . "\n";
		}

		return $result;
	}

	private function newOutlineItem( $row ) {

		$outlineItem = new OutlineItem( $row );

		foreach ( $row as $field ) {
			$name = $field->getPrintRequest()->getText( SMW_OUTPUT_HTML );

			if ( !in_array( $name, $this->params['outlineproperties'] ) ) {
				continue;
			}

			while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
				$outlineItem->addFieldValue(
					$name,
					$dataValue->getLongWikiText( $this->getLinker() )
				);
			}
		}

		return $outlineItem;
	}

}