summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/queryprinters/RdfResultPrinter.php
blob: 24006dbec38b7186439913773d5ab9c3c3256141 (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
<?php

namespace SMW;

use SMW\Query\PrintRequest;
use SMWExporter;
use SMWQueryResult;
use SMWRDFXMLSerializer;
use SMWTurtleSerializer;

/**
 * Printer class for generating RDF output
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class RdfResultPrinter extends FileExportPrinter {

	/**
	 * The syntax to be used for export. May be 'rdfxml' or 'turtle'.
	 */
	protected $syntax;

	/**
	 * @see SMWResultPrinter::handleParameters
	 *
	 * @since 1.7
	 *
	 * @param array $params
	 * @param $outputmode
	 */
	protected function handleParameters( array $params, $outputmode ) {
		parent::handleParameters( $params, $outputmode );
		$this->syntax = $params['syntax'];
	}

	/**
	 * @see SMWIExportPrinter::getMimeType
	 *
	 * @since 1.8
	 *
	 * @param SMWQueryResult $queryResult
	 *
	 * @return string
	 */
	public function getMimeType( SMWQueryResult $queryResult ) {
		return $this->syntax == 'turtle' ? 'application/x-turtle' : 'application/xml';
	}

	/**
	 * @see SMWIExportPrinter::getFileName
	 *
	 * @since 1.8
	 *
	 * @param SMWQueryResult $queryResult
	 *
	 * @return string|boolean
	 */
	public function getFileName( SMWQueryResult $queryResult ) {
		return $this->syntax == 'turtle' ? 'result.ttl' : 'result.rdf';
	}

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

	protected function getResultText( SMWQueryResult $res, $outputMode ) {
		if ( $outputMode == SMW_OUTPUT_FILE ) { // make RDF file
			$serializer = $this->syntax == 'turtle' ? new SMWTurtleSerializer() : new SMWRDFXMLSerializer();
			$serializer->startSerialization();
			$serializer->serializeExpData( SMWExporter::getInstance()->getOntologyExpData( '' ) );

			while ( $row = $res->getNext() ) {
				$subjectDi = reset( $row )->getResultSubject();
				$data = SMWExporter::getInstance()->makeExportDataForSubject( $subjectDi );

				foreach ( $row as $resultarray ) {
					$printreq = $resultarray->getPrintRequest();
					$property = null;

					switch ( $printreq->getMode() ) {
						case PrintRequest::PRINT_PROP:
							$property = $printreq->getData()->getDataItem();
						break;
						case PrintRequest::PRINT_CATS:
							$property = new DIProperty( '_TYPE' );
						break;
						case PrintRequest::PRINT_CCAT:
							// not serialised right now
						break;
						case PrintRequest::PRINT_THIS:
							// ignored here (object is always included in export)
						break;
					}

					if ( !is_null( $property ) ) {
						SMWExporter::getInstance()->addPropertyValues( $property, $resultarray->getContent(), $data, $subjectDi );
					}
				}
				$serializer->serializeExpData( $data );
			}

			$serializer->finishSerialization();

			return $serializer->flushContent();
		} else { // just make link to feed
			$this->isHTML = ( $outputMode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed

			return $this->getLink( $res, $outputMode )->getText( $outputMode, $this->mLinker );
		}
	}

	/**
	 * @see SMWResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * @param ParamDefinition[] $definitions
	 *
	 * @return array
	 */
	public function getParamDefinitions( array $definitions ) {
		$definitions = parent::getParamDefinitions( $definitions );

		$definitions['limit']->setDefault( 100 );

		$definitions['searchlabel']->setDefault( wfMessage( 'smw_rdf_link' )->inContentLanguage()->text() );

		$definitions[] = [
			'name' => 'syntax',
			'message' => 'smw-paramdesc-rdfsyntax',
			'values' => [ 'rdfxml', 'turtle' ],
			'default' => 'rdfxml',
		];

		return $definitions;
	}

}