Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SparqlStoreQueryEngine.php |
— | — | @@ -256,10 +256,21 @@ |
257 | 257 | */ |
258 | 258 | protected $m_store; |
259 | 259 | |
| 260 | + /** |
| 261 | + * Constructor. |
| 262 | + * |
| 263 | + * @param $store SMWStore that this object will use |
| 264 | + */ |
260 | 265 | public function __construct( SMWStore $store ) { |
261 | 266 | $this->m_store = $store; |
262 | 267 | } |
263 | 268 | |
| 269 | + /** |
| 270 | + * Get the output number for a query in counting mode. |
| 271 | + * |
| 272 | + * @param $query SMWQuery |
| 273 | + * @return integer |
| 274 | + */ |
264 | 275 | public function getCountQueryResult( SMWQuery $query ) { |
265 | 276 | $this->m_sortkeys = array(); // ignore sorting, just count |
266 | 277 | $sparqlCondition = $this->getSparqlCondition( $query->getDescription() ); |
— | — | @@ -294,6 +305,12 @@ |
295 | 306 | } |
296 | 307 | } |
297 | 308 | |
| 309 | + /** |
| 310 | + * Get the results for a query in instance retrieval mode. |
| 311 | + * |
| 312 | + * @param $query SMWQuery |
| 313 | + * @return SMWQueryResult |
| 314 | + */ |
298 | 315 | public function getInstanceQueryResult( SMWQuery $query ) { |
299 | 316 | $this->m_sortkeys = $query->sortkeys; |
300 | 317 | $sparqlCondition = $this->getSparqlCondition( $query->getDescription() ); |
— | — | @@ -326,6 +343,12 @@ |
327 | 344 | return $this->getQueryResultFromSparqlResult( $sparqlResultWrapper, $query ); |
328 | 345 | } |
329 | 346 | |
| 347 | + /** |
| 348 | + * Get the output string for a query in debugging mode. |
| 349 | + * |
| 350 | + * @param $query SMWQuery |
| 351 | + * @return string |
| 352 | + */ |
330 | 353 | public function getDebugQueryResult( SMWQuery $query ) { |
331 | 354 | $this->m_sortkeys = $query->sortkeys; |
332 | 355 | $sparqlCondition = $this->getSparqlCondition( $query->getDescription() ); |
— | — | @@ -424,6 +447,15 @@ |
425 | 448 | return $result; |
426 | 449 | } |
427 | 450 | |
| 451 | + /** |
| 452 | + * Get a SMWSparqlCondition object for an SMWDescription. |
| 453 | + * This conversion is implemented by a number of recursive functions, |
| 454 | + * and this is the main entry point for this recursion. In particular, |
| 455 | + * it resets global variables that are used for the construction. |
| 456 | + * |
| 457 | + * @param $description SMWDescription |
| 458 | + * @return SMWSparqlCondition |
| 459 | + */ |
428 | 460 | protected function getSparqlCondition( SMWDescription $description ) { |
429 | 461 | $this->m_variableCounter = 0; |
430 | 462 | $this->m_orderVariables = array(); |
— | — | @@ -433,7 +465,15 @@ |
434 | 466 | } |
435 | 467 | |
436 | 468 | /** |
437 | | - * Create an SMWSparqlCondition from the given SMWDescription. |
| 469 | + * Recursively create an SMWSparqlCondition from an SMWDescription. |
| 470 | + * |
| 471 | + * @param $description SMWDescription |
| 472 | + * @param $joinVariable string name of the variable that conditions |
| 473 | + * will refer to |
| 474 | + * @param $orderByProperty mixed SMWDIProperty or null, if given then |
| 475 | + * this is the property the values of which this condition will refer |
| 476 | + * to, and the condition should also enable ordering by this value |
| 477 | + * @return SMWSparqlCondition |
438 | 478 | */ |
439 | 479 | protected function buildSparqlCondition( SMWDescription $description, $joinVariable, $orderByProperty ) { |
440 | 480 | if ( $description instanceof SMWSomeProperty ) { |
— | — | @@ -455,6 +495,14 @@ |
456 | 496 | } |
457 | 497 | } |
458 | 498 | |
| 499 | + /** |
| 500 | + * Recursively create an SMWSparqlCondition from an SMWConjunction. |
| 501 | + * |
| 502 | + * @param $description SMWConjunction |
| 503 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 504 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 505 | + * @return SMWSparqlCondition |
| 506 | + */ |
459 | 507 | protected function buildConjunctionCondition( SMWConjunction $description, $joinVariable, $orderByProperty ) { |
460 | 508 | $subDescriptions = $description->getDescriptions(); |
461 | 509 | if ( count( $subDescriptions ) == 0 ) { // empty conjunction: true |
— | — | @@ -521,6 +569,14 @@ |
522 | 570 | return $result; |
523 | 571 | } |
524 | 572 | |
| 573 | + /** |
| 574 | + * Recursively create an SMWSparqlCondition from an SMWDisjunction. |
| 575 | + * |
| 576 | + * @param $description SMWDisjunction |
| 577 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 578 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 579 | + * @return SMWSparqlCondition |
| 580 | + */ |
525 | 581 | protected function buildDisjunctionCondition( SMWDisjunction $description, $joinVariable, $orderByProperty ) { |
526 | 582 | $subDescriptions = $description->getDescriptions(); |
527 | 583 | if ( count( $subDescriptions ) == 0 ) { // empty disjunction: false |
— | — | @@ -584,6 +640,14 @@ |
585 | 641 | |
586 | 642 | } |
587 | 643 | |
| 644 | + /** |
| 645 | + * Recursively create an SMWSparqlCondition from an SMWSomeProperty. |
| 646 | + * |
| 647 | + * @param $description SMWSomeProperty |
| 648 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 649 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 650 | + * @return SMWSparqlCondition |
| 651 | + */ |
588 | 652 | protected function buildPropertyCondition( SMWSomeProperty $description, $joinVariable, $orderByProperty ) { |
589 | 653 | $diProperty = $description->getProperty(); |
590 | 654 | |
— | — | @@ -642,6 +706,14 @@ |
643 | 707 | return $result; |
644 | 708 | } |
645 | 709 | |
| 710 | + /** |
| 711 | + * Create an SMWSparqlCondition from an SMWClassDescription. |
| 712 | + * |
| 713 | + * @param $description SMWClassDescription |
| 714 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 715 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 716 | + * @return SMWSparqlCondition |
| 717 | + */ |
646 | 718 | protected function buildClassCondition( SMWClassDescription $description, $joinVariable, $orderByProperty ) { |
647 | 719 | $condition = ''; |
648 | 720 | $namespaces = array(); |
— | — | @@ -669,6 +741,14 @@ |
670 | 742 | return $result; |
671 | 743 | } |
672 | 744 | |
| 745 | + /** |
| 746 | + * Create an SMWSparqlCondition from an SMWValueDescription. |
| 747 | + * |
| 748 | + * @param $description SMWValueDescription |
| 749 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 750 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 751 | + * @return SMWSparqlCondition |
| 752 | + */ |
673 | 753 | protected function buildValueCondition( SMWValueDescription $description, $joinVariable, $orderByProperty ) { |
674 | 754 | $dataItem = $description->getDataItem(); |
675 | 755 | $expElement = SMWExporter::getDataItemExpElement( $dataItem ); |
— | — | @@ -704,12 +784,25 @@ |
705 | 785 | return $result; |
706 | 786 | } |
707 | 787 | |
| 788 | + /** |
| 789 | + * Create an SMWSparqlCondition from an empty (true) description. |
| 790 | + * May still require helper conditions for ordering. |
| 791 | + * |
| 792 | + * @param $joinVariable string name, see buildSparqlCondition() |
| 793 | + * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() |
| 794 | + * @return SMWSparqlCondition |
| 795 | + */ |
708 | 796 | protected function buildTrueCondition( $joinVariable, $orderByProperty ) { |
709 | 797 | $result = new SMWSparqlTrueCondition(); |
710 | 798 | $this->addOrderByDataForProperty( $result, $joinVariable, $orderByProperty ); |
711 | 799 | return $result; |
712 | 800 | } |
713 | 801 | |
| 802 | + /** |
| 803 | + * Get a fresh unused variable name for building SPARQL conditions. |
| 804 | + * |
| 805 | + * @return string |
| 806 | + */ |
714 | 807 | protected function getNextVariable() { |
715 | 808 | return 'v' . ( ++$this->m_variableCounter ); |
716 | 809 | } |