r66988 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66987‎ | r66988 | r66989 >
Date:04:41, 28 May 2010
Author:yaron
Status:deferred
Tags:
Comment:
Version 0.5: RDF export added; more re-formatting of code
Modified paths:
  • /trunk/extensions/SemanticInternalObjects/README (modified) (history)
  • /trunk/extensions/SemanticInternalObjects/SemanticInternalObjects.php (modified) (history)
  • /trunk/extensions/SemanticInternalObjects/SemanticInternalObjects_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticInternalObjects/SemanticInternalObjects.php
@@ -9,7 +9,7 @@
1010
1111 if ( !defined( 'MEDIAWIKI' ) ) die();
1212
13 -define( 'SIO_VERSION', '0.4.1' );
 13+define( 'SIO_VERSION', '0.5' );
1414
1515 $wgExtensionCredits['parserhook'][] = array(
1616 'path' => __FILE__,
@@ -26,9 +26,12 @@
2727 $wgHooks['smwDeleteSemanticData'][] = 'SIOHandler::updateData';
2828 $wgHooks['smwUpdatePropertySubjects'][] = 'SIOHandler::handleUpdatingOfInternalObjects';
2929 $wgHooks['smwRefreshDataJobs'][] = 'SIOHandler::handleRefreshingOfInternalObjects';
 30+$wgHooks['smwAddToRDFExport'][] = 'SIOSQLStore::createRDF';
3031
31 -$wgExtensionMessagesFiles['SemanticInternalObjects'] = dirname( __FILE__ ) . '/SemanticInternalObjects.i18n.php';
32 -$wgAutoloadClasses['SIOHandler'] = dirname( __FILE__ ) . '/SemanticInternalObjects_body.php';
 32+$siogIP = dirname( __FILE__ );
 33+$wgExtensionMessagesFiles['SemanticInternalObjects'] = $siogIP . '/SemanticInternalObjects.i18n.php';
 34+$wgAutoloadClasses['SIOHandler'] = $siogIP . '/SemanticInternalObjects_body.php';
 35+$wgAutoloadClasses['SIOSQLStore'] = $siogIP . '/SemanticInternalObjects_body.php';
3336
3437 function siofRegisterParserFunctions( &$parser ) {
3538 $parser->setFunctionHook( 'set_internal', array( 'SIOHandler', 'doSetInternal' ) );
Index: trunk/extensions/SemanticInternalObjects/README
@@ -1,6 +1,6 @@
22 Semantic Internal Objects Extension
33
4 - Version 0.4.1
 4+ Version 0.5
55 Yaron Koren
66
77 This is free software licensed under the GNU General Public License. Please
Index: trunk/extensions/SemanticInternalObjects/SemanticInternalObjects_body.php
@@ -10,10 +10,9 @@
1111 * its properties.
1212 */
1313 class SIOInternalObject {
14 - public $mPropertyValuePairs;
15 -
1614 protected $mMainTitle;
1715 protected $mIndex;
 16+ protected $mPropertyValuePairs;
1817
1918 public function SIOInternalObject( $mainTitle, $index ) {
2019 $this->mMainTitle = $mainTitle;
@@ -24,98 +23,175 @@
2524 public function addPropertyAndValue( $propName, $value ) {
2625 $property = SMWPropertyValue::makeUserProperty( $propName );
2726 $dataValue = SMWDataValueFactory::newPropertyObjectValue( $property, $value );
28 -
2927 if ( $dataValue->isValid() ) {
3028 $this->mPropertyValuePairs[] = array( $property, $dataValue );
3129 } // else - show an error message?
3230 }
3331
 32+ public function getPropertyValuePairs() {
 33+ return $this->mPropertyValuePairs;
 34+ }
 35+
3436 public function getName() {
3537 return $this->mMainTitle->getDBkey() . '#' . $this->mIndex;
3638 }
3739 }
3840
3941 /**
 42+ * The SIOTitle and SIOInternalObjectValue exist for only one reason: in order
 43+ * to be used by SIOSQLStore::createRDF(), to spoof Semantic MediaWiki's
 44+ * RDF-exporting code into thinking that it's dealing with actual wiki pages.
 45+ */
 46+class SIOTitle {
 47+ function __construct ($name, $namespace) {
 48+ $this->mName = $name;
 49+ $this->mNamespace = $namespace;
 50+ }
 51+
 52+ /**
 53+ * Based on functions in Title class
 54+ */
 55+ function getPrefixedName() {
 56+ $s = '';
 57+ if ( 0 != $this->mNamespace ) {
 58+ global $wgContLang;
 59+ $s .= $wgContLang->getNsText( $this->mNamespace ) . ':';
 60+ }
 61+ $s .= $this->mName;
 62+ return $s;
 63+ }
 64+
 65+ function getPrefixedURL() {
 66+ $s = $this->getPrefixedName();
 67+ return wfUrlencode( str_replace( ' ', '_', $s ) );
 68+ }
 69+}
 70+
 71+class SIOInternalObjectValue extends SMWWikiPageValue {
 72+ function __construct($name, $namespace) {
 73+ $this->mSIOTitle = new SIOTitle( $name, $namespace );
 74+ }
 75+ function getExportData() {
 76+ global $smwgNamespace;
 77+ return new SMWExpData( new SMWExpResource( null ) );
 78+ }
 79+
 80+ function getTitle() {
 81+ return $this->mSIOTitle;
 82+ }
 83+
 84+ function getWikiValue() {
 85+ return $this->mSIOTitle->getPrefixedName();
 86+ }
 87+}
 88+
 89+/**
4090 * Class for all database-related actions.
4191 * This class exists mostly because SMWSQLStore2's functions makeSMWPageID()
4292 * and makeSMWPropertyID(), which are needed for the DB access, are both
4393 * protected, and thus can't be accessed externally.
4494 */
4595 class SIOSQLStore extends SMWSQLStore2 {
46 - function getIDsForDeletion( $page_name, $namespace ) {
 96+ static function getIDsForDeletion( $pageName, $namespace ) {
4797 $ids = array();
4898
4999 $iw = '';
50100 $db = wfGetDB( DB_SLAVE );
51 - $res = $db->select( 'smw_ids', array( 'smw_id' ), 'smw_title LIKE ' . $db->addQuotes( $page_name . '#%' ) . ' AND ' . 'smw_namespace=' . $db->addQuotes( $namespace ) . ' AND smw_iw=' . $db->addQuotes( $iw ), 'SIO::getSMWPageObjectIDs', array() );
52 -
 101+ $res = $db->select( 'smw_ids', array( 'smw_id' ), 'smw_title LIKE ' . $db->addQuotes( $pageName . '#%' ) . ' AND ' . 'smw_namespace=' . $db->addQuotes( $namespace ) . ' AND smw_iw=' . $db->addQuotes( $iw ), 'SIO::getSMWPageObjectIDs', array() );
53102 while ( $row = $db->fetchObject( $res ) ) {
54103 $ids[] = $row->smw_id;
55104 }
56 -
57105 return $ids;
58106 }
59107
60 - function getStorageSQL( $main_page_name, $namespace, $internal_object ) {
61 - $main_page_id = $this->makeSMWPageID( $main_page_name, $namespace, '' );
62 - $io_id = $this->makeSMWPageID( $internal_object->getName(), $namespace, '' );
63 -
64 - $up_rels2 = array();
65 - $up_atts2 = array();
66 - $up_text2 = array();
67 -
 108+ function getStorageSQL( $mainPageName, $namespace, $internalObject ) {
 109+ $mainPageID = $this->makeSMWPageID( $mainPageName, $namespace, '' );
 110+ $ioID = $this->makeSMWPageID( $internalObject->getName(), $namespace, '' );
 111+ $upRels2 = array();
 112+ $upAtts2 = array();
 113+ $upText2 = array();
 114+ $upCoords = array();
68115 // set all the properties pointing from this internal object
69 - foreach ( $internal_object->mPropertyValuePairs as $property_value_pair ) {
70 - list( $property, $value ) = $property_value_pair;
71 -
 116+ foreach ( $internalObject->getPropertyValuePairs() as $propertyValuePair ) {
 117+ list( $property, $value ) = $propertyValuePair;
72118 // handling changed in SMW 1.5
73119 if ( method_exists( 'SMWSQLStore2', 'findPropertyTableID' ) ) {
74120 $tableid = SMWSQLStore2::findPropertyTableID( $property );
75 - $is_relation = ( $tableid == 'smw_rels2' );
76 - $is_attribute = ( $tableid == 'smw_atts2' );
77 - $is_text = ( $tableid == 'smw_text2' );
 121+ $isRelation = ( $tableid == 'smw_rels2' );
 122+ $isAttribute = ( $tableid == 'smw_atts2' );
 123+ $isText = ( $tableid == 'smw_text2' );
 124+ // new with SMW 1.5.1 / SM 0.6
 125+ $isCoords = ( $tableid == 'smw_coords' );
78126 } else {
79127 $mode = SMWSQLStore2::getStorageMode( $property->getPropertyTypeID() );
80 - $is_relation = ( $mode == SMW_SQL2_RELS2 );
81 - $is_attribute = ( $mode == SMW_SQL2_ATTS2 );
82 - $is_text = ( $mode == SMW_SQL2_TEXT2 );
 128+ $isRelation = ( $mode == SMW_SQL2_RELS2 );
 129+ $isAttribute = ( $mode == SMW_SQL2_ATTS2 );
 130+ $isText = ( $mode == SMW_SQL2_TEXT2 );
 131+ $isCoords = false;
83132 }
84 -
85 - if ( $is_relation ) {
86 - $up_rels2[] = array(
87 - 's_id' => $io_id,
 133+ if ( $isRelation ) {
 134+ $upRels2[] = array(
 135+ 's_id' => $ioID,
88136 'p_id' => $this->makeSMWPropertyID( $property ),
89137 'o_id' => $this->makeSMWPageID( $value->getDBkey(), $value->getNamespace(), $value->getInterwiki() )
90138 );
91 - } elseif ( $is_attribute ) {
 139+ } elseif ( $isAttribute ) {
92140 $keys = $value->getDBkeys();
93 -
94141 if ( method_exists( $value, 'getValueKey' ) ) {
95 - $value_num = $value->getValueKey();
 142+ $valueNum = $value->getValueKey();
96143 } else {
97 - $value_num = $value->getNumericValue();
 144+ $valueNum = $value->getNumericValue();
98145 }
99 -
100 - $up_atts2[] = array(
101 - 's_id' => $io_id,
 146+ $upAtts2[] = array(
 147+ 's_id' => $ioID,
102148 'p_id' => $this->makeSMWPropertyID( $property ),
103149 'value_unit' => $value->getUnit(),
104 - 'value_xsd' => $keys[0], // FIXME: this fails for all datatypes with more then one value field
105 - 'value_num' => $value_num
 150+ 'value_xsd' => $keys[0],
 151+ 'value_num' => $valueNum
106152 );
107 - } elseif ( $is_text ) {
 153+ } elseif ( $isText ) {
108154 $keys = $value->getDBkeys();
109 -
110 - $up_text2[] = array(
111 - 's_id' => $io_id,
 155+ $upText2[] = array(
 156+ 's_id' => $ioID,
112157 'p_id' => $this->makeSMWPropertyID( $property ),
113 - 'value_blob' => $keys[0] // FIXME: this fails for all datatypes with more then one value field
 158+ 'value_blob' => $keys[0]
114159 );
 160+ } elseif ( $isCoords ) {
 161+ $keys = $value->getDBkeys();
 162+ $upCoords[] = array(
 163+ 's_id' => $ioID,
 164+ 'p_id' => $this->makeSMWPropertyID( $property ),
 165+ 'lat' => $keys[0],
 166+ 'lon' => $keys[1],
 167+ );
115168 }
116169 }
117 -
118 - return array( $up_rels2, $up_atts2, $up_text2 );
 170+ return array( $upRels2, $upAtts2, $upText2, $upCoords );
119171 }
 172+
 173+ static function createRDF( $title, $rdfDataArray ) {
 174+ $pageName = $title->getText();
 175+ $namespace = $title->getNamespace();
 176+
 177+ // go through all SIOs for the current page, create RDF for
 178+ // each one, and add it to the general array
 179+ $iw = '';
 180+ $db = wfGetDB( DB_SLAVE );
 181+ $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_namespace', 'smw_title' ), 'smw_title LIKE ' . $db->addQuotes( $pageName . '#%' ) . ' AND ' . 'smw_namespace=' . $db->addQuotes( $namespace ) . ' AND smw_iw=' . $db->addQuotes( $iw ), 'SIO::getSMWPageObjectIDs', array() );
 182+ while ( $row = $db->fetchObject( $res ) ) {
 183+ $value = new SIOInternalObjectValue( $row->smw_title, $row->smw_namespace );
 184+ $semdata = new SMWSemanticData( $value, false );
 185+ $propertyTables = SMWSQLStore2::getPropertyTables();
 186+ foreach ( $propertyTables as $tableName => $propertyTable ) {
 187+ $data = smwfGetStore()->fetchSemanticData( $row->smw_id, null, $propertyTable );
 188+ foreach ( $data as $d ) {
 189+ $semdata->addPropertyStubValue( reset( $d ), end( $d ) );
 190+ }
 191+ }
 192+ $rdfDataArray[] = SMWExporter::makeExportData( $semdata, null );
 193+ }
 194+ return true;
 195+ }
120196 }
121197
122198 /**
@@ -123,124 +199,109 @@
124200 */
125201 class SIOHandler {
126202
127 - static $cur_page_name = '';
128 - static $cur_page_namespace = 0;
129 - static $internal_object_index = 1;
130 - static $internal_objects = array();
 203+ static $mCurPageName = '';
 204+ static $mCurPageNamespace = 0;
 205+ static $mInternalObjectIndex = 1;
 206+ static $mInternalObjects = array();
131207
132208 public static function clearState( &$parser ) {
133 - self::$cur_page_name = '';
134 - self::$cur_page_namespace = 0;
135 - self::$internal_object_index = 1;
136 -
 209+ self::$mCurPageName = '';
 210+ self::$mCurPageNamespace = 0;
 211+ self::$mInternalObjectIndex = 1;
137212 return true;
138213 }
139214
140215 public static function doSetInternal( &$parser ) {
141 - $main_page_name = $parser->getTitle()->getDBKey();
142 - $main_page_namespace = $parser->getTitle()->getNamespace();
143 -
144 - if ( $main_page_name == self::$cur_page_name &&
145 - $main_page_namespace == self::$cur_page_namespace ) {
146 - self::$internal_object_index++;
 216+ $mainPageName = $parser->getTitle()->getDBKey();
 217+ $mainPageNamespace = $parser->getTitle()->getNamespace();
 218+ if ( $mainPageName == self::$mCurPageName &&
 219+ $mainPageNamespace == self::$mCurPageNamespace ) {
 220+ self::$mInternalObjectIndex++;
147221 } else {
148 - self::$cur_page_name = $main_page_name;
149 - self::$cur_page_namespace = $main_page_namespace;
150 - self::$internal_object_index = 1;
 222+ self::$mCurPageName = $mainPageName;
 223+ self::$mCurPageNamespace = $mainPageNamespace;
 224+ self::$mInternalObjectIndex = 1;
151225 }
152 -
153 - $cur_object_num = self::$internal_object_index;
154 -
 226+ $curObjectNum = self::$mInternalObjectIndex;
155227 $params = func_get_args();
156228 array_shift( $params ); // we already know the $parser...
157 -
158 - $internal_object = new SIOInternalObject( $parser->getTitle(), $cur_object_num );
159 - $obj_to_page_prop_name = array_shift( $params );
160 - $main_page_name = $parser->getTitle()->getText();
161 -
162 - if ( ( $ns_text = $parser->getTitle()->getNsText() ) != '' ) {
163 - $main_page_name = $ns_text . ':' . $main_page_name;
 229+ $internalObject = new SIOInternalObject( $parser->getTitle(), $curObjectNum );
 230+ $objToPagePropName = array_shift( $params );
 231+ $mainPageName = $parser->getTitle()->getText();
 232+ if ( ( $nsText = $parser->getTitle()->getNsText() ) != '' ) {
 233+ $mainPageName = $nsText . ':' . $mainPageName;
164234 }
165 -
166 - $internal_object->addPropertyAndValue( $obj_to_page_prop_name, $main_page_name );
167 -
 235+ $internalObject->addPropertyAndValue( $objToPagePropName, $mainPageName );
168236 foreach ( $params as $param ) {
169 - $parts = explode( '=', trim( $param ), 2 );
170 -
 237+ $parts = explode( "=", trim( $param ), 2 );
171238 if ( count( $parts ) == 2 ) {
172239 $key = $parts[0];
173240 $value = $parts[1];
174 -
175241 // if the property name ends with '#list', it's
176242 // a comma-delimited group of values
177243 if ( substr( $key, - 5 ) == '#list' ) {
178244 $key = substr( $key, 0, strlen( $key ) - 5 );
179 - $list_values = explode( ',', $value );
180 -
181 - foreach ( $list_values as $list_value ) {
182 - $internal_object->addPropertyAndValue( $key, trim( $list_value ) );
 245+ $listValues = explode( ',', $value );
 246+ foreach ( $listValues as $listValue ) {
 247+ $internalObject->addPropertyAndValue( $key, trim( $listValue ) );
183248 }
184249 } else {
185 - $internal_object->addPropertyAndValue( $key, $value );
 250+ $internalObject->addPropertyAndValue( $key, $value );
186251 }
187252 }
188253 }
189 -
190 - self::$internal_objects[] = $internal_object;
 254+ self::$mInternalObjects[] = $internalObject;
191255 }
192256
193257 public static function updateData( $subject ) {
194 - $sio_sql_store = new SIOSQLStore();
195 -
 258+ $sioSQLStore = new SIOSQLStore();
196259 // Find all "pages" in the SMW IDs table that are internal
197260 // objects for this page, and delete their properties from
198261 // the SMW tables.
199 - // Then save the current contents of the $internal_objects
 262+ // Then save the current contents of the $mInternalObjects
200263 // array.
201 - $page_name = $subject->getDBKey();
 264+ $pageName = $subject->getDBKey();
202265 $namespace = $subject->getNamespace();
203 - $ids_for_deletion = $sio_sql_store->getIDsForDeletion( $page_name, $namespace );
 266+ $idsForDeletion = SIOSQLStore::getIDsForDeletion( $pageName, $namespace );
204267
205 - $all_rels2_inserts = array();
206 - $all_atts2_inserts = array();
207 - $all_text2_inserts = array();
208 -
209 - foreach ( self::$internal_objects as $internal_object ) {
210 - list( $up_rels2, $up_atts2, $up_text2 ) = $sio_sql_store->getStorageSQL( $page_name, $namespace, $internal_object );
211 -
212 - $all_rels2_inserts = array_merge( $all_rels2_inserts, $up_rels2 );
213 - $all_atts2_inserts = array_merge( $all_atts2_inserts, $up_atts2 );
214 - $all_text2_inserts = array_merge( $all_text2_inserts, $up_text2 );
 268+ $allRels2Inserts = array();
 269+ $allAtts2Inserts = array();
 270+ $allText2Inserts = array();
 271+ $allCoordsInserts = array();
 272+ foreach ( self::$mInternalObjects as $internalObject ) {
 273+ list( $upRels2, $upAtts2, $upText2, $upCoords ) = $sioSQLStore->getStorageSQL( $pageName, $namespace, $internalObject );
 274+ $allRels2Inserts = array_merge( $allRels2Inserts, $upRels2 );
 275+ $allAtts2Inserts = array_merge( $allAtts2Inserts, $upAtts2 );
 276+ $allText2Inserts = array_merge( $allText2Inserts, $upText2 );
 277+ $allCoordsInserts = array_merge( $allCoordsInserts, $upCoords );
215278 }
216279
217280 // now save everything to the database, in a single transaction
218281 $db = wfGetDB( DB_MASTER );
219282 $db->begin( 'SIO::updatePageData' );
220 -
221 - if ( count( $ids_for_deletion ) > 0 ) {
222 - $ids_string = '(' . implode ( ', ', $ids_for_deletion ) . ')';
223 - $db->delete( 'smw_rels2', array( "(s_id IN $ids_string) OR (o_id IN $ids_string)" ), 'SIO::deleteRels2Data' );
224 - $db->delete( 'smw_atts2', array( "s_id IN $ids_string" ), 'SIO::deleteAtts2Data' );
225 - $db->delete( 'smw_text2', array( "s_id IN $ids_string" ), 'SIO::deleteText2Data' );
 283+ if ( count( $idsForDeletion ) > 0 ) {
 284+ $idsString = '(' . implode ( ', ', $idsForDeletion ) . ')';
 285+ $db->delete( 'smw_rels2', array( "(s_id IN $idsString) OR (o_id IN $idsString)" ), 'SIO::deleteRels2Data' );
 286+ $db->delete( 'smw_atts2', array( "s_id IN $idsString" ), 'SIO::deleteAtts2Data' );
 287+ $db->delete( 'smw_text2', array( "s_id IN $idsString" ), 'SIO::deleteText2Data' );
 288+ $db->delete( 'sm_coords', array( "s_id IN $idsString" ), 'SIO::deleteCoordsData' );
226289 }
227290
228 - if ( count( $all_rels2_inserts ) > 0 ) {
229 - $db->insert( 'smw_rels2', $all_rels2_inserts, 'SIO::updateRels2Data' );
 291+ if ( count( $allRels2Inserts ) > 0 ) {
 292+ $db->insert( 'smw_rels2', $allRels2Inserts, 'SIO::updateRels2Data' );
230293 }
231 -
232 - if ( count( $all_atts2_inserts ) > 0 ) {
233 - $db->insert( 'smw_atts2', $all_atts2_inserts, 'SIO::updateAtts2Data' );
 294+ if ( count( $allAtts2Inserts ) > 0 ) {
 295+ $db->insert( 'smw_atts2', $allAtts2Inserts, 'SIO::updateAtts2Data' );
234296 }
235 -
236 - if ( count( $all_text2_inserts ) > 0 ) {
237 - $db->insert( 'smw_text2', $all_text2_inserts, 'SIO::updateText2Data' );
 297+ if ( count( $allText2Inserts ) > 0 ) {
 298+ $db->insert( 'smw_text2', $allText2Inserts, 'SIO::updateText2Data' );
238299 }
239 -
 300+ if ( count( $allCoordsInserts ) > 0 ) {
 301+ $db->insert( 'sm_coords', $allCoordsInserts, 'SIO::updateCoordsData' );
 302+ }
240303 // end transaction
241304 $db->commit( 'SIO::updatePageData' );
242 -
243 - self::$internal_objects = array();
244 -
 305+ self::$mInternalObjects = array();
245306 return true;
246307 }
247308
@@ -251,20 +312,16 @@
252313 * etc. should be turned into just "Page name".
253314 */
254315 static function handleUpdatingOfInternalObjects( &$jobs ) {
255 - $unique_titles = array();
256 -
 316+ $uniqueTitles = array();
257317 foreach ( $jobs as $i => $job ) {
258318 $title = Title::makeTitleSafe( $job->title->getNamespace(), $job->title->getText() );
259319 $id = $title->getArticleID();
260 - $unique_titles[$id] = $title;
 320+ $uniqueTitles[$id] = $title;
261321 }
262 -
263322 $jobs = array();
264 -
265 - foreach ( $unique_titles as $id => $title ) {
 323+ foreach ( $uniqueTitles as $id => $title ) {
266324 $jobs[] = new SMWUpdateJob( $title );
267325 }
268 -
269326 return true;
270327 }
271328
@@ -276,16 +333,12 @@
277334 * a SIO object instead of filtering them down to unique titles.
278335 */
279336 static function handleRefreshingOfInternalObjects( &$jobs ) {
280 - $all_jobs = $jobs;
 337+ $allJobs = $jobs;
281338 $jobs = array();
282 -
283 - foreach ( $all_jobs as $job ) {
284 - if ( strpos( $job->title->getText(), '#' ) === false ) {
 339+ foreach ( $allJobs as $job ) {
 340+ if ( strpos( $job->title->getText(), '#' ) === false )
285341 $jobs[] = $job;
286 - }
287342 }
288 -
289 - return true;
290 - }
291 -
292 -}
\ No newline at end of file
 343+ return true;
 344+ }
 345+}

Status & tagging log