00001 <?php
00002
00018 class SMWQueryUIHelper {
00019
00024 protected $queryString = '';
00025
00031 protected $parameters = array();
00032
00037 protected $printOuts = array();
00038
00045 protected $printOutStrings = array();
00046
00051 private $errorsOccured = false;
00052
00058 private $context;
00059
00065 private $errors = array();
00066
00070 private $defaultResultPrinter = 'broadtable';
00071
00077 private $queryResult = null;
00078
00083 const SPECIAL_PAGE = 0;
00084 const WIKI_LINK = 1;
00085
00091 protected static $uiPages = array();
00092
00100 public function __construct( $context = SMWQueryUIHelper::SPECIAL_PAGE ) {
00101 $this->context = $context;
00102 }
00103
00109 public function hasError() {
00110 return $this->errorsOccured;
00111 }
00112
00118 public function getLimit() {
00119 if ( array_key_exists( 'limit', $this->parameters ) ) {
00120 return $this->parameters['limit'];
00121 } else {
00122 return 0;
00123 }
00124 }
00125
00132 public function getOffset() {
00133 if ( array_key_exists( 'offset', $this->parameters ) ) {
00134 return $this->parameters['offset'];
00135 } else {
00136 return 20;
00137 }
00138 }
00139
00145 public function hasFurtherResults() {
00146 if ( !is_null( $this->queryResult ) ) {
00147 return $this->queryResult->hasFurtherResults();
00148 } else {
00149 return false;
00150 }
00151 }
00152
00158 public function getResultObject() {
00159 return $this->queryResult;
00160 }
00161
00167 public function getErrors() {
00168 return $this->errors;
00169 }
00170
00181 public static function addUI( SpecialPage &$page ) {
00182
00183
00184
00185
00186 self::$uiPages[] = $page;
00187 }
00188
00195 public static function getUiList() {
00196 return self::$uiPages;
00197 }
00198
00206 public function setQueryString( $queryString = "", $enableValidation = false ) {
00207 $this -> queryString = $queryString;
00208
00209 $errors = array();
00210 if ( $enableValidation ) {
00211 if ( $queryString === '' ) {
00212 $errors[] = wfMsg( 'smw_qui_noquery' );
00213 } else {
00214 $query = SMWQueryProcessor::createQuery( $queryString, array() );
00215 $errors = $query ->getErrors();
00216 }
00217 if ( !empty ( $errors ) ) {
00218 $this->errorsOccured = true;
00219 }
00220 $this->errors = array_merge( $errors, $this->errors );
00221 }
00222
00223 return $errors;
00224 }
00225
00240 public function setPrintOuts( array $printOuts = array(), $enableValidation = false ) {
00241
00242
00243
00244
00245 $errors = array();
00246 if ( $enableValidation ) {
00247 foreach ( $printOuts as $key => $prop ) {
00248 if ( $prop[0] != '?' ) {
00249 $printOuts[$key] = "?" . $printOuts[$key];
00250 }
00251 if ( !$this->validateProperty( $prop ) ) {
00252 $errors[] = wfMsg( 'smw_qui_invalidprop', $prop );
00253 $this->errorsOccured = true;
00254 }
00255 }
00256 }
00257 $this -> printOutStrings = $printOuts;
00258 $this->errors = array_merge( $errors, $this->errors );
00259 return $errors;
00260 }
00261
00279 public function setParams( array $params = array(), $enableValidation = false ) {
00280 global $smwgQMaxInlineLimit, $smwgResultFormats;
00281 $errors = array();
00282
00283
00284 if ( !array_key_exists( 'format', $params ) || ! array_key_exists ( $params['format'], $smwgResultFormats ) ) {
00285 $params[ 'format' ] = $this->defaultResultPrinter;
00286 }
00287 if ( !array_key_exists( 'limit', $params ) ) {
00288 $params[ 'limit' ] = 20;
00289 }
00290 $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit );
00291 if ( !array_key_exists( 'offset', $params ) ) {
00292 $params['offset'] = 0;
00293 }
00294
00295 if ( $enableValidation ) {
00296 if ( !array_key_exists( $params['format'], $smwgResultFormats ) ) {
00297 $errors[] = wfMsg( 'smw_qui_invalidformat', $params['format'] );
00298 $this->errorsOccured = true;
00299 } else {
00300 $printer = SMWQueryProcessor::getResultPrinter( $params[ 'format' ] );
00301 $para_meters = $printer->getParameters();
00302 if ( is_array( $para_meters ) ) {
00303 $validator = new Validator();
00304 $validator->setParameters( $params, $para_meters );
00305 $validator->validateParameters();
00306 if ( $validator->hasFatalError() ) {
00307 array_merge ( $errors, $validator->getErrorMessages () );
00308 $this->errorsOccured = true;
00309 }
00310 }
00311 }
00312 }
00313
00314 $this->parameters = $params;
00315 $this->errors = array_merge( $errors, $this->errors );
00316 return $errors;
00317 }
00318
00324 public function extractParameters( $p ) {
00325 if ( $this->context == self::SPECIAL_PAGE ) {
00326
00327 $rawParams = array_merge( $this->parameters, array( $this->queryString ), $this->printOutStrings );
00328 } else {
00329 $rawParams = SMWInfolink::decodeParameters( $p, true );
00330
00331 $this->setParams( $rawParams );
00332 $rawParams = $this->parameters;
00333 }
00334
00335 SMWQueryProcessor::processFunctionParams( $rawParams, $this->queryString, $this->parameters, $this->printOuts );
00336 }
00337
00348 public function execute() {
00349 $errors = array();
00350
00351 if ( $this->queryString !== '' ) {
00352
00353 SMWQueryProcessor::addThisPrintout( $this->printOuts, $this->parameters );
00354 $params = SMWQueryProcessor::getProcessedParams( $this->parameters, $this->printOuts );
00355 $this->parameters['format'] = $params['format'];
00356 $this->params = $params;
00357
00358 $query = SMWQueryProcessor::createQuery(
00359 $this->queryString,
00360 $params,
00361 SMWQueryProcessor::SPECIAL_PAGE,
00362 $this->parameters['format'],
00363 $this->printOuts
00364 );
00365
00366 $res = smwfGetStore()->getQueryResult( $query );
00367 $this->queryResult = $res;
00368
00369 $errors = array_merge( $errors, $res->getErrors() );
00370 if ( !empty( $errors ) ) {
00371 $this->errorsOccured = true;
00372 $this->errors = array_merge( $errors, $this->errors );
00373 }
00374
00375
00376 if ( $this->parameters['format'] == 'rss' ) {
00377 $descKey = 'rssdescription';
00378 $titleKey = 'rsstitle';
00379 } elseif ( $this->parameters['format'] == 'icalendar' ) {
00380 $descKey = 'icalendardescription';
00381 $titleKey = 'icalendartitle';
00382 } else {
00383 $descKey = false;
00384 }
00385
00386 if ( $descKey && ( $query->getDescription() instanceof SMWConceptDescription ) &&
00387 ( !isset( $this->parameters[$descKey] ) || !isset( $this->parameters[$titleKey] ) ) ) {
00388 $concept = $query->getDescription()->getConcept();
00389
00390 if ( !isset( $this->parameters[$titleKey] ) ) {
00391 $this->parameters[$titleKey] = $concept->getText();
00392 }
00393
00394 if ( !isset( $this->parameters[$descKey] ) ) {
00395
00396 $dv = end( smwfGetStore()->getPropertyValues( SMWWikiPageValue::makePageFromTitle( $concept ), new SMWDIProperty( '_CONC' ) ) );
00397 if ( $dv instanceof SMWConceptValue ) {
00398 $this->parameters[$descKey] = $dv->getDocu();
00399 }
00400 }
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 $printer = SMWQueryProcessor::getResultPrinter(
00410 $this->parameters['format'],
00411 SMWQueryProcessor::SPECIAL_PAGE
00412 );
00413 $resultMime = $printer->getMimeType( $res );
00414 if ( $this->context == self::WIKI_LINK && $resultMime != false ) {
00415 global $wgOut;
00416 $result = $printer->getResult( $res, $this->parameters, SMW_OUTPUT_FILE );
00417 $resultName = $printer->getFileName( $res );
00418 $wgOut->disable();
00419 header( "Content-type: $resultMime; charset=UTF-8" );
00420 if ( $resultName !== false ) {
00421 header( "content-disposition: attachment; filename=$resultName" );
00422 }
00423 echo $result;
00424 }
00425 }
00426 }
00427
00436 public function getHTMLResult() {
00437 $result = '';
00438
00439 $res = $this->queryResult;
00440 $printer = SMWQueryProcessor::getResultPrinter( $this->parameters['format'],
00441 SMWQueryProcessor::SPECIAL_PAGE );
00442
00443 if ( $res->getCount() > 0 ) {
00444 $queryResult = $printer->getResult( $res, $this->params, SMW_OUTPUT_HTML );
00445
00446 if ( is_array( $queryResult ) ) {
00447 $result .= $queryResult[0];
00448 } else {
00449 $result .= $queryResult;
00450 }
00451 } else {
00452 $result = wfMsg( 'smw_result_noresults' );
00453 }
00454
00455 return $result;
00456 }
00457
00463 public function getAsk() {
00464 $result = '{{#ask:' . htmlspecialchars( $this->queryString ) . "\n";
00465 foreach ( $this->printOuts as $printout ) {
00466 $result .= '|' . $printout->getSerialisation() . "\n";
00467 }
00468 foreach ( $this->parameters as $param_name => $param_value ) {
00469 $result .= '|' . htmlspecialchars( $param_name ) .
00470 '=' . htmlspecialchars( $param_value ) . "\n";
00471 }
00472 $result .= '}}';
00473 return $result;
00474 }
00475
00481 public function getQueryString() {
00482 return $this->queryString;
00483 }
00484
00490 public function getResultCount() {
00491 if ( !is_null( $this->queryResult ) ) {
00492 return $this->queryResult->getCount();
00493 } else {
00494 return 0;
00495 }
00496 }
00497
00503 public function getParameters() {
00504 return $this->parameters;
00505 }
00506
00512 public function getPrintOuts() {
00513 if ( !empty( $this->printOuts ) &&
00514 ( $this->printOuts[0] instanceof SMWPrintRequest ) ) {
00515 return $this->printOuts;
00516 }
00517 return array();
00518 }
00519
00536 public static function makeForInfoLink( $p, $enableValidation = false ) {
00537 $result = new SMWQueryUIHelper( self::WIKI_LINK );
00538 $result->extractParameters( $p );
00539 $result->execute();
00540 return $result;
00541 }
00542
00560 public static function makeForUI( $query, array $params, array $printouts, $enableValidation = false ) {
00561 $result = new SMWQueryUIHelper( self::SPECIAL_PAGE );
00562 $result->setParams( $params, $enableValidation );
00563 $result->setPrintOuts( $printouts, $enableValidation );
00564 $result->setQueryString( $query, $enableValidation );
00565 $result->extractParameters( '' );
00566 return $result;
00567 }
00568
00575 protected static function validateProperty( $property ) {
00576
00577
00578
00579
00580 $prop = substr( $property, 1 );
00581 $property_page = Title::newFromText( $prop, SMW_NS_PROPERTY );
00582 if ( $property_page instanceof Title ) {
00583 return( $property_page->exists() );
00584 } else {
00585 return false;
00586 }
00587 }
00588
00589 }