summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/formats/outline/SRF_Outline.php
blob: 8e9d252920cf13107e76b3082a235e629417045c (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php

/**
 * A class to print query results in an outline format, along with some
 * helper classes to handle the aggregation
 */

/**
 * Represents a single item, or page, in the outline - contains both the
 * SMWResultArray and an array of some of its values, for easier aggregation
 */
class SRFOutlineItem {

	var $mRow;
	var $mVals;

	function __construct( $row ) {
		$this->mRow = $row;
		$this->mVals = [];
	}

	function addFieldValue( $field_name, $field_val ) {
		if ( array_key_exists( $field_name, $this->mVals ) ) {
			$this->mVals[$field_name][] = $field_val;
		} else {
			$this->mVals[$field_name] = [ $field_val ];
		}
	}

	function getFieldValues( $field_name ) {
		if ( array_key_exists( $field_name, $this->mVals ) ) {
			return $this->mVals[$field_name];
		} else {
			return [ wfMessage( 'srf_outline_novalue' )->text() ];
		}
	}
}

/**
 * A tree structure for holding the outline data
 */
class SRFOutlineTree {

	var $mTree;
	var $mUnsortedItems;

	function __construct( $items = [] ) {
		$this->mTree = [];
		$this->mUnsortedItems = $items;
	}

	function addItem( $item ) {
		$this->mUnsortedItems[] = $item;
	}

	function categorizeItem( $vals, $item ) {
		foreach ( $vals as $val ) {
			if ( array_key_exists( $val, $this->mTree ) ) {
				$this->mTree[$val]->mUnsortedItems[] = $item;
			} else {
				$this->mTree[$val] = new SRFOutlineTree( [ $item ] );
			}
		}
	}

	function addProperty( $property ) {
		if ( count( $this->mUnsortedItems ) > 0 ) {
			foreach ( $this->mUnsortedItems as $item ) {
				$cur_vals = $item->getFieldValues( $property );
				$this->categorizeItem( $cur_vals, $item );
			}
			$this->mUnsortedItems = null;
		} else {
			foreach ( $this->mTree as $i => $node ) {
				$this->mTree[$i]->addProperty( $property );
			}
		}
	}
}

class SRFOutline extends SMWResultPrinter {

	protected $mOutlineProperties = [];
	protected $mInnerFormat = '';

	protected function handleParameters( array $params, $outputmode ) {
		parent::handleParameters( $params, $outputmode );
		$this->mOutlineProperties = $params['outlineproperties'];
	}

	public function getName() {
		return wfMessage( 'srf_printername_outline' )->text();
	}

	/**
	 * Code mostly copied from SMW's SMWListResultPrinter::getResultText()
	 */
	function printItem( $item ) {
		$first_col = true;
		$found_values = false; // has anything but the first column been printed?
		$result = "";
		foreach ( $item->mRow as $orig_ra ) {
			// handling is somewhat simpler for SMW 1.5+
			$realFunction = [ 'SMWQueryResult', 'getResults' ];
			if ( is_callable( $realFunction ) ) {
				// make a new copy of this, so that the call to
				// getNextText() will work again
				$ra = clone ( $orig_ra );
			} else {
				// make a new copy of this, so that the call to
				// getNextText() will work again
				$ra = new SMWResultArray( $orig_ra->getContent(), $orig_ra->getPrintRequest() );
			}
			$val = $ra->getPrintRequest()->getText( SMW_OUTPUT_WIKI, null );
			if ( in_array( $val, $this->mOutlineProperties ) ) {
				continue;
			}
			$first_value = true;
			while ( ( $text = $ra->getNextText( SMW_OUTPUT_WIKI, $this->mLinker ) ) !== false ) {
				if ( !$first_col && !$found_values ) { // first values after first column
					$result .= ' (';
					$found_values = true;
				} elseif ( $found_values || !$first_value ) {
					// any value after '(' or non-first values on first column
					$result .= ', ';
				}
				if ( $first_value ) { // first value in any column, print header
					$first_value = false;
					if ( $this->mShowHeaders && ( '' != $ra->getPrintRequest()->getLabel() ) ) {
						$result .= $ra->getPrintRequest()->getText( SMW_OUTPUT_WIKI, $this->mLinker ) . ' ';
					}
				}
				$result .= $text; // actual output value
			}
			$first_col = false;
		}
		if ( $found_values ) {
			$result .= ')';
		}
		return $result;
	}

	function printTree( $outline_tree, $level = 0 ) {
		$text = "";
		if ( !is_null( $outline_tree->mUnsortedItems ) ) {
			$text .= "<ul>\n";
			foreach ( $outline_tree->mUnsortedItems as $item ) {
				$text .= "<li>{$this->printItem($item)}</li>\n";
			}
			$text .= "</ul>\n";
		}
		if ( $level > 0 ) {
			$text .= "<ul>\n";
		}
		$num_levels = count( $this->mOutlineProperties );
		// set font size and weight depending on level we're at
		$font_level = $level;
		if ( $num_levels < 4 ) {
			$font_level += ( 4 - $num_levels );
		}
		if ( $font_level == 0 ) {
			$font_size = 'x-large';
		} elseif ( $font_level == 1 ) {
			$font_size = 'large';
		} elseif ( $font_level == 2 ) {
			$font_size = 'medium';
		} else {
			$font_size = 'small';
		}
		if ( $font_level == 3 ) {
			$font_weight = 'bold';
		} else {
			$font_weight = 'regular';
		}
		foreach ( $outline_tree->mTree as $key => $node ) {
			$text .= "<p style=\"font-size: $font_size; font-weight: $font_weight;\">$key</p>\n";
			$text .= $this->printTree( $node, $level + 1 );
		}
		if ( $level > 0 ) {
			$text .= "</ul>\n";
		}
		return $text;
	}

	protected function getResultText( SMWQueryResult $res, $outputmode ) {
		$print_fields = [];
		foreach ( $res->getPrintRequests() as $pr ) {
			$field_name = $pr->getText( $outputmode, $this->mLinker );
			// only print it if it's not already part of the
			// outline
			if ( !in_array( $field_name, $this->mOutlineProperties ) ) {
				$print_fields[] = $field_name;
			}
		}

		// for each result row, create an array of the row itself
		// and all its sorted-on fields, and add it to the initial
		// 'tree'
		$outline_tree = new SRFOutlineTree();
		while ( $row = $res->getNext() ) {
			$item = new SRFOutlineItem( $row );
			foreach ( $row as $field ) {
				$field_name = $field->getPrintRequest()->getText( SMW_OUTPUT_HTML );
				if ( in_array( $field_name, $this->mOutlineProperties ) ) {
					while ( ( $object = $field->getNextDataValue() ) !== false ) {
						$field_val = $object->getLongWikiText( $this->mLinker );
						$item->addFieldValue( $field_name, $field_val );
					}
				}
			}
			$outline_tree->addItem( $item );
		}

		// now, cycle through the outline properties, creating the
		// tree
		foreach ( $this->mOutlineProperties as $outline_prop ) {
			$outline_tree->addProperty( $outline_prop );
		}
		$result = $this->printTree( $outline_tree );

		// print further results footer
		if ( $this->linkFurtherResults( $res ) ) {
			$link = $res->getQueryLink();
			if ( $this->getSearchLabel( $outputmode ) ) {
				$link->setCaption( $this->getSearchLabel( $outputmode ) );
			}
			$link->setParameter( 'outline', 'format' );
			if ( array_key_exists( 'outlineproperties', $this->params ) ) {
				$link->setParameter( $this->params['outlineproperties'], 'outlineproperties' );
			}
			$result .= $link->getText( $outputmode, $this->mLinker ) . "\n";
		}
		return $result;
	}

	/**
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * @param $definitions array of IParamDefinition
	 *
	 * @return array of IParamDefinition|array
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

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

		return $params;
	}

}