summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/PrintRequest/Serializer.php
blob: 85523c9c960ac43552a735279c5147312763b32f (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
<?php

namespace SMW\Query\PrintRequest;

use SMW\Localizer;
use SMW\Query\PrintRequest;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class Serializer {

	/**
	 * @since 2.5
	 *
	 * @param PrintRequest $printRequest
	 * @param boolean $showparams that sets if the serialization should include
	 * the extra print request parameters
	 *
	 * @return string
	 */
	public static function serialize( PrintRequest $printRequest, $showparams = false ) {
		$parameters = '';

		if ( $showparams ) {

			// #2037 index is required as helper parameter during the result
			// display but is not part of the original request
			if ( $printRequest->getParameter( 'lang' ) ) {
				$printRequest->removeParameter( 'index' );
			};

			foreach ( $printRequest->getParameters() as $key => $value ) {
				$parameters .= "|+" . $key . "=" . $value;
			}
		}

		switch ( $printRequest->getMode() ) {
			case PrintRequest::PRINT_CATS:
				return self::doSerializeCat( $printRequest, $parameters );
			case PrintRequest::PRINT_CCAT:
				return self::doSerializeCcat( $printRequest, $parameters );
			case PrintRequest::PRINT_CHAIN:
			case PrintRequest::PRINT_PROP:
				return self::doSerializeProp( $printRequest, $parameters );
			case PrintRequest::PRINT_THIS:
				return self::doSerializeThis( $printRequest, $parameters );
			default:
				return '';
		}

		return ''; // no current serialisation
	}

	private static function doSerializeCat( $printRequest, $parameters ) {

		$catlabel = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
		$result = '?' . $catlabel;

		if ( $printRequest->getLabel() != $catlabel ) {
			$result .= '=' . $printRequest->getLabel();
		}

		return $result . $parameters;
	}

	private static function doSerializeCcat( $printRequest, $parameters ) {

		$printname = $printRequest->getData()->getPrefixedText();
		$result = '?' . $printname;

		if ( $printRequest->getOutputFormat() != 'x' ) {
			$result .= '#' . $printRequest->getOutputFormat();
		}

		if ( $printRequest->getLabel() != $printname ) {
			$result .= '=' . $printRequest->getLabel();
		}

		return $result . $parameters;
	}

	private static function doSerializeProp( $printRequest, $parameters ) {

		$printname = '';

		$label = $printRequest->getLabel();
		$data = $printRequest->getData();

		if ( $data->isVisible() ) {
			// #1564
			// Use the canonical form for predefined properties to ensure
			// that local representations are for display but points to
			// the correct property
			if ( $printRequest->isMode( PrintRequest::PRINT_CHAIN ) ) {
				$printname = $data->getDataItem()->getString();
				// If the preferred label and invoked label are the same
				// then no additional label is required as the label is
				// recognized as being available by the system
				if ( $label === $data->getLastPropertyChainValue()->getDataItem()->getPreferredLabel() ) {
					$label = $printname;
				}
			} else {

				$printname = $data->getDataItem()->getCanonicalLabel();

				if ( $label === $data->getDataItem()->getPreferredLabel() ) {
					$label = $printname;
				}

				// Don't carry a localized label for a predefined property
				// (fetched via the wikiValue)
				if ( !$data->getDataItem()->isUserDefined() && $label === $data->getWikiValue() ) {
					$label = $data->getDataItem()->getCanonicalLabel();
				}
			}
		}

		$result = '?' . $printname;

		if ( $printRequest->getOutputFormat() !== '' ) {
			$result .= '#' . $printRequest->getOutputFormat();
		}

		if ( $printname != $label && $label !== '' ) {
			$result .= '=' . $label;
		}

		return $result . $parameters;
	}

	private static function doSerializeThis( $printRequest, $parameters ) {

		$result = '?';

		// Has leading ?#
		if ( $printRequest->hasLabelMarker() ) {
			$result .= '#';
		}

		if ( $printRequest->getLabel() !== '' ) {
			$result .= '=' . $printRequest->getLabel();
		}

		$outputFormat = $printRequest->getOutputFormat();

		if ( $outputFormat !== '' && $outputFormat !== false && $outputFormat !== null ) {

			// Handle ?, ?#- vs. ?#Foo=#-
			if ( $printRequest->getLabel() !== '' ) {
				$result .= '#';
			}

			$result .= $outputFormat;
		}

		return $result . $parameters;
	}

}