Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
— | — | @@ -0,0 +1,646 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * A base class for Semantic Search UIs. All Semantic Search UI's may subclass |
| 6 | + * from this. |
| 7 | + * |
| 8 | + * @author Markus Krötzsch |
| 9 | + * @author Yaron Koren |
| 10 | + * @author Sanyam Goyal |
| 11 | + * @author Jeroen De Dauw |
| 12 | + */ |
| 13 | +abstract class SMWQueryUI extends SpecialPage{ |
| 14 | + protected $m_ui_helper; |
| 15 | + protected abstract function execute($p){ |
| 16 | + /* |
| 17 | + * Extract the parameters from the UI. Use either the constructor or |
| 18 | + * $this->m_ui_helper=new SMWUIHelper() |
| 19 | + */ |
| 20 | + } |
| 21 | + protected function getUrlTail(){ |
| 22 | + $urltail='&q=' . urlencode( $this->m_ui_helper->getQuerystring() ); |
| 23 | + $tmp_parray = array(); |
| 24 | + $params=$this->m_ui_helper->getParams(); |
| 25 | + foreach ( $params as $key => $value ) { |
| 26 | + if ( !in_array( $key, array( 'sort', 'order', 'limit', 'offset', 'title' ) ) ) { |
| 27 | + $tmp_parray[$key] = $value; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + $urltail .= '&p=' . urlencode( SMWInfolink::encodeParameters( $tmp_parray ) ); |
| 32 | + $printoutstring = ''; |
| 33 | + foreach ( $this->m_ui_helper->getPrintOuts() as $printout ) { |
| 34 | + $printoutstring .= $printout->getSerialisation() . "\n"; |
| 35 | + } |
| 36 | + |
| 37 | + if ( $printoutstring != '' ) $urltail .= '&po=' . urlencode( $printoutstring ); |
| 38 | + if ( array_key_exists( 'sort', $params ) ) $urltail .= '&sort=' . $params['sort']; |
| 39 | + if ( array_key_exists( 'order', $params ) ) $urltail .= '&order=' . $params['order']; |
| 40 | + } |
| 41 | + protected function makeHtmlResult(){ |
| 42 | + global $wgOut; |
| 43 | + if(is_a($this->m_ui_helper, 'SMWQueryUIHelper')){ |
| 44 | + |
| 45 | + } |
| 46 | + } |
| 47 | + /** |
| 48 | + * Display a form section showing the options for a given format, |
| 49 | + * based on the getParameters() value for that format's query printer. |
| 50 | + * |
| 51 | + * @param string $format |
| 52 | + * @param array $paramValues The current values for the parameters (name => value) |
| 53 | + * |
| 54 | + * @return string |
| 55 | + */ |
| 56 | + protected function showFormatOptions( $format, array $paramValues ) { |
| 57 | + $text = ''; |
| 58 | + |
| 59 | + $printer = SMWQueryProcessor::getResultPrinter( $format, SMWQueryProcessor::SPECIAL_PAGE ); |
| 60 | + |
| 61 | + $params = method_exists( $printer, 'getParameters' ) ? $printer->getParameters() : array(); |
| 62 | + |
| 63 | + // Ignore the format parameter, as we got a special control in the GUI for it already. |
| 64 | + unset( $params['format'] ); |
| 65 | + |
| 66 | + $optionsHtml = array(); |
| 67 | + |
| 68 | + foreach ( $params as $param ) { |
| 69 | + $param = $this->toValidatorParam( $param ); |
| 70 | + $currentValue = array_key_exists( $param->getName(), $paramValues ) ? $paramValues[$param->getName()] : false; |
| 71 | + |
| 72 | + $optionsHtml[] = |
| 73 | + Html::rawElement( |
| 74 | + 'div', |
| 75 | + array( |
| 76 | + 'style' => 'width: 30%; padding: 5px; float: left;' |
| 77 | + ), |
| 78 | + htmlspecialchars( $param->getName() ) . ': ' . |
| 79 | + $this->showFormatOption( $param, $currentValue ) . |
| 80 | + '<br />' . |
| 81 | + Html::element( 'em', array(), $param->getDescription() ) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + for ( $i = 0, $n = count( $optionsHtml ); $i < $n; $i++ ) { |
| 86 | + if ( $i % 3 == 2 || $i == $n - 1 ) { |
| 87 | + $optionsHtml[$i] .= "<div style=\"clear: both\";></div>\n"; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + $i = 0; |
| 92 | + $rowHtml = ''; |
| 93 | + $resultHtml = ''; |
| 94 | + |
| 95 | + while ( $option = array_shift( $optionsHtml ) ) { |
| 96 | + $rowHtml .= $option; |
| 97 | + $i++; |
| 98 | + |
| 99 | + if ( $i % 3 == 0 ) { |
| 100 | + $resultHtml .= Html::rawElement( |
| 101 | + 'div', |
| 102 | + array( |
| 103 | + 'style' => 'background: ' . ( $i % 6 == 0 ? 'white' : '#dddddd' ) . ';' |
| 104 | + ), |
| 105 | + $rowHtml |
| 106 | + ); |
| 107 | + $rowHtml = ''; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return $resultHtml; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns a Validator style Parameter definition. |
| 116 | + * SMW 1.5.x style definitions are converted. |
| 117 | + * |
| 118 | + * @since 1.6 |
| 119 | + * |
| 120 | + * @param mixed $param |
| 121 | + * |
| 122 | + * @return Parameter |
| 123 | + */ |
| 124 | + protected function toValidatorParam( $param ) { |
| 125 | + static $typeMap = array( |
| 126 | + 'int' => Parameter::TYPE_INTEGER |
| 127 | + ); |
| 128 | + |
| 129 | + if ( !( $param instanceof Parameter ) ) { |
| 130 | + if ( !array_key_exists( 'type', $param ) ) { |
| 131 | + $param['type'] = 'string'; |
| 132 | + } |
| 133 | + |
| 134 | + $paramClass = $param['type'] == 'enum-list' ? 'ListParameter' : 'Parameter'; |
| 135 | + $paramType = array_key_exists( $param['type'], $typeMap ) ? $typeMap[$param['type']] : Parameter::TYPE_STRING; |
| 136 | + |
| 137 | + $parameter = new $paramClass( $param['name'], $paramType ); |
| 138 | + |
| 139 | + if ( array_key_exists( 'description', $param ) ) { |
| 140 | + $parameter->setDescription( $param['description'] ); |
| 141 | + } |
| 142 | + |
| 143 | + if ( array_key_exists( 'values', $param ) && is_array( $param['values'] ) ) { |
| 144 | + $parameter->addCriteria( new CriterionInArray( $param['values'] ) ); |
| 145 | + } |
| 146 | + |
| 147 | + return $parameter; |
| 148 | + } |
| 149 | + else { |
| 150 | + return $param; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the HTML for a single parameter input. |
| 156 | + * |
| 157 | + * @since 1.6 |
| 158 | + * |
| 159 | + * @param Parameter $parameter |
| 160 | + * @param mixed $currentValue |
| 161 | + * |
| 162 | + * @return string |
| 163 | + */ |
| 164 | + protected function showFormatOption( Parameter $parameter, $currentValue ) { |
| 165 | + $input = new ParameterInput( $parameter ); |
| 166 | + $input->setInputName( 'p[' . $parameter->getName() . ']' ); |
| 167 | + |
| 168 | + if ( $currentValue !== false ) { |
| 169 | + $input->setCurrentValue( $currentValue ); |
| 170 | + } |
| 171 | + |
| 172 | + return $input->getHtml(); |
| 173 | + } |
| 174 | + |
| 175 | + protected function usesNavigationBar(){ |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Build the navigation for some given query result, reuse url-tail parameters. |
| 181 | + * |
| 182 | + * @param SMWQueryResult $res |
| 183 | + * @param string $urltail |
| 184 | + * |
| 185 | + * @return string |
| 186 | + */ |
| 187 | + protected function getNavigationBar( SMWQueryResult $res, $urltail, $offset, $limit ) { |
| 188 | + global $smwgQMaxInlineLimit; |
| 189 | + |
| 190 | + // Prepare navigation bar. |
| 191 | + if ( $offset > 0 ) { |
| 192 | + $navigation = Html::element( |
| 193 | + 'a', |
| 194 | + array( |
| 195 | + 'href' => SpecialPage::getSafeTitleFor( 'Ask' )->getLocalURL( array( |
| 196 | + 'offset' => max( 0, $offset - $limit ), |
| 197 | + 'limit' => $limit . $urltail |
| 198 | + ) ), |
| 199 | + 'rel' => 'nofollow' |
| 200 | + ), |
| 201 | + wfMsg( 'smw_result_prev' ) |
| 202 | + ); |
| 203 | + |
| 204 | + } else { |
| 205 | + $navigation = wfMsg( 'smw_result_prev' ); |
| 206 | + } |
| 207 | + |
| 208 | + $navigation .= |
| 209 | + '     <b>' . |
| 210 | + wfMsg( 'smw_result_results' ) . ' ' . ( $offset + 1 ) . |
| 211 | + '– ' . |
| 212 | + ( $offset + $res->getCount() ) . |
| 213 | + '</b>    '; |
| 214 | + |
| 215 | + if ( $res->hasFurtherResults() ) { |
| 216 | + $navigation .= Html::element( |
| 217 | + 'a', |
| 218 | + array( |
| 219 | + 'href' => SpecialPage::getSafeTitleFor( 'Ask' )->getLocalURL( array( |
| 220 | + 'offset' => ( $offset + $limit ), |
| 221 | + 'limit' => $limit . $urltail |
| 222 | + ) ), |
| 223 | + 'rel' => 'nofollow' |
| 224 | + ), |
| 225 | + wfMsg( 'smw_result_next' ) |
| 226 | + ); |
| 227 | + } else { |
| 228 | + $navigation .= wfMsg( 'smw_result_next' ); |
| 229 | + } |
| 230 | + |
| 231 | + $first = true; |
| 232 | + |
| 233 | + foreach ( array( 20, 50, 100, 250, 500 ) as $l ) { |
| 234 | + if ( $l > $smwgQMaxInlineLimit ) break; |
| 235 | + |
| 236 | + if ( $first ) { |
| 237 | + $navigation .= '        ('; |
| 238 | + $first = false; |
| 239 | + } else { |
| 240 | + $navigation .= ' | '; |
| 241 | + } |
| 242 | + |
| 243 | + if ( $limit != $l ) { |
| 244 | + $navigation .= Html::element( |
| 245 | + 'a', |
| 246 | + array( |
| 247 | + 'href' => SpecialPage::getSafeTitleFor( 'Ask' )->getLocalURL( array( |
| 248 | + 'offset' => $offset, |
| 249 | + 'limit' => $l . $urltail |
| 250 | + ) ), |
| 251 | + 'rel' => 'nofollow' |
| 252 | + ), |
| 253 | + $l |
| 254 | + ); |
| 255 | + } else { |
| 256 | + $navigation .= '<b>' . $l . '</b>'; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + $navigation .= ')'; |
| 261 | + |
| 262 | + return $navigation; |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +/** |
| 267 | + * This class helps to implement a Special Page for creating and executing queries. |
| 268 | + * |
| 269 | + * Query UIs may use this class and override methods to create a customised UI |
| 270 | + * interface. |
| 271 | + * |
| 272 | + * @author Devayon Das |
| 273 | + * |
| 274 | + * @property boolean $enable_validation If set to TRUE causes each of the parametes to be checked for errors. |
| 275 | + */ |
| 276 | +class SMWQueryUIHelper { |
| 277 | + |
| 278 | +//members |
| 279 | + protected $m_querystring = ''; //The query |
| 280 | + protected $m_params = array(); //Parameters controlling how the results should be displayed |
| 281 | + protected $m_printouts = array(); //Properties to be printed along with results |
| 282 | + protected static $m_UIPages = array(); //A list of Query UIs |
| 283 | + public $enable_validation; |
| 284 | + private $fatal_errors= false; |
| 285 | + private $context; |
| 286 | + private $errors=array(); |
| 287 | + const SPECIAL_PAGE=0;//parameters passed from special page |
| 288 | + const WIKI_LINK=1;//parameters passed from 'further links' in the wiki. |
| 289 | + |
| 290 | + |
| 291 | +//constructor |
| 292 | + public function __construct($enable_validation = true, $context=self::SPECIAL_PAGE) { |
| 293 | + $this -> enable_validation = $enable_validation; |
| 294 | + $this->context=$context; |
| 295 | + } |
| 296 | + |
| 297 | + public function hasError(){ |
| 298 | + return $this->fatal_errors; |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * |
| 303 | + * Returns an array of errors, if any have occured. |
| 304 | + * @return array of strings |
| 305 | + */ |
| 306 | + public function getErrors(){ |
| 307 | + return $this->errors; |
| 308 | + } |
| 309 | + /** |
| 310 | + * Register a Semantic Search Special Page |
| 311 | + * @param SpecialPage $page |
| 312 | + */ |
| 313 | + public static function addUI( SpecialPage &$page ){ |
| 314 | + /* |
| 315 | + * This way of registering, instead of using a global variable will cause |
| 316 | + * SMWQueryUIHelper to AutoLoad, but the alternate would break encapsulation. |
| 317 | + */ |
| 318 | + self::$m_UIPages[]=$page; |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * Returns an array of Semantic Search Special Pages |
| 323 | + * @return array of SpecialPage |
| 324 | + */ |
| 325 | + public static function getUiList(){ |
| 326 | + return self::$m_UIPages; |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * Initialises the object with a Query. If the Query string is of incorrect syntax, |
| 331 | + * returns an array of errors. |
| 332 | + * |
| 333 | + * @param string $querystring The query |
| 334 | + * @return array array of errors, if any. |
| 335 | + */ |
| 336 | + public function setQueryString( $querystring =""){ |
| 337 | + $this -> m_querystring = $querystring; |
| 338 | + $errors=array(); |
| 339 | + if($this->enable_validation){ |
| 340 | + if($querystring==''){ |
| 341 | + $errors[]="No query has been specified"; //TODO i18n |
| 342 | + } |
| 343 | + else |
| 344 | + { |
| 345 | + $query = SMWQueryProcessor::createQuery($querystring, array()); |
| 346 | + $errors=$query ->getErrors(); |
| 347 | + } |
| 348 | + if (!empty ($errors)){ |
| 349 | + $this->fatal_errors=true; |
| 350 | + } |
| 351 | + $this->errors = array_merge($errors,$this->errors); |
| 352 | + return $errors; |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * |
| 358 | + * If $enable_validation is true, checks if all the values in $printouts are |
| 359 | + * properties which exist in the wiki and returns a warning string (for each |
| 360 | + * property). Returns null otherwise. |
| 361 | + * |
| 362 | + * @param array $printouts Array of additional properties to be shown in results |
| 363 | + * @return array array of errors, if any. |
| 364 | + */ |
| 365 | + public function setPrintOuts( array $printouts=array() ){ |
| 366 | + $errors = array(); |
| 367 | + if($this -> enable_validation){ |
| 368 | + foreach($printouts as $key => $prop){ |
| 369 | + if($prop[0]!='?'){ |
| 370 | + $printouts[$key]="?".$printouts[$key]; |
| 371 | + } |
| 372 | + if(!$this->validateProperty($prop)){ |
| 373 | + $errors[]="$prop may not be a valid property"; //TODO: add i18n |
| 374 | + $this->fatal_errors=true; |
| 375 | + } |
| 376 | + } |
| 377 | + } |
| 378 | + $this -> m_printouts = $printouts; |
| 379 | + $this->errors = array_merge($errors,$this->errors); |
| 380 | + return $errors; |
| 381 | + } |
| 382 | + |
| 383 | + public function setParams( array $params =array()){ |
| 384 | + /* |
| 385 | + *Validate, and add missing params. * |
| 386 | + */ |
| 387 | + global $smwgQMaxInlineLimit, $smwgResultFormats; |
| 388 | + $errors = array(); |
| 389 | + |
| 390 | + //checking for missing parameters and adding them |
| 391 | + if( !array_key_exists( 'format', $params ) ) |
| 392 | + $params[ 'format' ] = $this->getDefaultResultPrinter (); |
| 393 | + if( !array_key_exists('order', $params ) ) |
| 394 | + $params[ 'order' ] = ''; |
| 395 | + if( !array_key_exists( 'limit', $params) ) |
| 396 | + $params[ 'limit' ] = 20; |
| 397 | + $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit ); |
| 398 | + if(!array_key_exists('offset', $params)) |
| 399 | + $params['offset']=0; |
| 400 | + |
| 401 | + if ($this->enable_validation){ |
| 402 | + //validating the format |
| 403 | + if(!array_key_exists($params['format'], $smwgResultFormats)){ |
| 404 | + $errors[]="The chosen format "+$params['format']+" does not exist for this wiki"; //TODO i18n |
| 405 | + $this->fatal_errors=true; |
| 406 | + } |
| 407 | + else |
| 408 | + { //validating parameters for result printer |
| 409 | + $printer = SMWQueryProcessor::getResultPrinter( $params[ 'format' ] ); |
| 410 | + $parameters=$printer->getParameters(); |
| 411 | + if(is_array($parameters)){ |
| 412 | + //if(is_a($parameters[0], 'Parameter')){ |
| 413 | + $validator = new Validator(); |
| 414 | + $validator -> setParameters($params,$parameters); |
| 415 | + $validator->validateParameters(); |
| 416 | + $validator_has_error = $validator->hasFatalError(); |
| 417 | + if($validator_has_error){ |
| 418 | + array_merge ($errors, $validator->getErrorMessages ()); |
| 419 | + $this->fatal_errors=true; |
| 420 | + } |
| 421 | + //} |
| 422 | + } |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + $this -> m_params = $params; |
| 427 | + $this->errors = array_merge($errors,$this->errors); |
| 428 | + return $errors; |
| 429 | + } |
| 430 | + |
| 431 | + public function makeHTMLResult(){ |
| 432 | + /* |
| 433 | + * Once $m_querystring, $m_params, $m_printouts are set, generates the |
| 434 | + * results / or link. The pagination links (or navigation bar) are expected |
| 435 | + * to be created by the UI designer. (or maybe we can put a method here to |
| 436 | + * make the nav-bar which also calls makeHTMLResult(). |
| 437 | + */ |
| 438 | + $result=''; |
| 439 | + $errors=array(); |
| 440 | + $query=SMWQueryProcessor::createQuery($this->m_querystring, $this->m_params,SMWQueryProcessor::SPECIAL_PAGE , $this->m_params['format'], $this->m_printouts); |
| 441 | + $res=smwfGetStore()->getQueryResult($query); |
| 442 | + $errors= array_merge($errors, $res->getErrors()); |
| 443 | + if(!empty($errors)){ |
| 444 | + $this->fatal_errors=true; |
| 445 | + $this->errors = array_merge($errors,$this->errors); |
| 446 | + } |
| 447 | + |
| 448 | + // BEGIN: Try to be smart for rss/ical if no description/title is given and we have a concept query |
| 449 | + if ( $this->m_params['format'] == 'rss' ) { |
| 450 | + $desckey = 'rssdescription'; |
| 451 | + $titlekey = 'rsstitle'; |
| 452 | + } elseif ( $this->m_params['format'] == 'icalendar' ) { |
| 453 | + $desckey = 'icalendardescription'; |
| 454 | + $titlekey = 'icalendartitle'; |
| 455 | + } else { |
| 456 | + $desckey = false; |
| 457 | + } |
| 458 | + |
| 459 | + if ( ( $desckey ) && ( $query->getDescription() instanceof SMWConceptDescription ) && |
| 460 | + ( !isset( $this->m_params[$desckey] ) || !isset( $this->m_params[$titlekey] ) ) ) { |
| 461 | + $concept = $query->getDescription()->getConcept(); |
| 462 | + |
| 463 | + if ( !isset( $this->m_params[$titlekey] ) ) { |
| 464 | + $this->m_params[$titlekey] = $concept->getText(); |
| 465 | + } |
| 466 | + |
| 467 | + if ( !isset( $this->m_params[$desckey] ) ) { |
| 468 | + $dv = end( smwfGetStore()->getPropertyValues( SMWWikiPageValue::makePageFromTitle( $concept ), new SMWDIProperty( '_CONC' ) ) ); |
| 469 | + if ( $dv instanceof SMWConceptValue ) { |
| 470 | + $this->m_params[$desckey] = $dv->getDocu(); |
| 471 | + } |
| 472 | + } |
| 473 | + } |
| 474 | + // END: Try to be smart for rss/ical if no description/title is given and we have a concept query |
| 475 | + |
| 476 | + $printer = SMWQueryProcessor::getResultPrinter( $this->m_params['format'], SMWQueryProcessor::SPECIAL_PAGE ); |
| 477 | + $result_mime = $printer->getMimeType( $res ); |
| 478 | + |
| 479 | +// global $wgRequest; |
| 480 | +// |
| 481 | +// $hidequery = $wgRequest->getVal( 'eq' ) == 'no'; |
| 482 | +// |
| 483 | +// // if it's an export format (like CSV, JSON, etc.), |
| 484 | +// // don't actually export the data if 'eq' is set to |
| 485 | +// // either 'yes' or 'no' in the query string - just |
| 486 | +// // show the link instead |
| 487 | +// if ( $this->m_editquery || $hidequery ) $result_mime = false; |
| 488 | + |
| 489 | + //??/if ( $result_mime == false ) { |
| 490 | + if ( $res->getCount() > 0 ) { |
| 491 | +// if ( $this->m_editquery ) $urltail .= '&eq=yes'; |
| 492 | +// if ( $hidequery ) $urltail .= '&eq=no'; |
| 493 | + |
| 494 | + //$navigation = $this->getNavigationBar( $res, $urltail );//? |
| 495 | + //$result .= '<div style="text-align: center;">' . "\n" . $navigation . "\n</div>\n";//? |
| 496 | + $query_result = $printer->getResult( $res, $this->m_params, SMW_OUTPUT_HTML ); |
| 497 | + |
| 498 | + if ( is_array( $query_result ) ) { |
| 499 | + $result .= $query_result[0]; |
| 500 | + } else { |
| 501 | + $result .= $query_result; |
| 502 | + } |
| 503 | + |
| 504 | + //$result .= '<div style="text-align: center;">' . "\n" . $navigation . "\n</div>\n";//? |
| 505 | + } else { |
| 506 | + $result = wfMsg( 'smw_result_noresults' ); |
| 507 | + } |
| 508 | + return $result; |
| 509 | +// } else { // make a stand-alone file |
| 510 | +// $result = $printer->getResult( $res, $this->m_params, SMW_OUTPUT_FILE ); |
| 511 | +// $result_name = $printer->getFileName( $res ); // only fetch that after initialising the parameters |
| 512 | +// /////////////////////////////////////// |
| 513 | +// |
| 514 | +// //$result = $this->getInputForm( $printoutstring, 'offset=' . $this->m_params['offset'] . '&limit=' . $this->m_params['limit'] . $urltail ) . $result; |
| 515 | +// //$wgOut->addHTML( $result ); |
| 516 | +// global $wgOut; |
| 517 | +// $wgOut->disable(); |
| 518 | +// |
| 519 | +// header( "Content-type: $result_mime; charset=UTF-8" ); |
| 520 | +// |
| 521 | +// if ( $result_name !== false ) { |
| 522 | +// header( "content-disposition: attachment; filename=$result_name" ); |
| 523 | +// } |
| 524 | +// |
| 525 | +// echo $result; |
| 526 | +// } |
| 527 | + |
| 528 | + |
| 529 | + } |
| 530 | + |
| 531 | + //next come form-helper methods which may or may not be used by the UI designer |
| 532 | + |
| 533 | + public function extractParameters( $p ){ |
| 534 | + if($this->context==self::SPECIAL_PAGE){ |
| 535 | + //assume setParams(), setPintouts and setQueryString have been called |
| 536 | + $rawparams = array_merge($this->m_params, array($this->m_querystring), $this->m_printouts); |
| 537 | + } |
| 538 | + else //context is WIKI_LINK |
| 539 | + { |
| 540 | + $rawparams = SMWInfolink::decodeParameters( $p, true ); |
| 541 | + //calling setParams to fill in missing parameters |
| 542 | + $this->setParams($rawparams); |
| 543 | + $rawparams= array_merge($this->m_params, $rawparams); |
| 544 | + } |
| 545 | + |
| 546 | + //var_dump($this->m_params);var_dump($this->m_printouts);var_dump($this->m_querystring); |
| 547 | + //var_dump("after<br>"); |
| 548 | + SMWQueryProcessor::processFunctionParams( $rawparams, $this->m_querystring, $this->m_params, $this->m_printouts ); |
| 549 | + //var_dump($this->m_params);var_dump($this->m_printouts);var_dump($this->m_querystring); |
| 550 | + } |
| 551 | + /** |
| 552 | + * $m_querystring, $m_params, $m_printouts are set, returns the relevant #ask query |
| 553 | + */ |
| 554 | + public function makeAsk(){ |
| 555 | + $result = '{{#ask:' . htmlspecialchars( $this->m_querystring ) . "\n"; |
| 556 | + foreach ( $this->m_printouts as $printout ) { |
| 557 | + $result .= '|' . $printout->getSerialisation() . "\n"; |
| 558 | + } |
| 559 | + foreach ( $this->m_params as $param_name => $param_value ) { |
| 560 | + $result .= '|' . htmlspecialchars( $param_name ) . '=' . htmlspecialchars( $param_value ) . "\n"; |
| 561 | + } |
| 562 | + $result .= '}}'; |
| 563 | + return $result; |
| 564 | + } |
| 565 | + |
| 566 | + public function getQueryString(){ |
| 567 | + return $this->m_querystring; |
| 568 | + } |
| 569 | + |
| 570 | + public function getParams(){ |
| 571 | + return $this->m_params; |
| 572 | + } |
| 573 | + /** |
| 574 | + * |
| 575 | + * @return array of SMWPrintRequest |
| 576 | + */ |
| 577 | + public function getPrintOuts(){ |
| 578 | + if(!empty($this->printouts)){ |
| 579 | + if(is_a($this->printouts[0],'SMWPrintRequest')){ |
| 580 | + return $this->m_printouts; |
| 581 | + } |
| 582 | + } |
| 583 | + return array(); |
| 584 | + } |
| 585 | + /** |
| 586 | + * Constructs a new SMWQueryUIHelper when parameters are passed in the InfoLink style |
| 587 | + * |
| 588 | + * Errors, if any can be accessed from hasError() and getErrors() |
| 589 | + * |
| 590 | + * @param string $p parametrs |
| 591 | + * @param boolean $enable_validation |
| 592 | + * @return SMWQueryUIHelper |
| 593 | + */ |
| 594 | + public static function makeFromInfoLink($p, $enable_validation = true){ |
| 595 | + $result = new SMWQueryUIHelper($enable_validation, self::WIKI_LINK); |
| 596 | + $result->extractParameters($p); |
| 597 | + return $result; |
| 598 | + } |
| 599 | + /** |
| 600 | + * Constructs a new SMWQueryUIHelper when arguments are extracted from the UI |
| 601 | + * |
| 602 | + * Errors, if any can be accessed from hasError() and getErrors() |
| 603 | + * |
| 604 | + * @param string $query |
| 605 | + * @param array $params of key=>value pairs |
| 606 | + * @param array $printouts array of '?property' strings |
| 607 | + * @param boolean $enable_validation |
| 608 | + * @return SMWQueryUIHelper |
| 609 | + */ |
| 610 | + public static function makeFromUI($query, array $params, array $printouts, $enable_validation = true){ |
| 611 | + $result = new SMWQueryUIHelper($enable_validation, self::SPECIAL_PAGE); |
| 612 | + $result->setParams($params); |
| 613 | + $result->setPrintOuts($printouts); |
| 614 | + $result->setQueryString($query); |
| 615 | + $result->extractParameters(""); |
| 616 | + return $result; |
| 617 | + } |
| 618 | + /** |
| 619 | + * Checks if $property exists in the wiki or not |
| 620 | + * @return bool |
| 621 | + */ |
| 622 | + protected static function validateProperty($property){ |
| 623 | + /* |
| 624 | + * Curently there isn't a simple, back-end agnost way of searching for properties from |
| 625 | + * SMWStore. We hence we check if $property has a corresponding page describing it. |
| 626 | + */ |
| 627 | + $prop= substr ($property, 1);//removing the leading '?' while checking. |
| 628 | + $propertypage = Title::newFromText( $prop, SMW_NS_PROPERTY ); |
| 629 | + if(is_a($propertypage, 'Title')){ |
| 630 | + return($propertypage->exists()); |
| 631 | + } else { |
| 632 | + return false; |
| 633 | + } |
| 634 | + } |
| 635 | + |
| 636 | + /** |
| 637 | + * Returns the result printer which should be used if not specified by the user. |
| 638 | + * Overload if necessary. |
| 639 | + * |
| 640 | + * @return string |
| 641 | + */ |
| 642 | + protected static function getDefaultResultPrinter(){ |
| 643 | + return 'broadtable'; |
| 644 | + } |
| 645 | + |
| 646 | + |
| 647 | +} |
Property changes on: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 648 | + native |