summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/params/SMW_ParamFormat.php
blob: 935fd4e5026264864f2b573d8652ec85ab7b3122 (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
<?php

use ParamProcessor\Definition\StringParam;
use ParamProcessor\IParam;
use SMW\Query\PrintRequest;

/**
 * Definition for the format parameter.
 *
 * @since 1.6.2
 * @deprecated since 1.9
 *
 * @ingroup SMW
 * @ingroup ParamDefinition
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class SMWParamFormat extends StringParam {

	/**
	 * List of the queries print requests, used to determine the format
	 * when it's not provided. Set with setPrintRequests before passing
	 * to Validator.
	 *
	 * @since 1.6.2
	 *
	 * @var PrintRequest[]
	 */
	protected $printRequests = [];

	protected $showMode = false;

	/**
	 * Takes a format name, which can be an alias and returns a format name
	 * which will be valid for sure. Aliases are resolved. If the given
	 * format name is invalid, the predefined default format will be returned.
	 *
	 * @since 1.6.2
	 *
	 * @param string $value
	 *
	 * @return string
	 */
	protected function getValidFormatName( $value ) {
		global $smwgResultFormats;

		$value = strtolower( trim( $value ) );

		if ( !array_key_exists( $value, $smwgResultFormats ) ) {
			$isAlias = self::resolveFormatAliases( $value );

			if ( !$isAlias ) {
				$value = $this->getDefaultFormat();
				self::resolveFormatAliases( $value );
			}
		}

		return $value;
	}

	/**
	 * Turns format aliases into main formats.
	 *
	 * @since 1.6.2
	 *
	 * @param string $format
	 *
	 * @return boolean Indicates if the passed format was an alias, and thus was changed.
	 */
	public static function resolveFormatAliases( &$format ) {
		global $smwgResultAliases;

		$isAlias = false;

		foreach ( $smwgResultAliases as $mainFormat => $aliases ) {
			if ( in_array( $format, $aliases ) ) {
				$format = $mainFormat;
				$isAlias = true;
				break;
			}
		}

		return $isAlias;
	}

	/**
	 * Determines and returns the default format, based on the queries print
	 * requests, if provided.
	 *
	 * @since 1.6.2
	 *
	 * @return string Array key in $smwgResultFormats
	 */
	protected function getDefaultFormat() {

		if ( empty( $this->printRequests ) ) {
			return 'table';
		}

		$format = false;

		/**
		 * This hook allows extensions to override SMWs implementation of default result
		 * format handling.
		 *
		 * @since 1.5.2
		 */
		\Hooks::run( 'SMWResultFormat', [ &$format, $this->printRequests, [] ] );

		if ( $format !== false ) {
			return $format;
		}

		// If no default was set by an extension, use a table, plainlist or list, depending on showMode and column count.
		if ( count( $this->printRequests ) > 1 ) {
			return 'table';
		}

		return 'plainlist';
	}

	/**
	 * Sets the print requests of the query, used for determining
	 * the default format if none is provided.
	 *
	 * @since 1.6.2
	 *
	 * @param PrintRequest[] $printRequests
	 */
	public function setPrintRequests( array $printRequests ) {
		$this->printRequests = $printRequests;
	}

	/**
	 *
	 * @since 3.0
	 *
	 * @param bool $showMode
	 */
	public function setShowMode( $showMode ) {
		$this->showMode = $showMode;
	}

	/**
	 * Formats the parameter value to it's final result.
	 *
	 * @since 1.8
	 *
	 * @param mixed $value
	 * @param IParam $param
	 * @param IParamDefinition[] $definitions
	 * @param IParam[] $params
	 *
	 * @return mixed
	 */
	protected function formatValue( $value, IParam $param, array &$definitions, array $params ) {
		$value = parent::formatValue( $value, $param, $definitions, $params );

		// Make sure the format value is valid.
		$value = self::getValidFormatName( $value );

		// Add the formats parameters to the parameter list.
		$queryPrinter = SMWQueryProcessor::getResultPrinter( $value );

		$definitions = $queryPrinter->getParamDefinitions( $definitions );

		return $value;
	}

}