Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/50272


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/Setup.php
A includes/queryprinters/JSONResultPrinter.php
D includes/queryprinters/SMW_QP_JSONlink.php
4 files changed, 159 insertions(+), 360 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/72/50272/1

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/Setup.php b/includes/Setup.php
index 57ff719..66f1ed1 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -128,8 +128,6 @@
        $wgAutoloadClasses['SMWEmbeddedResultPrinter']  = $qpDir . 
'SMW_QP_Embedded.php';
        $wgAutoloadClasses['SMWCsvResultPrinter']       = $qpDir . 
'SMW_QP_CSV.php';
        $wgAutoloadClasses['SMWDSVResultPrinter']       = $qpDir . 
'SMW_QP_DSV.php';
-       $wgAutoloadClasses['SMWJSONResultPrinter']      = $qpDir . 
'SMW_QP_JSONlink.php';
-       $wgAutoloadClasses['SMWJSON']                   = $qpDir . 
'SMW_QP_JSONlink.php';
        $wgAutoloadClasses['SMWRDFResultPrinter']       = $qpDir . 
'SMW_QP_RDF.php';
        $wgAutoloadClasses['SMWResultPrinter']          = $qpDir . 
'ResultPrinter.php'; // deprecated in 1.9
        $wgAutoloadClasses['SMW\ResultPrinter']         = $qpDir . 
