r63201 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63200‎ | r63201 | r63202 >
Date:14:44, 3 March 2010
Author:mkroetzsch
Status:deferred
Tags:
Comment:
update from Fabian to fix Bug 20241
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php
@@ -41,18 +41,28 @@
4242 if ($outputmode == SMW_OUTPUT_FILE) { // create detached JSON file
4343 $itemstack = array(); //contains Items for the items section
4444 $propertystack = array(); //contains Properties for the property section
 45+ $firstPropertyIsPageDef = false; // we onlt drop first property if its of type wpg
 46+ $count = 0;
4547 //generate property section
4648 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" }';
4953 }
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;
5257 }
 58+ $count++;
5359 }
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+ }
5664
 65+ $properties ="\"properties\": {\n\t\t".implode(",\n\t\t",$propertystack). "\n\t}";
 66+
5767 //generate items section
5868 $row = $res->getNext();
5969 while ( $row !== false ) {
@@ -61,67 +71,71 @@
6272 $prefixedtext = ''; //save label for uri specification
6373 foreach ($row as $field) {
6474 $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) {
6678 $values = '';
67 - foreach($field->getContent() as $value){
 79+ foreach ($field->getContent() as $value) {
6880 $values = $value->getShortText($outputmode,null); //assign last value to label
6981 $prefixedtext = $value->getPrefixedText();
7082 }
71 - $valuestack[] = 'label: "'.$values.'"';
 83+ $valuestack[] = '"label": "'.$values.'"';
7284 $label = $values;
7385 } else {
7486 $values = array();
7587 $finalvalues = '';
76 - while ( ($value = $field->getNextObject()) !== false ){
 88+ while ( ($value = $field->getNextObject()) !== false ) {
7789 $finalvalues = '';
78 - switch($value->getTypeID()){
 90+ switch ($value->getTypeID()) {
7991 case '_geo':
80 - $values[] = '"'.$value->getXSDValue().'"';
 92+ $values[] = "\"".$value->getXSDValue()."\"";
8193 break;
8294 case '_num':
83 - $values[] = $value->getValueKey();
 95+ $values[] = "\"".$value->getNumericValue($outputmode,$this->getLinker(0))."\"";
8496 break;
8597 case '_dat':
8698 $values[] = "\"".$value->getYear()."-".str_pad($value->getMonth(),2,'0',STR_PAD_LEFT)."-".str_pad($value->getDay(),2,'0',STR_PAD_LEFT)." ".$value->getTimeString()."\"";
8799 break;
88100 default:
89 - $values[] = '"'.$value->getShortText($outputmode,null).'"';
 101+ $values[] = "\"".$value->getShortText($outputmode,null)."\"";
90102 }
91103
92 - if(sizeof($values)>1){
 104+ if (sizeof($values)>1) {
93105 $finalvalues = "[".implode(",",$values)."]";
94106 } else {
95107 $finalvalues = $values[0];
96108 }
97109 }
98 - if($finalvalues != '') $valuestack[] = '"'.str_replace(" ","_",strtolower($req->getLabel())).'": '.$finalvalues.'';
 110+ if ($finalvalues != '') $valuestack[] = '"'.str_replace(" ","_",strtolower($req->getLabel())).'": '.$finalvalues.'';
99111 }
100112 $count++;
101113 }
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+ }
103118
104 - //try to determine type/category
 119+ // try to determine type/category
105120 $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) {
110125 $catid = $arttitle->getArticleID();
111126 $catres = $dbr->select($cl, 'cl_to', "cl_from = $catid", __METHOD__, array('ORDER BY' => 'cl_sortkey'));
112127 while ($catrow = $dbr->fetchRow($catres)) $catlist[] = $catrow[0];
113128 $dbr->freeResult($catres);
114129 if(sizeof($catlist) > 0) $valuestack[] = '"type" : "'.$catlist[0].'"';
115130 }
116 -
117131 //create property list of item
118132 $itemstack[] = "\t{\n\t\t\t".implode(",\n\t\t\t",$valuestack)."\n\t\t}";
119133 $row = $res->getNext();
120134 }
121135
122 - $items = "items: [\n\t".implode(",\n\t",$itemstack)."\n\t]";
 136+ $items = "\"items\": [\n\t".implode(",\n\t",$itemstack)."\n\t]";
123137
124138 //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)) {
126140 $result = htmlspecialchars($this->m_params['callback'])."({\n\t".$properties.",\n\t".$items."\n})";
127141 } else {
128142 $result = "{\n\t".$properties.",\n\t".$items."\n}";
@@ -135,13 +149,13 @@
136150 $label = wfMsgForContent('smw_json_link');
137151 }
138152 $link = $res->getQueryLink($label);
139 - if(array_key_exists('callback', $this->m_params)) {
 153+ if (array_key_exists('callback', $this->m_params)) {
140154 $link->setParameter(htmlspecialchars($this->m_params['callback']),'callback');
141155 }
142156 if ($this->getSearchLabel(SMW_OUTPUT_WIKI) != '') { // used as a file name
143157 $link->setParameter($this->getSearchLabel(SMW_OUTPUT_WIKI),'searchlabel');
144158 }
145 - if(array_key_exists('limit', $this->m_params)) {
 159+ if (array_key_exists('limit', $this->m_params)) {
146160 $link->setParameter(htmlspecialchars($this->m_params['limit']),'limit');
147161 }
148162 $link->setParameter('json','format');
@@ -157,3 +171,6 @@
158172 }
159173
160174 }
 175+
 176+
 177+

Status & tagging log