Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php |
— | — | @@ -41,18 +41,28 @@ |
42 | 42 | if ($outputmode == SMW_OUTPUT_FILE) { // create detached JSON file |
43 | 43 | $itemstack = array(); //contains Items for the items section |
44 | 44 | $propertystack = array(); //contains Properties for the property section |
| 45 | + $firstPropertyIsPageDef = false; // we onlt drop first property if its of type wpg |
| 46 | + $count = 0; |
45 | 47 | //generate property section |
46 | 48 | foreach ($res->getPrintRequests() as $pr) { |
47 | | - if(array_key_exists($pr->getTypeID(), $this->types)){ |
48 | | - $propertystack[] = '"'.str_replace(" ","_",strtolower($pr->getLabel())).'" : { valueType: "'.$this->types[$pr->getTypeID()].'" }'; |
| 49 | + if (array_key_exists($pr->getTypeID(), $this->types)) { |
| 50 | + $propertystack[] = '"'.str_replace(" ","_",strtolower($pr->getLabel())).'" : { "valueType": "'.$this->types[$pr->getTypeID()].'" }'; |
| 51 | + } else { |
| 52 | + $propertystack[] = '"'.str_replace(" ","_",strtolower($pr->getLabel())).'" : { "valueType": "text" }'; |
49 | 53 | } |
50 | | - else { |
51 | | - $propertystack[] = '"'.str_replace(" ","_",strtolower($pr->getLabel())).'" : { valueType: "text" }'; |
| 54 | + // if we're at the first item and it doesn't have a label its the page |
| 55 | + if($count==0 && $pr->getLabel() == ""){ |
| 56 | + $firstPropertyIsPageDef = true; |
52 | 57 | } |
| 58 | + $count++; |
53 | 59 | } |
54 | | - array_shift($propertystack); //drop first property |
55 | | - $properties ="properties: {\n\t\t".implode(",\n\t\t",$propertystack). "\n\t}"; |
| 60 | + // drop bogus empty first item that got stuffed in here (I don't know why its here) |
| 61 | + if ($firstPropertyIsPageDef) { |
| 62 | + array_shift($propertystack); //drop first property |
| 63 | + } |
56 | 64 | |
| 65 | + $properties ="\"properties\": {\n\t\t".implode(",\n\t\t",$propertystack). "\n\t}"; |
| 66 | + |
57 | 67 | //generate items section |
58 | 68 | $row = $res->getNext(); |
59 | 69 | while ( $row !== false ) { |
— | — | @@ -61,67 +71,71 @@ |
62 | 72 | $prefixedtext = ''; //save label for uri specification |
63 | 73 | foreach ($row as $field) { |
64 | 74 | $req = $field->getPrintRequest(); |
65 | | - if($count==0){ |
| 75 | + // when we're processing the first item and its a page we process it a little differently |
| 76 | + // this is the original code |
| 77 | + if ($count==0 && $firstPropertyIsPageDef) { |
66 | 78 | $values = ''; |
67 | | - foreach($field->getContent() as $value){ |
| 79 | + foreach ($field->getContent() as $value) { |
68 | 80 | $values = $value->getShortText($outputmode,null); //assign last value to label |
69 | 81 | $prefixedtext = $value->getPrefixedText(); |
70 | 82 | } |
71 | | - $valuestack[] = 'label: "'.$values.'"'; |
| 83 | + $valuestack[] = '"label": "'.$values.'"'; |
72 | 84 | $label = $values; |
73 | 85 | } else { |
74 | 86 | $values = array(); |
75 | 87 | $finalvalues = ''; |
76 | | - while ( ($value = $field->getNextObject()) !== false ){ |
| 88 | + while ( ($value = $field->getNextObject()) !== false ) { |
77 | 89 | $finalvalues = ''; |
78 | | - switch($value->getTypeID()){ |
| 90 | + switch ($value->getTypeID()) { |
79 | 91 | case '_geo': |
80 | | - $values[] = '"'.$value->getXSDValue().'"'; |
| 92 | + $values[] = "\"".$value->getXSDValue()."\""; |
81 | 93 | break; |
82 | 94 | case '_num': |
83 | | - $values[] = $value->getValueKey(); |
| 95 | + $values[] = "\"".$value->getNumericValue($outputmode,$this->getLinker(0))."\""; |
84 | 96 | break; |
85 | 97 | case '_dat': |
86 | 98 | $values[] = "\"".$value->getYear()."-".str_pad($value->getMonth(),2,'0',STR_PAD_LEFT)."-".str_pad($value->getDay(),2,'0',STR_PAD_LEFT)." ".$value->getTimeString()."\""; |
87 | 99 | break; |
88 | 100 | default: |
89 | | - $values[] = '"'.$value->getShortText($outputmode,null).'"'; |
| 101 | + $values[] = "\"".$value->getShortText($outputmode,null)."\""; |
90 | 102 | } |
91 | 103 | |
92 | | - if(sizeof($values)>1){ |
| 104 | + if (sizeof($values)>1) { |
93 | 105 | $finalvalues = "[".implode(",",$values)."]"; |
94 | 106 | } else { |
95 | 107 | $finalvalues = $values[0]; |
96 | 108 | } |
97 | 109 | } |
98 | | - if($finalvalues != '') $valuestack[] = '"'.str_replace(" ","_",strtolower($req->getLabel())).'": '.$finalvalues.''; |
| 110 | + if ($finalvalues != '') $valuestack[] = '"'.str_replace(" ","_",strtolower($req->getLabel())).'": '.$finalvalues.''; |
99 | 111 | } |
100 | 112 | $count++; |
101 | 113 | } |
102 | | - $valuestack[] = '"uri" : "'.$wgServer.$wgScriptPath.'/index.php?title='.$prefixedtext.'"'; |
| 114 | + // if we had a page definition, stuff in the uri |
| 115 | + if ($firstPropertyIsPageDef) { |
| 116 | + $valuestack[] = '"uri" : "'.$wgServer.$wgScriptPath.'/index.php?title='.$prefixedtext.'"'; |
| 117 | + } |
103 | 118 | |
104 | | - //try to determine type/category |
| 119 | + // try to determine type/category |
105 | 120 | $catlist = array(); |
106 | | - $dbr = &wfGetDB(DB_SLAVE); |
107 | | - $cl = $dbr->tableName('categorylinks'); |
108 | | - $arttitle = Title::newFromText($label); |
109 | | - if($arttitle instanceof Title){ |
| 121 | + $dbr = wfGetDB(DB_SLAVE); |
| 122 | + $cl = $dbr->tableName('categorylinks'); |
| 123 | + $arttitle = Title::newFromText($label); |
| 124 | + if ($arttitle instanceof Title) { |
110 | 125 | $catid = $arttitle->getArticleID(); |
111 | 126 | $catres = $dbr->select($cl, 'cl_to', "cl_from = $catid", __METHOD__, array('ORDER BY' => 'cl_sortkey')); |
112 | 127 | while ($catrow = $dbr->fetchRow($catres)) $catlist[] = $catrow[0]; |
113 | 128 | $dbr->freeResult($catres); |
114 | 129 | if(sizeof($catlist) > 0) $valuestack[] = '"type" : "'.$catlist[0].'"'; |
115 | 130 | } |
116 | | - |
117 | 131 | //create property list of item |
118 | 132 | $itemstack[] = "\t{\n\t\t\t".implode(",\n\t\t\t",$valuestack)."\n\t\t}"; |
119 | 133 | $row = $res->getNext(); |
120 | 134 | } |
121 | 135 | |
122 | | - $items = "items: [\n\t".implode(",\n\t",$itemstack)."\n\t]"; |
| 136 | + $items = "\"items\": [\n\t".implode(",\n\t",$itemstack)."\n\t]"; |
123 | 137 | |
124 | 138 | //check whether a callback function is required |
125 | | - if(array_key_exists('callback', $this->m_params)){ |
| 139 | + if (array_key_exists('callback', $this->m_params)) { |
126 | 140 | $result = htmlspecialchars($this->m_params['callback'])."({\n\t".$properties.",\n\t".$items."\n})"; |
127 | 141 | } else { |
128 | 142 | $result = "{\n\t".$properties.",\n\t".$items."\n}"; |
— | — | @@ -135,13 +149,13 @@ |
136 | 150 | $label = wfMsgForContent('smw_json_link'); |
137 | 151 | } |
138 | 152 | $link = $res->getQueryLink($label); |
139 | | - if(array_key_exists('callback', $this->m_params)) { |
| 153 | + if (array_key_exists('callback', $this->m_params)) { |
140 | 154 | $link->setParameter(htmlspecialchars($this->m_params['callback']),'callback'); |
141 | 155 | } |
142 | 156 | if ($this->getSearchLabel(SMW_OUTPUT_WIKI) != '') { // used as a file name |
143 | 157 | $link->setParameter($this->getSearchLabel(SMW_OUTPUT_WIKI),'searchlabel'); |
144 | 158 | } |
145 | | - if(array_key_exists('limit', $this->m_params)) { |
| 159 | + if (array_key_exists('limit', $this->m_params)) { |
146 | 160 | $link->setParameter(htmlspecialchars($this->m_params['limit']),'limit'); |
147 | 161 | } |
148 | 162 | $link->setParameter('json','format'); |
— | — | @@ -157,3 +171,6 @@ |
158 | 172 | } |
159 | 173 | |
160 | 174 | } |
| 175 | + |
| 176 | + |
| 177 | + |