'ResultPrinter.php';
@@ -137,6 +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'; // 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
new file mode 100644
index 0000000..f6a3bcf
--- /dev/null
+++ b/includes/queryprinters/JSONResultPrinter.php
@@ -0,0 +1,156 @@
+<?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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.5.3
+ *
+ * @file SWM_QP_JSONlink.php
+ * @ingroup SMWQuery
+ *
+ * @licence GNU GPL v2 or later
+ * @author mwjames
+ * @author Jeroen De Dauw < [email protected] >
+ * @author Fabian Howahl
+ */
+class JSONResultPrinter extends \SMWExportPrinter {
+
+       /**
+        * Returns human readable label for this printer
+        *
+        * @return string
+        */
+       public function getName() {
+               return $this->getContext()->msg( 'smw_printername_json' 
)->text();
+       }
+
+       /**
+        * @see SMWIExportPrinter::getMimeType
+        *
+        * @since 1.8
+        *
+        * @param SMWQueryResult $queryResult
+        *
+        * @return string
+        */
+       public function getMimeType( SMWQueryResult $queryResult ) {
+               return 'application/JSON';
+       }
+
+       /**
+        * @see SMWIExportPrinter::getFileName
+        *
+        * @since 1.8
+        *
+        * @param SMWQueryResult $queryResult
+        *
+        * @return string|boolean
+        */
+       public function getFileName( SMWQueryResult $queryResult ) {
+               if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) !== '' ) {
+                       return str_replace( ' ', '_', $this->getSearchLabel( 
SMW_OUTPUT_WIKI ) ) . '.json';
+               } else {
+                       return 'result.json';
+               }
+       }
+
+       /**
+        * File exports use MODE_INSTANCES on special pages (so that instances 
are
+        * retrieved for the export) and MODE_NONE otherwise (displaying just a 
download link).
+        *
+        * @param $context
+        *
+        * @return integer
+        */
+       public function getQueryMode( $context ) {
+               return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? 
SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
+       }
+
+       /**
+        * Returns a filename that is to be sent to the caller
+        *
+        * @param SMWQueryResult $res
+        * @param $outputmode integer
+        *
+        * @return string
+        */
+       protected function getResultText( SMWQueryResult $res, $outputmode ) {
+
+               if ( $outputmode == SMW_OUTPUT_FILE ) {
+
+                       // No results, just bailout
+                       if ( $res->getCount() == 0 ){
+                               return $this->params['default'] !== '' ? 
$this->params['default'] : '';
+                       }
+
+                       // 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
+                       $result = $this->getLink( $res, $outputmode )->getText( 
$outputmode, $this->mLinker );
+
+                       // Code can be viewed as HTML if requested, no more 
parsing needed
+                       $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
+               }
+
+               return $result;
+       }
+
+       /**
+        * @see SMWResultPrinter::getParamDefinitions
+        *
+        * @since 1.8
+        *
+        * @param $definitions array of IParamDefinition
+        *
+        * @return array of IParamDefinition|array
+        */
+       public function getParamDefinitions( array $definitions ) {
+               $params = parent::getParamDefinitions( $definitions );
+
+               $params['searchlabel']->setDefault( wfMessage( 'smw_json_link' 
)->text() );
+               $params['limit']->setDefault( 100 );
+
+               $params['prettyprint'] = array(
+                       'type' => 'boolean',
+                       'default' => '',
+                       'message' => 'smw-paramdesc-prettyprint',
+               );
+
+               return $params;
+       }
+}
+
+/**
+ * SMWListResultPrinter
+ *
+ * @deprecated since SMW 1.9
+ */
+class_alias( 'SMW\JSONResultPrinter', 'SMWJSONResultPrinter' );
\ No newline at end of file
diff --git a/includes/queryprinters/SMW_QP_JSONlink.php 
b/includes/queryprinters/SMW_QP_JSONlink.php
deleted file mode 100644
index 27554ad..0000000
--- a/includes/queryprinters/SMW_QP_JSONlink.php
+++ /dev/null
@@ -1,357 +0,0 @@
-<?php
-
-/**
- * Print links to JSON files representing query results.
- *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 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
- * @ingroup SMWQuery
- *
- * @licence GNU GPL v2 or later
- * @author mwjames
- * @author Jeroen De Dauw < [email protected] >
- * @author Fabian Howahl
- */
-class SMWJSONResultPrinter extends SMWExportPrinter {
-
-       /**
-        * Returns human readable label for this printer
-        *
-        * @return string
-        */
-       public function getName() {
-               return wfMessage( 'smw_printername_json' )->text();
-       }
-
-       /**
-        * @see SMWIExportPrinter::getMimeType
-        *
-        * @since 1.8
-        *
-        * @param SMWQueryResult $queryResult
-        *
-        * @return string
-        */
-       public function getMimeType( SMWQueryResult $queryResult ) {
-               return 'application/JSON';
-       }
-
-       /**
-        * @see SMWIExportPrinter::getFileName
-        *
-        * @since 1.8
-        *
-        * @param SMWQueryResult $queryResult
-        *
-        * @return string|boolean
-        */
-       public function getFileName( SMWQueryResult $queryResult ) {
-               if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) !== '' ) {
-                       return str_replace( ' ', '_', $this->getSearchLabel( 
SMW_OUTPUT_WIKI ) ) . '.json';
-               } else {
-                       return 'result.json';
-               }
-       }
-
-       /**
-        * File exports use MODE_INSTANCES on special pages (so that instances 
are
-        * retrieved for the export) and MODE_NONE otherwise (displaying just a 
download link).
-        *
-        * @param $context
-        *
-        * @return integer
-        */
-       public function getQueryMode( $context ) {
-               return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? 
SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
-       }
-
-       /**
-        * Returns a filename that is to be sent to the caller
-        *
-        * @param SMWQueryResult $res
-        * @param $outputmode integer
-        *
-        * @return string
-        */
-       protected function getResultText( SMWQueryResult $res, $outputmode ) {
-
-               if ( $outputmode == SMW_OUTPUT_FILE ) {
-
-                       // No results, just bailout
-                       if ( $res->getCount() == 0 ){
-                               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'] );
-                       }
-
-               } else {
-                       // Create a link that points to the JSON file
-                       $result = $this->getLink( $res, $outputmode )->getText( 
$outputmode, $this->mLinker );
-
-                       // Code can be viewed as HTML if requested, no more 
parsing needed
-                       $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
-               }
-
-               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}";
-       }
-
-       /**
-        * @see SMWResultPrinter::getParamDefinitions
-        *
-        * @since 1.8
-        *
-        * @param $definitions array of IParamDefinition
-        *
-        * @return array of IParamDefinition|array
-        */
-       public function getParamDefinitions( array $definitions ) {
-               $params = parent::getParamDefinitions( $definitions );
-
-               $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' => '',
-                       'message' => 'smw-paramdesc-prettyprint',
-               );
-
-               return $params;
-       }
-}
-
-/**
- * Class representing SMW JSON objects
- *
- * @since 1.8
- *
- * @return array of SMWJSON|array
- */
-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 );
-       }
-}

-- 
To view, visit https://gerrit.wikimedia.org/r/50272
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3ec1e29fd65624259a0803d9e040fab164ebfc34
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to