Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
— | — | @@ -8,15 +8,170 @@ |
9 | 9 | * @author Yaron Koren |
10 | 10 | * @author Sanyam Goyal |
11 | 11 | * @author Jeroen De Dauw |
| 12 | + * @author Devayon Das |
12 | 13 | */ |
13 | 14 | abstract class SMWQueryUI extends SpecialPage { |
14 | 15 | protected $m_ui_helper; |
15 | | - protected abstract function execute( $p ) { |
| 16 | + |
| 17 | + protected function makeResults($p){ |
16 | 18 | /* |
17 | | - * Extract the parameters from the UI. Use either the constructor or |
18 | | - * $this->m_ui_helper=new SMWUIHelper() |
| 19 | + * TODO: extract parameters from $p and decide: |
| 20 | + * (1) if form elements need to be displayed |
| 21 | + * (2) if any results need to be displayed |
| 22 | + * (3) which factory method of UIhelper should be called |
| 23 | + * Most of the code here in this method will anyway be removed later |
19 | 24 | */ |
| 25 | + global $wgOut, $wgRequest; |
| 26 | + $htmloutput=""; |
| 27 | + $htmloutput.= $this->getForm(); |
| 28 | + $param=array(); |
| 29 | + |
| 30 | + $this->m_ui_helper = $helper = new SMWQueryUIHelper; //or some factory method |
| 31 | + //here come some driver lines for testing; this is very temporary |
| 32 | + |
| 33 | + // form parameters default values |
| 34 | + $helper->setQueryString( |
| 35 | + $wgRequest->getVal('q', '[[Located in:: Germany]]')); |
| 36 | + $helper->setParams(array( |
| 37 | + 'format' => $wgRequest->getVal('format', 'ol' ), |
| 38 | + 'offset' => $wgRequest->getVal('offset', '0' ), |
| 39 | + 'limit' => $wgRequest->getVal('limit', '20' ) |
| 40 | + )); |
| 41 | + $helper->setPrintOuts(array('?Population')); |
| 42 | + $helper->extractParameters($p); |
| 43 | + |
| 44 | + $helper->execute(); |
| 45 | + |
| 46 | + if($this->usesNavigationBar()){ |
| 47 | + $htmloutput.= $this->getNavigationBar ($helper->getLimit(),$helper->getOffset(),$helper->hasFurtherResults()); //? can we preload offset and limit? |
| 48 | + } |
| 49 | + |
| 50 | + $htmloutput.= $helper->getHTMLResult(); |
| 51 | + |
| 52 | + if($this->usesNavigationBar()){ |
| 53 | + $htmloutput.= $this->getNavigationBar ($helper->getLimit(),$helper->getOffset(),$helper->hasFurtherResults()); //? can we preload offset and limit? |
| 54 | + } |
| 55 | + $wgOut->addHTML($htmloutput); |
20 | 56 | } |
| 57 | + |
| 58 | + /** |
| 59 | + * Build the navigation bar for some given query result. |
| 60 | + * |
| 61 | + * UI may overload this for a different layout. The navigation bar |
| 62 | + * can be hidden by overloading usesNavigationBar(). To change the url format, |
| 63 | + * one may overload getUrlTail(); |
| 64 | + * |
| 65 | + * @global int $smwgQMaxInlineLimit |
| 66 | + * @param int $limit |
| 67 | + * @param int $offset |
| 68 | + * @param boolean $has_further_results |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + public function getNavigationBar($limit, $offset, $has_further_results) { |
| 73 | + global $smwgQMaxInlineLimit; |
| 74 | + $urltail = $this->getUrlTail(); |
| 75 | + // Prepare navigation bar. |
| 76 | + if ( $offset > 0 ) { |
| 77 | + $navigation = Html::element( |
| 78 | + 'a', |
| 79 | + array( |
| 80 | + 'href' => $this->getTitle()->getLocalURL( |
| 81 | + 'offset=' . max( 0, $offset - $limit ) . |
| 82 | + '&limit=' . $limit . $urltail |
| 83 | + ), |
| 84 | + 'rel' => 'nofollow' |
| 85 | + ), |
| 86 | + wfMsg( 'smw_result_prev' ) |
| 87 | + ); |
| 88 | + |
| 89 | + } else { |
| 90 | + $navigation = wfMsg( 'smw_result_prev' ); |
| 91 | + } |
| 92 | + |
| 93 | + $navigation .= |
| 94 | + '     <b>' . |
| 95 | + wfMsg( 'smw_result_results' ) . ' ' . ( $offset + 1 ) . |
| 96 | + '– ' . |
| 97 | + ( $offset + $this->m_ui_helper->getResultCount() ) . |
| 98 | + '</b>    '; |
| 99 | + |
| 100 | + if ( $has_further_results ) { |
| 101 | + $navigation .= Html::element( |
| 102 | + 'a', |
| 103 | + array( |
| 104 | + 'href' => $this->getTitle()->getLocalURL( |
| 105 | + 'offset=' . ( $offset + $limit ) . |
| 106 | + '&limit=' . $limit . $urltail |
| 107 | + ), |
| 108 | + 'rel' => 'nofollow' |
| 109 | + ), |
| 110 | + wfMsg( 'smw_result_next' ) |
| 111 | + ); |
| 112 | + } else { |
| 113 | + $navigation .= wfMsg( 'smw_result_next' ); |
| 114 | + } |
| 115 | + |
| 116 | + $first = true; |
| 117 | + |
| 118 | + foreach ( array( 20, 50, 100, 250, 500 ) as $l ) { |
| 119 | + if ( $l > $smwgQMaxInlineLimit ) break; |
| 120 | + |
| 121 | + if ( $first ) { |
| 122 | + $navigation .= '        ('; |
| 123 | + $first = false; |
| 124 | + } else { |
| 125 | + $navigation .= ' | '; |
| 126 | + } |
| 127 | + |
| 128 | + if ( $limit != $l ) { |
| 129 | + $navigation .= Html::element( |
| 130 | + 'a', |
| 131 | + array( |
| 132 | + 'href' => $this->getTitle()->getLocalURL( |
| 133 | + 'offset=' . $offset . |
| 134 | + '&limit=' . $l . $urltail |
| 135 | + ), |
| 136 | + 'rel' => 'nofollow' |
| 137 | + ), |
| 138 | + $l |
| 139 | + ); |
| 140 | + } else { |
| 141 | + $navigation .= '<b>' . $l . '</b>'; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + $navigation .= ')'; |
| 146 | + |
| 147 | + return $navigation; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Creates the form elements and populates them with parameters. |
| 152 | + * UI implementations need to overload this if a different layout and form |
| 153 | + * elements are desired |
| 154 | + * |
| 155 | + * @return string Form elements in HTML |
| 156 | + */ |
| 157 | + protected function getForm(){ |
| 158 | + /* |
| 159 | + * Although the following methods will retuen form elements, which can |
| 160 | + * then be placed in wOut as pleased, they will |
| 161 | + * also write javascript (if relevant) directly to wgOut. |
| 162 | + */ |
| 163 | + |
| 164 | + //$result=""; |
| 165 | + //$result.= getQueryFormBox($content); |
| 166 | + //$result.= getPOFormBox($content, $enableAutoComplete); |
| 167 | + //$result.= getParamBox($content); //avoid ajax, load form elements in the UI by default |
| 168 | + $result="<br>Stub: The Form elements come here<br><br>"; |
| 169 | + return $result; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * A method which generates the url parameters based on passed parameters. |
| 174 | + * UI implementations need to overload this if they use different form parameters |
| 175 | + */ |
21 | 176 | protected function getUrlTail() { |
22 | 177 | $urltail = '&q=' . urlencode( $this->m_ui_helper->getQuerystring() ); |
23 | 178 | $tmp_parray = array(); |
— | — | @@ -36,12 +191,10 @@ |
37 | 192 | if ( $printoutstring != '' ) $urltail .= '&po=' . urlencode( $printoutstring ); |
38 | 193 | if ( array_key_exists( 'sort', $params ) ) $urltail .= '&sort=' . $params['sort']; |
39 | 194 | if ( array_key_exists( 'order', $params ) ) $urltail .= '&order=' . $params['order']; |
| 195 | + return $urltail; |
40 | 196 | } |
41 | 197 | protected function makeHtmlResult() { |
42 | | - global $wgOut; |
43 | | - if ( is_a( $this->m_ui_helper, 'SMWQueryUIHelper' ) ) { |
44 | | - |
45 | | - } |
| 198 | + //STUB |
46 | 199 | } |
47 | 200 | /** |
48 | 201 | * Display a form section showing the options for a given format, |
— | — | @@ -175,91 +328,6 @@ |
176 | 329 | return true; |
177 | 330 | } |
178 | 331 | |
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 | 332 | } |
265 | 333 | |
266 | 334 | /** |
— | — | @@ -270,7 +338,6 @@ |
271 | 339 | * |
272 | 340 | * @author Devayon Das |
273 | 341 | * |
274 | | - * @property boolean $enable_validation If set to TRUE causes each of the parametes to be checked for errors. |
275 | 342 | */ |
276 | 343 | class SMWQueryUIHelper { |
277 | 344 | |
— | — | @@ -279,17 +346,17 @@ |
280 | 347 | protected $m_params = array(); // Parameters controlling how the results should be displayed |
281 | 348 | protected $m_printouts = array(); // Properties to be printed along with results |
282 | 349 | protected static $m_UIPages = array(); // A list of Query UIs |
283 | | - public $enable_validation; |
284 | 350 | private $fatal_errors = false; |
285 | 351 | private $context; |
286 | 352 | private $errors = array(); |
| 353 | + private $queryresult = null; |
| 354 | + |
287 | 355 | const SPECIAL_PAGE = 0;// parameters passed from special page |
288 | 356 | const WIKI_LINK = 1;// parameters passed from 'further links' in the wiki. |
289 | 357 | |
290 | 358 | |
291 | 359 | // constructor |
292 | | - public function __construct( $enable_validation = true, $context = self::SPECIAL_PAGE ) { |
293 | | - $this -> enable_validation = $enable_validation; |
| 360 | + public function __construct($context = self::SPECIAL_PAGE ) { |
294 | 361 | $this->context = $context; |
295 | 362 | } |
296 | 363 | |
— | — | @@ -297,6 +364,36 @@ |
298 | 365 | return $this->fatal_errors; |
299 | 366 | } |
300 | 367 | |
| 368 | + public function getLimit(){ |
| 369 | + if(key_exists('limit', $this->m_params)){ |
| 370 | + return $this->m_params['limit']; |
| 371 | + } |
| 372 | + else { |
| 373 | + return 0; |
| 374 | + } |
| 375 | + } |
| 376 | + |
| 377 | + public function getOffset(){ |
| 378 | + if(key_exists('offset', $this->m_params)){ |
| 379 | + return $this->m_params['offset']; |
| 380 | + } |
| 381 | + else{ |
| 382 | + return 20; |
| 383 | + } |
| 384 | + } |
| 385 | + public function hasFurtherResults(){ |
| 386 | + if(is_a($this->queryresult,'SMWQueryResult')){ |
| 387 | + return $this->queryresult->hasFurtherResults(); |
| 388 | + } |
| 389 | + else{ |
| 390 | + return false; |
| 391 | + } |
| 392 | + } |
| 393 | + |
| 394 | + public function getResultObject(){ |
| 395 | + return $this->getResultObject(); |
| 396 | + } |
| 397 | + |
301 | 398 | /** |
302 | 399 | * |
303 | 400 | * Returns an array of errors, if any have occured. |
— | — | @@ -332,10 +429,10 @@ |
333 | 430 | * @param string $querystring The query |
334 | 431 | * @return array array of errors, if any. |
335 | 432 | */ |
336 | | - public function setQueryString( $querystring = "" ) { |
| 433 | + public function setQueryString( $querystring = "", $enable_validation=true ) { |
337 | 434 | $this -> m_querystring = $querystring; |
338 | 435 | $errors = array(); |
339 | | - if ( $this->enable_validation ) { |
| 436 | + if ( $enable_validation ) { |
340 | 437 | if ( $querystring == '' ) { |
341 | 438 | $errors[] = "No query has been specified"; // TODO i18n |
342 | 439 | } |
— | — | @@ -361,9 +458,9 @@ |
362 | 459 | * @param array $printouts Array of additional properties to be shown in results |
363 | 460 | * @return array array of errors, if any. |
364 | 461 | */ |
365 | | - public function setPrintOuts( array $printouts = array() ) { |
| 462 | + public function setPrintOuts( array $printouts = array(), $enable_validation=true ) { |
366 | 463 | $errors = array(); |
367 | | - if ( $this -> enable_validation ) { |
| 464 | + if ( $enable_validation ) { |
368 | 465 | foreach ( $printouts as $key => $prop ) { |
369 | 466 | if ( $prop[0] != '?' ) { |
370 | 467 | $printouts[$key] = "?" . $printouts[$key]; |
— | — | @@ -379,7 +476,7 @@ |
380 | 477 | return $errors; |
381 | 478 | } |
382 | 479 | |
383 | | - public function setParams( array $params = array() ) { |
| 480 | + public function setParams( array $params = array(), $enable_validation=true ) { |
384 | 481 | /* |
385 | 482 | *Validate, and add missing params. * |
386 | 483 | */ |
— | — | @@ -397,7 +494,7 @@ |
398 | 495 | if ( !array_key_exists( 'offset', $params ) ) |
399 | 496 | $params['offset'] = 0; |
400 | 497 | |
401 | | - if ( $this->enable_validation ) { |
| 498 | + if ( $enable_validation ) { |
402 | 499 | // validating the format |
403 | 500 | if ( !array_key_exists( $params['format'], $smwgResultFormats ) ) { |
404 | 501 | $errors[] = "The chosen format " + $params['format'] + " does not exist for this wiki"; // TODO i18n |
— | — | @@ -421,21 +518,21 @@ |
422 | 519 | } |
423 | 520 | |
424 | 521 | $this -> m_params = $params; |
425 | | - $this->errors = array_merge( $errors, $this->errors ); |
| 522 | + $this -> errors = array_merge( $errors, $this->errors ); |
426 | 523 | return $errors; |
427 | 524 | } |
428 | 525 | |
429 | | - public function makeHTMLResult() { |
| 526 | + public function execute() { |
430 | 527 | /* |
431 | 528 | * Once $m_querystring, $m_params, $m_printouts are set, generates the |
432 | 529 | * results / or link. The pagination links (or navigation bar) are expected |
433 | 530 | * to be created by the UI designer. (or maybe we can put a method here to |
434 | 531 | * make the nav-bar which also calls makeHTMLResult(). |
435 | 532 | */ |
436 | | - $result = ''; |
437 | 533 | $errors = array(); |
438 | 534 | $query = SMWQueryProcessor::createQuery( $this->m_querystring, $this->m_params, SMWQueryProcessor::SPECIAL_PAGE , $this->m_params['format'], $this->m_printouts ); |
439 | 535 | $res = smwfGetStore()->getQueryResult( $query ); |
| 536 | + $this->queryresult=$res; |
440 | 537 | $errors = array_merge( $errors, $res->getErrors() ); |
441 | 538 | if ( !empty( $errors ) ) { |
442 | 539 | $this->fatal_errors = true; |
— | — | @@ -469,7 +566,11 @@ |
470 | 567 | } |
471 | 568 | } |
472 | 569 | // END: Try to be smart for rss/ical if no description/title is given and we have a concept query |
473 | | - |
| 570 | + } |
| 571 | + |
| 572 | + public function getHTMLResult(){ |
| 573 | + $result = ''; |
| 574 | + $res= $this->queryresult; |
474 | 575 | $printer = SMWQueryProcessor::getResultPrinter( $this->m_params['format'], SMWQueryProcessor::SPECIAL_PAGE ); |
475 | 576 | $result_mime = $printer->getMimeType( $res ); |
476 | 577 | |
— | — | @@ -526,6 +627,13 @@ |
527 | 628 | return $this->m_querystring; |
528 | 629 | } |
529 | 630 | |
| 631 | + public function getResultCount(){ |
| 632 | + if(is_a($this->queryresult, 'SMWQueryResult')){ |
| 633 | + return $this->queryresult->getCount(); |
| 634 | + } |
| 635 | + else return 0; |
| 636 | + |
| 637 | + } |
530 | 638 | public function getParams() { |
531 | 639 | return $this->m_params; |
532 | 640 | } |
— | — | @@ -551,8 +659,10 @@ |
552 | 660 | * @return SMWQueryUIHelper |
553 | 661 | */ |
554 | 662 | public static function makeFromInfoLink( $p, $enable_validation = true ) { |
555 | | - $result = new SMWQueryUIHelper( $enable_validation, self::WIKI_LINK ); |
| 663 | + //TODO handle validation for infolink parameters |
| 664 | + $result = new SMWQueryUIHelper(self::WIKI_LINK ); |
556 | 665 | $result->extractParameters( $p ); |
| 666 | + $result->execute(); |
557 | 667 | return $result; |
558 | 668 | } |
559 | 669 | /** |
— | — | @@ -567,11 +677,12 @@ |
568 | 678 | * @return SMWQueryUIHelper |
569 | 679 | */ |
570 | 680 | public static function makeFromUI( $query, array $params, array $printouts, $enable_validation = true ) { |
571 | | - $result = new SMWQueryUIHelper( $enable_validation, self::SPECIAL_PAGE ); |
572 | | - $result->setParams( $params ); |
573 | | - $result->setPrintOuts( $printouts ); |
574 | | - $result->setQueryString( $query ); |
| 681 | + $result = new SMWQueryUIHelper(self::SPECIAL_PAGE ); |
| 682 | + $result->setParams( $params, $enable_validation); |
| 683 | + $result->setPrintOuts( $printouts, $enable_validation ); |
| 684 | + $result->setQueryString( $query, $enable_validation ); |
575 | 685 | $result->extractParameters( "" ); |
| 686 | + $result->execute(); |
576 | 687 | return $result; |
577 | 688 | } |
578 | 689 | /** |