Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_JSONlink.php |
— | — | @@ -12,6 +12,7 @@ |
13 | 13 | * @ingroup SMWQuery |
14 | 14 | */ |
15 | 15 | class SMWJSONResultPrinter extends SMWResultPrinter { |
| 16 | + |
16 | 17 | protected $types = array( '_wpg' => 'text', '_num' => 'number', '_dat' => 'date', '_geo' => 'text', '_str' => 'text' ); |
17 | 18 | |
18 | 19 | public function getMimeType( $res ) { |
— | — | @@ -54,43 +55,52 @@ |
55 | 56 | $properties = "\"properties\": {\n\t\t" . implode( ",\n\t\t", $propertystack ) . "\n\t}"; |
56 | 57 | |
57 | 58 | // generate items section |
58 | | - while ( ( $row = $res->getNext() ) !== false ) { |
| 59 | + while ( ( /* array of SMWResultArray */ $row = $res->getNext() ) !== false ) { |
59 | 60 | $rowsubject = false; // the wiki page value that this row is about |
60 | 61 | $valuestack = array(); // contains Property-Value pairs to characterize an Item |
61 | | - foreach ( $row as $field ) { |
| 62 | + |
| 63 | + foreach ( $row as /* SMWResultArray */ $field ) { |
62 | 64 | $pr = $field->getPrintRequest(); |
| 65 | + |
63 | 66 | if ( $rowsubject === false ) { |
64 | | - $rowsubject = $field->getResultSubject(); |
65 | | - $valuestack[] = '"label": "' . $rowsubject->getShortText( $outputmode, null ) . '"'; |
| 67 | + $valuestack[] = '"label": "' . $field->getResultSubject()->getTitle()->getFullText() . '"'; |
66 | 68 | } |
| 69 | + |
67 | 70 | if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) { |
68 | 71 | $values = array(); |
69 | 72 | $finalvalues = ''; |
| 73 | + |
70 | 74 | while ( ( $dataValue = $field->getNextDataValue() ) !== false ) { |
71 | 75 | $finalvalues = ''; |
72 | 76 | switch ( $dataValue->getTypeID() ) { |
73 | 77 | case '_geo': |
74 | | - $values[] = "\"" . $dataValue->getWikiValue() . "\""; |
| 78 | + $values[] = '"' . $dataValue->getWikiValue() . '"'; |
75 | 79 | break; |
76 | 80 | case '_num': |
77 | | - $values[] = "\"" . $dataValue->getValueKey() . "\""; |
| 81 | + $values[] = $dataValue->getDataItem()->getNumber(); |
78 | 82 | break; |
79 | 83 | 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() . '"'; |
81 | 89 | break; |
82 | 90 | default: |
83 | | - $values[] = "\"" . $dataValue->getShortText( $outputmode, null ) . "\""; |
| 91 | + $values[] = '"' . $dataValue->getShortText( $outputmode, null ) . '"'; |
84 | 92 | } |
85 | 93 | |
86 | 94 | if ( sizeof( $values ) > 1 ) { |
87 | | - $finalvalues = "[" . implode( ",", $values ) . "]"; |
| 95 | + $finalvalues = '[' . implode( ',', $values ) . ']'; |
88 | 96 | } else { |
89 | 97 | $finalvalues = $values[0]; |
90 | 98 | } |
91 | 99 | } |
92 | | - if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( " ", "_", strtolower( $pr->getLabel() ) ) . '": ' . $finalvalues . ''; |
| 100 | + |
| 101 | + if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( ' ', '_', strtolower( $pr->getLabel() ) ) . '": ' . $finalvalues . ''; |
93 | 102 | } |
94 | 103 | } |
| 104 | + |
95 | 105 | if ( $rowsubject !== false ) { // stuff in the page URI and some category data |
96 | 106 | $valuestack[] = '"uri" : "' . $wgServer . $wgScriptPath . '/index.php?title=' . $rowsubject->getPrefixedText() . '"'; |
97 | 107 | $page_cats = smwfGetStore()->getPropertyValues( $rowsubject, new SMWDIProperty( '_INST' ) ); // TODO: set limit to 1 here |
— | — | @@ -119,16 +129,20 @@ |
120 | 130 | smwfLoadExtensionMessages( 'SemanticMediaWiki' ); |
121 | 131 | $label = wfMsgForContent( 'smw_json_link' ); |
122 | 132 | } |
| 133 | + |
123 | 134 | $link = $res->getQueryLink( $label ); |
124 | 135 | if ( array_key_exists( 'callback', $this->m_params ) ) { |
125 | 136 | $link->setParameter( htmlspecialchars( $this->m_params['callback'] ), 'callback' ); |
126 | 137 | } |
| 138 | + |
127 | 139 | if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) { // used as a file name |
128 | 140 | $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' ); |
129 | 141 | } |
| 142 | + |
130 | 143 | if ( array_key_exists( 'limit', $this->m_params ) ) { |
131 | 144 | $link->setParameter( htmlspecialchars( $this->m_params['limit'] ), 'limit' ); |
132 | 145 | } |
| 146 | + |
133 | 147 | $link->setParameter( 'json', 'format' ); |
134 | 148 | $result = $link->getText( $outputmode, $this->mLinker ); |
135 | 149 | $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed |