r66673 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66672‎ | r66673 | r66674 >
Date:01:08, 20 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Style improvements
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php
@@ -3,6 +3,8 @@
44 * New SQL implementation of SMW's storage abstraction layer.
55 *
66 * @author Markus Krötzsch
 7+ * @author Jeroen De Dauw
 8+ *
79 * @file
810 * @ingroup SMWStore
911 */
@@ -190,7 +192,10 @@
191193
192194 public function getSemanticData( $subject, $filter = false ) {
193195 wfProfileIn( "SMWSQLStore2::getSemanticData (SMW)" );
194 - self::$in_getSemanticData++; // do not clear the cache when called recursively
 196+
 197+ // Do not clear the cache when called recursively.
 198+ self::$in_getSemanticData++;
 199+
195200 // *** Find out if this subject exists ***//
196201 if ( $subject instanceof Title ) { ///TODO: can this still occur?
197202 $sid = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki() );
@@ -203,17 +208,20 @@
204209 } else {
205210 $sid = 0;
206211 }
 212+
207213 if ( $sid == 0 ) { // no data, safe our time
208214 /// NOTE: we consider redirects for getting $sid, so $sid == 0 also means "no redirects"
209215 self::$in_getSemanticData--;
210216 wfProfileOut( "SMWSQLStore2::getSemanticData (SMW)" );
211217 return isset( $svalue ) ? ( new SMWSemanticData( $svalue ) ):null;
212218 }
 219+
213220 // *** Prepare the cache ***//
214221 if ( !array_key_exists( $sid, $this->m_semdata ) ) { // new cache entry
215222 $this->m_semdata[$sid] = new SMWSemanticData( $svalue, false );
216223 $this->m_sdstate[$sid] = array();
217224 }
 225+
218226 if ( ( count( $this->m_semdata ) > 20 ) && ( self::$in_getSemanticData == 1 ) ) {
219227 // prevent memory leak;
220228 // It is not so easy to find the sweet spot between cache size and performance gains (both memory and time),
@@ -221,25 +229,34 @@
222230 $this->m_semdata = array( $sid => $this->m_semdata[$sid] );
223231 $this->m_sdstate = array( $sid => $this->m_sdstate[$sid] );
224232 }
 233+
225234 // *** Read the data ***//
226235 foreach ( self::getPropertyTables() as $tid => $proptable ) {
227236 if ( array_key_exists( $tid, $this->m_sdstate[$sid] ) ) continue;
 237+
228238 if ( $filter !== false ) {
229239 $relevant = false;
 240+
230241 foreach ( $filter as $typeid ) {
231242 $relevant = $relevant || self::tableFitsType( $tid, $typeid );
232243 }
 244+
233245 if ( !$relevant ) continue;
234246 }
 247+
235248 $data = $this->fetchSemanticData( $sid, $svalue, $proptable );
 249+
236250 foreach ( $data as $d ) {
237251 $this->m_semdata[$sid]->addPropertyStubValue( reset( $d ), end( $d ) );
238252 }
 253+
239254 $this->m_sdstate[$sid][$tid] = true;
240255 }
241256
242257 self::$in_getSemanticData--;
 258+
243259 wfProfileOut( "SMWSQLStore2::getSemanticData (SMW)" );
 260+
