r86237 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86236‎ | r86237 | r86238 >
Date:20:56, 16 April 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
basic fixes for using new data item system; not completely functional yet
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/export/SMW_Exporter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/export/SMW_Exporter.php
@@ -70,11 +70,12 @@
7171 * @param mixed $typesvalueforproperty either an SMWTypesValue or null
7272 */
7373 static public function makeExportDataForSubject( SMWDIWikiPage $subject, $typesvalueforproperty = null ) {
74 - $result = $subject->getExportData();
 74+ $result = self::getDataItemExpData( $subject );
 75+ $subj_title = Title::makeTitle( $subject->getNamespace(), $subject->getDBkey() );
7576 switch ( $subject->getNamespace() ) {
7677 case NS_CATEGORY: case SMW_NS_CONCEPT:
7778 $maintype_pe = SMWExporter::getSpecialElement( 'owl', 'Class' );
78 - $label = $subject->getText();
 79+ $label = $subj_title->getText();
7980 break;
8081 case SMW_NS_PROPERTY:
8182 if ( $typesvalueforproperty == null ) {
@@ -82,14 +83,13 @@
8384 $typesvalueforproperty = end( $types );
8485 }
8586 $maintype_pe = SMWExporter::getSpecialElement( 'owl', SMWExporter::getOWLPropertyType( $typesvalueforproperty ) );
86 - $label = $subject->getText();
 87+ $label = $subj_title->getText();
8788 break;
8889 default:
89 - $label = $subject->getWikiValue();
 90+ $label = $subj_title->getPrefixedText();
9091 $maintype_pe = SMWExporter::getSpecialElement( 'swivt', 'Subject' );
9192 }
9293 $ed = new SMWExpData( new SMWExpLiteral( $label ) );
93 - $subj_title = $subject->getTitle();
9494 $result->addPropertyObjectValue( SMWExporter::getSpecialElement( 'rdfs', 'label' ), $ed );
9595 $ed = new SMWExpData( new SMWExpResource( '&wikiurl;' . $subj_title->getPrefixedURL() ) );
9696 $result->addPropertyObjectValue( SMWExporter::getSpecialElement( 'swivt', 'page' ), $ed );
@@ -106,21 +106,21 @@
107107 * specified property data itme. This method is called when
108108 * constructing export data structures from SMWSemanticData objects.
109109 *
110 - * @param SMWDIProperty $property
111 - * @param array $values of SMWDataValue objects for the given property
112 - * @param SMWExpData $data to add the data to
 110+ * @param $property SMWDIProperty
 111+ * @param $dataItems array of SMWDataItem objects for the given property
 112+ * @param $data SMWExpData to add the data to
113113 */
114 - static public function addPropertyValues(SMWDIProperty $property, $values, SMWExpData &$data) {
 114+ static public function addPropertyValues( SMWDIProperty $property, $dataItems, SMWExpData &$expData ) {
115115 if ( $property->isUserDefined() ) {
116116 $pe = SMWExporter::getResourceElement( $property );
117 - foreach ( $values as $dv ) {
118 - $ed = $dv->getExportData();
 117+ foreach ( $dataItems as $dataItem ) {
 118+ $ed = self::getDataItemExpData( $dataItem );
119119 if ( $ed !== null ) {
120 - $data->addPropertyObjectValue( $pe, $ed );
 120+ $expData->addPropertyObjectValue( $pe, $ed );
121121 }
122122 }
123123 } else { // pre-defined property, only exported if known
124 - $subject = $data->getSubject()->getDatavalue();
 124+ $subject = $expData->getSubject()->getDatavalue();
125125 if ( $subject == null ) return; // subject datavalue (wikipage) required for treating special properties properly
126126 switch ( $subject->getNamespace() ) {
127127 case NS_CATEGORY: case SMW_NS_CONCEPT:
@@ -140,7 +140,7 @@
141141 }
142142 $pe = null;
143143 $cat_only = false; // basic namespace checking for equivalent categories
144 - switch ( $property->getPropertyID() ) {
 144+ switch ( $property->getKey() ) {
145145 ///TODO: distinguish instanceof and subclassof in the _INST case
146146 case '_INST': $pe = $category_pe; break;
147147 case '_CONC': $pe = $equality_pe; break;
@@ -159,25 +159,25 @@
160160 break;
161161 }
162162 if ( $pe === null ) return; // unknown special property, not exported
163 - foreach ( $values as $dv ) {
 163+ foreach ( $dataItems as $dataItem ) {
164164 if ( $cat_only ) {
165 - if ( !( $dv instanceof SMWDIWikiPage ) || ( $dv->getNamespace() != NS_CATEGORY ) ) {
 165+ if ( !( $dataItem instanceof SMWDIWikiPage ) || ( $dataItem->getNamespace() != NS_CATEGORY ) ) {
166166 continue;
167167 }
168168 }
169 - $ed = $dv->getExportData();
 169+ $ed = self::getDataItemExpData( $dataItem );
170170 if ( $ed !== null ) {
171 - if ( ( $property->getPropertyID() == '_CONC' ) && ( $ed->getSubject()->getName() == '' ) ) {
 171+ if ( ( $property->getKey() == '_CONC' ) && ( $ed->getSubject()->getName() == '' ) ) {
172172 // equivalent to anonymous class -> simplify description
173173 foreach ( $ed->getProperties() as $subp ) {
174174 if ( $subp->getName() != SMWExporter::getSpecialElement( 'rdf', 'type' )->getName() ) {
175175 foreach ( $ed->getValues( $subp ) as $subval ) {
176 - $data->addPropertyObjectValue( $subp, $subval );
 176+ $expData->addPropertyObjectValue( $subp, $subval );
177177 }
178178 }
179179 }
180180 } else {
181 - $data->addPropertyObjectValue( $pe, $ed );
 181+ $expData->addPropertyObjectValue( $pe, $ed );
182182 }
183183 }
184184 }
@@ -186,45 +186,47 @@
187187
188188 /**
189189 * Create an SMWExpElement for some internal resource, given by an
190 - * SMWDIWikiPage or SMWDIProperty object. Returns null on error.
 190+ * SMWDIWikiPage or SMWDIProperty object.
191191 * This is the one place in the code where URIs of wiki pages and
192192 * properties are defined.
193193 *
194 - * @param mixed SMWDIWikiPage or SMWDIProperty
195 - * @return SMWExpResource or null
 194+ * @param $resource SMWDataItem must be SMWDIWikiPage or SMWDIProperty
 195+ * @return SMWExpResource
196196 */
197197 static public function getResourceElement( SMWDataItem $resource ) {
 198+ global $wgContLang;
198199 if ( $resource instanceof SMWDIWikiPage ) {
199 - $dv = $resource;
 200+ $diWikiPage = $resource;
200201 } elseif ( $resource instanceof SMWDIProperty ) {
201 - $dv = $resource->getWikiPageValue();
 202+ $diWikiPage = $resource->getDiWikiPage();
 203+ if ( $diWikiPage === null ) { /// TODO Maybe treat special properties here, too
 204+ return null;
 205+ }
202206 } else {
203 - return null;
 207+ throw new InvalidArgumentException( 'SMWExporter::getResourceElement() expects an object of type SMWDIWikiPage or SMWDIProperty' );
204208 }
205 - $idvs = smwfGetStore()->getPropertyValues( $dv, new SMWDIProperty( '_IMPO' ) );
206 - if ( count( $idvs ) > 0 ) {
207 - $namespace = current( $idvs )->getNS();
208 - $namespaceid = current( $idvs )->getNSID();
209 - $localname = current( $idvs )->getLocalName();
 209+ $importDis = smwfGetStore()->getPropertyValues( $diWikiPage, new SMWDIProperty( '_IMPO' ) );
 210+ if ( count( $importDis ) > 0 ) {
 211+ $namespace = current( $importDis )->getNS();
 212+ $namespaceid = current( $importDis )->getNSID();
 213+ $localname = current( $importDis )->getLocalName();
210214 } else {
211215 $localname = '';
212 - if ( $dv->getNamespace() == SMW_NS_PROPERTY ) {
 216+ if ( $diWikiPage->getNamespace() == SMW_NS_PROPERTY ) {
213217 $namespace = '&property;';
214218 $namespaceid = 'property';
215 - $localname = SMWExporter::encodeURI( rawurlencode( $dv->getDBkey() ) );
216 - if ( in_array( $localname[0], array( '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ) ) ) {
217 - $namespace = '&wiki;';
218 - $namespaceid = 'wiki';
219 - $localname = SMWExporter::encodeURI( wfUrlencode( str_replace( ' ', '_', $dv->getPrefixedText() ) ) );
220 - }
221 - } else { // no QName needed, do not attempt to make one
222 - $namespace = false;
223 - $namespaceid = false;
224 - $localname = '&wiki;' . SMWExporter::encodeURI( wfUrlencode( str_replace( ' ', '_', $dv->getPrefixedText() ) ) );
 219+ $localname = SMWExporter::encodeURI( rawurlencode( $diWikiPage->getDBkey() ) );
225220 }
 221+ if ( ( $localname == '' ) ||
 222+ ( in_array( $localname[0], array( '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ) ) ) ) {
 223+ $namespace = '&wiki;';
 224+ $namespaceid = 'wiki';
 225+ $nsText = str_replace( ' ', '_', $wgContLang->getNSText( $diWikiPage->getNamespace() ) );
 226+ $localname = SMWExporter::encodeURI( wfUrlencode( $nsText . ':' . $diWikiPage->getDBkey() ) );
 227+ }
226228 }
227229
228 - return new SMWExpResource( $localname, $dv, $namespace, $namespaceid );
 230+ return new SMWExpResource( $localname, $diWikiPage, $namespace, $namespaceid );
229231 }
230232
231233 /**
@@ -326,59 +328,84 @@
327329
328330 /**
329331 * Create an SWMExpData container that encodes the data of the given
330 - * datavalue object.
 332+ * dataitem object.
331333 *
332 - * @param SMWDataValue $dv the datavalue to encode
 334+ * @param $dataItem SMWDataItem
 335+ * @return SMWExpData
333336 */
334 - static public function getDVExpData( SMWDataValue $dv ) {
335 - if ( !$dv->isValid() ) return null;
336 -
337 - switch ( $dv->getBaseType() ) {
338 - case SMWDataValue::TYPE_NUMBER:
339 - $lit = new SMWExpLiteral( $dv->getNumber(), $dv, 'http://www.w3.org/2001/XMLSchema#double' );
 337+ static public function getDataItemExpData( SMWDataItem $dataItem ) {
 338+ switch ( $dataItem->getDIType() ) {
 339+ case SMWDataItem::TYPE_NUMBER:
 340+ $lit = new SMWExpLiteral( $dataItem->getNumber(), $dataItem, 'http://www.w3.org/2001/XMLSchema#double' );
340341 return new SMWExpData( $lit );
341342 break;
342 - case SMWDataValue::TYPE_STRING: case SMWDataValue::TYPE_BLOB:
343 - $lit = new SMWExpLiteral( smwfHTMLtoUTF8( $dv->getWikiText() ), $dv, 'http://www.w3.org/2001/XMLSchema#string' );
 343+ case SMWDataItem::TYPE_STRING: case SMWDataItem::TYPE_BLOB:
 344+ $lit = new SMWExpLiteral( smwfHTMLtoUTF8( $dataItem->getString() ), $dataItem, 'http://www.w3.org/2001/XMLSchema#string' );
344345 return new SMWExpData( $lit );
345346 break;
346 - case SMWDataValue::TYPE_BOOL:
347 - $xsdvalue = $dv->getBoolean() ? 'true' : 'false';
348 - $lit = new SMWExpLiteral( $xsdvalue, $dv, 'http://www.w3.org/2001/XMLSchema#boolean' );
 347+ case SMWDataItem::TYPE_BOOLEAN:
 348+ $xsdvalue = $dataItem->getBoolean() ? 'true' : 'false';
 349+ $lit = new SMWExpLiteral( $xsdvalue, $dataItem, 'http://www.w3.org/2001/XMLSchema#boolean' );
349350 return new SMWExpData( $lit );
350351 break;
351 - case SMWDataValue::TYPE_URI:
 352+ case SMWDataItem::TYPE_URI:
352353 /// TODO This escaping seems very odd. The serialisation should handle such things.
353 - $res = new SMWExpResource( str_replace( '&', '&', $dv->getURI() ), $dv );
 354+ $res = new SMWExpResource( str_replace( '&', '&', $dataItem->getURI() ), $dataItem );
354355 return new SMWExpData( $res );
355356 break;
356 - case SMWDataValue::TYPE_TIME:
357 - $lit = new SMWExpLiteral( $dv->getXMLSchemaDate(), $dv, 'http://www.w3.org/2001/XMLSchema#dateTime' );
 357+ case SMWDataItem::TYPE_TIME:
 358+ $gregorianTime = $dataItem->getForCalendarModel( SMWDITime::CM_GREGORIAN );
 359+ if ( $gregorianTime->getYear() > 0 ) {
 360+ $xsdvalue = str_pad( $gregorianTime->getYear(), 4, "0", STR_PAD_LEFT );
 361+ } else {
 362+ $xsdvalue = '-' . str_pad( 1 - $gregorianTime->getYear(), 4, "0", STR_PAD_LEFT );
 363+ }
 364+ $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYear';
 365+ if ( $gregorianTime->getPrecision() >= SMWDITime::PREC_YM ) {
 366+ $xsdtype = 'http://www.w3.org/2001/XMLSchema#gYearMonth';
 367+ $xsdvalue .= '-' . str_pad( $gregorianTime->getMonth(), 2, "0", STR_PAD_LEFT );
 368+ if ( $gregorianTime->getPrecision() >= SMWDITime::PREC_YMD ) {
 369+ $xsdtype = 'http://www.w3.org/2001/XMLSchema#date';
 370+ $xsdvalue .= '-' . str_pad( $gregorianTime->getDay(), 2, "0", STR_PAD_LEFT );
 371+ if ( $gregorianTime->getPrecision() == SMWDITime::PREC_YMDT ) {
 372+ $xsdtype = 'http://www.w3.org/2001/XMLSchema#dateTime';
 373+ $xsdvalue .= 'T' .
 374+ sprintf( "%02d", $gregorianTime->getHour() ) . ':' .
 375+ sprintf( "%02d", $gregorianTime->getMinute()) . ':' .
 376+ sprintf( "%02d", $gregorianTime->getSecond() );
 377+ }
 378+ }
 379+ }
 380+ $xsdvalue .= 'Z';
 381+ $lit = new SMWExpLiteral( $xsdvalue, $gregorianTime, $xsdtype );
358382 return new SMWExpData( $lit );
359383 break;
360 - case SMWDataValue::TYPE_GEO:
 384+ case SMWDataItem::TYPE_GEO:
361385 /// TODO
 386+ return null;
362387 break;
363 - case SMWDataValue::TYPE_CONT:
 388+ case SMWDataItem::TYPE_CONTAINER:
364389 /// TODO
 390+ return null;
365391 break;
366 - case SMWDataValue::TYPE_PAGE:
367 - if ( $dv->getNamespace() == NS_MEDIA ) { // special handling for linking media files directly (object only)
368 - $file = wfFindFile( $dv->getTitle() );
 392+ case SMWDataItem::TYPE_WIKIPAGE:
 393+ if ( $dataItem->getNamespace() == NS_MEDIA ) { // special handling for linking media files directly (object only)
 394+ $title = Title::makeTitle( $dataItem->getNamespace(), $dataItem->getDBkey() ) ;
 395+ $file = wfFindFile( $title );
369396 if ( $file !== false ) {
370397 return new SMWExpData( new SMWExpResource( $file->getFullURL() ) );
371398 } else { // Medialink to non-existing file :-/
372 - return null;
 399+ return new SMWExpData( SMWExporter::getResourceElement( $dataItem ) );
373400 }
374401 } else {
375 - return new SMWExpData( SMWExporter::getResourceElement( $dv ) );
 402+ return new SMWExpData( SMWExporter::getResourceElement( $dataItem ) );
376403 }
377404 break;
378 - case SMWDataValue::TYPE_CONCEPT:
 405+ case SMWDataItem::TYPE_CONCEPT:
379406 /// TODO
380407 break;
381 - case SMWDataValue::TYPE_PROP:
382 - return new SMWExpData( SMWExporter::getResourceElement( $dv->getWikiPageValue() ) );
 408+ case SMWDataItem::TYPE_PROPERTY:
 409+ return new SMWExpData( SMWExporter::getResourceElement( $dataItem->getDiWikiPage() ) );
383410 break;
384411 }
385412 }

Status & tagging log