r89983 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89982‎ | r89983 | r89984 >
Date:17:58, 13 June 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
fixed compat probelem and some small issues with json printer
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_JSONlink.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_JSONlink.php
@@ -12,6 +12,7 @@
1313 * @ingroup SMWQuery
1414 */
1515 class SMWJSONResultPrinter extends SMWResultPrinter {
 16+
1617 protected $types = array( '_wpg' => 'text', '_num' => 'number', '_dat' => 'date', '_geo' => 'text', '_str' => 'text' );
1718
1819 public function getMimeType( $res ) {
@@ -54,43 +55,52 @@
5556 $properties = "\"properties\": {\n\t\t" . implode( ",\n\t\t", $propertystack ) . "\n\t}";
5657
5758 // generate items section
58 - while ( ( $row = $res->getNext() ) !== false ) {
 59+ while ( ( /* array of SMWResultArray */ $row = $res->getNext() ) !== false ) {
5960 $rowsubject = false; // the wiki page value that this row is about
6061 $valuestack = array(); // contains Property-Value pairs to characterize an Item
61 - foreach ( $row as $field ) {
 62+
 63+ foreach ( $row as /* SMWResultArray */ $field ) {
6264 $pr = $field->getPrintRequest();
 65+
6366 if ( $rowsubject === false ) {
64 - $rowsubject = $field->getResultSubject();
65 - $valuestack[] = '"label": "' . $rowsubject->getShortText( $outputmode, null ) . '"';
 67+ $valuestack[] = '"label": "' . $field->getResultSubject()->getTitle()->getFullText() . '"';
6668 }
 69+
6770 if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
6871 $values = array();
6972 $finalvalues = '';
 73+
7074 while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
7175 $finalvalues = '';
7276 switch ( $dataValue->getTypeID() ) {
7377 case '_geo':
74 - $values[] = "\"" . $dataValue->getWikiValue() . "\"";
 78+ $values[] = '"' . $dataValue->getWikiValue() . '"';
7579 break;
7680 case '_num':
77 - $values[] = "\"" . $dataValue->getValueKey() . "\"";
 81+ $values[] = $dataValue->getDataItem()->getNumber();
7882 break;
7983 case '_dat':
80 - $values[] = "\"" . $dataValue->getYear() . "-" . str_pad( $dataValue->getMonth(), 2, '0', STR_PAD_LEFT ) . "-" . str_pad( $dataValue->getDay(), 2, '0', STR_PAD_LEFT ) . " " . $dataValue->getTimeString() . "\"";
 84+ $values[] =
 85+ '"' . $dataValue->getYear() . '-' .
 86+ str_pad( $dataValue->getMonth(), 2, '0', STR_PAD_LEFT ) . '-' .
 87+ str_pad( $dataValue->getDay(), 2, '0', STR_PAD_LEFT ) . ' ' .
 88+ $dataValue->getTimeString() . '"';
8189 break;
8290 default:
83 - $values[] = "\"" . $dataValue->getShortText( $outputmode, null ) . "\"";
 91+ $values[] = '"' . $dataValue->getShortText( $outputmode, null ) . '"';
8492 }
8593
8694 if ( sizeof( $values ) > 1 ) {
87 - $finalvalues = "[" . implode( ",", $values ) . "]";
 95+ $finalvalues = '[' . implode( ',', $values ) . ']';
8896 } else {
8997 $finalvalues = $values[0];
9098 }
9199 }
92 - if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( " ", "_", strtolower( $pr->getLabel() ) ) . '": ' . $finalvalues . '';
 100+
 101+ if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( ' ', '_', strtolower( $pr->getLabel() ) ) . '": ' . $finalvalues . '';
93102 }
94103 }
 104+
95105 if ( $rowsubject !== false ) { // stuff in the page URI and some category data
96106 $valuestack[] = '"uri" : "' . $wgServer . $wgScriptPath . '/index.php?title=' . $rowsubject->getPrefixedText() . '"';
97107 $page_cats = smwfGetStore()->getPropertyValues( $rowsubject, new SMWDIProperty( '_INST' ) ); // TODO: set limit to 1 here
@@ -119,16 +129,20 @@
120130 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
121131 $label = wfMsgForContent( 'smw_json_link' );
122132 }
 133+
123134 $link = $res->getQueryLink( $label );
124135 if ( array_key_exists( 'callback', $this->m_params ) ) {
125136 $link->setParameter( htmlspecialchars( $this->m_params['callback'] ), 'callback' );
126137 }
 138+
127139 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) { // used as a file name
128140 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
129141 }
 142+
130143 if ( array_key_exists( 'limit', $this->m_params ) ) {
131144 $link->setParameter( htmlspecialchars( $this->m_params['limit'] ), 'limit' );
132145 }
 146+
133147 $link->setParameter( 'json', 'format' );
134148 $result = $link->getText( $outputmode, $this->mLinker );
135149 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed