00001 <?php
00013
00014 define( 'SMW_HEADERS_SHOW', 2 );
00015 define( 'SMW_HEADERS_PLAIN', 1 );
00016 define( 'SMW_HEADERS_HIDE', 0 );
00017
00026 abstract class SMWResultPrinter {
00027
00031 protected $m_params;
00032
00041 protected $params;
00042
00047 protected $mIntro = '';
00048
00053 protected $mOutro = '';
00054
00060 protected $mSearchlabel = null;
00061
00063 protected $mDefault = '';
00064
00065
00066 protected $mFormat;
00067 protected $mLinkFirst;
00068 protected $mLinkOthers;
00069 protected $mShowHeaders = SMW_HEADERS_SHOW;
00070 protected $mShowErrors = true;
00071 protected $mInline;
00072 protected $mLinker;
00073
00081 protected $mErrors = array();
00082
00089 protected $isHTML = false;
00090
00099 protected $hasTemplates = false;
00101 private static $mRecursionDepth = 0;
00104 public static $maxRecursionDepth = 2;
00105
00110 abstract protected function getResultText( SMWQueryResult $res, $outputmode );
00111
00120 public function __construct( $format, $inline, $useValidator = false ) {
00121 global $smwgQDefaultLinking;
00122
00123 $this->mFormat = $format;
00124 $this->mInline = $inline;
00125 $this->mLinkFirst = ( $smwgQDefaultLinking != 'none' );
00126 $this->mLinkOthers = ( $smwgQDefaultLinking == 'all' );
00127 $this->mLinker = class_exists( 'DummyLinker' ) ? new DummyLinker : new Linker;
00128 }
00129
00161 public function getResult( SMWQueryResult $results, array $params, $outputmode ) {
00162 $this->isHTML = false;
00163 $this->hasTemplates = false;
00164
00165 $this->handleParameters( $params, $outputmode );
00166
00167
00168 if ( ( $outputmode != SMW_OUTPUT_FILE ) &&
00169 ( $results->getCount() == 0 ) &&
00170 ( $this->getMimeType( $results ) === false ) ) {
00171 if ( !$results->hasFurtherResults() ) {
00172 return $this->escapeText( $this->mDefault, $outputmode ) . $this->getErrorString( $results );
00173 } elseif ( $this->mInline ) {
00174 $label = $this->mSearchlabel;
00175
00176 if ( $label === null ) {
00177 $label = wfMsgForContent( 'smw_iq_moreresults' );
00178 }
00179
00180 if ( $label !== '' ) {
00181 $link = $results->getQueryLink( $this->escapeText( $label, $outputmode ) );
00182 $result = $link->getText( $outputmode, $this->mLinker );
00183 } else {
00184 $result = '';
00185 }
00186
00187 $result .= $this->getErrorString( $results );
00188
00189 return $result;
00190 }
00191 }
00192
00193
00194 $result = $this->getResultText( $results, $outputmode );
00195
00196 if ( $outputmode != SMW_OUTPUT_FILE ) {
00197 $result = $this->handleNonFileResult( $result, $results, $outputmode );
00198 }
00199
00200 if ( $GLOBALS['wgDBtype'] == 'postgres' ) {
00201 $result = pg_unescape_bytea( $result );
00202 }
00203
00204 return $result;
00205 }
00206
00218 protected function handleNonFileResult( $result, SMWQueryResult $results, $outputmode ) {
00219 global $wgParser;
00220 $result .= $this->getErrorString( $results );
00221
00222 if ( ( !$this->isHTML ) && ( $this->hasTemplates ) ) {
00223 if ( ( $wgParser->getTitle() instanceof Title ) && ( $wgParser->getOptions() instanceof ParserOptions ) ) {
00224 SMWResultPrinter::$mRecursionDepth++;
00225
00226 if ( SMWResultPrinter::$mRecursionDepth <= SMWResultPrinter::$maxRecursionDepth ) {
00227 $result = '[[SMW::off]]' . $wgParser->replaceVariables( $result ) . '[[SMW::on]]';
00228 } else {
00229 $result = '';
00230 }
00231
00232 SMWResultPrinter::$mRecursionDepth--;
00233 } else {
00234 $result = '[[SMW::off]]' . $result . '[[SMW::on]]';
00235 }
00236 }
00237
00238 if ( ( $this->isHTML ) && ( $outputmode == SMW_OUTPUT_WIKI ) ) {
00239 $result = array( $result, 'isHTML' => true );
00240 } elseif ( ( !$this->isHTML ) && ( $outputmode == SMW_OUTPUT_HTML ) ) {
00241 SMWResultPrinter::$mRecursionDepth++;
00242
00243
00244 if ( SMWResultPrinter::$mRecursionDepth <= SMWResultPrinter::$maxRecursionDepth ) {
00245 if ( ( $wgParser->getTitle() instanceof Title ) && ( $wgParser->getOptions() instanceof ParserOptions ) ) {
00246 $result = $wgParser->recursiveTagParse( $result );
00247 } else {
00248 global $wgTitle;
00249
00250 $popt = new ParserOptions();
00251 $popt->setEditSection( false );
00252 $pout = $wgParser->parse( $result . '__NOTOC__', $wgTitle, $popt );
00253
00255 SMWOutputs::requireFromParserOutput( $pout );
00256 $result = $pout->getText();
00257 }
00258 } else {
00259 $result = '';
00260 }
00261
00262 SMWResultPrinter::$mRecursionDepth--;
00263 }
00264
00265 if ( ( $this->mIntro ) && ( $results->getCount() > 0 ) ) {
00266 if ( $outputmode == SMW_OUTPUT_HTML ) {
00267 global $wgParser;
00268 $result = $wgParser->recursiveTagParse( $this->mIntro ) . $result;
00269 } else {
00270 $result = $this->mIntro . $result;
00271 }
00272 }
00273
00274 if ( ( $this->mOutro ) && ( $results->getCount() > 0 ) ) {
00275 if ( $outputmode == SMW_OUTPUT_HTML ) {
00276 global $wgParser;
00277 $result = $result . $wgParser->recursiveTagParse( $this->mOutro );
00278 } else {
00279 $result = $result . $this->mOutro;
00280 }
00281 }
00282
00283 return $result;
00284 }
00285
00296 protected function handleParameters( array $params, $outputmode ) {
00297 $this->params = $params;
00298 $this->m_params = $params;
00299
00300 if ( array_key_exists( 'intro', $params ) ) { $this->mIntro = $params['intro']; }
00301 if ( array_key_exists( 'outro', $params ) ) { $this->mOutro = $params['outro']; }
00302
00303 if ( array_key_exists( 'searchlabel', $params ) ) {
00304 $this->mSearchlabel = $params['searchlabel'] === false ? null : $params['searchlabel'];
00305 }
00306
00307 switch ( $params['link'] ) {
00308 case 'head': case 'subject':
00309 $this->mLinkFirst = true;
00310 $this->mLinkOthers = false;
00311 break;
00312 case 'all':
00313 $this->mLinkFirst = true;
00314 $this->mLinkOthers = true;
00315 break;
00316 case 'none':
00317 $this->mLinkFirst = false;
00318 $this->mLinkOthers = false;
00319 break;
00320 }
00321
00322 if ( array_key_exists( 'default', $params ) ) { $this->mDefault = str_replace( '_', ' ', $params['default'] ); }
00323
00324 if ( $params['headers'] == 'hide' ) {
00325 $this->mShowHeaders = SMW_HEADERS_HIDE;
00326 } elseif ( $params['headers'] == 'plain' ) {
00327 $this->mShowHeaders = SMW_HEADERS_PLAIN;
00328 } else {
00329 $this->mShowHeaders = SMW_HEADERS_SHOW;
00330 }
00331 }
00332
00339 protected function getLinker( $firstcol = false ) {
00340 if ( ( $firstcol && $this->mLinkFirst ) || ( !$firstcol && $this->mLinkOthers ) ) {
00341 return $this->mLinker;
00342 } else {
00343 return null;
00344 }
00345 }
00346
00357 public function getMimeType( $res ) {
00358 return false;
00359 }
00360
00374 public function getQueryMode( $context ) {
00375 return SMWQuery::MODE_INSTANCES;
00376 }
00377
00387 public function getFileName( $res ) {
00388 return false;
00389 }
00390
00400 public function getName() {
00401 return $this->mFormat;
00402 }
00403
00413 public function getErrorString( SMWQueryResult $res ) {
00414 return $this->mShowErrors ? smwfEncodeMessages( array_merge( $this->mErrors, $res->getErrors() ) ) : '';
00415 }
00416
00422 public function setShowErrors( $show ) {
00423 $this->mShowErrors = $show;
00424 }
00425
00432 protected function escapeText( $text, $outputmode ) {
00433 return ( $outputmode == SMW_OUTPUT_HTML ) ? htmlspecialchars( $text ) : $text;
00434 }
00435
00442 protected function getSearchLabel( $outputmode ) {
00443 return $this->escapeText( $this->mSearchlabel, $outputmode );
00444 }
00445
00453 protected function linkFurtherResults( $results ) {
00454 return $this->mInline && $results->hasFurtherResults() && $this->mSearchlabel !== '';
00455 }
00456
00465 protected function addError( $errorMessage ) {
00466 $this->mErrors[] = $errorMessage;
00467 }
00468
00478 protected function textDisplayParameters() {
00479 $params = array();
00480
00481 $params['intro'] = new Parameter( 'intro' );
00482 $params['intro']->setMessage( 'smw_paramdesc_intro' );
00483 $params['intro']->setDefault( '' );
00484
00485 $params['outro'] = new Parameter( 'outro' );
00486 $params['outro']->setMessage( 'smw_paramdesc_outro' );
00487 $params['outro']->setDefault( '' );
00488
00489 $params['default'] = new Parameter( 'default' );
00490 $params['default']->setMessage( 'smw_paramdesc_default' );
00491 $params['default']->setDefault( '' );
00492
00493 return $params;
00494 }
00495
00504 protected function exportFormatParameters() {
00505 $params = array();
00506
00507 $params['searchlabel'] = new Parameter( 'searchlabel' );
00508 $params['searchlabel']->setMessage( 'smw_paramdesc_searchlabel' );
00509 $params['searchlabel']->setDefault( false, false );
00510
00511 return $params;
00512 }
00513
00522 public function getValidatorParameters() {
00523 $params = array();
00524
00525 foreach ( $this->getParameters() as $param ) {
00526 $param = $this->toValidatorParam( $param );
00527 $params[$param->getName()] = $param;
00528 }
00529
00530 return $params;
00531 }
00532
00543 protected function toValidatorParam( $param ) {
00544 static $typeMap = array(
00545 'int' => Parameter::TYPE_INTEGER
00546 );
00547
00548 if ( !( $param instanceof Parameter ) ) {
00549 if ( !array_key_exists( 'type', $param ) ) {
00550 $param['type'] = 'string';
00551 }
00552
00553 $paramClass = $param['type'] == 'enum-list' ? 'ListParameter' : 'Parameter';
00554 $paramType = array_key_exists( $param['type'], $typeMap ) ? $typeMap[$param['type']] : Parameter::TYPE_STRING;
00555
00556 $parameter = new $paramClass( $param['name'], $paramType );
00557
00558 if ( array_key_exists( 'description', $param ) ) {
00559 $parameter->setDescription( $param['description'] );
00560 }
00561
00562 if ( array_key_exists( 'values', $param ) && is_array( $param['values'] ) ) {
00563 $parameter->addCriteria( new CriterionInArray( $param['values'] ) );
00564 }
00565
00566 return $parameter;
00567 }
00568 else {
00569 return $param;
00570 }
00571 }
00572
00585 public function getParameters() {
00586 return array();
00587 }
00588
00589 }