244261 return $this->m_semdata[$sid];
245262 }
246263
@@ -254,6 +271,7 @@
255272 */
256273 public function getPropertyValues( $subject, SMWPropertyValue $property, $requestoptions = null, $outputformat = '' ) {
257274 wfProfileIn( "SMWSQLStore2::getPropertyValues (SMW)" );
 275+
258276 if ( $property->isInverse() ) { // inverses are working differently
259277 $noninverse = clone $property;
260278 $noninverse->setInverse( false );
@@ -261,22 +279,27 @@
262280 } elseif ( $subject !== null ) { // subject given, use semantic data cache:
263281 $sd = $this->getSemanticData( $subject, array( $property->getPropertyTypeID() ) );
264282 $result = $this->applyRequestOptions( $sd->getPropertyValues( $property ), $requestoptions );
 283+
265284 if ( $outputformat != '' ) { // reformat cached values
266285 $newres = array();
 286+
267287 foreach ( $result as $dv ) {
268288 $ndv = clone $dv;
269289 $ndv->setOutputFormat( $outputformat );
270290 $newres[] = $ndv;
271291 }
 292+
272293 $result = $newres;
273294 }
274295 } else { // no subject given, get all values for the given property
275296 $pid = $this->getSMWPropertyID( $property );
276297 $tableid = self::findPropertyTableID( $property );
 298+
277299 if ( ( $pid == 0 ) || ( $tableid == '' ) ) {
278300 wfProfileOut( "SMWSQLStore2::getPropertyValues (SMW)" );
279301 return array();
280302 }
 303+
281304 $proptables = self::getPropertyTables();
282305 $data = $this->fetchSemanticData( $pid, $property, $proptables[$tableid], false, $requestoptions );
283306 $result = array();
@@ -384,7 +407,6 @@
385408 }
386409
387410 if ( !$issubject ) { // Needed to apply sorting/string matching in query; only with fixed property.
388 - //.// TODO: support multiple value and label indexes.
389411 list( $sig, $valueIndex, $labelIndex ) = self::getTypeSignature( $object->getPropertyTypeID() );
390412 $valuecolumn = ( array_key_exists( $valueIndex, $selectvalues ) ) ? $selectvalues[$valueIndex] : '';
391413 $labelcolumn = ( array_key_exists( $labelIndex, $selectvalues ) ) ? $selectvalues[$labelIndex] : '';
@@ -404,6 +426,7 @@
405427 } elseif ( !$proptable->fixedproperty ) { // use joined or predefined property name
406428 if ( $proptable->specpropsonly ) {
407429 $propertyname = array_search( $row->p_id, self::$special_ids );
 430+
408431 if ( $propertyname === false ) { // unknown property that uses a special type, maybe by some extension; look it up in the DB
409432 // NOTE: this is just an emergency fallback but not a fast solution; extensions may prefer to use non-special datatypes for new properties!
410433 $propertyname = $db->selectField( 'smw_ids', 'smw_title', array( 'smw_id' => $row->p_id ), 'SMW::getSemanticData-LatePropertyFetch' );
@@ -1233,8 +1256,10 @@
12341257 */
12351258 protected function setupTables( $verbose, $db ) {
12361259 global $wgDBtype;
 1260+
12371261 extract( $db->tableNames( 'smw_ids', 'smw_spec2', 'smw_conccache', 'smw_conc2' ) );
12381262 $reportTo = $verbose ? $this:null; // use $this to report back from static SMWSQLHelpers
 1263+
12391264 // repeatedly used DB field types defined here for convenience
12401265 $dbtypes = array( 't' => SMWSQLHelpers::getStandardDBType( 'title' ),
12411266 'u' => ( $wgDBtype == 'postgres' ? 'TEXT' : 'VARCHAR(63) binary' ),
@@ -1245,7 +1270,7 @@
12461271 'p' => SMWSQLHelpers::getStandardDBType( 'id' ),
12471272 'n' => SMWSQLHelpers::getStandardDBType( 'namespace' ),
12481273 'w' => SMWSQLHelpers::getStandardDBType( 'iw' ) );
1249 -
 1274+//.//
12501275 // DB update: field renaming between SMW 1.3 and SMW 1.4
12511276 if ( ( $db->tableExists( $smw_spec2 ) ) && ( $db->fieldExists( $smw_spec2, 'sp_id', 'SMWSQLStore2::setup' ) ) ) {
12521277 if ( $wgDBtype == 'postgres' ) {
@@ -1268,7 +1293,9 @@
12691294 SMWSQLHelpers::setupTable( $smw_conccache,
12701295 array( 's_id' => $dbtypes['p'] . ' NOT NULL',
12711296 'o_id' => $dbtypes['p'] . ' NOT NULL' ), $db, $reportTo );
 1297+
12721298 SMWSQLHelpers::setupIndex( $smw_conccache, array( 'o_id' ), $db );
 1299+
12731300 // set up concept descriptions
12741301 SMWSQLHelpers::setupTable( $smw_conc2,
12751302 array( 's_id' => $dbtypes['p'] . ' NOT NULL' .
@@ -1291,14 +1318,18 @@
12921319 $fieldarray = array( 's_title' => $dbtypes['t'] . ' NOT NULL', 's_namespace' => $dbtypes['n'] . ' NOT NULL' );
12931320 $indexes = array( 's_title,s_namespace' );
12941321 }
 1322+
12951323 if ( $proptable->fixedproperty == false ) {
12961324 $fieldarray['p_id'] = $dbtypes['p'] . ' NOT NULL';
12971325 $indexes[] = 'p_id';
12981326 }
 1327+
12991328 foreach ( $proptable->objectfields as $fieldname => $typeid ) {
13001329 $fieldarray[$fieldname] = $dbtypes[$typeid];
13011330 }
 1331+
13021332 $indexes = array_merge( $indexes, $proptable->indexes );
 1333+
13031334 SMWSQLHelpers::setupTable( $db->tableName( $proptable->name ), $fieldarray, $db, $reportTo );
13041335 SMWSQLHelpers::setupIndex( $db->tableName( $proptable->name ), $indexes, $db );
13051336 }
@@ -1313,12 +1344,16 @@
13141345 */
13151346 protected function setupPredefinedProperties( $verbose, $db ) {
13161347 global $wgDBtype;
 1348+
13171349 $this->reportProgress( "Setting up internal property indices ...\n", $verbose );
 1350+
13181351 // Check if we already have this structure
13191352 $borderiw = $db->selectField( 'smw_ids', 'smw_iw', 'smw_id=' . $db->addQuotes( 50 ) );
 1353+
13201354 if ( $borderiw != SMW_SQL2_SMWBORDERIW ) {
13211355 $this->reportProgress( " ... allocate space for internal properties\n", $verbose );
13221356 $this->moveSMWPageID( 50 ); // make sure position 50 is empty
 1357+
13231358 $db->insert( 'smw_ids', array(
13241359 'smw_id' => 50,
13251360 'smw_title' => '',
@@ -1329,16 +1364,20 @@
13301365 ); // put dummy "border element" on index 50
13311366
13321367 $this->reportProgress( " ", $verbose );
 1368+
13331369 for ( $i = 0; $i < 50; $i++ ) { // make way for built-in ids
13341370 $this->moveSMWPageID( $i );
13351371 $this->reportProgress( ".", $verbose );
13361372 }
 1373+
13371374 $this->reportProgress( "done\n", $verbose );
13381375 } else {
13391376 $this->reportProgress( " ... space for internal properties already allocated.\n", $verbose );
13401377 }
 1378+
13411379 // now write actual properties; do that each time, it is cheap enough and we can update sortkeys by current language
13421380 $this->reportProgress( " ... writing entries for internal properties.\n", $verbose );
 1381+
13431382 foreach ( self::$special_ids as $prop => $id ) {
13441383 $p = SMWPropertyValue::makeProperty( $prop );
13451384 $db->replace( 'smw_ids', array( 'smw_id' ), array(
@@ -1350,29 +1389,36 @@
13511390 ), 'SMW::setup'
13521391 );
13531392 }
 1393+
13541394 if ( $wgDBtype == 'postgres' ) {
13551395 $this->reportProgress( " ... updating smw_ids_smw_id_seq sequence accordingly.\n", $verbose );
13561396 $max = $db->selectField( 'smw_ids', 'max(smw_id)', array(), __METHOD__ );
13571397 $max += 1;
13581398 $db->query( "ALTER SEQUENCE smw_ids_smw_id_seq RESTART WITH {$max}", __METHOD__ );
13591399 }
 1400+
13601401 $this->reportProgress( "Internal properties initialised successfully.\n", $verbose );
13611402 }
13621403
13631404 public function drop( $verbose = true ) {
13641405 global $wgDBtype;
 1406+
13651407 $this->reportProgress( "Deleting all database content and tables generated by SMW ...\n\n", $verbose );
13661408 $db = wfGetDB( DB_MASTER );
13671409 $tables = array( 'smw_ids', 'smw_conc2', 'smw_conccache' );
 1410+
13681411 foreach ( self::getPropertyTables() as $proptable ) {
13691412 $tables[] = $proptable->name;
13701413 }
 1414+
13711415 foreach ( $tables as $table ) {
13721416 $name = $db->tableName( $table );
13731417 $db->query( 'DROP TABLE' . ( $wgDBtype == 'postgres' ? '':' IF EXISTS' ) . $name, 'SMWSQLStore2::drop' );
13741418 $this->reportProgress( " ... dropped table $name.\n", $verbose );
13751419 }
 1420+
13761421 $this->reportProgress( "All data removed successfully.\n", $verbose );
 1422+
13771423 return true;
13781424 }
13791425
@@ -1382,16 +1428,20 @@
13831429
13841430 // update by MediaWiki page id --> make sure we get all pages
13851431 $tids = array();
 1432+
13861433 for ( $i = $index; $i < $index + $count; $i++ ) { // array of ids
13871434 $tids[] = $i;
13881435 }
 1436+
13891437 $titles = Title::newFromIDs( $tids );
 1438+
13901439 foreach ( $titles as $title ) {
13911440 // set $wgTitle, in case semantic data is set based
13921441 // on values not originating from the page (such as
13931442 // via the External Data extension)
13941443 global $wgTitle;
13951444 $wgTitle = $title;
 1445+
13961446 if ( ( $namespaces == false ) || ( in_array( $title->getNamespace(), $namespaces ) ) ) {
13971447 $updatejobs[] = new SMWUpdateJob( $title );
13981448 $emptyrange = false;
@@ -1402,12 +1452,16 @@
14031453 $db = wfGetDB( DB_SLAVE );
14041454 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_title', 'smw_namespace', 'smw_iw' ),
14051455 "smw_id >= $index AND smw_id < " . $db->addQuotes( $index + $count ), __METHOD__ );
 1456+
14061457 foreach ( $res as $row ) {
14071458 $emptyrange = false; // note this even if no jobs were created
 1459+
14081460 if ( ( $namespaces != false ) && ( !in_array( $row->smw_namespace, $namespaces ) ) ) continue;
 1461+
14091462 if ( ( $row->smw_iw == '' ) || ( $row->smw_iw == SMW_SQL2_SMWREDIIW ) ) { // objects representing pages in the wiki, even special pages
14101463 // TODO: special treament of redirects needed, since the store will not act on redirects that did not change according to its records
14111464 $title = Title::makeTitle( $row->smw_namespace, $row->smw_title );
 1465+
14121466 if ( !$title->exists() ) {
14131467 $updatejobs[] = new SMWUpdateJob( $title );
14141468 }
@@ -1426,15 +1480,19 @@
14271481 $job->run();
14281482 }
14291483 }
 1484+
14301485 $nextpos = $index + $count;
 1486+
14311487 if ( $emptyrange ) { // nothing found, check if there will be more pages later on
14321488 $next1 = $db->selectField( 'page', 'page_id', "page_id >= $nextpos", __METHOD__, array( 'ORDER BY' => "page_id ASC" ) );
14331489 $next2 = $db->selectField( 'smw_ids', 'smw_id', "smw_id >= $nextpos", __METHOD__, array( 'ORDER BY' => "smw_id ASC" ) );
14341490 $nextpos = ( ( $next2 != 0 ) && ( $next2 < $next1 ) ) ? $next2:$next1;
14351491 }
 1492+
14361493 $max1 = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
14371494 $max2 = $db->selectField( 'smw_ids', 'MAX(smw_id)', '', __METHOD__ );
14381495 $index = $nextpos ? $nextpos: - 1;
 1496+
14391497 return ( $index > 0 ) ? $index / max( $max1, $max2 ) : 1;
14401498 }
14411499
@@ -1449,10 +1507,14 @@
14501508 public function refreshConceptCache( $concept ) {
14511509 wfProfileIn( 'SMWSQLStore2::refreshConceptCache (SMW)' );
14521510 global $smwgIP;
 1511+
14531512 include_once( "$smwgIP/includes/storage/SMW_SQLStore2_Queries.php" );
 1513+
14541514 $qe = new SMWSQLStore2QueryEngine( $this, wfGetDB( DB_MASTER ) );
14551515 $result = $qe->refreshConceptCache( $concept );
 1516+
14561517 wfProfileOut( 'SMWSQLStore2::refreshConceptCache (SMW)' );
 1518+
14571519 return $result;
14581520 }
14591521
@@ -1464,10 +1526,14 @@
14651527 public function deleteConceptCache( $concept ) {
14661528 wfProfileIn( 'SMWSQLStore2::deleteConceptCache (SMW)' );
14671529 global $smwgIP;
 1530+
14681531 include_once( "$smwgIP/includes/storage/SMW_SQLStore2_Queries.php" );
 1532+
14691533 $qe = new SMWSQLStore2QueryEngine( $this, wfGetDB( DB_MASTER ) );
14701534 $result = $qe->deleteConceptCache( $concept );
 1535+
14711536 wfProfileOut( 'SMWSQLStore2::deleteConceptCache (SMW)' );
 1537+
14721538 return $result;
14731539 }
14741540
@@ -1483,13 +1549,17 @@
14841550 */
14851551 public function getConceptCacheStatus( $concept ) {
14861552 wfProfileIn( 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
 1553+
14871554 $db = wfGetDB( DB_SLAVE );
14881555 $cid = $this->getSMWPageID( $concept->getDBkey(), $concept->getNamespace(), '', false );
 1556+
14891557 $row = $db->selectRow( 'smw_conc2',
14901558 array( 'concept_txt', 'concept_features', 'concept_size', 'concept_depth', 'cache_date', 'cache_count' ),
14911559 array( 's_id' => $cid ), 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
 1560+
14921561 if ( $row !== false ) {
14931562 $result = array( 'size' => $row->concept_size, 'depth' => $row->concept_depth, 'features' => $row->concept_features );
 1563+
14941564 if ( $row->cache_date ) {
14951565 $result['status'] = 'full';
14961566 $result['date'] = $row->cache_date;
@@ -1500,7 +1570,9 @@
15011571 } else {
15021572 $result = array( 'status' => 'no' );
15031573 }
 1574+
15041575 wfProfileOut( 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
 1576+
15051577 return $result;
15061578 }
15071579
@@ -1514,17 +1586,21 @@
15151587 */
15161588 protected function getSQLOptions( $requestoptions, $valuecol = '' ) {
15171589 $sql_options = array();
 1590+
15181591 if ( $requestoptions !== null ) {
15191592 if ( $requestoptions->limit > 0 ) {
15201593 $sql_options['LIMIT'] = $requestoptions->limit;
15211594 }
 1595+
15221596 if ( $requestoptions->offset > 0 ) {
15231597 $sql_options['OFFSET'] = $requestoptions->offset;
15241598 }
 1599+
15251600 if ( ( $valuecol != '' ) && ( $requestoptions->sort ) ) {
15261601 $sql_options['ORDER BY'] = $requestoptions->ascending ? $valuecol : $valuecol . ' DESC';
15271602 }
15281603 }
 1604+
15291605 return $sql_options;
15301606 }
15311607
@@ -1679,6 +1755,7 @@
16801756 if ( ob_get_level() == 0 ) { // be sure to have some buffer, otherwise some PHPs complain
16811757 ob_start();
16821758 }
 1759+
16831760 print $msg;
16841761 ob_flush();
16851762 flush();
@@ -1798,36 +1875,46 @@
17991876 */
18001877 public function getSMWPageIDandSort( $title, $namespace, $iw, &$sort, $canonical ) {
18011878 global $smwgQEqualitySupport;
 1879+
18021880 wfProfileIn( 'SMWSQLStore2::getSMWPageID (SMW)' );
 1881+
18031882 $ckey = "$iw $namespace $title C";
18041883 $nkey = "$iw $namespace $title -";
18051884 $key = ( $canonical ? $ckey:$nkey );
 1885+
18061886 if ( array_key_exists( $key, $this->m_ids ) ) {
18071887 wfProfileOut( 'SMWSQLStore2::getSMWPageID (SMW)' );
18081888 return $this->m_ids[$key];
18091889 }
 1890+
18101891 if ( count( $this->m_ids ) > 1500 ) { // prevent memory leak in very long PHP runs
18111892 $this->m_ids = array();
18121893 }
 1894+
18131895 $db = wfGetDB( DB_SLAVE );
18141896 $id = 0;
 1897+
18151898 if ( $iw != '' ) { // external page; no need to think about redirects
18161899 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_sortkey' ),
18171900 array( 'smw_title' => $title, 'smw_namespace' => $namespace, 'smw_iw' => $iw ),
18181901 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
 1902+
18191903 if ( $row = $db->fetchObject( $res ) ) {
18201904 $id = $row->smw_id;
18211905 $sort = $row->smw_sortkey;
18221906 }
 1907+
18231908 $this->m_ids[ $canonical ? $nkey:$ckey ] = $id; // unique id, make sure cache for canonical+non-cacnonical gets filled
18241909 } else { // check for potential redirects also
18251910 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_iw', 'smw_sortkey' ),
18261911 'smw_title=' . $db->addQuotes( $title ) . ' AND smw_namespace=' . $db->addQuotes( $namespace ) .
18271912 ' AND (smw_iw=' . $db->addQuotes( '' ) . ' OR smw_iw=' . $db->addQuotes( SMW_SQL2_SMWREDIIW ) . ')',
18281913 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
 1914+
18291915 if ( $row = $db->fetchObject( $res ) ) {
18301916 $id = $row->smw_id; // set id in any case, the below check for properties will use even the redirect id in emergency
18311917 $sort = $row->smw_sortkey;
 1918+
18321919 if ( ( $row->smw_iw == '' ) ) { // the id found is unique (canonical and non-canonical); fill cache also for the case *not* asked for
18331920 $this->m_ids[ $canonical ? $nkey:$ckey ] = $id; // (the other cache is filled below)
18341921 } elseif ( $canonical && ( $smwgQEqualitySupport != SMW_EQ_NONE ) ) { // get redirect alias
@@ -1841,17 +1928,21 @@
18421929 's_title=' . $db->addQuotes( $title ) . ' AND s_namespace=' . $db->addQuotes( $namespace ),
18431930 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
18441931 }
 1932+
18451933 if ( $row = $db->fetchObject( $res2 ) ) {
18461934 $id = $row->o_id;
18471935 }
 1936+
18481937 $db->freeResult( $res2 );
18491938 }
18501939 }
18511940 }
 1941+
18521942 $db->freeResult( $res );
18531943
18541944 $this->m_ids[$key] = $id;
18551945 wfProfileOut( 'SMWSQLStore2::getSMWPageID (SMW)' );
 1946+
18561947 return $id;
18571948 }
18581949
@@ -1867,17 +1958,21 @@
18681959 */
18691960 protected function makeSMWPageID( $title, $namespace, $iw, $canonical = true, $sortkey = '' ) {
18701961 wfProfileIn( 'SMWSQLStore2::makeSMWPageID (SMW)' );
 1962+
18711963 $oldsort = '';
18721964 $id = $this->getSMWPageIDandSort( $title, $namespace, $iw, $oldsort, $canonical );
 1965+
18731966 if ( $id == 0 ) {
18741967 $db = wfGetDB( DB_MASTER );
18751968 $sortkey = $sortkey ? $sortkey:( str_replace( '_', ' ', $title ) );
 1969+
18761970 $db->insert( 'smw_ids',
18771971 array( 'smw_id' => $db->nextSequenceValue( 'smw_ids_smw_id_seq' ),
18781972 'smw_title' => $title,
18791973 'smw_namespace' => $namespace,
18801974 'smw_iw' => $iw,
18811975 'smw_sortkey' => $sortkey ), 'SMW::makeSMWPageID' );
 1976+
18821977 $id = $db->insertId();
18831978 $this->m_ids["$iw $namespace $title -"] = $id; // fill that cache, even if canonical was given
18841979 // This ID is also authorative for the canonical version.
@@ -1888,7 +1983,9 @@
18891984 $db = wfGetDB( DB_MASTER );
18901985 $db->update( 'smw_ids', array( 'smw_sortkey' => $sortkey ), array( 'smw_id' => $id ), 'SMW::makeSMWPageID' );
18911986 }
 1987+
18921988 wfProfileOut( 'SMWSQLStore2::makeSMWPageID (SMW)' );
 1989+
18931990 return $id;
18941991 }
18951992
@@ -1940,10 +2037,13 @@
19412038 public function cacheSMWPageID( $id, $title, $namespace, $iw ) {
19422039 $ckey = "$iw $namespace $title C";
19432040 $nkey = "$iw $namespace $title -";
 2041+
19442042 if ( count( $this->m_ids ) > 1500 ) { // prevent memory leak in very long PHP runs
19452043 $this->m_ids = array();
19462044 }
 2045+
19472046 $this->m_ids[$nkey] = $id;
 2047+
19482048 if ( $iw != SMW_SQL2_SMWREDIIW ) {
19492049 $this->m_ids[$ckey] = $id;
19502050 }
@@ -1962,18 +2062,22 @@
19632063 */
19642064 protected function makeSMWBnodeID( $sid ) {
19652065 $db = wfGetDB( DB_MASTER );
 2066+
19662067 // check if there is an unused bnode to take:
19672068 $res = $db->select( 'smw_ids', 'smw_id', array( 'smw_title' => '', 'smw_namespace' => 0, 'smw_iw' => SMW_SQL2_SMWIW ),
19682069 'SMW::makeSMWBnodeID', array( 'LIMIT' => 1 ) );
 2070+
19692071 $id = ( $row = $db->fetchObject( $res ) ) ? $row->smw_id:0;
19702072 $db->freeResult( $res );
 2073+
19712074 // claim that bnode:
19722075 if ( $id != 0 ) {
19732076 $db->update( 'smw_ids', array( 'smw_namespace' => $sid ),
19742077 array( 'smw_id' => $id,
19752078 'smw_title' => '',
19762079 'smw_namespace' => 0,
1977 - 'smw_iw' => SMW_SQL2_SMWIW ), 'SMW::makeSMWBnodeID', array( 'LIMIT' => 1 ) );
 2080+ 'smw_iw' => SMW_SQL2_SMWIW ), 'SMW::makeSMWBnodeID', array( 'LIMIT' => 1 ) );
 2081+
19782082 if ( $db->affectedRows() == 0 ) { // Oops, someone was faster (collisions are possible here, no locks)
19792083 $id = 0; // fallback: make a new node (TODO: we could also repeat to try another ID)
19802084 }
@@ -1985,8 +2089,10 @@
19862090 'smw_title' => '',
19872091 'smw_namespace' => $sid,
19882092 'smw_iw' => SMW_SQL2_SMWIW ), 'SMW::makeSMWBnodeID' );
 2093+
19892094 $id = $db->insertId();
19902095 }
 2096+
19912097 return $id;
19922098 }
19932099
@@ -2005,13 +2111,16 @@
20062112 $row = $db->selectRow( 'smw_ids',
20072113 array( 'smw_id', 'smw_namespace', 'smw_title', 'smw_iw', 'smw_sortkey' ),
20082114 array( 'smw_id' => $curid ), 'SMWSQLStore2::moveSMWPageID' );
 2115+
20092116 if ( $row === false ) return; // no id at current position, ignore
 2117+
20102118 if ( $targetid == 0 ) { // append new id
20112119 $db->insert( 'smw_ids', array( 'smw_id' => $db->nextSequenceValue( 'smw_ids_smw_id_seq' ),
20122120 'smw_title' => $row->smw_title,
20132121 'smw_namespace' => $row->smw_namespace,
20142122 'smw_iw' => $row->smw_iw,
20152123 'smw_sortkey' => $row->smw_sortkey ), 'SMW::moveSMWPageID' );
 2124+
20162125 $targetid = $db->insertId();
20172126 } else { // change to given id
20182127 $db->insert( 'smw_ids', array( 'smw_id' => $targetid,
@@ -2020,6 +2129,7 @@
20212130 'smw_iw' => $row->smw_iw,
20222131 'smw_sortkey' => $row->smw_sortkey ), 'SMW::moveSMWPageID' );
20232132 }
 2133+
20242134 $db->delete( 'smw_ids', array( 'smw_id' => $curid ), 'SMWSQLStore2::moveSMWPageID' );
20252135 $this->changeSMWPageID( $curid, $targetid, $row->smw_namespace, $row->smw_namespace );
20262136 }
@@ -2044,16 +2154,19 @@
20452155 protected function changeSMWPageID( $oldid, $newid, $oldnamespace = - 1, $newnamespace = - 1, $sdata = true, $podata = true ) {
20462156 $fname = 'SMW::changeSMWPageID';
20472157 $db = wfGetDB( DB_MASTER );
 2158+
20482159 // Update bnode references that use namespace field to store ids:
20492160 if ( $sdata ) { // bnodes are part of the data of a subject
20502161 $db->update( 'smw_ids', array( 'smw_namespace' => $newid ),
20512162 array( 'smw_title' => '', 'smw_namespace' => $oldid, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
20522163 }
 2164+
20532165 // change all id entries in property tables:
20542166 foreach ( self::getPropertyTables() as $proptable ) {
20552167 if ( $sdata && $proptable->idsubject ) {
20562168 $db->update( $proptable->name, array( 's_id' => $newid ), array( 's_id' => $oldid ), $fname );
20572169 }
 2170+
20582171 if ( $podata ) {
20592172 if ( ( ( $oldnamespace == - 1 ) || ( $oldnamespace == SMW_NS_PROPERTY ) ) && ( $proptable->fixedproperty == false ) ) {
20602173 if ( ( $newnamespace == - 1 ) || ( $newnamespace == SMW_NS_PROPERTY ) ) {
@@ -2062,6 +2175,7 @@
20632176 $db->delete( $proptable->name, array( 'p_id' => $oldid ), $fname );
20642177 }
20652178 }
 2179+
20662180 foreach ( $proptable->objectfields as $fieldname => $type ) {
20672181 if ( $type == 'p' ) {
20682182 $db->update( $proptable->name, array( $fieldname => $newid ), array( $fieldname => $oldid ), $fname );
@@ -2090,9 +2204,12 @@
20912205 */
20922206 protected function deleteSemanticData( SMWWikiPageValue $subject ) {
20932207 $db = wfGetDB( DB_MASTER );
 2208+
20942209 $fname = 'SMW::deleteSemanticData';
20952210 $id = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), false );
 2211+
20962212 if ( $id == 0 ) return; // not (directly) used anywhere yet, maybe a redirect but we do not care here
 2213+
20972214 foreach ( self::getPropertyTables() as $proptable ) {
20982215 if ( $proptable->idsubject ) {
20992216 $db->delete( $proptable->name, array( 's_id' => $id ), $fname );
@@ -2100,8 +2217,10 @@
21012218 $db->delete( $proptable->name, array( 's_title' => $subject->getDBkey(), 's_namespace' => $subject->getNamespace() ), $fname );
21022219 }
21032220 }
 2221+
21042222 // also find bnodes used by this ID ...
21052223 $res = $db->select( 'smw_ids', 'smw_id', array( 'smw_title' => '', 'smw_namespace' => $id, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
 2224+
21062225 // ... and delete them as well
21072226 while ( $row = $db->fetchObject( $res ) ) {
21082227 foreach ( self::getPropertyTables() as $proptable ) {
@@ -2110,9 +2229,12 @@
21112230 }
21122231 }
21132232 }
 2233+
21142234 $db->freeResult( $res );
 2235+
21152236 // free all affected bnodes in one call:
21162237 $db->update( 'smw_ids', array( 'smw_namespace' => 0 ), array( 'smw_title' => '', 'smw_namespace' => $id, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
 2238+
21172239 wfRunHooks( 'smwDeleteSemanticData', array( $subject ) );
21182240 }
21192241
@@ -2215,19 +2337,23 @@
22162338 $db->update( 'smw_ids', array( 'smw_iw' => SMW_SQL2_SMWREDIIW ), array( 'smw_id' => $sid ), $fname );
22172339 }
22182340 }
 2341+
22192342 $db->insert( 'smw_redi2', array( 's_title' => $subject_t, 's_namespace' => $subject_ns, 'o_id' => $new_tid ), $fname );
22202343 $this->m_ids[" $subject_ns $subject_t C"] = $new_tid; // "iw" is empty here
22212344 } else { // delete old redirect
22222345 // This case implies $old_tid != 0 (or we would have new_tid == old_tid above).
22232346 // Therefore $subject had a redirect, and it must also have an ID. This shows that $sid != 0 here.
22242347 $this->m_ids[" $subject_ns $subject_t C"] = $sid; // "iw" is empty here
 2348+
22252349 if ( $smwgQEqualitySupport != SMW_EQ_NONE ) { // mark subject as non-redirect
22262350 $db->update( 'smw_ids', array( 'smw_iw' => '' ), array( 'smw_id' => $sid ), $fname );
22272351 }
22282352 }
 2353+
22292354 // *** Flush some caches to be safe, though they are not essential in program runs with redirect updates ***//
22302355 unset( $this->m_semdata[$sid] ); unset( $this->m_semdata[$new_tid] ); unset( $this->m_semdata[$old_tid] );
22312356 unset( $this->m_sdstate[$sid] ); unset( $this->m_sdstate[$new_tid] ); unset( $this->m_sdstate[$old_tid] );
 2357+
22322358 return ( $new_tid == 0 ) ? $sid : $new_tid;
22332359 }
22342360

Follow-up revisions

RevisionCommit summaryAuthorDate
r66674Follow up to r66673jeroendedauw01:16, 20 May 2010

Status & tagging log