-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQueryEncoder.php
More file actions
91 lines (70 loc) · 2.06 KB
/
QueryEncoder.php
File metadata and controls
91 lines (70 loc) · 2.06 KB
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
<?php
namespace SEQL;
use SMWQuery as Query;
/**
* @license GNU GPL v2+
* @since 1.0
*
* @author mwjames
*/
class QueryEncoder {
/**
* @since 1.0
*
* @param Query $query
*
* @return string
*/
public static function rawUrlEncode( Query $query ) {
return rawurlencode( self::encode( $query ) );
}
/**
* @since 1.0
*
* @param Query $query
*
* @return string
*/
public static function encode( Query $query ) {
$serialized = array();
$serialized['conditions'] = $query->getQueryString();
$serialized['parameters'] = array(
'limit=' . $query->getLimit(),
'offset=' . $query->getOffset(),
'mainlabel=' . $query->getMainlabel(),
// 'source=' . $query->getQuerySource()
);
list( $serialized['sort'], $serialized['order'] ) = self::doSerializeSortKeys( $query );
$serialized['printouts'] = self::doSerializePrintouts( $query );
$encoded = $serialized['conditions'] . '|' .
( $serialized['printouts'] !== array() ? implode( '|', $serialized['printouts'] ) . '|' : '' ) .
implode( '|', $serialized['parameters'] ) .
( $serialized['sort'] !== array() ? '|sort=' . implode( ',', $serialized['sort'] ) : '' ) .
( $serialized['order'] !== array() ? '|order=' . implode( ',', $serialized['order'] ) : '' );
return $encoded;
}
private static function doSerializePrintouts( $query ) {
$printouts = array();
foreach ( $query->getExtraPrintouts() as $printout ) {
$serialization = $printout->getSerialisation();
if ( $serialization !== '?#' && $serialization !== '' ) {
// #show adds an extra = at the end which is interpret as
// requesting an empty result hence it is removed
$printouts[] = substr( $serialization, -1 ) === '=' ? substr( $serialization, 0, -1 ) : $serialization;
}
}
return $printouts;
}
private static function doSerializeSortKeys( $query ) {
$sort = array();
$order = array();
foreach ( $query->getSortKeys() as $key => $value ) {
if ( $key === '' ) {
continue;
}
$sort[] = $key;
$order[] = strtolower( $value );
}
return array( $sort, $order );
}
}