jenkins-bot has submitted this change and it was merged.
Change subject: SMW\JSONResultPrinter delete all the mumbo jumbo
......................................................................
SMW\JSONResultPrinter delete all the mumbo jumbo
Make it clean and lean and take off all the bloat that is unnessary
Change-Id: I3ec1e29fd65624259a0803d9e040fab164ebfc34
---
M SemanticMediaWiki.settings.php
M includes/Aliases.php
M includes/Setup.php
M includes/queryprinters/JSONResultPrinter.php
4 files changed, 23 insertions(+), 222 deletions(-)
Approvals:
Jeroen De Dauw: Looks good to me, approved
jenkins-bot: Verified
diff --git a/SemanticMediaWiki.settings.php b/SemanticMediaWiki.settings.php
index ac2a968..e31ed4d 100644
--- a/SemanticMediaWiki.settings.php
+++ b/SemanticMediaWiki.settings.php
@@ -353,7 +353,7 @@
'feed' => 'SMW\FeedResultPrinter',
'csv' => 'SMWCsvResultPrinter',
'dsv' => 'SMWDSVResultPrinter',
- 'json' => 'SMWJSONResultPrinter',
+ 'json' => 'SMW\JSONResultPrinter',
'rdf' => 'SMWRDFResultPrinter'
);
##
diff --git a/includes/Aliases.php b/includes/Aliases.php
index ced81ce..b6bb1fa 100644
--- a/includes/Aliases.php
+++ b/includes/Aliases.php
@@ -28,4 +28,6 @@
* @author Jeroen De Dauw < [email protected] >
*/
-abstract class SMWResultPrinter extends SMW\ResultPrinter {}
\ No newline at end of file
+abstract class SMWResultPrinter extends SMW\ResultPrinter {}
+
+class SMWJSONResultPrinter extends SMWExportPrinter {}
\ No newline at end of file
diff --git a/includes/Setup.php b/includes/Setup.php
index 297ab27..66f1ed1 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -135,8 +135,8 @@
$wgAutoloadClasses['SMWListResultPrinter'] = $qpDir .
'ListResultPrinter.php'; // deprecated in 1.9
$wgAutoloadClasses['SMW\ListResultPrinter'] = $qpDir .
'ListResultPrinter.php';
$wgAutoloadClasses['SMW\FeedResultPrinter'] = $qpDir .
'FeedResultPrinter.php';
- $wgAutoloadClasses['SMWJSONResultPrinter'] = $qpDir .
'JSONResultPrinter.php';
- $wgAutoloadClasses['SMWJSON'] = $qpDir .
'JSONResultPrinter.php';
+ $wgAutoloadClasses['SMWJSONResultPrinter'] = $qpDir .
'JSONResultPrinter.php'; // deprecated in 1.9
+ $wgAutoloadClasses['SMW\JSONResultPrinter'] = $qpDir .
'JSONResultPrinter.php';
// Data items
$diDir = $smwgIP . 'includes/dataitems/';
diff --git a/includes/queryprinters/JSONResultPrinter.php
b/includes/queryprinters/JSONResultPrinter.php
index 27554ad..692e9cd 100644
--- a/includes/queryprinters/JSONResultPrinter.php
+++ b/includes/queryprinters/JSONResultPrinter.php
@@ -1,7 +1,13 @@
<?php
+namespace SMW;
+use SMWQueryResult, SMWQueryProcessor, SMWQuery;
+use FormatJSON;
+
/**
* Print links to JSON files representing query results.
+ *
+ * @see http://www.semantic-mediawiki.org/wiki/Help:JSON_format
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,7 +24,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
- * @see http://www.semantic-mediawiki.org/wiki/Help:JSON_format
* @since 1.5.3
*
* @file SWM_QP_JSONlink.php
@@ -29,7 +34,7 @@
* @author Jeroen De Dauw < [email protected] >
* @author Fabian Howahl
*/
-class SMWJSONResultPrinter extends SMWExportPrinter {
+class JSONResultPrinter extends \SMWExportPrinter {
/**
* Returns human readable label for this printer
@@ -99,17 +104,13 @@
return $this->params['default'] !== '' ?
$this->params['default'] : '';
}
- // JSON instance
- $json = new SMWJSON( $res );
-
- // JSON export type
- if ( $this->params['syntax'] === 'obsolete' ) {
- $result = $this->getObsoleteJSON( $res ,
$outputmode );
- } elseif ( $this->params['syntax'] === 'basic' ) {
- $result = $json->getEncoding(
$this->params['syntax'] , $this->params['prettyprint'] );
- } else {
- $result = $json->getEncoding(
$this->params['syntax'] , $this->params['prettyprint'] );
- }
+ // Serialize queryResult
+ $result = FormatJSON::encode(
+ array_merge(
+ $res->serializeToArray(),
+ array ( 'rows' => $res->getCount() )
+ ), $this->params['prettyprint']
+ );
} else {
// Create a link that points to the JSON file
@@ -120,102 +121,6 @@
}
return $result;
- }
-
- /**
- * Compatibility layer for obsolete JSON format
- *
- * @since 1.8
- * @deprecated This method will be removed in 1.10
- *
- * @param SMWQueryResult $res
- * @param $outputmode integer
- *
- * @return string
- */
- private function getObsoleteJSON( SMWQueryResult $res, $outputmode ){
- wfDeprecated( __METHOD__, '1.8' );
-
- $types = array( '_wpg' => 'text', '_num' => 'number', '_dat' =>
'date', '_geo' => 'text', '_str' => 'text' );
-
- $itemstack = array(); // contains Items for the items section
- $propertystack = array(); // contains Properties for the
property section
-
- // generate property section
- foreach ( $res->getPrintRequests() as $pr ) {
- if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
- if ( array_key_exists( $pr->getTypeID(), $types
) ) {
- $propertystack[] = '"' . str_replace( "
", "_", strtolower( $pr->getLabel() ) ) . '" : { "valueType": "' .
$types[$pr->getTypeID()] . '" }';
- } else {
- $propertystack[] = '"' . str_replace( "
", "_", strtolower( $pr->getLabel() ) ) . '" : { "valueType": "text" }';
- }
- }
- }
- $properties = "\"properties\": {\n\t\t" . implode( ",\n\t\t",
$propertystack ) . "\n\t}";
-
- // generate items section
- while ( ( /* array of SMWResultArray */ $row = $res->getNext()
) !== false ) {
- $rowsubject = false; // the wiki page value that this
row is about
- $valuestack = array(); // contains Property-Value pairs
to characterize an Item
- $addedLabel = false;
-
- foreach ( $row as /* SMWResultArray */ $field ) {
- $pr = $field->getPrintRequest();
-
- if ( $rowsubject === false && !$addedLabel ) {
- $valuestack[] = '"label": "' .
$field->getResultSubject()->getTitle()->getFullText() . '"';
- $addedLabel = true;
- }
-
- if ( $pr->getMode() !=
SMWPrintRequest::PRINT_THIS ) {
- $values = array();
- $jsonObject = array();
-
- while ( ( $dataValue =
$field->getNextDataValue() ) !== false ) {
- switch (
$dataValue->getTypeID() ) {
- case '_geo':
- $jsonObject[] =
$dataValue->getDataItem()->getCoordinateSet();
- $values[] =
FormatJson::encode( $dataValue->getDataItem()->getCoordinateSet() );
- break;
- case '_num':
- $jsonObject[] =
$dataValue->getDataItem()->getNumber();
- break;
- case '_dat':
- $jsonObject[] =
-
$dataValue->getYear() . '-' .
-
str_pad( $dataValue->getMonth(), 2, '0', STR_PAD_LEFT ) . '-' .
-
str_pad( $dataValue->getDay(), 2, '0', STR_PAD_LEFT ) . ' ' .
-
$dataValue->getTimeString();
- break;
- default:
- $jsonObject[] =
$dataValue->getShortText( $outputmode, null );
- }
- }
-
- if ( !is_array( $jsonObject ) || count(
$jsonObject ) > 0 ) {
- $valuestack[] =
- '"' . str_replace( ' ',
'_', strtolower( $pr->getLabel() ) )
- . '": ' .
FormatJson::encode( $jsonObject ) . '';
- }
- }
- }
-
- if ( $rowsubject !== false ) { // stuff in the page URI
and some category data
- $valuestack[] = '"uri" : "' . $wgServer .
$wgScriptPath . '/index.php?title=' . $rowsubject->getPrefixedText() . '"';
- $page_cats = smwfGetStore()->getPropertyValues(
$rowsubject, new SMWDIProperty( '_INST' ) ); // TODO: set limit to 1 here
-
- if ( count( $page_cats ) > 0 ) {
- $valuestack[] = '"type" : "' .
reset($page_cats)->getShortHTMLText() . '"';
- }
- }
-
- // create property list of item
- $itemstack[] = "\t{\n\t\t\t" . implode( ",\n\t\t\t",
$valuestack ) . "\n\t\t}";
- }
-
- $items = "\"items\": [\n\t" . implode( ",\n\t", $itemstack ) .
"\n\t]";
-
- return "{\n\t" . $properties . ",\n\t" . $items . "\n}";
}
/**
@@ -233,13 +138,6 @@
$params['searchlabel']->setDefault( wfMessage( 'smw_json_link'
)->text() );
$params['limit']->setDefault( 100 );
- $params['syntax'] = array(
- 'type' => 'string',
- 'default' => 'complete',
- 'message' => 'smw-paramdesc-jsonsyntax',
- 'values' => array( 'obsolete', 'basic', 'standard' ),
- );
-
$params['prettyprint'] = array(
'type' => 'boolean',
'default' => '',
@@ -251,107 +149,8 @@
}
/**
- * Class representing SMW JSON objects
+ * SMWJSONResultPrinter
*
- * @since 1.8
- *
- * @return array of SMWJSON|array
+ * @deprecated since SMW 1.9
*/
-class SMWJSON {
- protected $results;
- protected $count;
-
- /**
- * Constructor
- *
- * @param SMWQueryResult $res
- */
- public function __construct( SMWQueryResult $res ){
- $this->results = $res;
- $this->count = $res->getCount();
- }
-
- /**
- * Standard SMW JSON layer
- *
- * The output structure resembles that of the api json format structure
- *
- * @since 1.8
- *
- * @return array
- */
- public function getSerialization() {
- return array_merge( SMWDISerializer::getSerializedQueryResult(
$this->results ), array ( 'rows' => $this->count ) );
- }
-
- /**
- * Basic SMW JSON layer
- *
- * This is a convenience layer which is eliminating some overhead from
the
- * standard SMW JSON
- *
- * @since 1.8
- *
- * @return array
- */
- public function getBasicSerialization( ) {
- $results = array();
- $printRequests = array();
-
- foreach ( $this->results->getPrintRequests() as /*
SMWPrintRequest */ $printRequest ) {
- $printRequests[$printRequest->getLabel()] = array(
- 'label' => $printRequest->getLabel(),
- 'typeid' => $printRequest->getTypeID()
- );
- }
-
- foreach ( $this->results->getResults() as /* SMWDIWikiPage */
$diWikiPage ) {
- $result = array( );
-
- foreach ( $this->results->getPrintRequests() as /*
SMWPrintRequest */ $printRequest ) {
- $serializationItems = array();
- $resultAarray = new SMWResultArray(
$diWikiPage, $printRequest, $this->results->getStore() );
-
- if ( $printRequest->getMode() ===
SMWPrintRequest::PRINT_THIS ) {
- $dataItems =
$resultAarray->getContent();
- $fulltext =
SMWDISerializer::getSerialization( array_shift( $dataItems ) );
- $result += array ( 'label' =>
$fulltext["fulltext"] );
- }
- else {
- $serializationItems = array_map(
- array( 'SMWDISerializer',
'getSerialization' ),
- $resultAarray->getContent()
- );
-
- $type = $printRequest->getTypeID();
- $items = array();
-
- foreach ( $serializationItems as $item
) {
- if ( $type == "_wpg" ) {
- $items[] =
$item["fulltext"];
- } else {
- $items[] =
$item;
- }
- }
- $result[$printRequest->getLabel()] =
$items;
- }
- }
- $results[$diWikiPage->getTitle()->getFullText()] =
$result;
- }
- return array( 'printrequests' => $printRequests, 'results' =>
$results, 'rows' => $this->count );
- }
-
- /**
- * JSON Encoding
- *
- * @since 1.8
- *
- * @param $syntax string
- * @param $isPretty boolean prettify JSON output
- *
- * @return string
- */
- public function getEncoding( $syntax = '' , $isPretty = false ){
- return FormatJSON::encode( $syntax === 'basic' ?
$this->getBasicSerialization() : $this->getSerialization(), $isPretty );
- }
-}
+class_alias( 'SMW\JSONResultPrinter', 'SMWJSONResultPrinter' );
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/50272
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I3ec1e29fd65624259a0803d9e040fab164ebfc34
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits