Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DISerializer.php |
— | — | @@ -0,0 +1,103 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for serializing SMWDataItem and SMWQueryResult objects to a context independent |
| 6 | + * object consisting of arrays and associative arrays, which can be fed |
| 7 | + * directly to json_encode, the MediaWiki API, and similar serializers. |
| 8 | + * |
| 9 | + * This class is distinct from SMWSerializer and the SMWExpData obejct |
| 10 | + * it takes, in that here semantic context is lost. |
| 11 | + * |
| 12 | + * @since 1.7 |
| 13 | + * |
| 14 | + * @file SMW_DISerializer.php |
| 15 | + * @ingroup SMW |
| 16 | + * |
| 17 | + * @licence GNU GPL v3+ |
| 18 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 19 | + */ |
| 20 | + |
| 21 | +class SMWDISerializer { |
| 22 | + |
| 23 | + /** |
| 24 | + * Get the serialization for the provided data item. |
| 25 | + * |
| 26 | + * @since 1.7 |
| 27 | + * |
| 28 | + * @param SMWDataItem $dataItem |
| 29 | + * |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + public static function getSerialization( SMWDataItem $dataItem ) { |
| 33 | + switch ( $dataItem->getDIType() ) { |
| 34 | + case SMWDataItem::TYPE_WIKIPAGE: |
| 35 | + $title = $dataItem->getTitle(); |
| 36 | + $result = array( |
| 37 | + 'fulltext' => $title->getFullText(), |
| 38 | + 'fullurl' => $title->getFullUrl(), |
| 39 | + ); |
| 40 | + break; |
| 41 | + case SMWDataItem::TYPE_NUMBER: |
| 42 | + $result = $dataItem->getNumber(); |
| 43 | + break; |
| 44 | + case SMWDataItem::TYPE_GEO: |
| 45 | + $result = $dataItem->getCoordinateSet(); |
| 46 | + break; |
| 47 | + case SMWDataItem::TYPE_TIME: |
| 48 | + $result = $dataItem->getMwTimestamp(); |
| 49 | + break; |
| 50 | + default: |
| 51 | + $result = $dataItem->getSerialization(); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + return $result; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the serialization for a SMWQueryResult object. |
| 60 | + * |
| 61 | + * @since 1.7 |
| 62 | + * |
| 63 | + * @param SMWQueryResult $result |
| 64 | + * |
| 65 | + * @return array |
| 66 | + */ |
| 67 | + public static function getSerializedQueryResult( SMWQueryResult $queryResult ) { |
| 68 | + $results = array(); |
| 69 | + $printRequests = array(); |
| 70 | + |
| 71 | + foreach ( $queryResult->getPrintRequests() as /* SMWPrintRequest */ $printRequest ) { |
| 72 | + $printRequests[] = array( |
| 73 | + 'label' => $printRequest->getLabel(), |
| 74 | + 'typeid' => $printRequest->getTypeID(), |
| 75 | + 'mode' => $printRequest->getMode(), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + foreach ( $queryResult->getResults() as /* SMWDIWikiPage */ $diWikiPage ) { |
| 80 | + $result = array( 'printeouts' => array() ); |
| 81 | + |
| 82 | + foreach ( $queryResult->getPrintRequests() as /* SMWPrintRequest */ $printRequest ) { |
| 83 | + $resultAarray = new SMWResultArray( $diWikiPage, $printRequest, $queryResult->getStore() ); |
| 84 | + |
| 85 | + if ( $printRequest->getMode() === SMWPrintRequest::PRINT_THIS ) { |
| 86 | + $dataItems = $resultAarray->getContent(); |
| 87 | + $result += self::getSerialization( array_shift( $dataItems ) ); |
| 88 | + } |
| 89 | + else { |
| 90 | + $result['printeouts'][$printRequest->getLabel()] = array_map( |
| 91 | + array( __class__, 'getSerialization' ), |
| 92 | + $resultAarray->getContent() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + $results[$diWikiPage->getTitle()->getFullText()] = $result; |
| 99 | + } |
| 100 | + |
| 101 | + return array( 'results' => $results, 'printrequests' => $printRequests ); |
| 102 | + } |
| 103 | + |
| 104 | +} |
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_DISerializer.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 105 | + native |
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php |
— | — | @@ -226,69 +226,15 @@ |
227 | 227 | return $result; |
228 | 228 | } |
229 | 229 | |
230 | | - public function getDataItemSerialization( SMWDataItem $dataItem ) { |
231 | | - switch ( $dataItem->getDIType() ) { |
232 | | - case SMWDataItem::TYPE_WIKIPAGE: |
233 | | - $title = $dataItem->getTitle(); |
234 | | - $result = array( |
235 | | - 'fulltext' => $title->getFullText(), |
236 | | - 'fullurl' => $title->getFullUrl(), |
237 | | - ); |
238 | | - break; |
239 | | - case SMWDataItem::TYPE_NUMBER: |
240 | | - $result = $dataItem->getNumber(); |
241 | | - break; |
242 | | - case SMWDataItem::TYPE_GEO: |
243 | | - $result = $dataItem->getCoordinateSet(); |
244 | | - break; |
245 | | - case SMWDataItem::TYPE_TIME: |
246 | | - $result = $dataItem->getMwTimestamp(); |
247 | | - break; |
248 | | - default: |
249 | | - $result = $dataItem->getSerialization(); |
250 | | - break; |
251 | | - } |
252 | | - |
253 | | - return $result; |
254 | | - } |
255 | | - |
| 230 | + /** |
| 231 | + * @see SMWDISerializer::getSerializedQueryResult |
| 232 | + * @since 1.7 |
| 233 | + * @return array |
| 234 | + */ |
256 | 235 | public function serializeToArray() { |
257 | | - $results = array(); |
258 | | - $printRequests = array(); |
259 | | - |
260 | | - foreach ( $this->mPrintRequests as /* SMWPrintRequest */ $printRequest ) { |
261 | | - $printRequests[] = array( |
262 | | - 'label' => $printRequest->getLabel(), |
263 | | - 'typeid' => $printRequest->getTypeID(), |
264 | | - 'mode' => $printRequest->getMode(), |
265 | | - ); |
266 | | - } |
267 | | - |
268 | | - foreach ( $this->mResults as /* SMWDIWikiPage */ $diWikiPage ) { |
269 | | - $result = array( 'printeouts' => array() ); |
270 | | - |
271 | | - foreach ( $this->mPrintRequests as /* SMWPrintRequest */ $printRequest ) { |
272 | | - $resultAarray = new SMWResultArray( $diWikiPage, $printRequest, $this->mStore ); |
273 | | - |
274 | | - if ( $printRequest->getMode() === SMWPrintRequest::PRINT_THIS ) { |
275 | | - $dataItems = $resultAarray->getContent(); |
276 | | - $result += $this->getDataItemSerialization( array_shift( $dataItems ) ); |
277 | | - } |
278 | | - else { |
279 | | - $result['printeouts'][$printRequest->getLabel()] = array_map( |
280 | | - array( __class__, 'getDataItemSerialization' ), |
281 | | - $resultAarray->getContent() |
282 | | - ); |
283 | | - } |
284 | | - |
285 | | - } |
286 | | - |
287 | | - $results[$diWikiPage->getTitle()->getFullText()] = $result; |
288 | | - } |
289 | | - |
290 | | - return array( 'results' => $results, 'printrequests' => $printRequests ); |
| 236 | + return SMWDISerializer::getSerializedQueryResult( $this ); |
291 | 237 | } |
292 | | - |
| 238 | + |
293 | 239 | } |
294 | 240 | |
295 | 241 | /** |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php |
— | — | @@ -180,6 +180,7 @@ |
181 | 181 | $incDir = $smwgIP . 'includes/'; |
182 | 182 | $wgAutoloadClasses['SMWCompatibilityHelpers'] = $incDir . 'SMW_CompatibilityHelpers.php'; |
183 | 183 | $wgAutoloadClasses['SMWDataValueFactory'] = $incDir . 'SMW_DataValueFactory.php'; |
| 184 | + $wgAutoloadClasses['SMWDISerializer'] = $incDir . 'SMW_DISerializer.php'; |
184 | 185 | $wgAutoloadClasses['SMWFactbox'] = $incDir . 'SMW_Factbox.php'; |
185 | 186 | $wgAutoloadClasses['SMWInfolink'] = $incDir . 'SMW_Infolink.php'; |
186 | 187 | $wgAutoloadClasses['SMWOutputs'] = $incDir . 'SMW_Outputs.php'; |