r66958 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66957‎ | r66958 | r66959 >
Date:12:40, 27 May 2010
Author:mkroetzsch
Status:deferred
Tags:
Comment:
typo in table name: redicret data got deleted accidentally durnig move operation, so that not all updates that are needed for redirects did happen when moving a page over a redirect (caused Bug 23189)
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php
@@ -5,7 +5,7 @@
66 *
77 * @author Markus Krötzsch
88 * @author Jeroen De Dauw
9 - *
 9+ *
1010 * @file
1111 * @ingroup SMWStore
1212 */
@@ -145,10 +145,10 @@
146146
147147 public function getSemanticData( $subject, $filter = false ) {
148148 wfProfileIn( "SMWSQLStore2::getSemanticData (SMW)" );
149 -
 149+
150150 // Do not clear the cache when called recursively.
151 - self::$in_getSemanticData++;
152 -
 151+ self::$in_getSemanticData++;
 152+
153153 // *** Find out if this subject exists ***//
154154 if ( $subject instanceof Title ) { ///TODO: can this still occur?
155155 $sid = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki() );
@@ -161,20 +161,20 @@
162162 } else {
163163 $sid = 0;
164164 }
165 -
 165+
166166 if ( $sid == 0 ) { // no data, safe our time
167167 /// NOTE: we consider redirects for getting $sid, so $sid == 0 also means "no redirects"
168168 self::$in_getSemanticData--;
169169 wfProfileOut( "SMWSQLStore2::getSemanticData (SMW)" );
170170 return isset( $svalue ) ? ( new SMWSemanticData( $svalue ) ):null;
171171 }
172 -
 172+
173173 // *** Prepare the cache ***//
174174 if ( !array_key_exists( $sid, $this->m_semdata ) ) { // new cache entry
175175 $this->m_semdata[$sid] = new SMWSemanticData( $svalue, false );
176176 $this->m_sdstate[$sid] = array();
177177 }
178 -
 178+
179179 if ( ( count( $this->m_semdata ) > 20 ) && ( self::$in_getSemanticData == 1 ) ) {
180180 // prevent memory leak;
181181 // It is not so easy to find the sweet spot between cache size and performance gains (both memory and time),
@@ -182,49 +182,49 @@
183183 $this->m_semdata = array( $sid => $this->m_semdata[$sid] );
184184 $this->m_sdstate = array( $sid => $this->m_sdstate[$sid] );
185185 }
186 -
 186+
187187 // *** Read the data ***//
188188 foreach ( self::getPropertyTables() as $tid => $proptable ) {
189189 if ( array_key_exists( $tid, $this->m_sdstate[$sid] ) ) continue;
190 -
 190+
191191 if ( $filter !== false ) {
192192 $relevant = false;
193 -
 193+
194194 foreach ( $filter as $typeid ) {
195195 $relevant = $relevant || self::tableFitsType( $tid, $typeid );
196196 }
197 -
 197+
198198 if ( !$relevant ) continue;
199199 }
200 -
 200+
201201 $data = $this->fetchSemanticData( $sid, $svalue, $proptable );
202 -
 202+
203203 foreach ( $data as $d ) {
204204 $this->m_semdata[$sid]->addPropertyStubValue( reset( $d ), end( $d ) );
205205 }
206 -
 206+
207207 $this->m_sdstate[$sid][$tid] = true;
208208 }
209209
210210 self::$in_getSemanticData--;
211 -
 211+
212212 wfProfileOut( "SMWSQLStore2::getSemanticData (SMW)" );
213 -
 213+
214214 return $this->m_semdata[$sid];
215215 }
216216
217217 /**
218 - *
 218+ *
219219 * @param $subject
220220 * @param SMWPropertyValue $property
221221 * @param SMWRequestOptions $requestoptions
222222 * @param string $outputformat
223 - *
 223+ *
224224 * @return array
225225 */
226226 public function getPropertyValues( $subject, SMWPropertyValue $property, $requestoptions = null, $outputformat = '' ) {
227227 wfProfileIn( "SMWSQLStore2::getPropertyValues (SMW)" );
228 -
 228+
229229 if ( $property->isInverse() ) { // inverses are working differently
230230 $noninverse = clone $property;
231231 $noninverse->setInverse( false );
@@ -232,31 +232,31 @@
233233 } elseif ( $subject !== null ) { // subject given, use semantic data cache:
234234 $sd = $this->getSemanticData( $subject, array( $property->getPropertyTypeID() ) );
235235 $result = $this->applyRequestOptions( $sd->getPropertyValues( $property ), $requestoptions );
236 -
 236+
237237 if ( $outputformat != '' ) { // reformat cached values
238238 $newres = array();
239 -
 239+
240240 foreach ( $result as $dv ) {
241241 $ndv = clone $dv;
242242 $ndv->setOutputFormat( $outputformat );
243243 $newres[] = $ndv;
244244 }
245 -
 245+
246246 $result = $newres;
247247 }
248248 } else { // no subject given, get all values for the given property
249249 $pid = $this->getSMWPropertyID( $property );
250250 $tableid = self::findPropertyTableID( $property );
251 -
 251+
252252 if ( ( $pid == 0 ) || ( $tableid == '' ) ) {
253253 wfProfileOut( "SMWSQLStore2::getPropertyValues (SMW)" );
254254 return array();
255255 }
256 -
 256+
257257 $proptables = self::getPropertyTables();
258258 $data = $this->fetchSemanticData( $pid, $property, $proptables[$tableid], false, $requestoptions );
259259 $result = array();
260 -
 260+
261261 foreach ( $data as $dbkeys ) {
262262 $dv = SMWDataValueFactory::newPropertyObjectValue( $property );
263263 if ( $outputformat != '' ) $dv->setOutputFormat( $outputformat );
@@ -264,9 +264,9 @@
265265 $result[] = $dv;
266266 }
267267 }
268 -
 268+
269269 wfProfileOut( "SMWSQLStore2::getPropertyValues (SMW)" );
270 -
 270+
271271 return $result;
272272 }
273273
@@ -295,20 +295,20 @@
296296 *
297297 * @todo Maybe share DB handler; asking for it seems to take quite some time and we do not want
298298 * to change it in one call.
299 - *
 299+ *
300300 * @param $id
301301 * @param $object
302302 * @param $proptable
303303 * @param boolean $issubject
304304 * @param SMWRequestOptions $requestoptions
305 - *
 305+ *
