Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
— | — | @@ -107,6 +107,26 @@ |
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
| 111 | + * A helper function to enable JQuery |
| 112 | + * |
| 113 | + * @global OutputPage $wgOut |
| 114 | + * @global boolean $smwgJQueryIncluded |
| 115 | + */ |
| 116 | + private function enableJQuery(){ |
| 117 | + global $wgOut, $smwgJQueryIncluded; |
| 118 | + if ( !$smwgJQueryIncluded ) { |
| 119 | + $realFunction = array( 'OutputPage', 'includeJQuery' ); |
| 120 | + if ( is_callable( $realFunction ) ) { |
| 121 | + $wgOut->includeJQuery(); |
| 122 | + } else { |
| 123 | + $wgOut->addScriptFile( "$smwgScriptPath/libs/jquery-1.4.2.min.js" ); |
| 124 | + } |
| 125 | + |
| 126 | + $smwgJQueryIncluded = true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
111 | 131 | * Adds common JS and CSS required for Autocompletion. |
112 | 132 | * @global OutputPage $wgOut |
113 | 133 | * @global string $smwgScriptPath |
— | — | @@ -118,19 +138,10 @@ |
119 | 139 | if ( $this->autocompleteenabled == false ) { |
120 | 140 | $wgOut->addExtensionStyle( "$smwgScriptPath/skins/jquery-ui/base/jquery.ui.all.css" ); |
121 | 141 | |
| 142 | + $this-> enableJQuery(); |
| 143 | + |
122 | 144 | $scripts = array(); |
123 | 145 | |
124 | | - if ( !$smwgJQueryIncluded ) { |
125 | | - $realFunction = array( 'OutputPage', 'includeJQuery' ); |
126 | | - if ( is_callable( $realFunction ) ) { |
127 | | - $wgOut->includeJQuery(); |
128 | | - } else { |
129 | | - $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js"; |
130 | | - } |
131 | | - |
132 | | - $smwgJQueryIncluded = true; |
133 | | - } |
134 | | - |
135 | 146 | if ( !$smwgJQueryUIIncluded ) { |
136 | 147 | $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.core.min.js"; |
137 | 148 | $scripts[] = "$smwgScriptPath/libs/jquery-ui/jquery.ui.widget.min.js"; |
— | — | @@ -538,7 +549,7 @@ |
539 | 550 | $urltail = '&q=' . urlencode( $this->uiCore->getQuerystring() ); |
540 | 551 | |
541 | 552 | $tmp_parray = array(); |
542 | | - $params = $this->uiCore->getParams(); |
| 553 | + $params = $this->uiCore->getParameters(); |
543 | 554 | foreach ( $params as $key => $value ) { |
544 | 555 | if ( !in_array( $key, array( 'sort', 'order', 'limit', 'offset', 'title' ) ) ) { |
545 | 556 | $tmp_parray[$key] = $value; |
— | — | @@ -594,7 +605,7 @@ |
595 | 606 | 'style' => 'width: 30%; padding: 5px; float: left;' |
596 | 607 | ), |
597 | 608 | htmlspecialchars( $param->getName() ) . ': ' . |
598 | | - $this->showFormatOption( $param, $currentValue ) . |
| 609 | + $this->getFormatOption( $param, $currentValue ) . |
599 | 610 | '<br />' . |
600 | 611 | Html::element( 'em', array(), $param->getDescription() ) |
601 | 612 | ); |
— | — | @@ -610,21 +621,17 @@ |
611 | 622 | $rowHtml = ''; |
612 | 623 | $resultHtml = ''; |
613 | 624 | |
614 | | - /// @todo Check if this code works if the number of options is not a multiple of 3! |
615 | 625 | while ( $option = array_shift( $optionsHtml ) ) { |
616 | 626 | $rowHtml .= $option; |
617 | 627 | $i += 1; |
618 | | - |
619 | | - if ( $i % 3 == 0 ) { |
620 | | - $resultHtml .= Html::rawElement( |
621 | | - 'div', |
622 | | - array( |
623 | | - 'style' => 'background: ' . ( $i % 6 == 0 ? 'white' : '#dddddd' ) . ';' |
624 | | - ), |
625 | | - $rowHtml |
626 | | - ); |
627 | | - $rowHtml = ''; |
628 | | - } |
| 628 | + $resultHtml .= Html::rawElement( |
| 629 | + 'div', |
| 630 | + array( |
| 631 | + 'style' => 'background: ' . ( $i % 6 == 0 ? 'white' : '#dddddd' ) . ';' |
| 632 | + ), |
| 633 | + $rowHtml |
| 634 | + ); |
| 635 | + $rowHtml = ''; |
629 | 636 | } |
630 | 637 | |
631 | 638 | return $resultHtml; |
— | — | @@ -671,15 +678,14 @@ |
672 | 679 | |
673 | 680 | /** |
674 | 681 | * Get the HTML for a single parameter input. |
| 682 | + * A helper method for showFormatOptions() |
675 | 683 | * |
676 | 684 | * @param Parameter $parameter |
677 | | - * @param mixed $currentValue |
| 685 | + * @param mixed $currentValue curretly set value of the parameter, or false if unknown. |
678 | 686 | * |
679 | | - * @return string |
680 | | - * @todo Change method name to reflect behaviour (it does not "show" anything). |
681 | | - * @todo Document what kind of types are expected for $currentValue, or at least say what its meaning is. |
| 687 | + * @return string generated HTML |
682 | 688 | */ |
683 | | - private function showFormatOption( Parameter $parameter, $currentValue ) { |
| 689 | + private function getFormatOption( Parameter $parameter, $currentValue ) { |
684 | 690 | $input = new ParameterInput( $parameter ); |
685 | 691 | $input->setInputName( 'p[' . $parameter->getName() . ']' ); |
686 | 692 | |
— | — | @@ -702,18 +708,9 @@ |
703 | 709 | * @return string |
704 | 710 | */ |
705 | 711 | protected function getFormatSelectBox( $defaultformat = 'broadtable' ) { |
706 | | - global $smwgResultFormats, $smwgJQueryIncluded, $wgOut; |
| 712 | + global $smwgResultFormats, $wgOut; |
707 | 713 | |
708 | | - /// @todo The very same code for JQuery inclusion occurs multiple times. Should be a helper function. |
709 | | - if ( !$smwgJQueryIncluded ) { |
710 | | - $realFunction = array( 'OutputPage', 'includeJQuery' ); |
711 | | - if ( is_callable( $realFunction ) ) { |
712 | | - $wgOut->includeJQuery(); |
713 | | - } else { ///@bug $scripts is undefined and not used later on |
714 | | - $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js"; |
715 | | - } |
716 | | - $smwgJQueryIncluded = true; |
717 | | - } |
| 714 | + $this->enableJQuery(); |
718 | 715 | |
719 | 716 | // checking argument |
720 | 717 | $default_format = 'broadtable'; |
— | — | @@ -725,7 +722,7 @@ |
726 | 723 | $printer = SMWQueryProcessor::getResultPrinter( $default_format, SMWQueryProcessor::SPECIAL_PAGE ); |
727 | 724 | $url = $this->getTitle()->getLocalURL( "showformatoptions=' + this.value + '" ); |
728 | 725 | |
729 | | - foreach ( $this->uiCore->getParams() as $param => $value ) { |
| 726 | + foreach ( $this->uiCore->getParameters() as $param => $value ) { |
730 | 727 | if ( $param !== 'format' ) { |
731 | 728 | $url .= '¶ms[' . Xml::escapeJsString( $param ) . ']=' . Xml::escapeJsString( $value ); |
732 | 729 | } |
— | — | @@ -746,7 +743,7 @@ |
747 | 744 | } |
748 | 745 | natcasesort( $formats ); |
749 | 746 | |
750 | | - $params = $this->uiCore->getParams(); |
| 747 | + $params = $this->uiCore->getParameters(); |
751 | 748 | foreach ( $formats as $format => $name ) { |
752 | 749 | $result .= '<option value="' . $format . '"' . ( $params['format'] == $format ? ' selected' : '' ) . '>' . $name . "</option>\n"; |
753 | 750 | } |
— | — | @@ -872,7 +869,6 @@ |
873 | 870 | * |
874 | 871 | * @author Devayon Das |
875 | 872 | * |
876 | | - * @todo The is_a function is deprecated in PHP and instanceof should be used instead. In many cases as simple check for "is_null" would be even better. |
877 | 873 | */ |
878 | 874 | class SMWQueryUIHelper { |
879 | 875 | |
— | — | @@ -1002,7 +998,7 @@ |
1003 | 999 | * @return boolean |
1004 | 1000 | */ |
1005 | 1001 | public function hasFurtherResults() { |
1006 | | - if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { // The queryResult may not be set |
| 1002 | + if ( !is_null( $this->queryResult ) ) { // The queryResult may not be set |
1007 | 1003 | return $this->queryResult->hasFurtherResults(); |
1008 | 1004 | } else { |
1009 | 1005 | return false; |
— | — | @@ -1180,6 +1176,8 @@ |
1181 | 1177 | * Processes the QueryString, Params, and PrintOuts. |
1182 | 1178 | * |
1183 | 1179 | * @todo Combine this method with execute() or remove it altogether. |
| 1180 | + * @todo for wikilink context, try to avoid computation if no query is set, |
| 1181 | + * also check for pagination problems, if any. |
1184 | 1182 | */ |
1185 | 1183 | public function extractParameters( $p ) { |
1186 | 1184 | if ( $this->context == self::SPECIAL_PAGE ) { |
— | — | @@ -1312,7 +1310,7 @@ |
1313 | 1311 | * @return int |
1314 | 1312 | */ |
1315 | 1313 | public function getResultCount() { |
1316 | | - if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { |
| 1314 | + if ( !is_null( $this->queryResult ) ) { |
1317 | 1315 | return $this->queryResult->getCount(); |
1318 | 1316 | } else { |
1319 | 1317 | return 0; |
— | — | @@ -1323,9 +1321,8 @@ |
1324 | 1322 | * Returns the parameter array. |
1325 | 1323 | * |
1326 | 1324 | * @return array |
1327 | | - * @todo Always avoid abbreviations (unless they are a special technical term used everywhere). Call this getParameters(). |
1328 | 1325 | */ |
1329 | | - public function getParams() { |
| 1326 | + public function getParameters() { |
1330 | 1327 | return $this->parameters; |
1331 | 1328 | } |
1332 | 1329 | |
— | — | @@ -1336,26 +1333,29 @@ |
1337 | 1334 | */ |
1338 | 1335 | public function getPrintOuts() { |
1339 | 1336 | if ( !empty( $this->printOuts ) && |
1340 | | - is_a( $this->printOuts[0], 'SMWPrintRequest' ) ) { |
| 1337 | + ( $this->printOuts[0] instanceof SMWPrintRequest ) ) { |
1341 | 1338 | return $this->printOuts; |
1342 | 1339 | } |
1343 | 1340 | return array(); |
1344 | 1341 | } |
1345 | 1342 | |
1346 | 1343 | /** |
1347 | | - * Constructs a new SMWQueryUIHelper when parameters are passed in the |
1348 | | - * InfoLink style. |
| 1344 | + * Constructs a new SMWQueryUIHelper object when the query is passed to |
| 1345 | + * the UI in the Info-link format. This constructor should be used for |
| 1346 | + * handling the "further results" links in wiki-pages that use #ask. If |
| 1347 | + * your search UI handles form parameters only, then consider using |
| 1348 | + * makeForUI(). |
| 1349 | + * |
| 1350 | + * If any errors do occur while parsing parameters, they may be accessed |
| 1351 | + * from hasError() and getErrors(). |
1349 | 1352 | * |
1350 | | - * Errors, if any can be accessed from hasError() and getErrors() |
1351 | | - * |
1352 | | - * @param string $p parameters |
| 1353 | + * @param string $p parameters of the query. |
1353 | 1354 | * @param boolean $enable_validation |
1354 | 1355 | * @return SMWQueryUIHelper |
1355 | 1356 | * |
1356 | | - * @todo The above documentation contains an unclear sequence of words that do not form a sentence. |
1357 | 1357 | * @todo Handle validation for infolink parameters |
1358 | 1358 | */ |
1359 | | - public static function makeForInfoLink( $p, $enable_validation = true ) { |
| 1359 | + public static function makeForInfoLink( $p, $enable_validation = false ) { |
1360 | 1360 | $result = new SMWQueryUIHelper( self::WIKI_LINK ); |
1361 | 1361 | $result->extractParameters( $p ); |
1362 | 1362 | $result->execute(); |
— | — | @@ -1363,10 +1363,14 @@ |
1364 | 1364 | } |
1365 | 1365 | |
1366 | 1366 | /** |
1367 | | - * Constructs a new SMWQueryUIHelper when arguments are extracted from |
1368 | | - * the UI. |
| 1367 | + * Constructs a new SMWQueryUIHelper when the query is passed to the UI |
| 1368 | + * from a web form. This constructor should be used to handle form |
| 1369 | + * parameters sent from the UI itself. If your search UI must also handle |
| 1370 | + * "further results" links from a wiki page, consider using |
| 1371 | + * makeForInfoLink(). |
1369 | 1372 | * |
1370 | | - * Errors, if any can be accessed from hasError() and getErrors() |
| 1373 | + * If any errors do occur while parsing parameters, they may be accessed |
| 1374 | + * from hasError() and getErrors(). |
1371 | 1375 | * |
1372 | 1376 | * @param string $query |
1373 | 1377 | * @param array $params of key=>value pairs |
— | — | @@ -1374,9 +1378,8 @@ |
1375 | 1379 | * @param boolean $enable_validation |
1376 | 1380 | * @return SMWQueryUIHelper |
1377 | 1381 | * |
1378 | | - * @todo The above documentation contains an unclear sequence of words that do not form a sentence. |
1379 | 1382 | */ |
1380 | | - public static function makeForUI( $query, array $params, array $printouts, $enable_validation = true ) { |
| 1383 | + public static function makeForUI( $query, array $params, array $printouts, $enable_validation = false ) { |
1381 | 1384 | $result = new SMWQueryUIHelper( self::SPECIAL_PAGE ); |
1382 | 1385 | $result->setParams( $params, $enable_validation ); |
1383 | 1386 | $result->setPrintOuts( $printouts, $enable_validation ); |
— | — | @@ -1389,8 +1392,8 @@ |
1390 | 1393 | /** |
1391 | 1394 | * Checks if $property exists in the wiki or not. |
1392 | 1395 | * |
1393 | | - * @return bool |
1394 | | - * @todo Document parameter type and format. |
| 1396 | + * @param string $property a property name in "?property" format |
| 1397 | + * @return boolean |
1395 | 1398 | */ |
1396 | 1399 | protected static function validateProperty( $property ) { |
1397 | 1400 | /* |
— | — | @@ -1399,7 +1402,7 @@ |
1400 | 1403 | */ |
1401 | 1404 | $prop = substr( $property, 1 ); // removing the leading '?' while checking. |
1402 | 1405 | $propertypage = Title::newFromText( $prop, SMW_NS_PROPERTY ); |
1403 | | - if ( is_a( $propertypage, 'Title' ) ) { |
| 1406 | + if ( $propertypage instanceof Title ) { |
1404 | 1407 | return( $propertypage->exists() ); |
1405 | 1408 | } else { |
1406 | 1409 | return false; |