summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/ProfileAnnotatorFactory.php
blob: 52f1e618a2ef01839aae67d2e68dac95b96a0c6d (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
<?php

namespace SMW\Query;

use SMW\DIWikiPage;
use SMW\Query\ProfileAnnotators\DescriptionProfileAnnotator;
use SMW\Query\ProfileAnnotators\DurationProfileAnnotator;
use SMW\Query\ProfileAnnotators\FormatProfileAnnotator;
use SMW\Query\ProfileAnnotators\NullProfileAnnotator;
use SMW\Query\ProfileAnnotators\ParametersProfileAnnotator;
use SMW\Query\ProfileAnnotators\SourceProfileAnnotator;
use SMW\Query\ProfileAnnotators\StatusCodeProfileAnnotator;
use SMW\Query\ProfileAnnotators\SchemaLinkProfileAnnotator;
use SMWContainerSemanticData as ContainerSemanticData;
use SMWDIContainer as DIContainer;
use SMWQuery as Query;

/**
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class ProfileAnnotatorFactory {

	/**
	 * @since 2.1
	 *
	 * @param Query $query
	 * @param string $format
	 *
	 * @return ProfileAnnotator
	 */
	public function newProfileAnnotator( Query $query, $format ) {

		$profileAnnotator = $this->newDescriptionProfileAnnotator(
			$query
		);

		$profileAnnotator = $this->newFormatProfileAnnotator(
			$profileAnnotator,
			$format
		);

		$profileAnnotator = $this->newParametersProfileAnnotator(
			$profileAnnotator,
			$query
		);

		$profileAnnotator = $this->newDurationProfileAnnotator(
			$profileAnnotator,
			$query->getOption( Query::PROC_QUERY_TIME )
		);

		$profileAnnotator = $this->newSourceProfileAnnotator(
			$profileAnnotator,
			$query->getQuerySource()
		);

		$profileAnnotator = $this->newStatusCodeProfileAnnotator(
			$profileAnnotator,
			$query->getOption( Query::PROC_STATUS_CODE )
		);

		$profileAnnotator = $this->newSchemaLinkProfileAnnotator(
			$profileAnnotator,
			$query->getOption( 'schema_link' )
		);

		return $profileAnnotator;
	}

	/**
	 * @since 2.5
	 *
	 * @param Query $query
	 *
	 * @return DescriptionProfileAnnotator
	 */
	public function newDescriptionProfileAnnotator( Query $query ) {

		$profileAnnotator = new NullProfileAnnotator(
			$this->newDIContainer( $query )
		);

		$profileAnnotator = new DescriptionProfileAnnotator(
			$profileAnnotator,
			$query->getDescription()
		);

		return $profileAnnotator;
	}

	private function newFormatProfileAnnotator( $profileAnnotator, $format ) {
		return new FormatProfileAnnotator( $profileAnnotator, $format );
	}

	private function newParametersProfileAnnotator( $profileAnnotator, $query ) {

		if ( $query->getOption( Query::OPT_PARAMETERS ) === false ) {
			return $profileAnnotator;
		}

		return new ParametersProfileAnnotator( $profileAnnotator, $query );
	}

	private function newDurationProfileAnnotator( $profileAnnotator, $duration ) {

		if ( $duration == 0 ) {
			return $profileAnnotator;
		}

		return new DurationProfileAnnotator( $profileAnnotator, $duration );
	}

	private function newSourceProfileAnnotator( $profileAnnotator, $querySource ) {

		if ( $querySource === '' || $querySource === null ) {
			return $profileAnnotator;
		}

		return new SourceProfileAnnotator( $profileAnnotator, $querySource );
	}

	private function newStatusCodeProfileAnnotator( $profileAnnotator, $statusCodes ) {

		if ( $statusCodes === false || $statusCodes === null || $statusCodes === [] ) {
			return $profileAnnotator;
		}

		return new StatusCodeProfileAnnotator( $profileAnnotator, $statusCodes );
	}

	private function newSchemaLinkProfileAnnotator( $profileAnnotator, $schemaLink ) {

		if ( $schemaLink === false || $schemaLink === null ) {
			return $profileAnnotator;
		}

		return new SchemaLinkProfileAnnotator( $profileAnnotator, $schemaLink );
	}

	/**
	 * #1416 create container manually to avoid any issues that may arise from
	 * a failed Title::makeTitleSafe.
	 */
	private function newDIContainer( Query $query ) {

		$subject = $query->getContextPage();

		if ( $subject === null ) {
			$containerSemanticData = ContainerSemanticData::makeAnonymousContainer();
		} else {
			$subject = new DIWikiPage(
				$subject->getDBkey(),
				$subject->getNamespace(),
				$subject->getInterwiki(),
				$query->getQueryId()
			);

			$containerSemanticData = new ContainerSemanticData( $subject );
		}

		return new DIContainer(
			$containerSemanticData
		);
	}

}