r86693 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86692‎ | r86693 | r86694 >
Date:09:12, 22 April 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
changed file name to match naming conventions
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_CompatibilityHelpers.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Compatibility_Helpers.php (deleted) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Compatibility_Helpers.php
@@ -1,162 +0,0 @@
2 -<?php
3 -/**
4 - * This file contains the SMWCompatibilityHelper class.
5 - * @note This file and its contents will vanish before SMW 1.7. Please modify your code to not require these helpers.
6 - *
7 - * @author Markus Krötzsch
8 - *
9 - * @file
10 - * @ingroup SMW
11 - */
12 -
13 -/**
14 - * Helper class to collect various static functions that provide some
15 - * interfaces used in SMW 1.6 that are no longer available in SMW 1.7.
16 - * In particular, this relates to the new data model based on data items
17 - * instead of data value classes with DB key arrays and singatures.
18 - *
19 - * @note This class will vanish before SMW 1.7. Please change your code to not require the old interfaces at all.
20 - *
21 - * @ingroup SMW
22 - */
23 -class SMWCompatibilityHelpers {
24 -
25 - /**
26 - * Method to create a dataitem from a type ID and array of DB keys.
27 - * Throws SMWDataItemException if problems occur, to get our callers
28 - * used to it.
29 - *
30 - * @param $typeid id string for the given type
31 - * @param $dbkeys array of mixed
32 - *
33 - * @return SMWDataItem
34 - */
35 - static public function dataItemFromDBKeys( $typeid, $dbkeys ) {
36 - switch ( SMWDataValueFactory::getDataItemId( $typeid ) ) {
37 - case SMWDataItem::TYPE_ERROR:
38 - break;
39 - case SMWDataItem::TYPE_NUMBER:
40 - return SMWDINumber::doUnserialize( $dbkeys[0], $typeid );
41 - case SMWDataItem::TYPE_STRING:
42 - return new SMWDIString( $dbkeys[0], $typeid );
43 - case SMWDataItem::TYPE_BLOB:
44 - return new SMWDIBlob( $dbkeys[0], $typeid );
45 - case SMWDataItem::TYPE_BOOLEAN:
46 - return new SMWDIBoolean( ( $dbkeys[0] == '1' ), $typeid );
47 - case SMWDataItem::TYPE_URI:
48 - return SMWDIUri::doUnserialize( $dbkeys[0], $typeid);
49 - case SMWDataItem::TYPE_TIME:
50 - $timedate = explode( 'T', $dbkeys[0], 2 );
51 - if ( ( count( $dbkeys ) == 2 ) && ( count( $timedate ) == 2 ) ) {
52 - $date = reset( $timedate );
53 - $year = $month = $day = $hours = $minutes = $seconds = $timeoffset = false;
54 - if ( ( end( $timedate ) == '' ) ||
55 - ( SMWTimeValue::parseTimeString( end( $timedate ), $hours, $minutes, $seconds, $timeoffset ) == true ) ) {
56 - $d = explode( '/', $date, 3 );
57 - if ( count( $d ) == 3 ) {
58 - list( $year, $month, $day ) = $d;
59 - } elseif ( count( $d ) == 2 ) {
60 - list( $year, $month ) = $d;
61 - } elseif ( count( $d ) == 1 ) {
62 - list( $year ) = $d;
63 - }
64 - if ( $month == '' ) $month = false;
65 - if ( $day == '' ) $day = false;
66 - $calendarmodel = SMWDITime::CM_GREGORIAN;
67 - return new SMWDITime( $calendarmodel, $year, $month, $day, $hours, $minutes, $seconds, $typeid );
68 - }
69 - }
70 - break;
71 - case SMWDataItem::TYPE_GEO:
72 - return new SMWDIGeoCoord( array( 'lat' => (float)$dbkeys[0], 'lon' => (float)$dbkeys[1] ), $typeid );
73 - break;
74 - case SMWDataItem::TYPE_CONTAINER:
75 - break;
76 - case SMWDataItem::TYPE_WIKIPAGE:
77 - if ( $typeid == '__typ' ) { // DBkeys for types values are special (used to be a SMWSimpleWikiPageValue)
78 - $pagedbkey = str_replace( ' ', '_', SMWDataValueFactory::findTypeLabel( $dbkeys[0] ) );
79 - return new SMWDIWikiPage( $pagedbkey, SMW_NS_TYPE, '', $typeid );
80 - } elseif ( count( $dbkeys ) >= 3 ) {
81 - return new SMWDIWikiPage( $dbkeys[0], floatval( $dbkeys[1] ), $dbkeys[2], $typeid );
82 - }
83 - break;
84 - case SMWDataItem::TYPE_CONCEPT:
85 - if ( count( $dbkeys ) >= 5 ) {
86 - new SMWDIConcept( $dbkeys[0], smwfXMLContentEncode( $dbkeys[1] ), $dbkeys[2], $dbkeys[3], $dbkeys[4], $typeid );
87 - }
88 - break;
89 - case SMWDataItem::TYPE_PROPERTY:
90 - return new SMWDIProperty( $dbkeys[0], false, $typeid );
91 - case SMWDataItem::TYPE_NOTYPE:
92 - if ( ( $typeid != '' ) && ( $typeid{0} != '_' ) ) { // linear conversion type
93 - return SMWDINumber::doUnserialize( $dbkeys[0], $typeid );
94 - }
95 - }
96 - throw new SMWDataItemException( 'Failed to create data item from DB keys.' );
97 - }
98 -
99 - /**
100 - * Compatibility function for computing the old getDBkeys() array for the new SMW data items.
101 - */
102 - public static function getDBkeysFromDataItem( SMWDataItem $dataItem ) {
103 - switch ( $dataItem->getTypeId() ) {
104 - case '_txt': case '_cod': case '_str': case '__sps': case '__tls': case '__imp':
105 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_STRING ) break;
106 - return array( $dataItem->getString() );
107 - case '_ema': case '_uri': case '_anu': case '_tel': case '__spu':
108 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_URI ) break;
109 - return array( $dataItem->getSerialization() );
110 - case '_wpg': case '_wpp': case '_wpc': case '_wpf': case '__sup':
111 - case '__suc': case '__spf': case '__sin': case '__red':
112 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) break;
113 - return array( $dataItem->getDBkey(), $dataItem->getNamespace(), $dataItem->getInterwiki(), $dataItem->getDBkey() );
114 - case '_num': case '_tem': case '__lin':
115 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_NUMBER ) break;
116 - return array( $dataItem->getSerialization(), floatval( $dataItem->getNumber() ) );
117 - case '_dat':
118 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_TIME ) break;
119 - $xsdvalue = $dataItem->getYear() . "/" .
120 - ( ( $dataItem->getPrecision() >= SMWDITime::PREC_YM ) ? $dataItem->getMonth() : '' ) . "/" .
121 - ( ( $dataItem->getPrecision() >= SMWDITime::PREC_YMD ) ? $dataItem->getDay() : '' ) . "T";
122 - if ( $dataItem->getPrecision() == SMWDITime::PREC_YMDT ) {
123 - $xsdvalue .= sprintf( "%02d", $dataItem->getHour() ) . ':' .
124 - sprintf( "%02d", $dataItem->getMinute()) . ':' .
125 - sprintf( "%02d", $dataItem->getSecond() );
126 - }
127 - return array( $xsdvalue, $dataItem->getSortKey() );
128 - case '_boo':
129 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_BOOLEAN ) break;
130 - return $dataItem->getBoolean() ? array( '1', 1 ) : array( '0', 0 );
131 - case '_rec':
132 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_CONTAINER ) break;
133 - return array( false );
134 - case '__typ':
135 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) break;
136 - return array( SMWDataValueFactory::findTypeID( str_replace( '_', ' ', $dataItem->getDBkey() ) ) );
137 - case '__con':
138 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_CONCEPT ) break;
139 - return array( $dataItem->getConceptQuery(), $dataItem->getDocumentation(), $dataItem->getQueryFeatures(), $dataItem->getSize(), $dataItem->getDepth() );
140 - case '__err':
141 - return array( false );
142 - case '__pro':
143 - if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) break;
144 - return array( $dataItem->getKey() );
145 - case '_geo':
146 - $coordinateSet = $dataItem->getCoordinateSet();
147 -
148 - return array(
149 - $coordinateSet['lat'],
150 - $coordinateSet['lon']
151 - );
152 - break;
153 - default:
154 - $typeid = $dataItem->getTypeId();
155 - if ( ( $typeid != '' ) && ( $typeid{0} != '_' ) &&
156 - ( $dataItem->getDIType() == SMWDataItem::TYPE_NUMBER ) ) { // linear conversion type
157 - return array( $dataItem->getSerialization(), floatval( $dataItem->getNumber() ) );
158 - }
159 - }
160 - return array( false );
161 - }
162 -
163 -}
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_CompatibilityHelpers.php
@@ -0,0 +1,162 @@
 2+<?php
 3+/**
 4+ * This file contains the SMWCompatibilityHelper class.
 5+ * @note This file and its contents will vanish before SMW 1.7. Please modify your code to not require these helpers.
 6+ *
 7+ * @author Markus Krötzsch
 8+ *
 9+ * @file
 10+ * @ingroup SMW
 11+ */
 12+
 13+/**
 14+ * Helper class to collect various static functions that provide some
 15+ * interfaces used in SMW 1.6 that are no longer available in SMW 1.7.
 16+ * In particular, this relates to the new data model based on data items
 17+ * instead of data value classes with DB key arrays and singatures.
 18+ *
 19+ * @note This class will vanish before SMW 1.7. Please change your code to not require the old interfaces at all.
 20+ *
 21+ * @ingroup SMW
 22+ */
 23+class SMWCompatibilityHelpers {
 24+
 25+ /**
 26+ * Method to create a dataitem from a type ID and array of DB keys.
 27+ * Throws SMWDataItemException if problems occur, to get our callers
 28+ * used to it.
 29+ *
 30+ * @param $typeid id string for the given type
 31+ * @param $dbkeys array of mixed
 32+ *
 33+ * @return SMWDataItem
 34+ */
 35+ static public function dataItemFromDBKeys( $typeid, $dbkeys ) {
 36+ switch ( SMWDataValueFactory::getDataItemId( $typeid ) ) {
 37+ case SMWDataItem::TYPE_ERROR:
 38+ break;
 39+ case SMWDataItem::TYPE_NUMBER:
 40+ return SMWDINumber::doUnserialize( $dbkeys[0], $typeid );
 41+ case SMWDataItem::TYPE_STRING:
 42+ return new SMWDIString( $dbkeys[0], $typeid );
 43+ case SMWDataItem::TYPE_BLOB:
 44+ return new SMWDIBlob( $dbkeys[0], $typeid );
 45+ case SMWDataItem::TYPE_BOOLEAN:
 46+ return new SMWDIBoolean( ( $dbkeys[0] == '1' ), $typeid );
 47+ case SMWDataItem::TYPE_URI:
 48+ return SMWDIUri::doUnserialize( $dbkeys[0], $typeid);
 49+ case SMWDataItem::TYPE_TIME:
 50+ $timedate = explode( 'T', $dbkeys[0], 2 );
 51+ if ( ( count( $dbkeys ) == 2 ) && ( count( $timedate ) == 2 ) ) {
 52+ $date = reset( $timedate );
 53+ $year = $month = $day = $hours = $minutes = $seconds = $timeoffset = false;
 54+ if ( ( end( $timedate ) == '' ) ||
 55+ ( SMWTimeValue::parseTimeString( end( $timedate ), $hours, $minutes, $seconds, $timeoffset ) == true ) ) {
 56+ $d = explode( '/', $date, 3 );
 57+ if ( count( $d ) == 3 ) {
 58+ list( $year, $month, $day ) = $d;
 59+ } elseif ( count( $d ) == 2 ) {
 60+ list( $year, $month ) = $d;
 61+ } elseif ( count( $d ) == 1 ) {
 62+ list( $year ) = $d;
 63+ }
 64+ if ( $month == '' ) $month = false;
 65+ if ( $day == '' ) $day = false;
 66+ $calendarmodel = SMWDITime::CM_GREGORIAN;
 67+ return new SMWDITime( $calendarmodel, $year, $month, $day, $hours, $minutes, $seconds, $typeid );
 68+ }
 69+ }
 70+ break;
 71+ case SMWDataItem::TYPE_GEO:
 72+ return new SMWDIGeoCoord( array( 'lat' => (float)$dbkeys[0], 'lon' => (float)$dbkeys[1] ), $typeid );
 73+ break;
 74+ case SMWDataItem::TYPE_CONTAINER:
 75+ break;
 76+ case SMWDataItem::TYPE_WIKIPAGE:
 77+ if ( $typeid == '__typ' ) { // DBkeys for types values are special (used to be a SMWSimpleWikiPageValue)
 78+ $pagedbkey = str_replace( ' ', '_', SMWDataValueFactory::findTypeLabel( $dbkeys[0] ) );
 79+ return new SMWDIWikiPage( $pagedbkey, SMW_NS_TYPE, '', $typeid );
 80+ } elseif ( count( $dbkeys ) >= 3 ) {
 81+ return new SMWDIWikiPage( $dbkeys[0], floatval( $dbkeys[1] ), $dbkeys[2], $typeid );
 82+ }
 83+ break;
 84+ case SMWDataItem::TYPE_CONCEPT:
 85+ if ( count( $dbkeys ) >= 5 ) {
 86+ new SMWDIConcept( $dbkeys[0], smwfXMLContentEncode( $dbkeys[1] ), $dbkeys[2], $dbkeys[3], $dbkeys[4], $typeid );
 87+ }
 88+ break;
 89+ case SMWDataItem::TYPE_PROPERTY:
 90+ return new SMWDIProperty( $dbkeys[0], false, $typeid );
 91+ case SMWDataItem::TYPE_NOTYPE:
 92+ if ( ( $typeid != '' ) && ( $typeid{0} != '_' ) ) { // linear conversion type
 93+ return SMWDINumber::doUnserialize( $dbkeys[0], $typeid );
 94+ }
 95+ }
 96+ throw new SMWDataItemException( 'Failed to create data item from DB keys.' );
 97+ }
 98+
 99+ /**
 100+ * Compatibility function for computing the old getDBkeys() array for the new SMW data items.
 101+ */
 102+ public static function getDBkeysFromDataItem( SMWDataItem $dataItem ) {
 103+ switch ( $dataItem->getTypeId() ) {
 104+ case '_txt': case '_cod': case '_str': case '__sps': case '__tls': case '__imp':
 105+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_STRING ) break;
 106+ return array( $dataItem->getString() );
 107+ case '_ema': case '_uri': case '_anu': case '_tel': case '__spu':
 108+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_URI ) break;
 109+ return array( $dataItem->getSerialization() );
 110+ case '_wpg': case '_wpp': case '_wpc': case '_wpf': case '__sup':
 111+ case '__suc': case '__spf': case '__sin': case '__red':
 112+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) break;
 113+ return array( $dataItem->getDBkey(), $dataItem->getNamespace(), $dataItem->getInterwiki(), $dataItem->getDBkey() );
 114+ case '_num': case '_tem': case '__lin':
 115+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_NUMBER ) break;
 116+ return array( $dataItem->getSerialization(), floatval( $dataItem->getNumber() ) );
 117+ case '_dat':
 118+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_TIME ) break;
 119+ $xsdvalue = $dataItem->getYear() . "/" .
 120+ ( ( $dataItem->getPrecision() >= SMWDITime::PREC_YM ) ? $dataItem->getMonth() : '' ) . "/" .
 121+ ( ( $dataItem->getPrecision() >= SMWDITime::PREC_YMD ) ? $dataItem->getDay() : '' ) . "T";
 122+ if ( $dataItem->getPrecision() == SMWDITime::PREC_YMDT ) {
 123+ $xsdvalue .= sprintf( "%02d", $dataItem->getHour() ) . ':' .
 124+ sprintf( "%02d", $dataItem->getMinute()) . ':' .
 125+ sprintf( "%02d", $dataItem->getSecond() );
 126+ }
 127+ return array( $xsdvalue, $dataItem->getSortKey() );
 128+ case '_boo':
 129+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_BOOLEAN ) break;
 130+ return $dataItem->getBoolean() ? array( '1', 1 ) : array( '0', 0 );
 131+ case '_rec':
 132+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_CONTAINER ) break;
 133+ return array( false );
 134+ case '__typ':
 135+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_WIKIPAGE ) break;
 136+ return array( SMWDataValueFactory::findTypeID( str_replace( '_', ' ', $dataItem->getDBkey() ) ) );
 137+ case '__con':
 138+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_CONCEPT ) break;
 139+ return array( $dataItem->getConceptQuery(), $dataItem->getDocumentation(), $dataItem->getQueryFeatures(), $dataItem->getSize(), $dataItem->getDepth() );
 140+ case '__err':
 141+ return array( false );
 142+ case '__pro':
 143+ if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) break;
 144+ return array( $dataItem->getKey() );
 145+ case '_geo':
 146+ $coordinateSet = $dataItem->getCoordinateSet();
 147+
 148+ return array(
 149+ $coordinateSet['lat'],
 150+ $coordinateSet['lon']
 151+ );
 152+ break;
 153+ default:
 154+ $typeid = $dataItem->getTypeId();
 155+ if ( ( $typeid != '' ) && ( $typeid{0} != '_' ) &&
 156+ ( $dataItem->getDIType() == SMWDataItem::TYPE_NUMBER ) ) { // linear conversion type
 157+ return array( $dataItem->getSerialization(), floatval( $dataItem->getNumber() ) );
 158+ }
 159+ }
 160+ return array( false );
 161+ }
 162+
 163+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_CompatibilityHelpers.php
___________________________________________________________________
Added: svn:eol-style
1164 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -109,7 +109,7 @@
110110 $wgAutoloadClasses['SMWDataValueFactory'] = $smwgIP . 'includes/SMW_DataValueFactory.php';
111111 $wgAutoloadClasses['SMWDataValue'] = $smwgIP . 'includes/SMW_DataValue.php';
112112 $wgAutoloadClasses['SMWQueryLanguage'] = $smwgIP . 'includes/SMW_QueryLanguage.php';
113 - $wgAutoloadClasses['SMWCompatibilityHelpers'] = $smwgIP . 'includes/SMW_Compatibility_Helpers.php';
 113+ $wgAutoloadClasses['SMWCompatibilityHelpers'] = $smwgIP . 'includes/SMW_CompatibilityHelpers.php';
114114
115115 // Article pages
116116 $apDir = $smwgIP . 'includes/articlepages/';

Status & tagging log