00001 <?php
00002
00014 class SMQueryHandler {
00015
00016 protected $queryResult;
00017 protected $outputmode;
00018
00019 protected $locations = false;
00020
00028 protected $template = false;
00029
00037 public $icon = '';
00038
00046 public $text = '';
00047
00055 public $title = '';
00056
00064 public $titleLinkSeparate;
00065
00073 protected $linkAbsolute;
00074
00082 protected $pageLinkText;
00083
00091 protected $subjectSeparator = '<hr />';
00092
00100 protected $boldSubject = true;
00101
00109 protected $showSubject = true;
00110
00122 public function __construct( SMWQueryResult $queryResult, $outputmode, $linkAbsolute = false, $pageLinkText = '$1', $titleLinkSeparate = false ) {
00123 $this->queryResult = $queryResult;
00124 $this->outputmode = $outputmode;
00125
00126 $this->linkAbsolute = $linkAbsolute;
00127 $this->pageLinkText = $pageLinkText;
00128 $this->titleLinkSeparate = $titleLinkSeparate;
00129 }
00130
00138 public function setTemplate( $template ) {
00139 $this->template = $template === '' ? false : $template;
00140 }
00141
00149 public function setIcon( $icon ) {
00150 $this->icon = $icon;
00151 }
00152
00160 public function setTitle( $title ) {
00161 $this->title = $title;
00162 }
00163
00171 public function setText( $text ) {
00172 $this->text = $text;
00173 }
00174
00182 public function setSubjectSeparator( $subjectSeparator ) {
00183 $this->subjectSeparator = $subjectSeparator;
00184 }
00185
00193 public function setBoldSubject( $boldSubject ) {
00194 $this->boldSubject = $boldSubject;
00195 }
00196
00204 public function setShowSubject( $showSubject ) {
00205 $this->showSubject = $showSubject;
00206 }
00207
00215 public function setPageLinkText( $text ) {
00216 $this->pageLinkText = $text;
00217 }
00218
00226 public function getLocations() {
00227 if ( $this->locations === false ) {
00228 $this->locations = $this->findLocations();
00229 }
00230
00231 return $this->locations;
00232 }
00233
00241 protected function findLocations() {
00242 $locations = array();
00243
00244 while ( ( $row = $this->queryResult->getNext() ) !== false ) {
00245 $locations = array_merge( $locations, $this->handleResultRow( $row ) );
00246 }
00247
00248 return $locations;
00249 }
00250
00260 protected function handleResultRow( array $row ) {
00261 $locations = array();
00262 $properties = array();
00263
00264 $title = '';
00265 $text = '';
00266
00267
00268 foreach ( $row as $i => $resultArray ) {
00269 $printRequest = $resultArray->getPrintRequest();
00270
00271
00272 while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
00273 if ( $dataValue->getTypeID() == '_wpg' && $i == 0 ) {
00274 list( $title, $text ) = $this->handleResultSubject( $dataValue );
00275 }
00276 else if ( $dataValue->getTypeID() == '_str' && $i == 0 ) {
00277 $title = $dataValue->getLongText( $this->outputmode, null );
00278 $text = $dataValue->getLongText( $this->outputmode, smwfGetLinker() );
00279 }
00280 else if ( $dataValue->getTypeID() != '_geo' && $i != 0 ) {
00281 $properties[] = $this->handleResultProperty( $dataValue, $printRequest );
00282 }
00283 else if ( $printRequest->getMode() == SMWPrintRequest::PRINT_PROP && $printRequest->getTypeID() == '_geo' ) {
00284 $dataItem = $dataValue->getDataItem();
00285
00286 $location = MapsLocation::newFromLatLon( $dataItem->getLatitude(), $dataItem->getLongitude() );
00287
00288 if ( $location->isValid() ) {
00289 $locations[] = $location;
00290 }
00291
00292 }
00293 }
00294 }
00295
00296 if ( count( $properties ) > 0 && $text !== '' ) {
00297 $text .= $this->subjectSeparator;
00298 }
00299
00300 $icon = $this->getLocationIcon( $row );
00301
00302 return $this->buildLocationsList( $locations, $title, $text, $icon, $properties );
00303 }
00304
00315 protected function handleResultSubject( SMWWikiPageValue $object ) {
00316 global $wgUser;
00317
00318 $title = $object->getLongText( $this->outputmode, null );
00319 $text = '';
00320
00321 if ( $this->showSubject ) {
00322 if ( !$this->titleLinkSeparate && $this->linkAbsolute ) {
00323 $text = Html::element(
00324 'a',
00325 array( 'href' => $object->getTitle()->getFullUrl() ),
00326 $object->getTitle()->getText()
00327 );
00328 }
00329 else {
00330 $text = $object->getLongText( $this->outputmode, smwfGetLinker() );
00331 }
00332
00333 if ( $this->boldSubject ) {
00334 $text = '<b>' . $text . '</b>';
00335 }
00336
00337 if ( $this->titleLinkSeparate ) {
00338 $txt = $object->getTitle()->getText();
00339
00340 if ( $this->pageLinkText !== '' ) {
00341 $txt = str_replace( '$1', $txt, $this->pageLinkText );
00342 }
00343 $text .= Html::element(
00344 'a',
00345 array( 'href' => $object->getTitle()->getFullUrl() ),
00346 $txt
00347 );
00348 }
00349 }
00350
00351 return array( $title, $text );
00352 }
00353
00364 protected function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) {
00365 global $wgUser;
00366
00367 if ( $this->template ) {
00368 if ( $object instanceof SMWWikiPageValue ) {
00369 return $object->getTitle()->getPrefixedText();
00370 } else {
00371 return $object->getLongText( SMW_OUTPUT_WIKI, NULL );
00372 }
00373 }
00374
00375 if ( $this->linkAbsolute ) {
00376 $t = Title::newFromText( $printRequest->getHTMLText( NULL ), SMW_NS_PROPERTY );
00377
00378 if ( $t instanceof Title && $t->exists() ) {
00379 $propertyName = $propertyName = Html::element(
00380 'a',
00381 array( 'href' => $t->getFullUrl() ),
00382 $printRequest->getHTMLText( NULL )
00383 );
00384 }
00385 else {
00386 $propertyName = $printRequest->getHTMLText( NULL );
00387 }
00388 }
00389 else {
00390 $propertyName = $printRequest->getHTMLText( smwfGetLinker() );
00391 }
00392
00393 if ( $this->linkAbsolute ) {
00394 $hasPage = $object->getTypeID() == '_wpg';
00395
00396 if ( $hasPage ) {
00397 $t = Title::newFromText( $object->getLongText( $this->outputmode, NULL ), NS_MAIN );
00398 $hasPage = $t->exists();
00399 }
00400
00401 if ( $hasPage ) {
00402 $propertyValue = Html::element(
00403 'a',
00404 array( 'href' => $t->getFullUrl() ),
00405 $object->getLongText( $this->outputmode, NULL )
00406 );
00407 }
00408 else {
00409 $propertyValue = $object->getLongText( $this->outputmode, NULL );
00410 }
00411 }
00412 else {
00413 $propertyValue = $object->getLongText( $this->outputmode, smwfGetLinker() );
00414 }
00415
00416 return $propertyName . ( $propertyName === '' ? '' : ': ' ) . $propertyValue;
00417 }
00418
00432 protected function buildLocationsList( array $locations, $title, $text, $icon, array $properties ) {
00433 if ( $this->template ) {
00434 global $wgParser;
00435 $parser = version_compare( $GLOBALS['wgVersion'], '1.18', '<' ) ? $wgParser : clone $wgParser;
00436 }
00437 else {
00438 $text .= implode( '<br />', $properties );
00439 }
00440
00441 foreach ( $locations as $location ) {
00442 if ( $this->template ) {
00443 $segments = array_merge(
00444 array( $this->template, 'title=' . $title, 'latitude=' . $location->getLatitude(), 'longitude=' . $location->getLongitude() ),
00445 $properties
00446 );
00447
00448 $text .= $parser->parse( '{{' . implode( '|', $segments ) . '}}', $parser->getTitle(), new ParserOptions() )->getText();
00449 }
00450
00451 $location->setTitle( $title );
00452 $location->setText( $text );
00453 $location->setIcon( $icon );
00454
00455 $locations[] = $location;
00456 }
00457
00458 return $locations;
00459 }
00460
00470 protected function getLocationIcon( array $row ) {
00471 $icon = '';
00472 $legend_labels = array();
00473
00474
00475
00476 $display_location = method_exists( $row[0], 'getResultSubject' ) ? $row[0]->getResultSubject() : $row[0];
00477
00478 if ( property_exists( $display_location, 'display_options' ) && is_array( $display_location->display_options ) ) {
00479 $display_options = $display_location->display_options;
00480 if ( array_key_exists( 'icon', $display_options ) ) {
00481 $icon = $display_options['icon'];
00482
00483
00484 if ( array_key_exists( 'legend label', $display_options ) ) {
00485
00486 $legend_label = $display_options['legend label'];
00487
00488 if ( ! array_key_exists( $icon, $legend_labels ) ) {
00489 $legend_labels[$icon] = $legend_label;
00490 }
00491 }
00492 }
00493 }
00494 elseif ( $this->icon !== '' ) {
00495 $icon = MapsMapper::getFileUrl( $this->icon );
00496 }
00497
00498 return $icon;
00499 }
00500
00501 }