306306 * @return array
307307 */
308308 protected function fetchSemanticData( $id, $object, $proptable, $issubject = true, $requestoptions = null ) {
309309 // stop if there is not enough data:
310310 // properties always need to be given as object, subjects at least if !$proptable->idsubject
311311 if ( ( $id == 0 ) || ( ( $object === null ) && ( !$issubject || !$proptable->idsubject ) ) ) return array();
312 -
 312+
313313 wfProfileIn( "SMWSQLStore2::fetchSemanticData-" . $proptable->name . " (SMW)" );
314314 $result = array();
315315 $db = wfGetDB( DB_SLAVE );
@@ -317,7 +317,7 @@
318318 $from = $db->tableName( $proptable->name ); // always use actual table
319319 $select = '';
320320 $where = '';
321 -
 321+
322322 if ( $issubject != 0 ) { // restrict subject, select property
323323 $where .= ( $proptable->idsubject ) ? 's_id=' . $db->addQuotes( $id ) :
324324 's_title=' . $db->addQuotes( $object->getDBkey() ) .
@@ -331,36 +331,36 @@
332332 } elseif ( !$proptable->fixedproperty ) { // restrict property, but don't select subject
333333 $where .= 'p_id=' . $db->addQuotes( $id );
334334 }
335 -
 335+
336336 $valuecount = 0;
337337 $pagevalues = array(); // collect indices of page-type components of this table (typically at most 1)
338338 $usedistinct = true; // use DISTINCT option only if no text blobs are among values
339339 $selectvalues = array(); // array for all values to be selected, kept to help finding value and label fields below
340 -
 340+
341341 foreach ( $proptable->objectfields as $fieldname => $typeid ) { // now add select entries for object column(s)
342342 if ( $typeid == 'p' ) { // Special case: page id, use smw_id table to insert 4 page-specific values instead of internal id
343343 $from .= ' INNER JOIN ' . $db->tableName( 'smw_ids' ) . " AS o$valuecount ON $fieldname=o$valuecount.smw_id";
344344 $select .= ( ( $select != '' ) ? ',' : '' ) . "$fieldname AS id$valuecount";
345 -
 345+
346346 $selectvalues[$valuecount] = "o$valuecount.smw_title";
347347 $selectvalues[$valuecount + 1] = "o$valuecount.smw_namespace";
348348 $selectvalues[$valuecount + 2] = "o$valuecount.smw_iw";
349349 $selectvalues[$valuecount + 3] = "o$valuecount.smw_sortkey";
350 -
 350+
351351 $pagevalues[] = $valuecount;
352352 $valuecount += 3;
353353 } else { // Just use value as given.
354354 $selectvalues[$valuecount] = $fieldname;
355355 }
356 -
 356+
357357 if ( $typeid == 'l' ) $usedistinct = false;
358358 $valuecount++;
359359 }
360 -
 360+
361361 foreach ( $selectvalues as $index => $field ) {
362362 $select .= ( ( $select != '' ) ? ',' : '' ) . "$field AS v$index";
363363 }
364 -
 364+
365365 if ( !$issubject ) { // Needed to apply sorting/string matching in query; only with fixed property.
366366 list( $sig, $valueIndex, $labelIndex ) = self::getTypeSignature( $object->getPropertyTypeID() );
367367 $valuecolumn = ( array_key_exists( $valueIndex, $selectvalues ) ) ? $selectvalues[$valueIndex] : '';
@@ -374,14 +374,14 @@
375375 $res = $db->select( $from, $select, $where, 'SMW::getSemanticData',
376376 ( $usedistinct ? $this->getSQLOptions( $requestoptions, $valuecolumn ) + array( 'DISTINCT' ) :
377377 $this->getSQLOptions( $requestoptions, $valuecolumn ) ) );
378 -
 378+
379379 while ( $row = $db->fetchObject( $res ) ) {
380380 if ( !$issubject ) {
381381 $propertyname = 'fixed'; // irrelevant, but use this to check if the data is good
382382 } elseif ( !$proptable->fixedproperty ) { // use joined or predefined property name
383383 if ( $proptable->specpropsonly ) {
384384 $propertyname = array_search( $row->p_id, self::$special_ids );
385 -
 385+
386386 if ( $propertyname === false ) { // unknown property that uses a special type, maybe by some extension; look it up in the DB
387387 // NOTE: this is just an emergency fallback but not a fast solution; extensions may prefer to use non-special datatypes for new properties!
388388 $propertyname = $db->selectField( 'smw_ids', 'smw_title', array( 'smw_id' => $row->p_id ), 'SMW::getSemanticData-LatePropertyFetch' );
@@ -392,68 +392,68 @@
393393 } else { // use fixed property name
394394 $propertyname = $proptable->fixedproperty;
395395 }
396 -
 396+
397397 $valuekeys = array();
398398 reset( $pagevalues );
399 -
 399+
400400 for ( $i = 0; $i < $valuecount; $i++ ) { // read the value fields from the current row
401401 $fieldname = "v$i";
402402 $newvalue = $row->$fieldname;
403 -
 403+
404404 if ( $i === current( $pagevalues ) ) { // special check for pages to filter out internal objects
405405 $iwfield = 'v' . ( $i + 2 );
406406 $iw = $row->$iwfield;
407 -
 407+
408408 if ( ( $iw == SMW_SQL2_SMWIW ) && ( $valuecount == 4 ) && ( $object !== null ) ) {
409409 // read container objects recursively; but only if proptable is of form "p"
410410 // also avoid (hypothetical) double recursion by requiring $object!==null
411411 $i += 3; // skip other page fields of this bnode
412412 $oidfield = 'id' . current( $pagevalues );
413 -
 413+
414414 $newvalue = array();
415 -
 415+
416416 foreach ( self::getPropertyTables() as $tid => $pt ) { // just read all
417417 $newvalue = array_merge( $newvalue, $this->fetchSemanticData( $row->$oidfield, null, $pt ) );
418418 }
419419 } elseif ( ( $iw != '' ) && ( $iw { 0 } == ' : ' ) ) { // other internal object, maybe a DB inconsistency; ignore row
420420 $propertyname = '';
421421 }
422 -
 422+
423423 next( $pagevalues );
424424 }
425 -
 425+
426426 $valuekeys[] = $newvalue;
427427 }
428 -
 428+
429429 if ( $propertyname != '' ) $result[] = $issubject ? array( $propertyname, $valuekeys ):$valuekeys;
430430 }
431 -
 431+
432432 $db->freeResult( $res );
433433 wfProfileOut( "SMWSQLStore2::fetchSemanticData-" . $proptable->name . " (SMW)" );
434 -
 434+
435435 return $result;
436436 }
437 -
 437+
438438 /**
439439 * returns an array of SMWWikiPageValue.
440 - *
 440+ *
441441 * @param SMWPropertyValue $property
442442 * @param $value
443443 * @param SMWRequestOptions $requestoptions
444 - *
 444+ *
445445 * @return array
446446 */
447447 public function getPropertySubjects( SMWPropertyValue $property, $value, $requestoptions = null ) {
448448 /// TODO: should we share code with #ask query computation here? Just use queries?
449449 wfProfileIn( "SMWSQLStore2::getPropertySubjects (SMW)" );
450 -
 450+
451451 if ( $property->isInverse() ) { // inverses are working differently
452452 $noninverse = clone $property;
453453 $noninverse->setInverse( false );
454 -
 454+
455455 $result = $this->getPropertyValues( $value, $noninverse, $requestoptions );
456456 wfProfileOut( "SMWSQLStore2::getPropertySubjects (SMW)" );
457 -
 457+
458458 return $result;
459459 }
460460
@@ -461,19 +461,19 @@
462462 $select = $where = $from = '';
463463 $pid = $this->getSMWPropertyID( $property );
464464 $tableid = self::findPropertyTableID( $property );
465 -
 465+
466466 if ( ( $tableid == '' ) && ( $value !== null ) ) { // maybe a type-polymorphic property like _1; use value to find type
467467 $tableid = self::findTypeTableID( $value->getTypeID() );
468468 }
469 -
 469+
470470 if ( ( $pid == 0 ) || ( $tableid == '' ) || ( ( $value !== null ) && ( !$value->isValid() ) ) ) {
471471 return array();
472472 }
473 -
 473+
474474 $proptables = self::getPropertyTables();
475475 $proptable = $proptables[$tableid];
476476 $db = wfGetDB( DB_SLAVE );
477 -
 477+
478478 if ( $proptable->idsubject ) { // join in smw_ids to get title data
479479 $from = $db->tableName( 'smw_ids' ) . " INNER JOIN " . $db->tableName( $proptable->name ) . " AS t1 ON t1.s_id=smw_id";
480480 $select = 'smw_title AS title, smw_namespace AS namespace, smw_sortkey';
@@ -481,11 +481,11 @@
482482 $from = $db->tableName( $proptable->name ) . " AS t1";
483483 $select = 's_title AS title, s_namespace AS namespace, s_title AS smw_sortkey';
484484 }
485 -
 485+
486486 if ( $proptable->fixedproperty == false ) {
487487 $where .= ( $where ? ' AND ' : '' ) . "t1.p_id=" . $db->addQuotes( $pid );
488488 }
489 -
 489+
490490 $this->prepareValueQuery( $from, $where, $proptable, $value, 1 );
491491
492492 // *** Now execute the query and read the results ***//
@@ -494,14 +494,14 @@
495495 $where . $this->getSQLConditions( $requestoptions, 'smw_sortkey', 'smw_sortkey', $where != '' ),
496496 'SMW::getPropertySubjects',
497497 $this->getSQLOptions( $requestoptions, 'smw_sortkey' ) );
498 -
 498+
499499 while ( $row = $db->fetchObject( $res ) ) {
500500 $result[] = SMWWikiPageValue::makePage( $row->title, $row->namespace, $row->smw_sortkey );
501501 }
502 -
 502+
503503 $db->freeResult( $res );
504504 wfProfileOut( "SMWSQLStore2::getPropertySubjects (SMW)" );
505 -
 505+
506506 return $result;
507507 }
508508
@@ -514,8 +514,8 @@
515515 * queried.
516516 *
517517 * @todo Maybe do something about redirects. The old code was
518 - * $oid = $this->getSMWPageID($value->getDBkey(),$value->getNamespace(),$value->getInterwiki(),false);
519 - *
 518+ * $oid = $this->getSMWPageID($value->getDBkey(),$value->getNamespace(),$value->getInterwiki(),false);
 519+ *
520520 * @param $from
521521 * @param $where
522522 * @param $proptable
@@ -524,23 +524,23 @@
525525 */
526526 protected function prepareValueQuery( &$from, &$where, $proptable, $value, $tableindex = 1 ) {
527527 $db = wfGetDB( DB_SLAVE );
528 -
 528+
529529 if ( $value instanceof SMWContainerValue ) { // recursive handling of containers
530530 $joinfield = "t$tableindex." . reset( array_keys( $proptable->objectfields ) ); // this must be a type 'p' object
531531 $proptables = self::getPropertyTables();
532 -
 532+
533533 foreach ( $value->getData()->getProperties() as $subproperty ) {
534534 $tableid = self::findPropertyTableID( $subproperty );
535 -
 535+
536536 if ( ( $tableid == '' ) && ( $value !== null ) ) { // maybe a type-polymorphic property like _1; use value to find type
537537 $tableid = self::findTypeTableID( reset( $value->getData()->getPropertyValues( $subproperty ) )->getTypeID() );
538538 }
539 -
 539+
540540 $subproptable = $proptables[$tableid];
541 -
 541+
542542 foreach ( $value->getData()->getPropertyValues( $subproperty ) as $subvalue ) {
543543 $tableindex++;
544 -
 544+
545545 if ( $subproptable->idsubject ) { // simply add property table to check values
546546 $from .= " INNER JOIN " . $db->tableName( $subproptable->name ) . " AS t$tableindex ON t$tableindex.s_id=$joinfield";
547547 } else { // exotic case with table that uses subject title+namespace in container object (should never happen in SMW core)
@@ -548,21 +548,21 @@
549549 " INNER JOIN " . $db->tableName( $subproptable->name ) . " AS t$tableindex ON " .
550550 "t$tableindex.s_title=ids$tableindex.smw_title AND t$tableindex.s_namespace=ids$tableindex.smw_namespace";
551551 }
552 -
 552+
553553 if ( $subproptable->fixedproperty == false ) { // the ID we get should be !=0, so no point in filtering the converse
554554 $where .= ( $where ? ' AND ' : '' ) . "t$tableindex.p_id=" . $db->addQuotes( $this->getSMWPropertyID( $subproperty ) );
555555 }
556 -
 556+
557557 $this->prepareValueQuery( $from, $where, $subproptable, $subvalue, $tableindex );
558558 }
559559 }
560560 } elseif ( $value !== null ) { // add conditions for given value
561561 $dbkeys = $value->getDBkeys();
562562 $i = 0;
563 -
 563+
564564 foreach ( $proptable->objectfields as $fieldname => $typeid ) {
565565 if ( $i >= count( $dbkeys ) ) break;
566 -
 566+
567567 if ( $typeid == 'p' ) { // Special case: page id, resolve this in advance
568568 $oid = $this->getSMWPageID( $dbkeys[$i], $dbkeys[$i + 1], $dbkeys[$i + 2] );
569569 $i += 3; // skip these additional values (sortkey not needed here)
@@ -570,15 +570,15 @@
571571 } elseif ( $typeid != 'l' ) { // plain value, but not a text blob
572572 $where .= ( $where ? ' AND ' : '' ) . "t$tableindex.$fieldname=" . $db->addQuotes( $dbkeys[$i] );
573573 }
574 -
 574+
575575 $i++;
576576 }
577577 }
578578 }
579579
580580 /**
581 - *
582 - *
 581+ *
 582+ *
583583 * @param SMWPropertyValue $property
584584 * @param SMWRequestOptions $requestoptions
585585 */
@@ -586,20 +586,20 @@
587587 wfProfileIn( "SMWSQLStore2::getAllPropertySubjects (SMW)" );
588588 $result = $this->getPropertySubjects( $property, null, $requestoptions );
589589 wfProfileOut( "SMWSQLStore2::getAllPropertySubjects (SMW)" );
590 -
 590+
591591 return $result;
592592 }
593593
594594 /**
595595 * @todo Restrict this function to SMWWikiPageValue subjects.
596 - *
 596+ *
597597 * @param $subject
598598 * @param SMWRequestOptions $requestoptions
599599 */
600600 public function getProperties( $subject, $requestoptions = null ) {
601601 wfProfileIn( "SMWSQLStore2::getProperties (SMW)" );
602602 $sid = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki() );
603 -
 603+
604604 if ( $sid == 0 ) { // no id, no page, no properties
605605 wfProfileOut( "SMWSQLStore2::getProperties (SMW)" );
606606 return array();
@@ -607,7 +607,7 @@
608608
609609 $db = wfGetDB( DB_SLAVE );
610610 $result = array();
611 -
 611+
612612 if ( $requestoptions !== null ) { // potentially need to get more results, since options apply to union
613613 $suboptions = clone $requestoptions;
614614 $suboptions->limit = $requestoptions->limit + $requestoptions->offset;
@@ -615,10 +615,10 @@
616616 } else {
617617 $suboptions = null;
618618 }
619 -
 619+
620620 foreach ( self::getPropertyTables() as $tid => $proptable ) {
621621 $from = $db->tableName( $proptable->name );
622 -
 622+
623623 if ( $proptable->idsubject ) {
624624 $where = 's_id=' . $db->addQuotes( $sid );
625625 } elseif ( $subject->getInterwiki() == '' ) {
@@ -626,30 +626,30 @@
627627 } else { // subjects with non-emtpy interwiki cannot have properties
628628 continue;
629629 }
630 -
 630+
631631 if ( $proptable->fixedproperty == false ) { // select all properties
632632 $from .= " INNER JOIN " . $db->tableName( 'smw_ids' ) . " ON smw_id=p_id";
633633 $res = $db->select( $from, 'DISTINCT smw_title,smw_sortkey', // select sortkey since it might be used in ordering (needed by Postgres)
634634 $where . $this->getSQLConditions( $suboptions, 'smw_sortkey', 'smw_sortkey' ),
635635 'SMW::getProperties', $this->getSQLOptions( $suboptions, 'smw_sortkey' ) );
636 -
 636+
637637 while ( $row = $db->fetchObject( $res ) ) {
638638 $result[] = SMWPropertyValue::makeProperty( $row->smw_title );
639639 }
640640 } else { // just check if subject occurs in table
641641 $res = $db->select( $from, '*', $where, 'SMW::getProperties', array( 'LIMIT' => 1 ) );
642 -
 642+
643643 if ( $db->numRows( $res ) > 0 ) {
644644 $result[] = SMWPropertyValue::makeProperty( $proptable->fixedproperty );
645645 }
646646 }
647 -
 647+
648648 $db->freeResult( $res );
649649 }
650 -
 650+
651651 $result = $this->applyRequestOptions( $result, $requestoptions ); // apply options to overall result
652652 wfProfileOut( "SMWSQLStore2::getProperties (SMW)" );
653 -
 653+
654654 return $result;
655655 }
656656
@@ -662,38 +662,38 @@
663663 * property to the type of the queried data. So values with the same DB keys
664664 * can be confused. This is a minor issue now since no code is known to use
665665 * this function in cases where this occurs.
666 - *
 666+ *
667667 * @param SMWDataValue $value
668668 * @param SMWRequestOptions $requestoptions
669669 */
670670 public function getInProperties( SMWDataValue $value, $requestoptions = null ) {
671671 wfProfileIn( "SMWSQLStore2::getInProperties (SMW)" );
672 -
 672+
673673 $db = wfGetDB( DB_SLAVE );
674674 $result = array();
675675 $typeid = $value->getTypeID();
676676
677677 // Potentially need to get more results, since options apply to union.
678 - if ( $requestoptions !== null ) {
 678+ if ( $requestoptions !== null ) {
679679 $suboptions = clone $requestoptions;
680680 $suboptions->limit = $requestoptions->limit + $requestoptions->offset;
681681 $suboptions->offset = 0;
682682 } else {
683683 $suboptions = null;
684684 }
685 -
 685+
686686 foreach ( self::getPropertyTables() as $tid => $proptable ) {
687687 if ( !$this->tableFitsType( $tid, $typeid ) ) continue;
688688 $select = $where = $from = '';
689 -
 689+
690690 if ( $proptable->fixedproperty == false ) { // join smw_ids to get property titles
691691 $from = $db->tableName( 'smw_ids' ) . " INNER JOIN " . $db->tableName( $proptable->name ) . " AS t1 ON t1.p_id=smw_id";
692692 $this->prepareValueQuery( $from, $where, $proptable, $value, 1 );
693 -
 693+
694694 $res = $db->select( $from, 'DISTINCT smw_title,smw_sortkey', // select sortkey since it might be used in ordering (needed by Postgres)
695695 $where . $this->getSQLConditions( $suboptions, 'smw_sortkey', 'smw_sortkey', $where != '' ),
696696 'SMW::getInProperties', $this->getSQLOptions( $suboptions, 'smw_sortkey' ) );
697 -
 697+
698698 while ( $row = $db->fetchObject( $res ) ) {
699699 $result[] = SMWPropertyValue::makeProperty( $row->smw_title );
700700 }
@@ -701,17 +701,17 @@
702702 $from = $db->tableName( $proptable->name ) . " AS t1";
703703 $this->prepareValueQuery( $from, $where, $proptable, $value, 1 );
704704 $res = $db->select( $from, '*', $where, 'SMW::getProperties', array( 'LIMIT' => 1 ) );
705 -
 705+
706706 if ( $db->numRows( $res ) > 0 ) {
707707 $result[] = SMWPropertyValue::makeProperty( $proptable->fixedproperty );
708708 }
709709 }
710710 $db->freeResult( $res );
711711 }
712 -
 712+
713713 $result = $this->applyRequestOptions( $result, $requestoptions ); // apply options to overall result
714714 wfProfileOut( "SMWSQLStore2::getInProperties (SMW)" );
715 -
 715+
716716 return $result;
717717 }
718718
@@ -720,17 +720,17 @@
721721 public function deleteSubject( Title $subject ) {
722722 wfProfileIn( 'SMWSQLStore2::deleteSubject (SMW)' );
723723 wfRunHooks( 'SMWSQLStore2::deleteSubjectBefore', array( $this, $subject ) );
724 -
 724+
725725 $this->deleteSemanticData( SMWWikiPageValue::makePageFromTitle( $subject ) );
726726 $this->updateRedirects( $subject->getDBkey(), $subject->getNamespace() ); // also delete redirects, may trigger update jobs!
727 -
 727+
728728 if ( $subject->getNamespace() == SMW_NS_CONCEPT ) { // make sure to clear caches
729729 $db = wfGetDB( DB_MASTER );
730730 $id = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), false );
731731 $db->delete( 'smw_conc2', array( 's_id' => $id ), 'SMW::deleteSubject::Conc2' );
732732 $db->delete( 'smw_conccache', array( 'o_id' => $id ), 'SMW::deleteSubject::Conccache' );
733733 }
734 -
 734+
735735 ///FIXME: if a property page is deleted, more pages may need to be updated by jobs!
736736 ///TODO: who is responsible for these updates? Some update jobs are currently created in SMW_Hooks, some internally in the store
737737 ///TODO: Possibly delete ID here (at least for non-properties/categories, if not used in any place in rels2)
@@ -742,11 +742,11 @@
743743 public function updateData( SMWSemanticData $data ) {
744744 wfProfileIn( "SMWSQLStore2::updateData (SMW)" );
745745 wfRunHooks( 'SMWSQLStore2::updateDataBefore', array( $this, $data ) );
746 -
 746+
747747 $subject = $data->getSubject();
748748 $this->deleteSemanticData( $subject );
749749 $redirects = $data->getPropertyValues( SMWPropertyValue::makeProperty( '_REDI' ) );
750 -
 750+
751751 if ( count( $redirects ) > 0 ) {
752752 $redirect = end( $redirects ); // at most one redirect per page
753753 $this->updateRedirects( $subject->getDBkey(), $subject->getNamespace(), $redirect->getDBkey(), $redirect->getNameSpace() );
@@ -755,7 +755,7 @@
756756 } else {
757757 $this->updateRedirects( $subject->getDBkey(), $subject->getNamespace() );
758758 }
759 -
 759+
760760 // always make an ID (pages without ID cannot be in query results, not even in fixed value queries!):
761761 $sid = $this->makeSMWPageID( $subject->getDBkey(), $subject->getNamespace(), '', true, $subject->getSortkey() );
762762 $updates = array(); // collect data for bulk updates; format: tableid => updatearray
@@ -771,7 +771,7 @@
772772 if ( $subject->getNamespace() == SMW_NS_CONCEPT ) {
773773 $property = SMWPropertyValue::makeProperty( '_CONC' );
774774 $concept_desc = end( $data->getPropertyValues( $property ) );
775 -
 775+
776776 if ( ( $concept_desc !== null ) && ( $concept_desc->isValid() ) ) {
777777 $up_conc2 = array(
778778 'concept_txt' => $concept_desc->getConceptText(),
@@ -789,14 +789,14 @@
790790 'concept_depth' => - 1
791791 );
792792 }
793 -
 793+
794794 $row = $db->selectRow(
795795 'smw_conc2',
796796 array( 'cache_date', 'cache_count' ),
797797 array( 's_id' => $sid ),
798798 'SMWSQLStore2Queries::updateConst2Data'
799799 );
800 -
 800+
801801 if ( ( $row === false ) && ( $up_conc2['concept_txt'] != '' ) ) { // insert newly given data
802802 $up_conc2['s_id'] = $sid;
803803 $db->insert( 'smw_conc2', $up_conc2, 'SMW::updateConc2Data' );
@@ -821,7 +821,7 @@
822822 *
823823 * The function returns the id that was used for writing. Especially, any newly created
824824 * internal id is returned.
825 - *
 825+ *
826826 * @param $updates
827827 * @param SMWSemanticData $data
828828 * @param $pageid
@@ -830,21 +830,21 @@
831831 $subject = $data->getSubject();
832832 $sid = ( $subject !== null ) ? $pageid:$this->makeSMWBnodeID( $pageid );
833833 $proptables = self::getPropertyTables();
834 -
 834+
835835 foreach ( $data->getProperties() as $property ) {
836836 $tableid = self::findPropertyTableID( $property );
837 -
 837+
838838 if ( !$tableid ) { // happens when table is not determined by property; use values to find type
839839 $dv = reset( $data->getPropertyValues( $property ) );
840840 $tableid = self::findTypeTableID( $dv->getTypeID() );
841841 }
842 -
 842+
843843 if ( !$tableid ) { // can't store this data, sorry
844844 return $sid;
845845 }
846 -
 846+
847847 $proptable = $proptables[$tableid];
848 -
 848+
849849 foreach ( $data->getPropertyValues( $property ) as $dv ) {
850850 if ( !$dv->isValid() || ( $tableid == 'smw_redi2' ) ) continue;
851851 // errors are already recorded separately, no need to store them here;
@@ -855,7 +855,7 @@
856856 if ( $proptable->fixedproperty == false ) {
857857 $uvals['p_id'] = $this->makeSMWPropertyID( $property );
858858 }
859 -
 859+
860860 if ( $dv instanceof SMWContainerValue ) { // process subobjects recursively
861861 $bnode = $this->prepareDBUpdates( $updates, $dv->getData(), $pageid );
862862 // Note: tables for container objects MUST have objectfields == array(<somename> => 'p')
@@ -864,7 +864,7 @@
865865 } else {
866866 $dbkeys = $dv->getDBkeys();
867867 reset( $dbkeys );
868 -
 868+
869869 foreach ( $proptable->objectfields as $fieldname => $typeid ) {
870870 if ( $typeid != 'p' ) {
871871 $uvals[$fieldname] = current( $dbkeys );
@@ -875,15 +875,15 @@
876876 $sortkey = next( $dbkeys ); // not used; sortkeys are not set on writing objects
877877 $uvals[$fieldname] = $this->makeSMWPageID( $title, $namespace, $iw );
878878 }
879 -
 879+
880880 next( $dbkeys );
881881 }
882882 }
883 -
 883+
884884 if ( !array_key_exists( $proptable->name, $updates ) ) {
885885 $updates[$proptable->name] = array();
886886 }
887 -
 887+
888888 $updates[$proptable->name][] = $uvals;
889889 }
890890 }
@@ -912,7 +912,7 @@
913913 * not possible to move it reliably in all cases: we cannot distinguish an
914914 * unset sortkey from one that was set to the name of oldtitle. Maybe use
915915 * update jobs right away?
916 - *
 916+ *
917917 * @param Title $oldtitle
918918 * @param Title $newtitle
919919 * @param $pageid
@@ -921,7 +921,7 @@
922922 public function changeTitle( Title $oldtitle, Title $newtitle, $pageid, $redirid = 0 ) {
923923 global $smwgQEqualitySupport;
924924 wfProfileIn( "SMWSQLStore2::changeTitle (SMW)" );
925 -
 925+
926926 // get IDs but do not resolve redirects:
927927 $sid = $this->getSMWPageID( $oldtitle->getDBkey(), $oldtitle->getNamespace(), '', false );
928928 $tid = $this->getSMWPageID( $newtitle->getDBkey(), $newtitle->getNamespace(), '', false );
@@ -936,7 +936,7 @@
937937 } else { // make new (target) id for use in redirect table
938938 $sid = $this->makeSMWPageID( $newtitle->getDBkey(), $newtitle->getNamespace(), '' );
939939 } // at this point, $sid is the id of the target page (according to smw_ids)
940 -
 940+
941941 $this->makeSMWPageID( $oldtitle->getDBkey(), $oldtitle->getNamespace(), SMW_SQL2_SMWREDIIW ); // make redirect id for oldtitle
942942 $db->insert( 'smw_redi2', array( 's_title' => $oldtitle->getDBkey(), 's_namespace' => $oldtitle->getNamespace(), 'o_id' => $sid ),
943943 'SMWSQLStore2::changeTitle' );
@@ -954,12 +954,12 @@
955955 // Delete any existing data from new title:
956956 $this->deleteSemanticData( SMWWikiPageValue::makePageFromTitle( $newtitle ) ); // $newtitle should not have data, but let's be sure
957957 $this->updateRedirects( $newtitle->getDBkey(), $newtitle->getNamespace() ); // may trigger update jobs!
958 -
 958+
959959 // Move all data of old title to new position:
960960 if ( $sid != 0 ) {
961961 $this->changeSMWPageID( $sid, $tid, $oldtitle->getNamespace(), $newtitle->getNamespace(), true, false );
962962 }
963 -
 963+
964964 // Now write a redirect from old title to new one; this also updates references in other tables as needed.
965965 /// TODO: may not be optimal for the standard case that newtitle existed and redirected to oldtitle (PERFORMANCE)
966966 $this->updateRedirects( $oldtitle->getDBkey(), $oldtitle->getNamespace(), $newtitle->getDBkey(), $newtitle->getNamespace() );
@@ -973,11 +973,11 @@
974974 wfProfileIn( 'SMWSQLStore2::getQueryResult (SMW)' );
975975 global $smwgIP;
976976 include_once( "$smwgIP/includes/storage/SMW_SQLStore2_Queries.php" );
977 -
 977+
978978 $qe = new SMWSQLStore2QueryEngine( $this, wfGetDB( DB_SLAVE ) );
979979 $result = $qe->getQueryResult( $query );
980980 wfProfileOut( 'SMWSQLStore2::getQueryResult (SMW)' );
981 -
 981+
982982 return $result;
983983 }
984984
@@ -992,7 +992,7 @@
993993 $db = wfGetDB( DB_SLAVE );
994994 // the query needs to do the filtering of internal properties, else LIMIT is wrong
995995 $queries = array();
996 -
 996+
997997 foreach ( self::getPropertyTables() as $proptable ) {
998998 if ( $proptable->fixedproperty == false ) {
999999 $queries[] = 'SELECT smw_id, smw_title, COUNT(*) as count, smw_sortkey FROM ' .
@@ -1000,24 +1000,24 @@
10011001 $db->addQuotes( '' ) . ' OR smw_iw=' . $db->addQuotes( SMW_SQL2_SMWPREDEFIW ) . ' GROUP BY smw_id,smw_title,smw_sortkey';
10021002 } // else: properties with special tables are ignored for now; maybe fix in the future
10031003 }
1004 -
 1004+
10051005 $query = '(' . implode( ') UNION (', $queries ) . ') ORDER BY smw_sortkey';
10061006 // The following line is possible in MW 1.6 and above only:
10071007 // $query = $db->unionQueries($queries, false) . ' ORDER BY smw_sortkey'; // should probably use $db->makeSelectOptions()
10081008 if ( $requestoptions->limit > 0 ) {
10091009 $query = $db->limitResult( $query, $requestoptions->limit, ( $requestoptions->offset > 0 ) ? $requestoptions->offset:0 );
10101010 }
1011 -
 1011+
10121012 $res = $db->query( $query, 'SMW::getPropertySubjects' );
10131013 $result = array();
1014 -
 1014+
10151015 while ( $row = $db->fetchObject( $res ) ) {
10161016 $result[] = array( SMWPropertyValue::makeProperty( $row->smw_title ), $row->count );
10171017 }
1018 -
 1018+
10191019 $db->freeResult( $res );
10201020 wfProfileOut( "SMWSQLStore2::getPropertiesSpecial (SMW)" );
1021 -
 1021+
10221022 return $result;
10231023 }
10241024
@@ -1030,7 +1030,7 @@
10311031 */
10321032 public function getUnusedPropertiesSpecial( $requestoptions = null ) {
10331033 global $wgDBtype;
1034 -
 1034+
10351035 wfProfileIn( "SMWSQLStore2::getUnusedPropertiesSpecial (SMW)" );
10361036 $db = wfGetDB( DB_SLAVE );
10371037 $fname = 'SMW::getUnusedPropertySubjects';
@@ -1053,17 +1053,17 @@
10541054 } else { // MySQL: use temporary in-memory table
10551055 $sql = "CREATE TEMPORARY TABLE " . $smw_tmp_unusedprops . "( title VARCHAR(255) ) TYPE=MEMORY";
10561056 }
1057 -
 1057+
10581058 $db->query( $sql, $fname );
10591059
10601060 $db->insertSelect( $smw_tmp_unusedprops, 'page', array( 'title' => 'page_title' ),
10611061 array( "page_namespace" => SMW_NS_PROPERTY ), $fname );
10621062
10631063 $smw_ids = $db->tableName( 'smw_ids' );
1064 -
 1064+
10651065 // all predefined properties are assumed to be used:
10661066 $db->deleteJoin( $smw_tmp_unusedprops, $smw_ids, 'title', 'smw_title', array( 'smw_iw' => SMW_SQL2_SMWPREDEFIW ), $fname );
1067 -
 1067+
10681068 // all tables occurring in some property table are used:
10691069 foreach ( self::getPropertyTables() as $proptable ) {
10701070 if ( $proptable->fixedproperty == false ) { // MW does not seem to have a suitable wrapper for this
@@ -1071,11 +1071,11 @@
10721072 " INNER JOIN $smw_ids ON p_id=smw_id WHERE title=smw_title AND smw_iw=" . $db->addQuotes( '' ), $fname );
10731073 } // else: todo
10741074 }
1075 -
 1075+
10761076 // properties that have subproperties are considered to be used
10771077 $proptables = self::getPropertyTables();
10781078 $subtable = $proptables[self::findTypeTableID( '__sup' )]; // find the subproperty table, but consider its signature to be known
1079 -
 1079+
10801080 // (again we have no fitting MW wrapper here:)
10811081 $db->query( "DELETE $smw_tmp_unusedprops.* FROM $smw_tmp_unusedprops," . $db->tableName( $subtable->name ) .
10821082 " INNER JOIN $smw_ids ON o_id=smw_id WHERE title=smw_title", $fname );
@@ -1088,18 +1088,18 @@
10891089 $options = $this->getSQLOptions( $requestoptions, 'title' );
10901090 $options['ORDER BY'] = 'title';
10911091 $res = $db->select( $smw_tmp_unusedprops, 'title', '', $fname, $options );
1092 -
 1092+
10931093 $result = array();
1094 -
 1094+
10951095 while ( $row = $db->fetchObject( $res ) ) {
10961096 $result[] = SMWPropertyValue::makeProperty( $row->title );
10971097 }
1098 -
 1098+
10991099 $db->freeResult( $res );
11001100
11011101 $db->query( "DROP TEMPORARY table $smw_tmp_unusedprops", $fname );
11021102 wfProfileOut( "SMWSQLStore2::getUnusedPropertiesSpecial (SMW)" );
1103 -
 1103+
11041104 return $result;
11051105 }
11061106
@@ -1107,22 +1107,22 @@
11081108 * Implementation of SMWStore::getWantedPropertiesSpecial(). Like all
11091109 * WantedFoo specials, this function is very resource intensive and needs
11101110 * to be cached on medium/large wikis.
1111 - *
 1111+ *
