Completed
Pull Request — master (#16)
by
unknown
16:24
created

ScribuntoLuaLibrary   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 90.63%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 145
ccs 29
cts 32
cp 0.9063
rs 10
c 2
b 0
f 0
wmc 15
lcom 0
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 1
B getQueryResult() 0 25 2
A getPropertyType() 0 17 3
C set() 0 49 9
1
<?php
2
3
namespace SMW\Scribunto;
4
5
use Scribunto_LuaLibraryBase;
6
use SMW\DIProperty;
7
use FauxRequest;
8
use ApiMain;
9
10
use SMWQueryProcessor as QueryProcessor;
11
use SMW\ApplicationFactory;
12
use SMW\ParameterProcessorFactory;
13
use SMW\ParserFunctionFactory;
14
15
/**
16
 * @license GNU GPL v2+
17
 * @since 1.0
18
 *
19
 * @author mwjames
20
 */
21
class ScribuntoLuaLibrary extends Scribunto_LuaLibraryBase {
22
23
	/**
24 5
	 * This is the name of the key for error messages
25
	 *
26
	 * @var string
27 5
	 * @since 1.0
28 5
	 */
29 5
	const SMW_ERROR_FIELD='error';
30
31 5
	/**
32 5
	 * @since 1.0
33
	 */
34
	public function register() {
35
36
		$lib = array(
37
			'getQueryResult'  => array( $this, 'getQueryResult' ),
38
			'getPropertyType' => array( $this, 'getPropertyType' ),
39
			'set'             => array( $this, 'set' ),
40
		);
41
42
		$this->getEngine()->registerInterface( __DIR__ . '/' . 'mw.smw.lua', $lib, array() );
43 2
	}
44
45 2
	/**
46
	 * Returns query results in for of the standard API return format
47 2
	 *
48 2
	 * @since 1.0
49
	 *
50 2
	 * @param string $argString
51
	 *
52 2
	 * @return array
53
	 */
54 2
	public function getQueryResult( $argString = null ) {
55 2
56 2
		$rawParameters = preg_split( "/(?<=[^\|])\|(?=[^\|])/", $argString );
57 2
58 2
		list( $queryString, $parameters, $printouts ) = QueryProcessor::getComponentsFromFunctionParams(
59
		    $rawParameters,
60 2
		    false
61
		);
62 2
63 2
		QueryProcessor::addThisPrintout( $printouts, $parameters );
64
65
		$query = QueryProcessor::createQuery(
66 2
		    $queryString,
67
		    QueryProcessor::getProcessedParams( $parameters, $printouts ),
68
		    QueryProcessor::SPECIAL_PAGE,
69
		    '',
70
		    $printouts
71
		);
72
73
		$queryResult = ApplicationFactory::getInstance()->getStore()->getQueryResult( $query )->toArray();
74
		if(!empty($queryResult["results"])) {
75
		    $queryResult["results"] = array_combine(range(1, count($queryResult["results"])), array_values($queryResult["results"]));
76
		}
77
		return array( $queryResult );
78 3
	}
79
80 3
	/**
81 3
	 * Returns property type
82
	 *
83 3
	 * @since 1.0
84 1
	 *
85
	 * @param string $propertyName
86
	 *
87 2
	 * @return array
88
	 */
89 2
	public function getPropertyType( $propertyName = null ) {
90
91
		$this->checkType( 'getPropertyType', 1, $propertyName, 'string' );
92
		$propertyName = trim( $propertyName );
93 2
94
		if ( $propertyName === '' ) {
95
			return array( null );
96
		}
97
98
		$property = DIProperty::newFromUserLabel( $propertyName );
99
100
		if ( $property === null ) {
101
			return array( null );
102
		}
103
104
		return array( $property->findPropertyTypeID() );
105
	}
106
107
	/**
108
	 * This mirrors the functionality of the parser function #set and makes it available in lua.
109
	 *
110
	 * @param string|array	$parameters	parameters passed from lua, string or array depending on call
111
	 *
112
	 * @uses \SMW\ParserFunctionFactory::__construct, ParameterProcessorFactory::newFromArray
113
	 *
114
	 * @return array|array[]
115
	 */
116
	public function set( $parameters )
117
	{
118
		$parser = $this->getEngine()->getParser();
119
120
		# make sure, we have an array of parameters
121
		if ( !is_array($parameters) ) {
122
			$parameters = array($parameters);
123
		}
124
125
		# if $parameters were supplied as key => value pair (aka associative array), we have to rectify this here
126
		$argumentsToParserFunction = array();
127
		foreach ( $parameters as $key => $value ) {
128
			if ( !is_int($key) && !preg_match('/[0-9]+/', $key) ) {
129
				$value = $key . '=' . $value;
130
			}
131
			$argumentsToParserFunction[] = $value;
132
		}
133
134
		# prepare setParserFunction object
135
		$parserFunctionFactory = new ParserFunctionFactory( $parser );
136
		$setParserFunction = $parserFunctionFactory->newSetParserFunction( $parser );
137
138
		# call parse on setParserFunction
139
		$parserFunctionCallResult = $setParserFunction->parse(
140
			ParameterProcessorFactory::newFromArray( $argumentsToParserFunction )
141
		);
142
143
		# get result
144
		if ( is_array($parserFunctionCallResult) ) {
145
			$result = $parserFunctionCallResult[0];
146
			$noParse = isset($parserFunctionCallResult['noparse']) ? $parserFunctionCallResult['noparse'] : true;
147
		} else {
148
			$result = $parserFunctionCallResult;
149
			$noParse = true;
150
		}
151
152
		if ( ! $noParse ) {
153
			$result = $parser->recursiveTagParseFully( $result );
154
		}
155
		$result = trim($result);
156
157
		if ( strlen ($result) ) {
158
			# if result a non empty string, assume an error message
159
			return array( [ 1 => false, self::SMW_ERROR_FIELD => preg_replace('/<[^>]+>/', '', $result) ] );
160
		} else {
161
			# on success, return true
162
			return array( 1 => true );
163
		}
164
	}
165
}
166