Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
— | — | @@ -13,6 +13,9 @@ |
14 | 14 | * @author Sanyam Goyal |
15 | 15 | * @author Jeroen De Dauw |
16 | 16 | * @author Devayon Das |
| 17 | + * |
| 18 | + * @ingroup SMWSpecialPage |
| 19 | + * @ingroup SpecialPage |
17 | 20 | */ |
18 | 21 | abstract class SMWQueryUI extends SpecialPage { |
19 | 22 | /** |
— | — | @@ -33,15 +36,18 @@ |
34 | 37 | const DISABLE_AUTO_SUGGEST = false; |
35 | 38 | |
36 | 39 | /** |
37 | | - * Initialises the page. Sets the property $uiCore to the appropriate helper object. |
| 40 | + * Initialises the page. Sets the property $uiCore to the appropriate helper |
| 41 | + * object. |
38 | 42 | * |
39 | | - * To create a custom UI, adding changes to makePage() is usually enough, but one might want to |
40 | | - * overload this method to get better handling of form parameters. |
| 43 | + * To create a custom UI, adding changes to makePage() is usually enough, |
| 44 | + * but one might want to overload this method to get better handling of form |
| 45 | + * parameters. |
41 | 46 | * |
42 | 47 | * @global OutputPage $wgOut |
43 | 48 | * @global WebRequest $wgRequest |
44 | 49 | * @global boolean $smwgQEnabled |
45 | 50 | * @param string $p the sub-page string |
| 51 | + * @todo: using processXXXBox() methods here goes against the general architecture. |
46 | 52 | */ |
47 | 53 | public function execute( $p ) { |
48 | 54 | global $wgOut, $wgRequest, $smwgQEnabled; |
— | — | @@ -61,8 +67,9 @@ |
62 | 68 | array( |
63 | 69 | 'format' => $wgRequest->getVal( 'format' ), |
64 | 70 | 'offset' => $wgRequest->getVal( 'offset', '0' ), |
65 | | - 'limit' => $wgRequest->getVal( 'limit', '20' ) |
66 | | - ), $this->processFormatSelectBox( $wgRequest ) ); |
| 71 | + 'limit' => $wgRequest->getVal( 'limit', '20' ) ), |
| 72 | + $this->processSortingFormBox( $wgRequest ), |
| 73 | + $this->processFormatSelectBox( $wgRequest ) ); |
67 | 74 | $this->uiCore = SMWQueryUIHelper::makeForUI( |
68 | 75 | $this->processQueryFormBox( $wgRequest ), |
69 | 76 | $params, |
— | — | @@ -71,8 +78,7 @@ |
72 | 79 | if ( $this->uiCore->getQueryString() != "" ) { |
73 | 80 | $this->uiCore->execute( $p ); |
74 | 81 | } |
75 | | - } |
76 | | - else { |
| 82 | + } else { |
77 | 83 | // the user has entered this page from a wiki-page using an infolink, |
78 | 84 | // or no query has been set |
79 | 85 | $this->uiCore = SMWQueryUIHelper::makeForInfoLink( $p ); |
— | — | @@ -85,13 +91,14 @@ |
86 | 92 | } |
87 | 93 | |
88 | 94 | /** |
89 | | - * The main entrypoint for your UI. Call the various methods of SMWQueryUI and |
90 | | - * SMWQueryUIHelper to build ui elements and to process them. |
| 95 | + * The main entrypoint for your UI. Call the various methods of SMWQueryUI |
| 96 | + * and SMWQueryUIHelper to build ui elements and to process them. |
91 | 97 | */ |
92 | 98 | protected abstract function makePage( $p ); |
93 | 99 | |
94 | 100 | /** |
95 | | - * Builds a read-only #ask embed code of the given query. |
| 101 | + * Builds a read-only #ask embed code of the given query. The code is |
| 102 | + * presented in html code. |
96 | 103 | * |
97 | 104 | * @return string |
98 | 105 | */ |
— | — | @@ -112,7 +119,7 @@ |
113 | 120 | * @global OutputPage $wgOut |
114 | 121 | * @global boolean $smwgJQueryIncluded |
115 | 122 | */ |
116 | | - private function enableJQuery(){ |
| 123 | + private function enableJQuery() { |
117 | 124 | global $wgOut, $smwgJQueryIncluded; |
118 | 125 | if ( !$smwgJQueryIncluded ) { |
119 | 126 | $realFunction = array( 'OutputPage', 'includeJQuery' ); |
— | — | @@ -206,8 +213,8 @@ |
207 | 214 | /** |
208 | 215 | * Build the navigation bar for some given query result. |
209 | 216 | * |
210 | | - * UI may overload this for a different layout. The navigation bar |
211 | | - * can be hidden by overloading usesNavigationBar(). To change the url format, |
| 217 | + * UI may overload this for a different layout. The navigation bar can |
| 218 | + * be hidden by overloading usesNavigationBar(). To change the url format, |
212 | 219 | * one may overload getUrlTail(); |
213 | 220 | * |
214 | 221 | * @global int $smwgQMaxInlineLimit |
— | — | @@ -304,8 +311,8 @@ |
305 | 312 | * @param string $errors |
306 | 313 | * @return string |
307 | 314 | */ |
308 | | - protected function getQueryFormBox( $content, $errors = "" ) { |
309 | | - $result = ""; |
| 315 | + protected function getQueryFormBox( $content, $errors = '' ) { |
| 316 | + $result = ''; |
310 | 317 | $result = Html::element( 'textarea', array( 'name' => 'q', 'id' => 'querybox', 'rows' => '6' ), $content ); |
311 | 318 | // TODO:enable/disable on checking for errors; perhaps show error messages right below the box |
312 | 319 | return $result; |
— | — | @@ -313,13 +320,14 @@ |
314 | 321 | |
315 | 322 | /** |
316 | 323 | * Decodes form data sent through form-elements generated by |
317 | | - * its complement, getQueryFormBox. UIs may overload both to change form parameters. |
| 324 | + * its complement, getQueryFormBox. UIs may overload both to change form |
| 325 | + * parameters. |
318 | 326 | * |
319 | 327 | * @param WebRequest $wgRequest |
320 | 328 | * @return string |
321 | 329 | */ |
322 | 330 | protected function processQueryFormBox( WebRequest $wgRequest ) { |
323 | | - $query = ""; |
| 331 | + $query = ''; |
324 | 332 | if ( $wgRequest->getCheck( 'q' ) ) $query = $wgRequest->getVal( 'q' ); |
325 | 333 | return $query; |
326 | 334 | } |
— | — | @@ -331,17 +339,19 @@ |
332 | 340 | * |
333 | 341 | * @return string |
334 | 342 | */ |
335 | | - protected function addSortingFormBox() { |
336 | | - global $smwgQSortingSupport, $wgRequest, $wgOut, $smwgJQueryIncluded; |
| 343 | + protected function getSortingFormBox() { |
| 344 | + global $smwgQSortingSupport, $wgRequest, $wgOut; |
| 345 | + |
337 | 346 | if ( !$smwgQSortingSupport ) return ''; |
| 347 | + $params = $this->uiCore->getParameters(); |
338 | 348 | |
339 | 349 | $result = ''; |
340 | | - if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) { |
| 350 | + if ( array_key_exists( 'sort', $params ) && array_key_exists( 'order', $params ) ) { |
| 351 | + $sorts = explode( ',', $params['sort'] ); |
| 352 | + $orders = explode( ',', $params['order'] ); |
| 353 | + reset( $sorts ); |
| 354 | + } else { |
341 | 355 | $orders = array(); // do not even show one sort input here |
342 | | - } else { |
343 | | - $sorts = explode( ',', $this->m_params['sort'] ); |
344 | | - $orders = explode( ',', $this->m_params['order'] ); |
345 | | - reset( $sorts ); |
346 | 356 | } |
347 | 357 | |
348 | 358 | foreach ( $orders as $i => $order ) { |
— | — | @@ -356,7 +366,7 @@ |
357 | 367 | $result .= "</div>\n"; |
358 | 368 | } |
359 | 369 | |
360 | | - $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n"; |
| 370 | + $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" size="35" />' . "\n"; |
361 | 371 | $result .= ' <select name="order_num">' . "\n"; |
362 | 372 | $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n"; |
363 | 373 | $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n"; |
— | — | @@ -364,23 +374,23 @@ |
365 | 375 | $result .= '<div id="sorting_main"></div>' . "\n"; |
366 | 376 | $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n"; |
367 | 377 | |
368 | | - $this->m_num_sort_values = 0; |
| 378 | + $num_sort_values = 0; |
369 | 379 | |
370 | | - if ( !array_key_exists( 'sort', $this->m_params ) ) { |
| 380 | + if ( !array_key_exists( 'sort', $params ) ) { |
371 | 381 | $sort_values = $wgRequest->getArray( 'sort' ); |
372 | 382 | if ( is_array( $sort_values ) ) { |
373 | | - $this->m_params['sort'] = implode( ',', $sort_values ); |
374 | | - $this->m_num_sort_values = count( $sort_values ); |
| 383 | + $params['sort'] = implode( ',', $sort_values ); |
| 384 | + $num_sort_values = count( $sort_values ); |
375 | 385 | } |
376 | 386 | } |
377 | 387 | // Javascript code for handling adding and removing the "sort" inputs |
378 | 388 | $delete_msg = wfMsg( 'delete' ); |
379 | 389 | |
380 | | - |
| 390 | + $this->enableJQuery(); |
381 | 391 | $javascript_text = <<<EOT |
382 | 392 | <script type="text/javascript"> |
383 | 393 | // code for handling adding and removing the "sort" inputs |
384 | | -var num_elements = {$this->m_num_sort_values}; |
| 394 | +var num_elements = {$num_sort_values}; |
385 | 395 | |
386 | 396 | function addInstance(starter_div_id, main_div_id) { |
387 | 397 | var starter_div = document.getElementById(starter_div_id); |
— | — | @@ -420,43 +430,51 @@ |
421 | 431 | EOT; |
422 | 432 | |
423 | 433 | $wgOut->addScript( $javascript_text ); |
424 | | - |
425 | | - if ( !$smwgJQueryIncluded ) { |
426 | | - $realFunction = array( 'OutputPage', 'includeJQuery' ); |
427 | | - if ( is_callable( $realFunction ) ) { |
428 | | - $wgOut->includeJQuery(); |
429 | | - } else { |
430 | | - $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js"; |
431 | | - } |
432 | | - |
433 | | - $smwgJQueryIncluded = true; |
434 | | - } |
435 | | - |
436 | 434 | return $result; |
437 | 435 | } |
438 | 436 | |
| 437 | + /** |
| 438 | + * Decodes form Sorting options sent through form-elements generated by |
| 439 | + * its complement, getSortingFormBox(). UIs may overload both to change form |
| 440 | + * parameters. |
| 441 | + * |
| 442 | + * @global boolean $smwgQSortingSupport |
| 443 | + * @param WebRequest $wgRequest |
| 444 | + * @return string |
| 445 | + * @todo build in validation for sorting |
| 446 | + */ |
439 | 447 | protected function processSortingFormBox( WebRequest $wgRequest ) { |
| 448 | + global $smwgQSortingSupport; |
| 449 | + if ( !$smwgQSortingSupport ) return array(); |
| 450 | + |
| 451 | + $params = array(); |
440 | 452 | $order_values = $wgRequest->getArray( 'order' ); |
441 | | - |
442 | 453 | if ( is_array( $order_values ) ) { |
443 | 454 | $params['order'] = ''; |
444 | | - |
445 | 455 | foreach ( $order_values as $order_value ) { |
446 | 456 | if ( $order_value == '' ) { |
447 | 457 | $order_value = 'ASC'; |
448 | 458 | } |
449 | 459 | $params['order'] .= ( $params['order'] != '' ? ',' : '' ) . $order_value; |
450 | 460 | } |
451 | | - return $params; |
452 | | - } else { |
453 | | - return array(); |
454 | 461 | } |
| 462 | + |
| 463 | + $sort_values = $wgRequest->getArray( 'sort' ); |
| 464 | + if ( is_array( $sort_values ) ) { |
| 465 | + $params['sort'] = ''; |
| 466 | + foreach ( $sort_values as $sort_value ) { |
| 467 | + $params['sort'] .= ( $params['sort'] != '' ? ',' : '' ) . $sort_value; |
| 468 | + } |
| 469 | + } |
| 470 | + return $params; |
| 471 | + |
455 | 472 | } |
456 | 473 | |
457 | 474 | /** |
458 | 475 | * Generates the form element(s) for PrintOuts. |
459 | 476 | * Use its complement processPOFormBox() to decode data sent through these |
460 | | - * form elements. UIs may overload both to change the form parameter or the html elements. |
| 477 | + * form elements. UIs may overload both to change the form parameter or the |
| 478 | + * html elements. |
461 | 479 | * |
462 | 480 | * @param string $content The content expected to appear in the box |
463 | 481 | * @param boolean $enableAutocomplete If set to true, adds the relevant JS and CSS to the page |
— | — | @@ -1236,7 +1254,7 @@ |
1237 | 1255 | } |
1238 | 1256 | |
1239 | 1257 | if ( !isset( $this->parameters[$desckey] ) ) { |
1240 | | - /// @bug The current SMWStore will never return SMWConceptValue (an SMWDataValue) here; it might return SMWDIConcept (an SMWDataItem) |
| 1258 | + // / @bug The current SMWStore will never return SMWConceptValue (an SMWDataValue) here; it might return SMWDIConcept (an SMWDataItem) |
1241 | 1259 | $dv = end( smwfGetStore()->getPropertyValues( SMWWikiPageValue::makePageFromTitle( $concept ), new SMWDIProperty( '_CONC' ) ) ); |
1242 | 1260 | if ( $dv instanceof SMWConceptValue ) { |
1243 | 1261 | $this->parameters[$desckey] = $dv->getDocu(); |
— | — | @@ -1345,7 +1363,7 @@ |
1346 | 1364 | * handling the "further results" links in wiki-pages that use #ask. If |
1347 | 1365 | * your search UI handles form parameters only, then consider using |
1348 | 1366 | * makeForUI(). |
1349 | | - * |
| 1367 | + * |
1350 | 1368 | * If any errors do occur while parsing parameters, they may be accessed |
1351 | 1369 | * from hasError() and getErrors(). |
1352 | 1370 | * |