11121112 * @param SMWRequestOptions $requestoptions
1113 - *
 1113+ *
11141114 * @return array
11151115 */
11161116 public function getWantedPropertiesSpecial( $requestoptions = null ) {
11171117 global $smwgPDefaultType;
1118 -
 1118+
11191119 wfProfileIn( "SMWSQLStore2::getWantedPropertiesSpecial (SMW)" );
1120 -
 1120+
11211121 // Note that Wanted Properties must have the default type.
11221122 $proptables = self::getPropertyTables();
11231123 $proptable = $proptables[self::findTypeTableID( $smwgPDefaultType )];
1124 -
 1124+
11251125 $result = array();
1126 -
 1126+
11271127 if ( $proptable->fixedproperty == false ) { // anything else would be crazy, but let's fail gracefully even if the whole world is crazy
11281128 $db = wfGetDB( DB_SLAVE );
11291129 $options = $this->getSQLOptions( $requestoptions, 'title' );
@@ -1132,24 +1132,24 @@
11331133 $db->addQuotes( SMW_NS_PROPERTY ) . ' AND page_title=smw_title)',
11341134 'smw_title, COUNT(*) as count', 'smw_id > 50 AND page_id IS NULL GROUP BY smw_title',
11351135 'SMW::getWantedPropertiesSpecial', $options );
1136 -
 1136+
11371137 while ( $row = $db->fetchObject( $res ) ) {
11381138 $result[] = array( SMWPropertyValue::makeProperty( $row->smw_title ), $row->count );
11391139 }
11401140 }
1141 -
 1141+
