00001 <?php
00017 abstract class SMWDescription {
00018
00022 protected $m_printreqs = array();
00023
00030 public function getPrintRequests() {
00031 return $this->m_printreqs;
00032 }
00033
00039 public function setPrintRequests( array $printrequests ) {
00040 $this->m_printreqs = $printrequests;
00041 }
00042
00048 public function addPrintRequest( SMWPrintRequest $printrequest ) {
00049 $this->m_printreqs[] = $printrequest;
00050 }
00051
00058 public function prependPrintRequest( SMWPrintRequest $printrequest ) {
00059 array_unshift( $this->m_printreqs, $printrequest );
00060 }
00061
00076 abstract public function getQueryString( $asvalue = false );
00077
00084 abstract public function isSingleton();
00085
00091 public function getSize() {
00092 return 1;
00093 }
00094
00100 public function getDepth() {
00101 return 0;
00102 }
00103
00108 public function getQueryFeatures() {
00109 return 0;
00110 }
00111
00124 public function prune( &$maxsize, &$maxdepth, &$log ) {
00125 if ( ( $maxsize < $this->getSize() ) || ( $maxdepth < $this->getDepth() ) ) {
00126 $log[] = $this->getQueryString();
00127
00128 $result = new SMWThingDescription();
00129 $result->setPrintRequests( $this->getPrintRequests() );
00130
00131 return $result;
00132 } else {
00133 $maxsize = $maxsize - $this->getSize();
00134 $maxdepth = $maxdepth - $this->getDepth();
00135
00136 return $this;
00137 }
00138 }
00139
00140 }
00141
00150 class SMWThingDescription extends SMWDescription {
00151
00152 public function getQueryString( $asvalue = false ) {
00153 return $asvalue ? '+' : '';
00154 }
00155
00156 public function isSingleton() {
00157 return false;
00158 }
00159
00160 public function getSize() {
00161 return 0;
00162 }
00163
00164 public function prune( &$maxsize, &$maxdepth, &$log ) {
00165 return $this;
00166 }
00167
00168 }
00169
00176 class SMWClassDescription extends SMWDescription {
00177
00181 protected $m_diWikiPages;
00182
00190 public function __construct( $content ) {
00191 if ( $content instanceof SMWDIWikiPage ) {
00192 $this->m_diWikiPages = array( $content );
00193 } elseif ( is_array( $content ) ) {
00194 $this->m_diWikiPages = $content;
00195 } else {
00196 throw new Exception( "SMWClassDescription::__construct(): parameter must be an SMWDIWikiPage object or an array of such objects." );
00197 }
00198 }
00199
00203 public function addDescription( SMWClassDescription $description ) {
00204 $this->m_diWikiPages = array_merge( $this->m_diWikiPages, $description->getCategories() );
00205 }
00206
00210 public function getCategories() {
00211 return $this->m_diWikiPages;
00212 }
00213
00214 public function getQueryString( $asvalue = false ) {
00215 $first = true;
00216 foreach ( $this->m_diWikiPages as $wikiPage ) {
00217 $wikiValue = SMWDataValueFactory::newDataItemValue( $wikiPage, null );
00218 if ( $first ) {
00219 $result = '[[' . $wikiValue->getPrefixedText();
00220 $first = false;
00221 } else {
00222 $result .= '||' . $wikiValue->getText();
00223 }
00224 }
00225
00226 $result .= ']]';
00227
00228 if ( $asvalue ) {
00229 return ' <q>' . $result . '</q> ';
00230 } else {
00231 return $result;
00232 }
00233 }
00234
00235 public function isSingleton() {
00236 return false;
00237 }
00238
00239 public function getSize() {
00240 global $smwgQSubcategoryDepth;
00241 if ( $smwgQSubcategoryDepth > 0 ) {
00242 return 1;
00243 } else {
00244 return count( $this->m_diWikiPages );
00245 }
00246 }
00247
00248 public function getQueryFeatures() {
00249 if ( count( $this->m_diWikiPages ) > 1 ) {
00250 return SMW_CATEGORY_QUERY | SMW_DISJUNCTION_QUERY;
00251 } else {
00252 return SMW_CATEGORY_QUERY;
00253 }
00254 }
00255
00256 public function prune( &$maxsize, &$maxdepth, &$log ) {
00257 if ( $maxsize >= $this->getSize() ) {
00258 $maxsize = $maxsize - $this->getSize();
00259 return $this;
00260 } elseif ( $maxsize <= 0 ) {
00261 $log[] = $this->getQueryString();
00262 $result = new SMWThingDescription();
00263 } else {
00264 $result = new SMWClassDescription( array_slice( $this->m_diWikiPages, 0, $maxsize ) );
00265 $rest = new SMWClassDescription( array_slice( $this->m_diWikiPages, $maxsize ) );
00266
00267 $log[] = $rest->getQueryString();
00268 $maxsize = 0;
00269 }
00270
00271 $result->setPrintRequests( $this->getPrintRequests() );
00272 return $result;
00273 }
00274
00275 }
00276
00277
00284 class SMWConceptDescription extends SMWDescription {
00285
00289 protected $m_concept;
00290
00296 public function __construct( SMWDIWikiPage $concept ) {
00297 $this->m_concept = $concept;
00298 }
00299
00303 public function getConcept() {
00304 return $this->m_concept;
00305 }
00306
00307 public function getQueryString( $asvalue = false ) {
00308 $pageValue = SMWDataValueFactory::newDataItemValue( $this->m_concept, null );
00309 $result = '[[' . $pageValue->getPrefixedText() . ']]';
00310 if ( $asvalue ) {
00311 return ' <q>' . $result . '</q> ';
00312 } else {
00313 return $result;
00314 }
00315 }
00316
00317 public function isSingleton() {
00318 return false;
00319 }
00320
00321 public function getQueryFeatures() {
00322 return SMW_CONCEPT_QUERY;
00323 }
00324
00330 }
00331
00332
00340 class SMWNamespaceDescription extends SMWDescription {
00341
00345 protected $m_namespace;
00346
00352 public function __construct( $namespace ) {
00353 $this->m_namespace = $namespace;
00354 }
00355
00359 public function getNamespace() {
00360 return $this->m_namespace;
00361 }
00362
00363 public function getQueryString( $asvalue = false ) {
00364 global $wgContLang;
00365 if ( $asvalue ) {
00366 return ' <q>[[' . $wgContLang->getNSText( $this->m_namespace ) . ':+]]</q> ';
00367 } else {
00368 return '[[' . $wgContLang->getNSText( $this->m_namespace ) . ':+]]';
00369 }
00370 }
00371
00372 public function isSingleton() {
00373 return false;
00374 }
00375
00376 public function getQueryFeatures() {
00377 return SMW_NAMESPACE_QUERY;
00378 }
00379
00380 }
00381
00392 class SMWValueDescription extends SMWDescription {
00393
00394 protected $m_dataItem;
00395 protected $m_comparator;
00396 protected $m_property;
00397
00398 public function __construct( SMWDataItem $dataItem, $property, $comparator = SMW_CMP_EQ ) {
00399 $this->m_dataItem = $dataItem;
00400 $this->m_comparator = $comparator;
00401 $this->m_property = $property;
00402 }
00403
00405 public function getDataValue() {
00406 return $this->m_dataItem;
00407 }
00408
00409 public function getDataItem() {
00410 return $this->m_dataItem;
00411 }
00412
00413 public function getComparator() {
00414 return $this->m_comparator;
00415 }
00416
00417 public function getQueryString( $asvalue = false ) {
00418 $comparator = SMWQueryLanguage::getStringForComparator( $this->m_comparator );
00419 $dataValue = SMWDataValueFactory::newDataItemValue( $this->m_dataItem, $this->m_property );
00420 if ( $asvalue ) {
00421 return $comparator . $dataValue->getWikiValue();
00422 } else {
00423 if ( $comparator === '' ) {
00424 return '[[:' . $dataValue->getWikiValue() . ']]';
00425 } else {
00426 return '[[' . $comparator . $dataValue->getWikiValue() . ']]';
00427 }
00428 }
00429 }
00430
00431 public function isSingleton() {
00432 if ( $this->m_comparator == SMW_CMP_EQ ) {
00433 return true;
00434 } else {
00435 return false;
00436 }
00437 }
00438
00439 public function getSize() {
00440 return 1;
00441 }
00442
00443 }
00444
00445
00453 class SMWConjunction extends SMWDescription {
00454
00455 protected $m_descriptions;
00456
00457 public function __construct( $descriptions = array() ) {
00458 $this->m_descriptions = $descriptions;
00459 }
00460
00461 public function getDescriptions() {
00462 return $this->m_descriptions;
00463 }
00464
00465 public function addDescription( SMWDescription $description ) {
00466 if ( ! ( $description instanceof SMWThingDescription ) ) {
00467 if ( $description instanceof SMWConjunction ) {
00468 foreach ( $description->getDescriptions() as $subdesc ) {
00469 $this->m_descriptions[] = $subdesc;
00470 }
00471 } else {
00472 $this->m_descriptions[] = $description;
00473 }
00474
00475
00477 $this->m_printreqs = array_merge( $this->m_printreqs, $description->getPrintRequests() );
00478 $description->setPrintRequests( array() );
00479 }
00480 }
00481
00482 public function getQueryString( $asvalue = false ) {
00483 $result = '';
00484
00485 foreach ( $this->m_descriptions as $desc ) {
00486 $result .= ( $result ? ' ' : '' ) . $desc->getQueryString( false );
00487 }
00488
00489 if ( $result === '' ) {
00490 return $asvalue ? '+' : '';
00491 } else {
00492 return $asvalue ? " <q>{$result}</q> " : $result;
00493 }
00494 }
00495
00496 public function isSingleton() {
00497 foreach ( $this->m_descriptions as $d ) {
00498 if ( $d->isSingleton() ) {
00499 return true;
00500 }
00501 }
00502 return false;
00503 }
00504
00505 public function getSize() {
00506 $size = 0;
00507
00508 foreach ( $this->m_descriptions as $desc ) {
00509 $size += $desc->getSize();
00510 }
00511
00512 return $size;
00513 }
00514
00515 public function getDepth() {
00516 $depth = 0;
00517
00518 foreach ( $this->m_descriptions as $desc ) {
00519 $depth = max( $depth, $desc->getDepth() );
00520 }
00521
00522 return $depth;
00523 }
00524
00525 public function getQueryFeatures() {
00526 $result = SMW_CONJUNCTION_QUERY;
00527
00528 foreach ( $this->m_descriptions as $desc ) {
00529 $result = $result | $desc->getQueryFeatures();
00530 }
00531
00532 return $result;
00533 }
00534
00535 public function prune( &$maxsize, &$maxdepth, &$log ) {
00536 if ( $maxsize <= 0 ) {
00537 $log[] = $this->getQueryString();
00538 return new SMWThingDescription();
00539 }
00540
00541 $prunelog = array();
00542 $newdepth = $maxdepth;
00543 $result = new SMWConjunction();
00544
00545 foreach ( $this->m_descriptions as $desc ) {
00546 $restdepth = $maxdepth;
00547 $result->addDescription( $desc->prune( $maxsize, $restdepth, $prunelog ) );
00548 $newdepth = min( $newdepth, $restdepth );
00549 }
00550
00551 if ( count( $result->getDescriptions() ) > 0 ) {
00552 $log = array_merge( $log, $prunelog );
00553 $maxdepth = $newdepth;
00554
00555 if ( count( $result->getDescriptions() ) == 1 ) {
00556 $descriptions = $result->getDescriptions();
00557 $result = array_shift( $descriptions );
00558 }
00559
00560 $result->setPrintRequests( $this->getPrintRequests() );
00561
00562 return $result;
00563 } else {
00564 $log[] = $this->getQueryString();
00565
00566 $result = new SMWThingDescription();
00567 $result->setPrintRequests( $this->getPrintRequests() );
00568
00569 return $result;
00570 }
00571 }
00572
00573 }
00574
00582 class SMWDisjunction extends SMWDescription {
00583 protected $m_descriptions;
00584 protected $m_classdesc = null;
00585
00586 protected $m_true = false;
00587
00588 public function __construct( $descriptions = array() ) {
00589 foreach ( $descriptions as $desc ) {
00590 $this->addDescription( $desc );
00591 }
00592 }
00593
00594 public function getDescriptions() {
00595 return $this->m_descriptions;
00596 }
00597
00598 public function addDescription( SMWDescription $description ) {
00599 if ( $description instanceof SMWThingDescription ) {
00600 $this->m_true = true;
00601 $this->m_descriptions = array();
00602 $this->m_classdesc = null;
00603 }
00604
00605 if ( !$this->m_true ) {
00606 if ( $description instanceof SMWClassDescription ) {
00607 if ( is_null( $this->m_classdesc ) ) {
00608 $this->m_classdesc = $description;
00609 $this->m_descriptions[] = $description;
00610 } else {
00611 $this->m_classdesc->addDescription( $description );
00612 }
00613 } elseif ( $description instanceof SMWDisjunction ) {
00614 foreach ( $description->getDescriptions() as $subdesc ) {
00615 $this->m_descriptions[] = $subdesc;
00616 }
00617
00619 } else {
00620 $this->m_descriptions[] = $description;
00621 }
00622 }
00623
00624
00626 $this->m_printreqs = array_merge( $this->m_printreqs, $description->getPrintRequests() );
00627 $description->setPrintRequests( array() );
00628 }
00629
00630 public function getQueryString( $asvalue = false ) {
00631 if ( $this->m_true ) {
00632 return '+';
00633 }
00634
00635 $result = '';
00636 $sep = $asvalue ? '||':' OR ';
00637
00638 foreach ( $this->m_descriptions as $desc ) {
00639 $subdesc = $desc->getQueryString( $asvalue );
00640
00641 if ( $desc instanceof SMWSomeProperty ) {
00642 if ( $asvalue ) {
00643 $subdesc = ' <q>[[' . $subdesc . ']]</q> ';
00644 } else {
00645 $subdesc = ' <q>' . $subdesc . '</q> ';
00646 }
00647 }
00648
00649 $result .= ( $result ? $sep:'' ) . $subdesc;
00650 }
00651 if ( $asvalue ) {
00652 return $result;
00653 } else {
00654 return ' <q>' . $result . '</q> ';
00655 }
00656 }
00657
00658 public function isSingleton() {
00660 if ( count( $this->m_descriptions ) != 1 ) {
00661 return false;
00662 } else {
00663 return $this->m_descriptions[0]->isSingleton();
00664 }
00665 }
00666
00667 public function getSize() {
00668 $size = 0;
00669
00670 foreach ( $this->m_descriptions as $desc ) {
00671 $size += $desc->getSize();
00672 }
00673
00674 return $size;
00675 }
00676
00677 public function getDepth() {
00678 $depth = 0;
00679
00680 foreach ( $this->m_descriptions as $desc ) {
00681 $depth = max( $depth, $desc->getDepth() );
00682 }
00683
00684 return $depth;
00685 }
00686
00687 public function getQueryFeatures() {
00688 $result = SMW_DISJUNCTION_QUERY;
00689
00690 foreach ( $this->m_descriptions as $desc ) {
00691 $result = $result | $desc->getQueryFeatures();
00692 }
00693
00694 return $result;
00695 }
00696
00697 public function prune( &$maxsize, &$maxdepth, &$log ) {
00698 if ( $maxsize <= 0 ) {
00699 $log[] = $this->getQueryString();
00700 return new SMWThingDescription();
00701 }
00702
00703 $prunelog = array();
00704 $newdepth = $maxdepth;
00705 $result = new SMWDisjunction();
00706
00707 foreach ( $this->m_descriptions as $desc ) {
00708 $restdepth = $maxdepth;
00709 $result->addDescription( $desc->prune( $maxsize, $restdepth, $prunelog ) );
00710 $newdepth = min( $newdepth, $restdepth );
00711 }
00712
00713 if ( count( $result->getDescriptions() ) > 0 ) {
00714 $log = array_merge( $log, $prunelog );
00715 $maxdepth = $newdepth;
00716
00717 if ( count( $result->getDescriptions() ) == 1 ) {
00718 $descriptions = $result->getDescriptions();
00719 $result = array_shift( $descriptions );
00720 }
00721
00722 $result->setPrintRequests( $this->getPrintRequests() );
00723
00724 return $result;
00725 } else {
00726 $log[] = $this->getQueryString();
00727
00728 $result = new SMWThingDescription();
00729 $result->setPrintRequests( $this->getPrintRequests() );
00730
00731 return $result;
00732 }
00733 }
00734 }
00735
00745 class SMWSomeProperty extends SMWDescription {
00746
00747 protected $m_description;
00748 protected $m_property;
00749
00750 public function __construct( SMWDIProperty $property, SMWDescription $description ) {
00751 $this->m_property = $property;
00752 $this->m_description = $description;
00753 }
00754
00755 public function getProperty() {
00756 return $this->m_property;
00757 }
00758
00759 public function getDescription() {
00760 return $this->m_description;
00761 }
00762
00763 public function getQueryString( $asvalue = false ) {
00764 $subdesc = $this->m_description;
00765 $propertyChainString = $this->m_property->getLabel();
00766 $propertyname = $propertyChainString;
00767
00768 while ( ( $propertyname !== '' ) && ( $subdesc instanceof SMWSomeProperty ) ) {
00769 $propertyname = $subdesc->getProperty()->getLabel();
00770 if ( $propertyname !== '' ) {
00771 $propertyChainString .= '.' . $propertyname;
00772 $subdesc = $subdesc->getDescription();
00773 }
00774 }
00775
00776 if ( $asvalue ) {
00777 return '<q>[[' . $propertyChainString . '::' . $subdesc->getQueryString( true ) . ']]</q>';
00778 } else {
00779 return '[[' . $propertyChainString . '::' . $subdesc->getQueryString( true ) . ']]';
00780 }
00781 }
00782
00783 public function isSingleton() {
00784 return false;
00785 }
00786
00787 public function getSize() {
00788 return 1 + $this->getDescription()->getSize();
00789 }
00790
00791 public function getDepth() {
00792 return 1 + $this->getDescription()->getDepth();
00793 }
00794
00795 public function getQueryFeatures() {
00796 return SMW_PROPERTY_QUERY | $this->m_description->getQueryFeatures();
00797 }
00798
00799 public function prune( &$maxsize, &$maxdepth, &$log ) {
00800 if ( ( $maxsize <= 0 ) || ( $maxdepth <= 0 ) ) {
00801 $log[] = $this->getQueryString();
00802 return new SMWThingDescription();
00803 }
00804
00805 $maxsize--;
00806 $maxdepth--;
00807
00808 $result = new SMWSomeProperty( $this->m_property, $this->m_description->prune( $maxsize, $maxdepth, $log ) );
00809 $result->setPrintRequests( $this->getPrintRequests() );
00810
00811 return $result;
00812 }
00813
00814 }