r68300 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68299‎ | r68300 | r68301 >
Date:06:12, 20 June 2010
Author:mkroetzsch
Status:deferred
Tags:
Comment:
cleaned up code a litte, fixing Bug 24030
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php
@@ -41,47 +41,30 @@
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;
 45+
4746 // generate property section
4847 foreach ( $res->getPrintRequests() as $pr ) {
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" }';
 48+ if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
 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" }';
 53+ }
5354 }
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;
57 - }
58 - $count++;
5955 }
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 - }
64 -
6556 $properties = "\"properties\": {\n\t\t" . implode( ",\n\t\t", $propertystack ) . "\n\t}";
6657
6758 // generate items section
68 - $row = $res->getNext();
69 - while ( $row !== false ) {
 59+ while ( ( $row = $res->getNext() ) !== false ) {
 60+ $rowsubject = false; // the wiki page value that this row is about
7061 $valuestack = array(); // contains Property-Value pairs to characterize an Item
71 - $count = 0; // counter
72 - $prefixedtext = ''; // save label for uri specification
7362 foreach ( $row as $field ) {
74 - $req = $field->getPrintRequest();
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 ) {
78 - $values = '';
79 - foreach ( $field->getContent() as $value ) {
80 - $values = $value->getShortText( $outputmode, null ); // assign last value to label
81 - $prefixedtext = $value->getPrefixedText();
82 - }
83 - $valuestack[] = '"label": "' . $values . '"';
84 - $label = $values;
85 - } else {
 63+ $pr = $field->getPrintRequest();
 64+ if ( $rowsubject === false ) {
 65+ $rowsubject = $field->getResultSubject();
 66+ $valuestack[] = '"label": "' . $rowsubject->getShortText( $outputmode, null ) . '"';
 67+ }
 68+ if ( $pr->getMode() != SMWPrintRequest::PRINT_THIS ) {
8669 $values = array();
8770 $finalvalues = '';
8871 while ( ( $value = $field->getNextObject() ) !== false ) {
@@ -106,30 +89,19 @@
10790 $finalvalues = $values[0];
10891 }
10992 }
110 - if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( " ", "_", strtolower( $req->getLabel() ) ) . '": ' . $finalvalues . '';
 93+ if ( $finalvalues != '' ) $valuestack[] = '"' . str_replace( " ", "_", strtolower( $pr->getLabel() ) ) . '": ' . $finalvalues . '';
11194 }
112 - $count++;
11395 }
114 - // if we had a page definition, stuff in the uri
115 - if ( $firstPropertyIsPageDef ) {
116 - $valuestack[] = '"uri" : "' . $wgServer . $wgScriptPath . '/index.php?title=' . $prefixedtext . '"';
 96+ if ( $rowsubject !== false ) { // stuff in the page URI and some category data
 97+ $valuestack[] = '"uri" : "' . $wgServer . $wgScriptPath . '/index.php?title=' . $rowsubject->getPrefixedText() . '"';
 98+ $page_cats = smwfGetStore()->getPropertyValues( $rowsubject, SMWPropertyValue::makeProperty( '_INST' ) ); // TODO: set limit to 1 here
 99+ if ( count( $page_cats ) > 0 ) {
 100+ $valuestack[] = '"type" : "' . reset($page_cats)->getShortHTMLText() . '"';
 101+ }
117102 }
118103
119 - // try to determine type/category
120 - $catlist = array();
121 - $dbr = wfGetDB( DB_SLAVE );
122 - $cl = $dbr->tableName( 'categorylinks' );
123 - $arttitle = Title::newFromText( $label );
124 - if ( $arttitle instanceof Title ) {
125 - $catid = $arttitle->getArticleID();
126 - $catres = $dbr->select( $cl, 'cl_to', "cl_from = $catid", __METHOD__, array( 'ORDER BY' => 'cl_sortkey' ) );
127 - while ( $catrow = $dbr->fetchRow( $catres ) ) $catlist[] = $catrow[0];
128 - $dbr->freeResult( $catres );
129 - if ( sizeof( $catlist ) > 0 ) $valuestack[] = '"type" : "' . $catlist[0] . '"';
130 - }
131104 // create property list of item
132105 $itemstack[] = "\t{\n\t\t\t" . implode( ",\n\t\t\t", $valuestack ) . "\n\t\t}";
133 - $row = $res->getNext();
134106 }
135107
136108 $items = "\"items\": [\n\t" . implode( ",\n\t", $itemstack ) . "\n\t]";

Status & tagging log