11421142 wfProfileOut( "SMWSQLStore2::getWantedPropertiesSpecial (SMW)" );
1143 -
 1143+
11441144 return $result;
11451145 }
11461146
11471147 public function getStatistics() {
11481148 wfProfileIn( 'SMWSQLStore2::getStatistics (SMW)' );
1149 -
 1149+
11501150 $db = wfGetDB( DB_SLAVE );
11511151 $result = array();
11521152 $proptables = self::getPropertyTables();
1153 -
 1153+
11541154 // count number of declared properties by counting "has type" annotations
11551155 $typeprop = SMWPropertyValue::makeProperty( '_TYPE' );
11561156 $typetable = $proptables[self::findPropertyTableID( $typeprop )];
@@ -1157,12 +1157,12 @@
11581158 $row = $db->fetchObject( $res );
11591159 $result['DECLPROPS'] = $row->count;
11601160 $db->freeResult( $res );
1161 -
 1161+
11621162 // count property uses by counting rows in property tables,
11631163 // count used properties by counting distinct properties in each table
11641164 $result['PROPUSES'] = 0;
11651165 $result['USEDPROPS'] = 0;
1166 -
 1166+
11671167 foreach ( self::getPropertyTables() as $proptable ) {
11681168 /// Note: subproperties that are part of container values are counted individually;
11691169 /// It does not seem to be important to filter them by adding more conditions.
@@ -1170,7 +1170,7 @@
11711171 $row = $db->fetchObject( $res );
11721172 $result['PROPUSES'] += $row->count;
11731173 $db->freeResult( $res );
1174 -
 1174+
11751175 if ( $proptable->fixedproperty == false ) {
11761176 $res = $db->select( $proptable->name, 'COUNT(DISTINCT(p_id)) AS count', '', 'SMW::getStatistics' );
11771177 $row = $db->fetchObject( $res );
@@ -1179,7 +1179,7 @@
11801180 $res = $db->select( $proptable->name, '*', '', 'SMW::getStatistics', array( 'LIMIT' => 1 ) );
11811181 if ( $db->numRows( $res ) > 0 ) $result['USEDPROPS']++;
11821182 }
1183 -
 1183+
11841184 $db->freeResult( $res );
11851185 }
11861186
@@ -1192,12 +1192,12 @@
11931193 public function setup( $verbose = true ) {
11941194 $this->reportProgress( "Setting up standard database configuration for SMW ...\n\n", $verbose );
11951195 $this->reportProgress( "Selected storage engine is \"SMWSQLStore2\" (or an extension thereof)\n\n", $verbose );
1196 -
 1196+
11971197 $db = wfGetDB( DB_MASTER );
1198 -
 1198+
11991199 $this->setupTables( $verbose, $db );
12001200 $this->setupPredefinedProperties( $verbose, $db );
1201 -
 1201+
12021202 return true;
12031203 }
12041204
@@ -1228,9 +1228,9 @@
12291229 */
12301230 protected function setupTables( $verbose, $db ) {
12311231 global $wgDBtype;
1232 -
 1232+
12331233 $reportTo = $verbose ? $this : null; // Use $this to report back from static SMWSQLHelpers.
1234 -
 1234+
12351235 // Repeatedly used DB field types defined here for convenience.
12361236 $dbtypes = array(
12371237 't' => SMWSQLHelpers::getStandardDBType( 'title' ),
@@ -1245,7 +1245,7 @@
12461246 );
12471247
12481248 $smw_spec2 = $db->tableName( 'smw_spec2' );
1249 -
 1249+
12501250 // DB update: field renaming between SMW 1.3 and SMW 1.4.
12511251 if ( ( $db->tableExists( $smw_spec2 ) ) && ( $db->fieldExists( $smw_spec2, 'sp_id', 'SMWSQLStore2::setup' ) ) ) {
12521252 if ( $wgDBtype == 'postgres' ) {
@@ -1268,7 +1268,7 @@
12691269 $db,
12701270 $reportTo
12711271 );
1272 -
 1272+
12731273 SMWSQLHelpers::setupIndex( 'smw_ids', array( 'smw_id', 'smw_title,smw_namespace,smw_iw', 'smw_sortkey' ), $db );
12741274
12751275 // Set up concept cache: member elements (s)->concepts (o)
@@ -1281,9 +1281,9 @@
12821282 $db,
12831283 $reportTo
12841284 );
1285 -
 1285+
12861286 SMWSQLHelpers::setupIndex( 'smw_conccache', array( 'o_id' ), $db );
1287 -
 1287+
12881288 // Set up concept descriptions.
12891289 SMWSQLHelpers::setupTable(
12901290 'smw_conc2',
@@ -1300,7 +1300,7 @@
13011301 $db,
13021302 $reportTo
13031303 );
1304 -
 1304+
13051305 SMWSQLHelpers::setupIndex( 'smw_conc2', array( 's_id' ), $db );
13061306
13071307 // Set up all property tables as defined:
@@ -1311,14 +1311,14 @@
13121312
13131313 /**
13141314 * Sets up the property tables.
1315 - *
 1315+ *
13161316 * @param array $dbtypes
13171317 * @param $db
13181318 * @param $reportTo SMWSQLStore2 or null
13191319 */
13201320 protected function setupPropertyTables( array $dbtypes, $db, $reportTo ) {
13211321 $addedCustomTypeSignatures = false;
1322 -
 1322+
13231323 foreach ( self::getPropertyTables() as $proptable ) {
13241324 if ( $proptable->idsubject ) {
13251325 $fieldarray = array( 's_id' => $dbtypes['p'] . ' NOT NULL' );
@@ -1327,32 +1327,32 @@
13281328 $fieldarray = array( 's_title' => $dbtypes['t'] . ' NOT NULL', 's_namespace' => $dbtypes['n'] . ' NOT NULL' );
13291329 $indexes = array( 's_title,s_namespace' );
13301330 }
1331 -
 1331+
13321332 if ( !$proptable->fixedproperty ) {
13331333 $fieldarray['p_id'] = $dbtypes['p'] . ' NOT NULL';
13341334 $indexes[] = 'p_id';
13351335 }
1336 -
 1336+
13371337 foreach ( $proptable->objectfields as $fieldname => $typeid ) {
13381338 // If the type signature is not recognized and the custom signatures have not been added, add them.
13391339 if ( !$addedCustomTypeSignatures && !array_key_exists( $typeid, $dbtypes ) ) {
13401340 wfRunHooks( 'SMWCustomSQLStoreFieldType', array( &$dbtypes ) );
13411341 $addedCustomTypeSignatures = true;
13421342 }
1343 -
 1343+
13441344 // Only add the type when the signature was recognized, otherwise ignore it silently.
13451345 if ( array_key_exists( $typeid, $dbtypes ) ) {
13461346 $fieldarray[$fieldname] = $dbtypes[$typeid];
13471347 }
13481348 }
1349 -
 1349+
13501350 $indexes = array_merge( $indexes, $proptable->indexes );
1351 -
 1351+
13521352 SMWSQLHelpers::setupTable( $proptable->name, $fieldarray, $db, $reportTo );
1353 - SMWSQLHelpers::setupIndex( $proptable->name, $indexes, $db );
1354 - }
 1353+ SMWSQLHelpers::setupIndex( $proptable->name, $indexes, $db );
 1354+ }
13551355 }
1356 -
 1356+
13571357 /**
13581358 * Create some initial DB entries for important built-in properties. Having the DB contents predefined
13591359 * allows us to safe DB calls when certain data is needed. At the same time, the entries in the DB
@@ -1360,16 +1360,16 @@
13611361 */
13621362 protected function setupPredefinedProperties( $verbose, $db ) {
13631363 global $wgDBtype;
1364 -
 1364+
13651365 $this->reportProgress( "Setting up internal property indices ...\n", $verbose );
1366 -
 1366+
13671367 // Check if we already have this structure
13681368 $borderiw = $db->selectField( 'smw_ids', 'smw_iw', 'smw_id=' . $db->addQuotes( 50 ) );
1369 -
 1369+
13701370 if ( $borderiw != SMW_SQL2_SMWBORDERIW ) {
13711371 $this->reportProgress( " ... allocating space for internal properties...\n", $verbose );
13721372 $this->moveSMWPageID( 50 ); // make sure position 50 is empty
1373 -
 1373+
13741374 $db->insert( 'smw_ids', array(
13751375 'smw_id' => 50,
13761376 'smw_title' => '',
@@ -1380,20 +1380,20 @@
13811381 ); // put dummy "border element" on index 50
13821382
13831383 $this->reportProgress( ' ', $verbose );
1384 -
 1384+
13851385 for ( $i = 0; $i < 50; $i++ ) { // make way for built-in ids
13861386 $this->moveSMWPageID( $i );
13871387 $this->reportProgress( '.', $verbose );
13881388 }
1389 -
 1389+
13901390 $this->reportProgress( " done.\n", $verbose );
13911391 } else {
13921392 $this->reportProgress( " ... space for internal properties already allocated.\n", $verbose );
13931393 }
1394 -
 1394+
13951395 // now write actual properties; do that each time, it is cheap enough and we can update sortkeys by current language
13961396 $this->reportProgress( " ... writing entries for internal properties.", $verbose );
1397 -
 1397+
13981398 foreach ( self::$special_ids as $prop => $id ) {
13991399 $p = SMWPropertyValue::makeProperty( $prop );
14001400 $db->replace( 'smw_ids', array( 'smw_id' ), array(
@@ -1405,40 +1405,40 @@
14061406 ), 'SMW::setup'
14071407 );
14081408 }
1409 -
 1409+
14101410 $this->reportProgress( " done.\n", $verbose );
1411 -
 1411+
14121412 if ( $wgDBtype == 'postgres' ) {
14131413 $this->reportProgress( " ... updating smw_ids_smw_id_seq sequence accordingly.\n", $verbose );
1414 -
 1414+
14151415 $max = $db->selectField( 'smw_ids', 'max(smw_id)', array(), __METHOD__ );
14161416 $max += 1;
1417 -
 1417+
14181418 $db->query( "ALTER SEQUENCE smw_ids_smw_id_seq RESTART WITH {$max}", __METHOD__ );
14191419 }
1420 -
 1420+
14211421 $this->reportProgress( "Internal properties initialised successfully.\n", $verbose );
14221422 }
14231423
14241424 public function drop( $verbose = true ) {
14251425 global $wgDBtype;
1426 -
 1426+
14271427 $this->reportProgress( "Deleting all database content and tables generated by SMW ...\n\n", $verbose );
14281428 $db = wfGetDB( DB_MASTER );
14291429 $tables = array( 'smw_ids', 'smw_conc2', 'smw_conccache' );
1430 -
 1430+
14311431 foreach ( self::getPropertyTables() as $proptable ) {
14321432 $tables[] = $proptable->name;
14331433 }
1434 -
 1434+
14351435 foreach ( $tables as $table ) {
14361436 $name = $db->tableName( $table );
14371437 $db->query( 'DROP TABLE' . ( $wgDBtype == 'postgres' ? '' : ' IF EXISTS' ) . $name, 'SMWSQLStore2::drop' );
14381438 $this->reportProgress( " ... dropped table $name.\n", $verbose );
14391439 }
1440 -
 1440+
14411441 $this->reportProgress( "All data removed successfully.\n", $verbose );
1442 -
 1442+
14431443 return true;
14441444 }
14451445
@@ -1448,20 +1448,20 @@
14491449
14501450 // update by MediaWiki page id --> make sure we get all pages
14511451 $tids = array();
1452 -
 1452+
14531453 for ( $i = $index; $i < $index + $count; $i++ ) { // array of ids
14541454 $tids[] = $i;
14551455 }
1456 -
 1456+
14571457 $titles = Title::newFromIDs( $tids );
1458 -
 1458+
14591459 foreach ( $titles as $title ) {
14601460 // set $wgTitle, in case semantic data is set based
14611461 // on values not originating from the page (such as
14621462 // via the External Data extension)
14631463 global $wgTitle;
14641464 $wgTitle = $title;
1465 -
 1465+
14661466 if ( ( $namespaces == false ) || ( in_array( $title->getNamespace(), $namespaces ) ) ) {
14671467 $updatejobs[] = new SMWUpdateJob( $title );
14681468 $emptyrange = false;
@@ -1472,16 +1472,16 @@
14731473 $db = wfGetDB( DB_SLAVE );
14741474 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_title', 'smw_namespace', 'smw_iw' ),
14751475 "smw_id >= $index AND smw_id < " . $db->addQuotes( $index + $count ), __METHOD__ );
1476 -
 1476+
14771477 foreach ( $res as $row ) {
14781478 $emptyrange = false; // note this even if no jobs were created
1479 -
 1479+
14801480 if ( ( $namespaces != false ) && ( !in_array( $row->smw_namespace, $namespaces ) ) ) continue;
1481 -
 1481+
14821482 if ( ( $row->smw_iw == '' ) || ( $row->smw_iw == SMW_SQL2_SMWREDIIW ) ) { // objects representing pages in the wiki, even special pages
14831483 // TODO: special treament of redirects needed, since the store will not act on redirects that did not change according to its records
14841484 $title = Title::makeTitle( $row->smw_namespace, $row->smw_title );
1485 -
 1485+
14861486 if ( !$title->exists() ) {
14871487 $updatejobs[] = new SMWUpdateJob( $title );
14881488 }
@@ -1500,19 +1500,19 @@
15011501 $job->run();
15021502 }
15031503 }
1504 -
 1504+
15051505 $nextpos = $index + $count;
1506 -
 1506+
15071507 if ( $emptyrange ) { // nothing found, check if there will be more pages later on
15081508 $next1 = $db->selectField( 'page', 'page_id', "page_id >= $nextpos", __METHOD__, array( 'ORDER BY' => "page_id ASC" ) );
15091509 $next2 = $db->selectField( 'smw_ids', 'smw_id', "smw_id >= $nextpos", __METHOD__, array( 'ORDER BY' => "smw_id ASC" ) );
15101510 $nextpos = ( ( $next2 != 0 ) && ( $next2 < $next1 ) ) ? $next2:$next1;
15111511 }
1512 -
 1512+
15131513 $max1 = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
15141514 $max2 = $db->selectField( 'smw_ids', 'MAX(smw_id)', '', __METHOD__ );
15151515 $index = $nextpos ? $nextpos: - 1;
1516 -
 1516+
15171517 return ( $index > 0 ) ? $index / max( $max1, $max2 ) : 1;
15181518 }
15191519
@@ -1527,14 +1527,14 @@
15281528 public function refreshConceptCache( $concept ) {
15291529 wfProfileIn( 'SMWSQLStore2::refreshConceptCache (SMW)' );
15301530 global $smwgIP;
1531 -
 1531+
15321532 include_once( "$smwgIP/includes/storage/SMW_SQLStore2_Queries.php" );
1533 -
 1533+
15341534 $qe = new SMWSQLStore2QueryEngine( $this, wfGetDB( DB_MASTER ) );
15351535 $result = $qe->refreshConceptCache( $concept );
1536 -
 1536+
15371537 wfProfileOut( 'SMWSQLStore2::refreshConceptCache (SMW)' );
1538 -
 1538+
15391539 return $result;
15401540 }
15411541
@@ -1546,14 +1546,14 @@
15471547 public function deleteConceptCache( $concept ) {
15481548 wfProfileIn( 'SMWSQLStore2::deleteConceptCache (SMW)' );
15491549 global $smwgIP;
1550 -
 1550+
15511551 include_once( "$smwgIP/includes/storage/SMW_SQLStore2_Queries.php" );
1552 -
 1552+
15531553 $qe = new SMWSQLStore2QueryEngine( $this, wfGetDB( DB_MASTER ) );
15541554 $result = $qe->deleteConceptCache( $concept );
1555 -
 1555+
15561556 wfProfileOut( 'SMWSQLStore2::deleteConceptCache (SMW)' );
1557 -
 1557+
15581558 return $result;
15591559 }
15601560
@@ -1569,17 +1569,17 @@
15701570 */
15711571 public function getConceptCacheStatus( $concept ) {
15721572 wfProfileIn( 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
1573 -
 1573+
15741574 $db = wfGetDB( DB_SLAVE );
15751575 $cid = $this->getSMWPageID( $concept->getDBkey(), $concept->getNamespace(), '', false );
1576 -
 1576+
15771577 $row = $db->selectRow( 'smw_conc2',
15781578 array( 'concept_txt', 'concept_features', 'concept_size', 'concept_depth', 'cache_date', 'cache_count' ),
15791579 array( 's_id' => $cid ), 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
1580 -
 1580+
15811581 if ( $row !== false ) {
15821582 $result = array( 'size' => $row->concept_size, 'depth' => $row->concept_depth, 'features' => $row->concept_features );
1583 -
 1583+
15841584 if ( $row->cache_date ) {
15851585 $result['status'] = 'full';
15861586 $result['date'] = $row->cache_date;
@@ -1590,9 +1590,9 @@
15911591 } else {
15921592 $result = array( 'status' => 'no' );
15931593 }
1594 -
 1594+
15951595 wfProfileOut( 'SMWSQLStore2::getConceptCacheStatus (SMW)' );
1596 -
 1596+
15971597 return $result;
15981598 }
15991599
@@ -1606,21 +1606,21 @@
16071607 */
16081608 protected function getSQLOptions( $requestoptions, $valuecol = '' ) {
16091609 $sql_options = array();
1610 -
 1610+
16111611 if ( $requestoptions !== null ) {
16121612 if ( $requestoptions->limit > 0 ) {
16131613 $sql_options['LIMIT'] = $requestoptions->limit;
16141614 }
1615 -
 1615+
16161616 if ( $requestoptions->offset > 0 ) {
16171617 $sql_options['OFFSET'] = $requestoptions->offset;
16181618 }
1619 -
 1619+
16201620 if ( ( $valuecol != '' ) && ( $requestoptions->sort ) ) {
16211621 $sql_options['ORDER BY'] = $requestoptions->ascending ? $valuecol : $valuecol . ' DESC';
16221622 }
16231623 }
1624 -
 1624+
16251625 return $sql_options;
16261626 }
16271627
@@ -1628,20 +1628,20 @@
16291629 * Transform input parameters into a suitable string of additional SQL conditions.
16301630 * The parameter $valuecol defines the string name of the column to which
16311631 * value restrictions etc. are to be applied.
1632 - *
 1632+ *
16331633 * @param $requestoptions object with options
16341634 * @param $valuecol name of SQL column to which conditions apply
16351635 * @param $labelcol name of SQL column to which string conditions apply, if any
16361636 * @param $addand Boolean to indicate whether the string should begin with " AND " if non-empty
1637 - *
 1637+ *
16381638 * @return string
16391639 */
16401640 protected function getSQLConditions( $requestoptions, $valuecol = '', $labelcol = '', $addand = true ) {
16411641 $sql_conds = '';
1642 -
 1642+
16431643 if ( $requestoptions !== null ) {
16441644 $db = wfGetDB( DB_SLAVE ); /// TODO avoid doing this here again, all callers should have one
1645 -
 1645+
16461646 if ( ( $valuecol != '' ) && ( $requestoptions->boundary !== null ) ) { // Apply value boundary.
16471647 if ( $requestoptions->ascending ) {
16481648 $op = $requestoptions->include_boundary ? ' >= ' : ' > ';
@@ -1650,22 +1650,22 @@
16511651 }
16521652 $sql_conds .= ( $addand ? ' AND ' : '' ) . $valuecol . $op . $db->addQuotes( $requestoptions->boundary );
16531653 }
1654 -
 1654+
16551655 if ( $labelcol != '' ) { // Apply string conditions.
16561656 foreach ( $requestoptions->getStringConditions() as $strcond ) {
16571657 $string = str_replace( '_', '\_', $strcond->string );
1658 -
 1658+
16591659 switch ( $strcond->condition ) {
16601660 case SMWStringCondition::STRCOND_PRE: $string .= '%'; break;
16611661 case SMWStringCondition::STRCOND_POST: $string = '%' . $string; break;
16621662 case SMWStringCondition::STRCOND_MID: $string = '%' . $string . '%'; break;
16631663 }
1664 -
 1664+
16651665 $sql_conds .= ( ( $addand || ( $sql_conds != '' ) ) ? ' AND ' : '' ) . $labelcol . ' LIKE ' . $db->addQuotes( $string );
16661666 }
16671667 }
16681668 }
1669 -
 1669+
16701670 return $sql_conds;
16711671 }
16721672
@@ -1678,30 +1678,30 @@
16791679 */
16801680 protected function applyRequestOptions( $data, $requestoptions ) {
16811681 wfProfileIn( "SMWSQLStore2::applyRequestOptions (SMW)" );
1682 -
 1682+
16831683 if ( ( count( $data ) == 0 ) || ( $requestoptions === null ) ) {
16841684 wfProfileOut( "SMWSQLStore2::applyRequestOptions (SMW)" );
16851685 return $data;
16861686 }
1687 -
 1687+
16881688 $result = array();
16891689 $sortres = array();
1690 -
 1690+
16911691 list( $sig, $valueIndex, $labelIndex ) = self::getTypeSignature( reset( $data )->getTypeID() );
1692 -
 1692+
16931693 $numeric = ( ( $valueIndex >= 0 ) && ( strlen( $sig ) > $valueIndex ) &&
16941694 ( ( $sig { $valueIndex } != 'f' ) || ( $sig { $valueIndex } != 'n' ) ) );
16951695 $i = 0;
1696 -
 1696+
16971697 foreach ( $data as $item ) {
16981698 $ok = true; // keep datavalue only if this remains true
16991699 $keys = $item->getDBkeys();
17001700 $value = array_key_exists( $valueIndex, $keys ) ? $keys[$valueIndex] : '';
17011701 $label = array_key_exists( $labelIndex, $keys ) ? $keys[$labelIndex] : '';
1702 -
 1702+
17031703 if ( $requestoptions->boundary !== null ) { // apply value boundary
17041704 $strc = $numeric ? 0 : strcmp( $value, $requestoptions->boundary );
1705 -
 1705+
17061706 if ( $requestoptions->ascending ) {
17071707 if ( $requestoptions->include_boundary ) {
17081708 $ok = $numeric ? ( $value >= $requestoptions->boundary ) : ( $strc >= 0 );
@@ -1716,7 +1716,7 @@
17171717 }
17181718 }
17191719 }
1720 -
 1720+
17211721 foreach ( $requestoptions->getStringConditions() as $strcond ) { // apply string conditions
17221722 switch ( $strcond->condition ) {
17231723 case SMWStringCondition::STRCOND_PRE:
@@ -1730,40 +1730,40 @@
17311731 break;
17321732 }
17331733 }
1734 -
 1734+
17351735 if ( $ok ) {
17361736 $result[$i] = $item;
17371737 $sortres[$i] = $value; // we cannot use $value as key: it is not unique if there are units!
17381738 $i++;
17391739 }
17401740 }
1741 -
 1741+
17421742 if ( $requestoptions->sort ) {
17431743 $flag = $numeric ? SORT_NUMERIC:SORT_LOCALE_STRING;
1744 -
 1744+
17451745 if ( $requestoptions->ascending ) {
17461746 asort( $sortres, $flag );
17471747 } else {
17481748 arsort( $sortres, $flag );
17491749 }
1750 -
 1750+
17511751 $newres = array();
1752 -
 1752+
17531753 foreach ( $sortres as $key => $value ) {
17541754 $newres[] = $result[$key];
17551755 }
1756 -
 1756+
17571757 $result = $newres;
17581758 }
1759 -
 1759+
17601760 if ( $requestoptions->limit > 0 ) {
17611761 $result = array_slice( $result, $requestoptions->offset, $requestoptions->limit );
17621762 } else {
17631763 $result = array_slice( $result, $requestoptions->offset );
17641764 }
1765 -
 1765+
17661766 wfProfileOut( "SMWSQLStore2::applyRequestOptions (SMW)" );
1767 -
 1767+
17681768 return $result;
17691769 }
17701770
@@ -1776,7 +1776,7 @@
17771777 if ( ob_get_level() == 0 ) { // be sure to have some buffer, otherwise some PHPs complain
17781778 ob_start();
17791779 }
1780 -
 1780+
17811781 print $msg;
17821782 ob_flush();
17831783 flush();
@@ -1800,7 +1800,7 @@
18011801 $dv = SMWDataValueFactory::newTypeIDValue( $typeid );
18021802 self::$type_signatures[$typeid] = array( $dv->getSignature(), $dv->getValueIndex(), $dv->getLabelIndex() );
18031803 }
1804 -
 1804+
18051805 return self::$type_signatures[$typeid];
18061806 }
18071807
@@ -1811,10 +1811,10 @@
18121812 */
18131813 public static function tableFitsSignature( $tableid, $signature ) {
18141814 $proptables = self::getPropertyTables();
1815 -
 1815+
18161816 $tablesig = str_replace( 'p', 'tnwt', $proptables[$tableid]->getFieldSignature() ); // expand internal page type to single fields
18171817 $valuesig = reset( $signature );
1818 -
 1818+
18191819 return ( $valuesig == substr( $tablesig, 0, strlen( $valuesig ) ) );
18201820 }
18211821
@@ -1834,17 +1834,17 @@
18351835 public static function findTypeTableID( $typeid ) {
18361836 if ( !array_key_exists( $typeid, self::$property_table_ids ) ) {
18371837 $signature = self::getTypeSignature( $typeid );
1838 -
 1838+
18391839 foreach ( self::getPropertyTables() as $tid => $proptable ) {
18401840 if ( self::tableFitsSignature( $tid, $signature ) ) {
18411841 self::$property_table_ids[$typeid] = $tid;
18421842 return $tid;
18431843 }
18441844 }
1845 -
 1845+
18461846 self::$property_table_ids[$typeid] = ''; // No matching table found.
18471847 }
1848 -
 1848+
18491849 return self::$property_table_ids[$typeid];
18501850 }
18511851
@@ -1855,19 +1855,19 @@
18561856 public static function findPropertyTableID( $property ) {
18571857 if ( self::$fixed_prop_tables === null ) { // Build lookup array once.
18581858 self::$fixed_prop_tables = array();
1859 -
 1859+
18601860 foreach ( self::getPropertyTables() as $tid => $proptable ) {
18611861 if ( $proptable->fixedproperty != false ) {
18621862 self::$fixed_prop_tables[$proptable->fixedproperty] = $tid;
18631863 }
18641864 }
18651865 }
1866 -
 1866+
18671867 $propertykey = ( $property->isUserDefined() ) ? $property->getDBkey():$property->getPropertyId();
1868 -
 1868+
18691869 if ( array_key_exists( $propertykey, self::$fixed_prop_tables ) ) {
18701870 $signature = self::getTypeSignature( $property->getPropertyTypeID() );
1871 -
 1871+
18721872 if ( self::tableFitsSignature( SMWSQLStore2::$fixed_prop_tables[$propertykey], $signature ) ) {
18731873 return self::$fixed_prop_tables[$propertykey];
18741874 }
@@ -1896,46 +1896,46 @@
18971897 */
18981898 public function getSMWPageIDandSort( $title, $namespace, $iw, &$sort, $canonical ) {
18991899 global $smwgQEqualitySupport;
1900 -
 1900+
19011901 wfProfileIn( 'SMWSQLStore2::getSMWPageID (SMW)' );
1902 -
 1902+
19031903 $ckey = "$iw $namespace $title C";
19041904 $nkey = "$iw $namespace $title -";
19051905 $key = ( $canonical ? $ckey:$nkey );
1906 -
 1906+
19071907 if ( array_key_exists( $key, $this->m_ids ) ) {
19081908 wfProfileOut( 'SMWSQLStore2::getSMWPageID (SMW)' );
19091909 return $this->m_ids[$key];
19101910 }
1911 -
 1911+
19121912 if ( count( $this->m_ids ) > 1500 ) { // prevent memory leak in very long PHP runs
19131913 $this->m_ids = array();
19141914 }
1915 -
 1915+
19161916 $db = wfGetDB( DB_SLAVE );
19171917 $id = 0;
1918 -
 1918+
19191919 if ( $iw != '' ) { // external page; no need to think about redirects
19201920 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_sortkey' ),
19211921 array( 'smw_title' => $title, 'smw_namespace' => $namespace, 'smw_iw' => $iw ),
19221922 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
1923 -
 1923+
19241924 if ( $row = $db->fetchObject( $res ) ) {
19251925 $id = $row->smw_id;
19261926 $sort = $row->smw_sortkey;
19271927 }
1928 -
 1928+
19291929 $this->m_ids[ $canonical ? $nkey:$ckey ] = $id; // unique id, make sure cache for canonical+non-cacnonical gets filled
19301930 } else { // check for potential redirects also
19311931 $res = $db->select( 'smw_ids', array( 'smw_id', 'smw_iw', 'smw_sortkey' ),
19321932 'smw_title=' . $db->addQuotes( $title ) . ' AND smw_namespace=' . $db->addQuotes( $namespace ) .
19331933 ' AND (smw_iw=' . $db->addQuotes( '' ) . ' OR smw_iw=' . $db->addQuotes( SMW_SQL2_SMWREDIIW ) . ')',
19341934 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
1935 -
 1935+
19361936 if ( $row = $db->fetchObject( $res ) ) {
19371937 $id = $row->smw_id; // set id in any case, the below check for properties will use even the redirect id in emergency
19381938 $sort = $row->smw_sortkey;
1939 -
 1939+
19401940 if ( ( $row->smw_iw == '' ) ) { // the id found is unique (canonical and non-canonical); fill cache also for the case *not* asked for
19411941 $this->m_ids[ $canonical ? $nkey:$ckey ] = $id; // (the other cache is filled below)
19421942 } elseif ( $canonical && ( $smwgQEqualitySupport != SMW_EQ_NONE ) ) { // get redirect alias
@@ -1949,21 +1949,21 @@
19501950 's_title=' . $db->addQuotes( $title ) . ' AND s_namespace=' . $db->addQuotes( $namespace ),
19511951 'SMW::getSMWPageID', array( 'LIMIT' => 1 ) );
19521952 }
1953 -
 1953+
19541954 if ( $row = $db->fetchObject( $res2 ) ) {
19551955 $id = $row->o_id;
19561956 }
1957 -
 1957+
19581958 $db->freeResult( $res2 );
19591959 }
19601960 }
19611961 }
1962 -
 1962+
19631963 $db->freeResult( $res );
19641964
19651965 $this->m_ids[$key] = $id;
19661966 wfProfileOut( 'SMWSQLStore2::getSMWPageID (SMW)' );
1967 -
 1967+
19681968 return $id;
19691969 }
19701970
@@ -1979,21 +1979,21 @@
19801980 */
19811981 protected function makeSMWPageID( $title, $namespace, $iw, $canonical = true, $sortkey = '' ) {
19821982 wfProfileIn( 'SMWSQLStore2::makeSMWPageID (SMW)' );
1983 -
 1983+
19841984 $oldsort = '';
19851985 $id = $this->getSMWPageIDandSort( $title, $namespace, $iw, $oldsort, $canonical );
1986 -
 1986+
19871987 if ( $id == 0 ) {
19881988 $db = wfGetDB( DB_MASTER );
19891989 $sortkey = $sortkey ? $sortkey:( str_replace( '_', ' ', $title ) );
1990 -
 1990+
19911991 $db->insert( 'smw_ids',
19921992 array( 'smw_id' => $db->nextSequenceValue( 'smw_ids_smw_id_seq' ),
19931993 'smw_title' => $title,
19941994 'smw_namespace' => $namespace,
19951995 'smw_iw' => $iw,
19961996 'smw_sortkey' => $sortkey ), 'SMW::makeSMWPageID' );
1997 -
 1997+
19981998 $id = $db->insertId();
19991999 $this->m_ids["$iw $namespace $title -"] = $id; // fill that cache, even if canonical was given
20002000 // This ID is also authorative for the canonical version.
@@ -2004,9 +2004,9 @@
20052005 $db = wfGetDB( DB_MASTER );
20062006 $db->update( 'smw_ids', array( 'smw_sortkey' => $sortkey ), array( 'smw_id' => $id ), 'SMW::makeSMWPageID' );
20072007 }
2008 -
 2008+
20092009 wfProfileOut( 'SMWSQLStore2::makeSMWPageID (SMW)' );
2010 -
 2010+
20112011 return $id;
20122012 }
20132013
@@ -2058,13 +2058,13 @@
20592059 public function cacheSMWPageID( $id, $title, $namespace, $iw ) {
20602060 $ckey = "$iw $namespace $title C";
20612061 $nkey = "$iw $namespace $title -";
2062 -
 2062+
20632063 if ( count( $this->m_ids ) > 1500 ) { // prevent memory leak in very long PHP runs
20642064 $this->m_ids = array();
20652065 }
2066 -
 2066+
20672067 $this->m_ids[$nkey] = $id;
2068 -
 2068+
20692069 if ( $iw != SMW_SQL2_SMWREDIIW ) {
20702070 $this->m_ids[$ckey] = $id;
20712071 }
@@ -2083,14 +2083,14 @@
20842084 */
20852085 protected function makeSMWBnodeID( $sid ) {
20862086 $db = wfGetDB( DB_MASTER );
2087 -
 2087+
20882088 // check if there is an unused bnode to take:
20892089 $res = $db->select( 'smw_ids', 'smw_id', array( 'smw_title' => '', 'smw_namespace' => 0, 'smw_iw' => SMW_SQL2_SMWIW ),
20902090 'SMW::makeSMWBnodeID', array( 'LIMIT' => 1 ) );
2091 -
 2091+
20922092 $id = ( $row = $db->fetchObject( $res ) ) ? $row->smw_id:0;
20932093 $db->freeResult( $res );
2094 -
 2094+
20952095 // claim that bnode:
20962096 if ( $id != 0 ) {
20972097 $db->update( 'smw_ids', array( 'smw_namespace' => $sid ),
@@ -2098,7 +2098,7 @@
20992099 'smw_title' => '',
21002100 'smw_namespace' => 0,
21012101 'smw_iw' => SMW_SQL2_SMWIW ), 'SMW::makeSMWBnodeID', array( 'LIMIT' => 1 ) );
2102 -
 2102+
21032103 if ( $db->affectedRows() == 0 ) { // Oops, someone was faster (collisions are possible here, no locks)
21042104 $id = 0; // fallback: make a new node (TODO: we could also repeat to try another ID)
21052105 }
@@ -2110,10 +2110,10 @@
21112111 'smw_title' => '',
21122112 'smw_namespace' => $sid,
21132113 'smw_iw' => SMW_SQL2_SMWIW ), 'SMW::makeSMWBnodeID' );
2114 -
 2114+
21152115 $id = $db->insertId();
21162116 }
2117 -
 2117+
21182118 return $id;
21192119 }
21202120
@@ -2132,16 +2132,16 @@
21332133 $row = $db->selectRow( 'smw_ids',
21342134 array( 'smw_id', 'smw_namespace', 'smw_title', 'smw_iw', 'smw_sortkey' ),
21352135 array( 'smw_id' => $curid ), 'SMWSQLStore2::moveSMWPageID' );
2136 -
 2136+
21372137 if ( $row === false ) return; // no id at current position, ignore
2138 -
 2138+
21392139 if ( $targetid == 0 ) { // append new id
21402140 $db->insert( 'smw_ids', array( 'smw_id' => $db->nextSequenceValue( 'smw_ids_smw_id_seq' ),
21412141 'smw_title' => $row->smw_title,
21422142 'smw_namespace' => $row->smw_namespace,
21432143 'smw_iw' => $row->smw_iw,
21442144 'smw_sortkey' => $row->smw_sortkey ), 'SMW::moveSMWPageID' );
2145 -
 2145+
21462146 $targetid = $db->insertId();
21472147 } else { // change to given id
21482148 $db->insert( 'smw_ids', array( 'smw_id' => $targetid,
@@ -2150,7 +2150,7 @@
21512151 'smw_iw' => $row->smw_iw,
21522152 'smw_sortkey' => $row->smw_sortkey ), 'SMW::moveSMWPageID' );
21532153 }
2154 -
 2154+
21552155 $db->delete( 'smw_ids', array( 'smw_id' => $curid ), 'SMWSQLStore2::moveSMWPageID' );
21562156 $this->changeSMWPageID( $curid, $targetid, $row->smw_namespace, $row->smw_namespace );
21572157 }
@@ -2172,31 +2172,31 @@
21732173 * @param $sdata boolean stating whether to update subject references
21742174 * @param $podata boolean stating if to update property/object references
21752175 */
2176 - protected function changeSMWPageID( $oldid, $newid, $oldnamespace = - 1, $newnamespace = - 1, $sdata = true, $podata = true ) {
 2176+ protected function changeSMWPageID( $oldid, $newid, $oldnamespace = -1, $newnamespace = -1, $sdata = true, $podata = true ) {
21772177 $fname = 'SMW::changeSMWPageID';
21782178 $db = wfGetDB( DB_MASTER );
2179 -
 2179+
21802180 // Update bnode references that use namespace field to store ids:
21812181 if ( $sdata ) { // bnodes are part of the data of a subject
21822182 $db->update( 'smw_ids', array( 'smw_namespace' => $newid ),
21832183 array( 'smw_title' => '', 'smw_namespace' => $oldid, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
21842184 }
2185 -
 2185+
21862186 // change all id entries in property tables:
21872187 foreach ( self::getPropertyTables() as $proptable ) {
21882188 if ( $sdata && $proptable->idsubject ) {
21892189 $db->update( $proptable->name, array( 's_id' => $newid ), array( 's_id' => $oldid ), $fname );
21902190 }
2191 -
 2191+
21922192 if ( $podata ) {
2193 - if ( ( ( $oldnamespace == - 1 ) || ( $oldnamespace == SMW_NS_PROPERTY ) ) && ( $proptable->fixedproperty == false ) ) {
2194 - if ( ( $newnamespace == - 1 ) || ( $newnamespace == SMW_NS_PROPERTY ) ) {
 2193+ if ( ( ( $oldnamespace == -1 ) || ( $oldnamespace == SMW_NS_PROPERTY ) ) && ( $proptable->fixedproperty == false ) ) {
 2194+ if ( ( $newnamespace == -1 ) || ( $newnamespace == SMW_NS_PROPERTY ) ) {
21952195 $db->update( $proptable->name, array( 'p_id' => $newid ), array( 'p_id' => $oldid ), $fname );
21962196 } else {
21972197 $db->delete( $proptable->name, array( 'p_id' => $oldid ), $fname );
21982198 }
21992199 }
2200 -
 2200+
22012201 foreach ( $proptable->objectfields as $fieldname => $type ) {
22022202 if ( $type == 'p' ) {
22032203 $db->update( $proptable->name, array( $fieldname => $newid ), array( $fieldname => $oldid ), $fname );
@@ -2205,8 +2205,8 @@
22062206 }
22072207 }
22082208 // change id entries in concept-related tables:
2209 - if ( $sdata && ( ( $oldnamespace == - 1 ) || ( $oldnamespace == SMW_NS_CONCEPT ) ) ) {
2210 - if ( ( $newnamespace == - 1 ) || ( $newnamespace == SMW_NS_CONCEPT ) ) {
 2209+ if ( $sdata && ( ( $oldnamespace == -1 ) || ( $oldnamespace == SMW_NS_CONCEPT ) ) ) {
 2210+ if ( ( $newnamespace == -1 ) || ( $newnamespace == SMW_NS_CONCEPT ) ) {
22112211 $db->update( 'smw_conc2', array( 's_id' => $newid ), array( 's_id' => $oldid ), $fname );
22122212 $db->update( 'smw_conccache', array( 's_id' => $newid ), array( 's_id' => $oldid ), $fname );
22132213 } else {
@@ -2225,23 +2225,23 @@
22262226 */
22272227 protected function deleteSemanticData( SMWWikiPageValue $subject ) {
22282228 $db = wfGetDB( DB_MASTER );
2229 -
 2229+
22302230 $fname = 'SMW::deleteSemanticData';
22312231 $id = $this->getSMWPageID( $subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), false );
2232 -
 2232+
22332233 if ( $id == 0 ) return; // not (directly) used anywhere yet, maybe a redirect but we do not care here
2234 -
 2234+
22352235 foreach ( self::getPropertyTables() as $proptable ) {
22362236 if ( $proptable->idsubject ) {
22372237 $db->delete( $proptable->name, array( 's_id' => $id ), $fname );
2238 - } elseif ( $proptable->name != 'smw_redi' ) { /// NOTE: redirects are handled by updateRedirects(), not here!
 2238+ } elseif ( $proptable->name != 'smw_redi2' ) { /// NOTE: redirects are handled by updateRedirects(), not here!
22392239 $db->delete( $proptable->name, array( 's_title' => $subject->getDBkey(), 's_namespace' => $subject->getNamespace() ), $fname );
22402240 }
22412241 }
2242 -
 2242+
22432243 // also find bnodes used by this ID ...
22442244 $res = $db->select( 'smw_ids', 'smw_id', array( 'smw_title' => '', 'smw_namespace' => $id, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
2245 -
 2245+
22462246 // ... and delete them as well
22472247 while ( $row = $db->fetchObject( $res ) ) {
22482248 foreach ( self::getPropertyTables() as $proptable ) {
@@ -2250,21 +2250,21 @@
22512251 }
22522252 }
22532253 }
2254 -
 2254+
22552255 $db->freeResult( $res );
2256 -
 2256+
22572257 // free all affected bnodes in one call:
22582258 $db->update( 'smw_ids', array( 'smw_namespace' => 0 ), array( 'smw_title' => '', 'smw_namespace' => $id, 'smw_iw' => SMW_SQL2_SMWIW ), $fname );
2259 -
 2259+
22602260 wfRunHooks( 'smwDeleteSemanticData', array( $subject ) );
22612261 }
22622262
22632263 /**
22642264 * Helper method to write information about some redirect. Various updates
2265 - * can be necessary if redirects are resolved as identities SMW. The title
2266 - * and namespace of the affected page and of its updated redirect target
2267 - * are given. The target can be empty ('') to delete any redirect. Returns
2268 - * the canonical ID that is now to be used for the subject.
 2265+ * can be necessary if redirects are resolved as identities in SMW. The
 2266+ * title and namespace of the affected page and of its updated redirect
 2267+ * target are given. The target can be empty ('') to delete any redirect.
 2268+ * Returns the canonical ID that is now to be used for the subject.
22692269 *
22702270 * This method does not change the ids of the affected pages, and thus it
22712271 * is not concerned with updates of the data that is currently stored for
@@ -2275,7 +2275,7 @@
22762276 * changes here. Keeping the redirect structure consistent is important,
22772277 * and errors in this code can go unnoticed for quite some time.
22782278 */
2279 - protected function updateRedirects( $subject_t, $subject_ns, $curtarget_t = '', $curtarget_ns = - 1 ) {
 2279+ protected function updateRedirects( $subject_t, $subject_ns, $curtarget_t = '', $curtarget_ns = -1 ) {
22802280 global $smwgQEqualitySupport, $smwgEnableUpdateJobs;
22812281 $fname = 'SMW::updateRedirects';
22822282
@@ -2284,7 +2284,7 @@
22852285 /// NOTE: $sid can be 0 here; this is useful to know since it means that fewer table updates are needed
22862286 $new_tid = $curtarget_t ? ( $this->makeSMWPageID( $curtarget_t, $curtarget_ns, '', false ) ):0; // real id of new target, if given
22872287 $db = wfGetDB( DB_SLAVE );
2288 -
 2288+
22892289 $res = $db->select( array( 'smw_redi2' ), 'o_id', array( 's_title' => $subject_t, 's_namespace' => $subject_ns ), $fname, array( 'LIMIT' => 1 ) );
22902290 $old_tid = ( $row = $db->fetchObject( $res ) ) ? $row->o_id:0; // real id of old target, if any
22912291 $db->freeResult( $res );
@@ -2296,7 +2296,7 @@
22972297
22982298 // *** Make relevant changes in property tables (don't write the new redirect yet) ***//
22992299 $db = wfGetDB( DB_MASTER ); // now we need to write something
2300 -
 2300+
23012301 if ( ( $old_tid == 0 ) && ( $sid != 0 ) && ( $smwgQEqualitySupport != SMW_EQ_NONE ) ) { // new redirect
23022302 // $smwgQEqualitySupport requires us to change all tables' page references from $sid to $new_tid.
23032303 // Since references must not be 0, we don't have to do this is $sid == 0.
@@ -2307,10 +2307,10 @@
23082308 if ( $smwgEnableUpdateJobs && ( $smwgQEqualitySupport != SMW_EQ_NONE ) ) {
23092309 // entries that refer to old target may in fact refer to subject, but we don't know which: schedule affected pages for update
23102310 $jobs = array();
2311 -
 2311+
23122312 foreach ( self::getPropertyTables() as $proptable ) {
23132313 if ( $proptable->name == 'smw_redi2' ) continue; // can safely be skipped
2314 -
 2314+
23152315 if ( $proptable->idsubject ) {
23162316 $from = $db->tableName( $proptable->name ) . ' INNER JOIN ' . $db->tableName( 'smw_ids' ) . ' ON s_id=smw_id';
23172317 $select = 'DISTINCT smw_title AS title,smw_namespace AS namespace';
@@ -2318,25 +2318,25 @@
23192319 $from = $db->tableName( $proptable->name );
23202320 $select = 'DISTINCT s_title AS title,s_namespace AS namespace';
23212321 }
2322 -
 2322+
23232323 if ( ( $subject_ns == SMW_NS_PROPERTY ) && ( $proptable->fixedproperty == false ) ) {
23242324 $res = $db->select( $from, $select, array( 'p_id' => $old_tid ), $fname );
2325 -
 2325+
23262326 while ( $row = $db->fetchObject( $res ) ) {
23272327 $jobs[] = new SMWUpdateJob( Title::makeTitle( $row->namespace, $row->title ) );
23282328 }
2329 -
 2329+
23302330 $db->freeResult( $res );
23312331 }
2332 -
 2332+
23332333 foreach ( $proptable->objectfields as $fieldname => $type ) {
23342334 if ( $type == 'p' ) {
23352335 $res = $db->select( $from, $select, array( $fieldname => $old_tid ), $fname );
2336 -
 2336+
23372337 while ( $row = $db->fetchObject( $res ) ) {
23382338 $jobs[] = new SMWUpdateJob( Title::makeTitle( $row->namespace, $row->title ) );
23392339 }
2340 -
 2340+
23412341 $db->freeResult( $res );
23422342 }
23432343 }
@@ -2358,23 +2358,23 @@
23592359 $db->update( 'smw_ids', array( 'smw_iw' => SMW_SQL2_SMWREDIIW ), array( 'smw_id' => $sid ), $fname );
23602360 }
23612361 }
2362 -
 2362+
23632363 $db->insert( 'smw_redi2', array( 's_title' => $subject_t, 's_namespace' => $subject_ns, 'o_id' => $new_tid ), $fname );
23642364 $this->m_ids[" $subject_ns $subject_t C"] = $new_tid; // "iw" is empty here
23652365 } else { // delete old redirect
23662366 // This case implies $old_tid != 0 (or we would have new_tid == old_tid above).
23672367 // Therefore $subject had a redirect, and it must also have an ID. This shows that $sid != 0 here.
23682368 $this->m_ids[" $subject_ns $subject_t C"] = $sid; // "iw" is empty here
2369 -
 2369+
23702370 if ( $smwgQEqualitySupport != SMW_EQ_NONE ) { // mark subject as non-redirect
23712371 $db->update( 'smw_ids', array( 'smw_iw' => '' ), array( 'smw_id' => $sid ), $fname );
23722372 }
23732373 }
2374 -
 2374+
23752375 // *** Flush some caches to be safe, though they are not essential in program runs with redirect updates ***//
23762376 unset( $this->m_semdata[$sid] ); unset( $this->m_semdata[$new_tid] ); unset( $this->m_semdata[$old_tid] );
23772377 unset( $this->m_sdstate[$sid] ); unset( $this->m_sdstate[$new_tid] ); unset( $this->m_sdstate[$old_tid] );
2378 -
 2378+
23792379 return ( $new_tid == 0 ) ? $sid : $new_tid;
23802380 }
23812381
@@ -2401,12 +2401,12 @@
24022402 * used in SMWDataValue::getSignature()!
24032403 *
24042404 * @todo Add a hook for registering additional or modifying given tables.
2405 - *
 2405+ *
24062406 * @return array of SMWSQLStore2Table
24072407 */
24082408 public static function getPropertyTables() {
24092409 if ( count( self::$prop_tables ) > 0 ) return self::$prop_tables; // don't initialise twice
2410 -
 2410+
24112411 self::$prop_tables['smw_rels2'] = new SMWSQLStore2Table( 'smw_rels2',
24122412 array( 'o_id' => 'p' ),
24132413 array( 'o_id' ) );

Status & tagging log