1 | <?php |
||
20 | class LuaAskResultProcessor { |
||
21 | |||
22 | /** |
||
23 | * Holds all possible representations of "true" in this smw instance |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $msgTrue; |
||
28 | |||
29 | /** |
||
30 | * This counter serves as fallback, if no label for printout is specified |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | private $numericIndex; |
||
35 | |||
36 | /** |
||
37 | * Holds the query result for this object |
||
38 | * |
||
39 | * @var QueryResult |
||
40 | */ |
||
41 | private $queryResult; |
||
42 | |||
43 | /** |
||
44 | * LuaAskResultProcessor constructor. |
||
45 | * |
||
46 | * @param QueryResult $queryResult |
||
47 | */ |
||
48 | 16 | public function __construct( QueryResult $queryResult ) { |
|
53 | |||
54 | /** |
||
55 | * Extracts the data in {@see $queryResult} and returns it as a table |
||
56 | * usable in lua context |
||
57 | * |
||
58 | * @uses getDataFromQueryResultRow |
||
59 | * |
||
60 | * @return array|null |
||
61 | */ |
||
62 | 4 | public function getProcessedResult() { |
|
63 | |||
64 | 4 | $result = null; |
|
65 | |||
66 | 4 | if ( $this->queryResult->getCount() ) { |
|
67 | |||
68 | 4 | $result = []; |
|
69 | |||
70 | 4 | while ( $resultRow = $this->queryResult->getNext() ) { |
|
71 | 4 | $result[] = $this->getDataFromQueryResultRow( $resultRow ); |
|
72 | } |
||
73 | } |
||
74 | |||
75 | 4 | return $result; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Extracts the data of a single result row in the {@see $queryResult} |
||
80 | * and returns it as a table usable in lua context |
||
81 | * |
||
82 | * @param array $resultRow result row as an array of {@see ResultArray} objects |
||
83 | * |
||
84 | * @uses getDataFromResultArray |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 5 | public function getDataFromQueryResultRow( array $resultRow ) { |
|
89 | |||
90 | 5 | $rowData = []; |
|
91 | |||
92 | /** @var ResultArray $resultArray */ |
||
93 | 5 | foreach ( $resultRow as $resultArray ) { |
|
94 | |||
95 | // get key and data |
||
96 | 5 | list ( $key, $data ) = $this->getDataFromResultArray( $resultArray ); |
|
97 | |||
98 | 5 | $rowData[$key] = $data; |
|
99 | } |
||
100 | 5 | return $rowData; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Extracts the data of a single printRequest / query field |
||
105 | * |
||
106 | * @param ResultArray $resultArray |
||
107 | * |
||
108 | * @uses getKeyFromPrintRequest, getValueFromDataValue, extractLuaDataFromDVData |
||
109 | * |
||
110 | * @return array array ( int|string, array ) |
||
111 | */ |
||
112 | 6 | public function getDataFromResultArray( ResultArray $resultArray ) { |
|
113 | |||
114 | // first, extract the key (label), if any |
||
115 | 6 | $key = $this->getKeyFromPrintRequest( $resultArray->getPrintRequest() ); |
|
116 | |||
117 | 6 | $resultArrayData = []; |
|
118 | |||
119 | // then get all data that is stored in this printRequest / query field |
||
120 | /** @var DataValue $dataValue */ |
||
121 | 6 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { |
|
122 | |||
123 | 6 | $resultArrayData[] = $this->getValueFromDataValue( $dataValue ); |
|
124 | } |
||
125 | |||
126 | 6 | $resultArrayData = $this->extractLuaDataFromDVData( $resultArrayData ); |
|
127 | |||
128 | 6 | return [ $key, $resultArrayData ]; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Takes an smw query print request and tries to retrieve the label |
||
133 | * falls back to {@see getNumericIndex} if none was found |
||
134 | * |
||
135 | * @param PrintRequest $printRequest |
||
136 | * |
||
137 | * @uses getNumericIndex |
||
138 | * |
||
139 | * @return int|string |
||
140 | */ |
||
141 | 7 | public function getKeyFromPrintRequest( PrintRequest $printRequest ) { |
|
149 | |||
150 | /** |
||
151 | * Extracts the data of a single value of the current printRequest / query field |
||
152 | * The value is formatted according to the type of the property |
||
153 | * |
||
154 | * @param DataValue $dataValue |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | 10 | public function getValueFromDataValue( DataValue $dataValue ) { |
|
159 | |||
160 | 10 | switch ( $dataValue->getTypeID() ) { |
|
161 | 10 | case '_boo': |
|
162 | // boolean value found, convert it |
||
163 | 3 | $value = in_array( strtolower( $dataValue->getWikiValue() ), $this->msgTrue ); |
|
164 | 3 | break; |
|
165 | 9 | case '_num': |
|
166 | // number value found |
||
167 | /** @var \SMWNumberValue $dataValue */ |
||
168 | 2 | $value = $dataValue->getNumber(); |
|
169 | 2 | $value = ( $value == (int)$value ) ? intval( $value ) : $value; |
|
170 | 2 | break; |
|
171 | default: |
||
172 | # FIXME ignores parameter link=none|subject |
||
173 | 8 | $value = $dataValue->getShortText( SMW_OUTPUT_WIKI, new \Linker() ); |
|
174 | } |
||
175 | |||
176 | 10 | return $value; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Takes an array of data fields and returns either null, a single value or a lua table. |
||
181 | * |
||
182 | * @param array $resultArrayData |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | 9 | public function extractLuaDataFromDVData( $resultArrayData ) { |
|
201 | |||
202 | /** |
||
203 | * Returns current numeric index (and increases it afterwards) |
||
204 | * |
||
205 | * @uses $numericIndex |
||
206 | * |
||
207 | * @return int |
||
208 | */ |
||
209 | 2 | public function getNumericIndex() { |
|
212 | } |
||
213 |