00001 <?php
00014 class SMWJSONResultPrinter extends SMWResultPrinter {
00015
00016 protected $types = array( '_wpg' => 'text', '_num' => 'number', '_dat' => 'date', '_geo' => 'text', '_str' => 'text' );
00017
00018 public function getMimeType( $res ) {
00019 return 'application/JSON';
00020 }
00021
00022 public function getFileName( $res ) {
00023 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) !== '' ) {
00024 return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.json';
00025 } else {
00026 return 'result.json';
00027 }
00028 }
00029
00030 public function getQueryMode( $context ) {
00031 return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
00032 }
00033
00034 public function getName() {
00035 return wfMsg( 'smw_printername_json' );
00036 }
00037
00038 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00039 global $wgServer, $wgScriptPath;
00040 if ( $outputmode == SMW_OUTPUT_FILE ) {
00041 $itemstack = array();
00042 $propertystack = array();
00043
00044
00045 foreach ( $res->getPrintRequests() as $pr ) {
00046 if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
00047 if ( array_key_exists( $pr->getTypeID(), $this->types ) ) {
00048 $propertystack[] = '"' . str_replace( " ", "_", strtolower( $pr->getLabel() ) ) . '" : { "valueType": "' . $this->types[$pr->getTypeID()] . '" }';
00049 } else {
00050 $propertystack[] = '"' . str_replace( " ", "_", strtolower( $pr->getLabel() ) ) . '" : { "valueType": "text" }';
00051 }
00052 }
00053 }
00054 $properties = "\"properties\": {\n\t\t" . implode( ",\n\t\t", $propertystack ) . "\n\t}";
00055
00056
00057 while ( ( $row = $res->getNext() ) !== false ) {
00058 $rowsubject = false;
00059 $valuestack = array();
00060 $addedLabel = false;
00061
00062 foreach ( $row as $field ) {
00063 $pr = $field->getPrintRequest();
00064
00065 if ( $rowsubject === false && !$addedLabel ) {
00066 $valuestack[] = '"label": "' . $field->getResultSubject()->getTitle()->getFullText() . '"';
00067 $addedLabel = true;
00068 }
00069
00070 if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
00071 $values = array();
00072 $jsonObject = array();
00073
00074 while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
00075 switch ( $dataValue->getTypeID() ) {
00076 case '_geo':
00077 $jsonObject[] = $dataValue->getDataItem()->getCoordinateSet();
00078 $values[] = FormatJson::encode( $dataValue->getDataItem()->getCoordinateSet() );
00079 break;
00080 case '_num':
00081 $jsonObject[] = $dataValue->getDataItem()->getNumber();
00082 break;
00083 case '_dat':
00084 $jsonObject[] =
00085 $dataValue->getYear() . '-' .
00086 str_pad( $dataValue->getMonth(), 2, '0', STR_PAD_LEFT ) . '-' .
00087 str_pad( $dataValue->getDay(), 2, '0', STR_PAD_LEFT ) . ' ' .
00088 $dataValue->getTimeString();
00089 break;
00090 default:
00091 $jsonObject[] = $dataValue->getShortText( $outputmode, null );
00092 }
00093 }
00094
00095 if ( !is_array( $jsonObject ) || count( $jsonObject ) > 0 ) {
00096 $valuestack[] =
00097 '"' . str_replace( ' ', '_', strtolower( $pr->getLabel() ) )
00098 . '": ' . FormatJson::encode( $jsonObject ) . '';
00099 }
00100 }
00101 }
00102
00103 if ( $rowsubject !== false ) {
00104 $valuestack[] = '"uri" : "' . $wgServer . $wgScriptPath . '/index.php?title=' . $rowsubject->getPrefixedText() . '"';
00105 $page_cats = smwfGetStore()->getPropertyValues( $rowsubject, new SMWDIProperty( '_INST' ) );
00106
00107 if ( count( $page_cats ) > 0 ) {
00108 $valuestack[] = '"type" : "' . reset($page_cats)->getShortHTMLText() . '"';
00109 }
00110 }
00111
00112
00113 $itemstack[] = "\t{\n\t\t\t" . implode( ",\n\t\t\t", $valuestack ) . "\n\t\t}";
00114 }
00115
00116 $items = "\"items\": [\n\t" . implode( ",\n\t", $itemstack ) . "\n\t]";
00117
00118
00119 if ( array_key_exists( 'callback', $this->params ) ) {
00120 $result = htmlspecialchars( $this->params['callback'] ) . "({\n\t" . $properties . ",\n\t" . $items . "\n})";
00121 } else {
00122 $result = "{\n\t" . $properties . ",\n\t" . $items . "\n}";
00123 }
00124
00125 } else {
00126 if ( $this->getSearchLabel( $outputmode ) ) {
00127 $label = $this->getSearchLabel( $outputmode );
00128 } else {
00129 $label = wfMsgForContent( 'smw_json_link' );
00130 }
00131
00132 $link = $res->getQueryLink( $label );
00133 if ( array_key_exists( 'callback', $this->params ) ) {
00134 $link->setParameter( htmlspecialchars( $this->params['callback'] ), 'callback' );
00135 }
00136
00137 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) !== '' ) {
00138 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
00139 }
00140
00141 if ( array_key_exists( 'limit', $this->params ) ) {
00142 $link->setParameter( htmlspecialchars( $this->params['limit'] ), 'limit' );
00143 }
00144
00145 $link->setParameter( 'json', 'format' );
00146 $result = $link->getText( $outputmode, $this->mLinker );
00147
00148
00149 $this->isHTML = $outputmode == SMW_OUTPUT_HTML;
00150 }
00151
00152 return $result;
00153 }
00154
00155 public function getParameters() {
00156 return array_merge( parent::getParameters(), $this->exportFormatParameters() );
00157 }
00158
00159 }