r92056 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92055‎ | r92056 | r92057 >
Date:11:32, 13 July 2011
Author:mkroetzsch
Status:deferred
Tags:
Comment:
reviewed code, smaller improvements in style and logic, some comments/todos for bigger tasks
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -322,54 +322,51 @@
323323 */
324324 protected function addSortingFormBox() {
325325 global $smwgQSortingSupport, $wgRequest, $wgOut, $smwgJQueryIncluded;
 326+ if ( !$smwgQSortingSupport ) return '';
 327+
326328 $result = '';
327 - if ( $smwgQSortingSupport ) {
 329+ if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) {
 330+ $orders = array(); // do not even show one sort input here
 331+ } else {
 332+ $sorts = explode( ',', $this->m_params['sort'] );
 333+ $orders = explode( ',', $this->m_params['order'] );
 334+ reset( $sorts );
 335+ }
328336
329 - if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) {
330 - $orders = array(); // do not even show one sort input here
331 - } else {
332 - $sorts = explode( ',', $this->m_params['sort'] );
333 - $orders = explode( ',', $this->m_params['order'] );
334 - reset( $sorts );
335 - }
 337+ foreach ( $orders as $i => $order ) {
 338+ $result .= "<div id=\"sort_div_$i\">" . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort[' . $i . ']" value="' .
 339+ htmlspecialchars( $sorts[$i] ) . "\" size=\"35\"/>\n" . '<select name="order[' . $i . ']"><option ';
 340+ if ( $order == 'ASC' ) $result .= 'selected="selected" ';
 341+ $result .= 'value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . '</option><option ';
 342+ if ( $order == 'DESC' ) $result .= 'selected="selected" ';
336343
337 - foreach ( $orders as $i => $order ) {
338 - $result .= "<div id=\"sort_div_$i\">" . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort[' . $i . ']" value="' .
339 - htmlspecialchars( $sorts[$i] ) . "\" size=\"35\"/>\n" . '<select name="order[' . $i . ']"><option ';
340 - if ( $order == 'ASC' ) $result .= 'selected="selected" ';
341 - $result .= 'value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . '</option><option ';
342 - if ( $order == 'DESC' ) $result .= 'selected="selected" ';
343 -
344 - $result .= 'value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option></select>\n";
345 - $result .= '[<a href="javascript:removeInstance(\'sort_div_' . $i . '\')">' . wfMsg( 'delete' ) . '</a>]' . "\n";
346 - $result .= "</div>\n";
347 - }
348 -
349 - $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n";
350 - $result .= ' <select name="order_num">' . "\n";
351 - $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n";
352 - $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n";
 344+ $result .= 'value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option></select>\n";
 345+ $result .= '[<a href="javascript:removeInstance(\'sort_div_' . $i . '\')">' . wfMsg( 'delete' ) . '</a>]' . "\n";
353346 $result .= "</div>\n";
354 - $result .= '<div id="sorting_main"></div>' . "\n";
355 - $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n";
 347+ }
356348
 349+ $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n";
 350+ $result .= ' <select name="order_num">' . "\n";
 351+ $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n";
 352+ $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n";
 353+ $result .= "</div>\n";
 354+ $result .= '<div id="sorting_main"></div>' . "\n";
 355+ $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n";
357356
 357+ $this->m_num_sort_values = 0;
358358
359 -
360 - $this->m_num_sort_values = 0;
361 -
362 - if ( !array_key_exists( 'sort', $this->m_params ) ) {
363 - $sort_values = $wgRequest->getArray( 'sort' );
364 - if ( is_array( $sort_values ) ) {
365 - $this->m_params['sort'] = implode( ',', $sort_values );
366 - $this->m_num_sort_values = count( $sort_values );
367 - }
 359+ if ( !array_key_exists( 'sort', $this->m_params ) ) {
 360+ $sort_values = $wgRequest->getArray( 'sort' );
 361+ if ( is_array( $sort_values ) ) {
 362+ $this->m_params['sort'] = implode( ',', $sort_values );
 363+ $this->m_num_sort_values = count( $sort_values );
368364 }
 365+ }
369366 // Javascript code for handling adding and removing the "sort" inputs
370 - $delete_msg = wfMsg( 'delete' );
 367+ $delete_msg = wfMsg( 'delete' );
371368
372369
373 - $javascript_text = <<<EOT
 370+ $javascript_text = <<<EOT
374371 <script type="text/javascript">
375372 // code for handling adding and removing the "sort" inputs
376373 var num_elements = {$this->m_num_sort_values};
@@ -411,20 +408,20 @@
412409
413410 EOT;
414411
415 - $wgOut->addScript( $javascript_text );
 412+ $wgOut->addScript( $javascript_text );
416413
417 - if ( !$smwgJQueryIncluded ) {
418 - $realFunction = array( 'OutputPage', 'includeJQuery' );
419 - if ( is_callable( $realFunction ) ) {
420 - $wgOut->includeJQuery();
421 - } else {
422 - $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
423 - }
 414+ if ( !$smwgJQueryIncluded ) {
 415+ $realFunction = array( 'OutputPage', 'includeJQuery' );
 416+ if ( is_callable( $realFunction ) ) {
 417+ $wgOut->includeJQuery();
 418+ } else {
 419+ $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
 420+ }
424421
425 - $smwgJQueryIncluded = true;
426 - }
427 - }
428 - return $result;
 422+ $smwgJQueryIncluded = true;
 423+ }
 424+
 425+ return $result;
429426 }
430427
431428 protected function processSortingFormBox( WebRequest $wgRequest ) {
@@ -434,12 +431,15 @@
435432 $params['order'] = '';
436433
437434 foreach ( $order_values as $order_value ) {
438 - if ( $order_value == '' ) $order_value = 'ASC';
 435+ if ( $order_value == '' ) {
 436+ $order_value = 'ASC';
 437+ }
439438 $params['order'] .= ( $params['order'] != '' ? ',' : '' ) . $order_value;
440439 }
441440 return $params;
 441+ } else {
 442+ return array();
442443 }
443 - return array();
444444 }
445445
446446 /**
@@ -452,9 +452,9 @@
453453 * @return string The HTML code
454454 */
455455 protected function getPOFormBox( $content, $enableAutocomplete = SMWQueryUI::ENABLE_AUTO_SUGGEST ) {
 456+ global $wgOut;
 457+
456458 if ( $enableAutocomplete ) {
457 - global $wgOut;
458 -
459459 $this->addAutocompletionJavascriptAndCSS();
460460 $javascript_autocomplete_text = <<<EOT
461461 <script type="text/javascript">
@@ -494,14 +494,14 @@
495495 $wgOut->addScript( $javascript_autocomplete_text );
496496
497497 }
498 - $result = "";
499 - $result = Html::element( 'textarea', array( 'id' => 'add_property', 'name' => 'po', 'cols' => '20', 'rows' => '6' ), $content );
500 - return $result;
 498+
 499+ return Html::element( 'textarea', array( 'id' => 'add_property', 'name' => 'po', 'cols' => '20', 'rows' => '6' ), $content );
501500 }
502501
503502 /**
504503 * Decodes form data sent through form-elements generated by
505 - * its complement, getPOFormBox(). UIs may overload both to change form parameters.
 504+ * its complement, getPOFormBox(). UIs may overload both to change form
 505+ * parameters.
506506 *
507507 * @param WebRequest $wgRequest
508508 * @return array
@@ -523,17 +523,20 @@
524524 $poarray[] = $param;
525525 }
526526 }
 527+
527528 return $poarray;
528529 }
529530
530531 /**
531532 * Generates the url parameters based on passed parameters.
532 - * UI implementations need to overload this if they use different form parameters.
 533+ * UI implementations need to overload this if they use different form
 534+ * parameters.
533535 *
534536 * @return string An url-encoded string.
535537 */
536538 protected function getUrlTail() {
537539 $urltail = '&q=' . urlencode( $this->uiCore->getQuerystring() );
 540+
538541 $tmp_parray = array();
539542 $params = $this->uiCore->getParams();
540543 foreach ( $params as $key => $value ) {
@@ -541,16 +544,23 @@
542545 $tmp_parray[$key] = $value;
543546 }
544547 }
 548+ $urltail .= '&p=' . urlencode( SMWInfolink::encodeParameters( $tmp_parray ) );
545549
546 - $urltail .= '&p=' . urlencode( SMWInfolink::encodeParameters( $tmp_parray ) );
547550 $printoutstring = '';
548551 foreach ( $this->uiCore->getPrintOuts() as $printout ) {
549552 $printoutstring .= $printout->getSerialisation() . "\n";
550553 }
551554
552 - if ( $printoutstring != '' ) $urltail .= '&po=' . urlencode( $printoutstring );
553 - if ( array_key_exists( 'sort', $params ) ) $urltail .= '&sort=' . $params['sort'];
554 - if ( array_key_exists( 'order', $params ) ) $urltail .= '&order=' . $params['order'];
 555+ if ( $printoutstring != '' ) {
 556+ $urltail .= '&po=' . urlencode( $printoutstring );
 557+ }
 558+ if ( array_key_exists( 'sort', $params ) ) {
 559+ $urltail .= '&sort=' . $params['sort'];
 560+ }
 561+ if ( array_key_exists( 'order', $params ) ) {
 562+ $urltail .= '&order=' . $params['order'];
 563+ }
 564+
555565 return $urltail;
556566 }
557567
@@ -564,8 +574,6 @@
565575 * @return string
566576 */
567577 protected function showFormatOptions( $format, array $paramValues ) {
568 - $text = '';
569 -
570578 $printer = SMWQueryProcessor::getResultPrinter( $format, SMWQueryProcessor::SPECIAL_PAGE );
571579
572580 $params = method_exists( $printer, 'getParameters' ) ? $printer->getParameters() : array();
@@ -592,7 +600,7 @@
593601 );
594602 }
595603
596 - for ( $i = 0, $n = count( $optionsHtml ); $i < $n; $i++ ) {
 604+ for ( $i = 0, $n = count( $optionsHtml ); $i < $n; $i += 1 ) {
597605 if ( $i % 3 == 2 || $i == $n - 1 ) {
598606 $optionsHtml[$i] .= Html::element( 'div', array( 'style' => 'clear: both;' ) ) . "\n";
599607 }
@@ -602,9 +610,10 @@
603611 $rowHtml = '';
604612 $resultHtml = '';
605613
 614+ /// @todo Check if this code works if the number of options is not a multiple of 3!
606615 while ( $option = array_shift( $optionsHtml ) ) {
607616 $rowHtml .= $option;
608 - $i++;
 617+ $i += 1;
609618
610619 if ( $i % 3 == 0 ) {
611620 $resultHtml .= Html::rawElement(
@@ -625,7 +634,7 @@
626635 * Returns a Validator style Parameter definition.
627636 * SMW 1.5.x style definitions are converted.
628637 *
629 - * @param mixed $param
 638+ * @param mixed $param Parameter or array
630639 *
631640 * @return Parameter
632641 */
@@ -639,8 +648,10 @@
640649 $param['type'] = 'string';
641650 }
642651
643 - $paramClass = $param['type'] == 'enum-list' ? 'ListParameter' : 'Parameter';
644 - $paramType = array_key_exists( $param['type'], $typeMap ) ? $typeMap[$param['type']] : Parameter::TYPE_STRING;
 652+ $paramClass = $param['type'] == 'enum-list' ?
 653+ 'ListParameter' : 'Parameter';
 654+ $paramType = array_key_exists( $param['type'], $typeMap ) ?
 655+ $typeMap[$param['type']] : Parameter::TYPE_STRING;
645656
646657 $parameter = new $paramClass( $param['name'], $paramType );
647658
@@ -653,8 +664,7 @@
654665 }
655666
656667 return $parameter;
657 - }
658 - else {
 668+ } else {
659669 return $param;
660670 }
661671 }
@@ -666,6 +676,8 @@
667677 * @param mixed $currentValue
668678 *
669679 * @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.
670682 */
671683 private function showFormatOption( Parameter $parameter, $currentValue ) {
672684 $input = new ParameterInput( $parameter );
@@ -679,24 +691,25 @@
680692 }
681693
682694 /**
683 - * Creates form elements for choosing the result-format and their associated
684 - * format. Use in conjunction with processFormatOptions() to supply formats
685 - * options using ajax. Also, use its complement processFormatSelectBox() to
686 - * decode form data sent by these elements. UI's may overload these methods
687 - * to change behaviour or form parameters.
 695+ * Creates form elements for choosing the result-format and their
 696+ * associated format. Use in conjunction with processFormatOptions() to
 697+ * supply formats options using ajax. Also, use its complement
 698+ * processFormatSelectBox() to decode form data sent by these elements.
 699+ * UI's may overload these methods to change behaviour or form
 700+ * parameters.
688701 *
689702 * @param string $defaultformat The default format which remains selected in the form
690703 * @return string
691704 */
692705 protected function getFormatSelectBox( $defaultformat = 'broadtable' ) {
693 -
694706 global $smwgResultFormats, $smwgJQueryIncluded, $wgOut;
695707
 708+ /// @todo The very same code for JQuery inclusion occurs multiple times. Should be a helper function.
696709 if ( !$smwgJQueryIncluded ) {
697710 $realFunction = array( 'OutputPage', 'includeJQuery' );
698711 if ( is_callable( $realFunction ) ) {
699712 $wgOut->includeJQuery();
700 - } else {
 713+ } else { ///@bug $scripts is undefined and not used later on
701714 $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
702715 }
703716 $smwgJQueryIncluded = true;
@@ -708,7 +721,7 @@
709722 $default_format = $defaultformat;
710723 }
711724
712 - $result = "";
 725+ $result = '';
713726 $printer = SMWQueryProcessor::getResultPrinter( $default_format, SMWQueryProcessor::SPECIAL_PAGE );
714727 $url = $this->getTitle()->getLocalURL( "showformatoptions=' + this.value + '" );
715728
@@ -719,11 +732,11 @@
720733 }
721734
722735 $result .= "\n<p>" . wfMsg( 'smw_ask_format_as' ) . "\n" .
723 - '<select id="formatSelector" name="p[format]" onChange="JavaScript:updateOtherOptions(\'' . $url . '\')">' . "\n" .
724 - ' <option value="' . $default_format . '">' . $printer->getName() . ' (' . wfMsg( 'smw_ask_defaultformat' ) . ')</option>' . "\n";
 736+ '<select id="formatSelector" name="p[format]" onChange="JavaScript:updateOtherOptions(\'' . $url . '\')">' . "\n" .
 737+ '<option value="' . $default_format . '">' . $printer->getName() .
 738+ ' (' . wfMsg( 'smw_ask_defaultformat' ) . ')</option>' . "\n";
725739
726740 $formats = array();
727 -
728741 foreach ( array_keys( $smwgResultFormats ) as $format ) {
729742 // Special formats "count" and "debug" currently not supported.
730743 if ( $format != $default_format && $format != 'count' && $format != 'debug' ) {
@@ -731,12 +744,13 @@
732745 $formats[$format] = $printer->getName();
733746 }
734747 }
 748+ natcasesort( $formats );
735749
736 - natcasesort( $formats );
737750 $params = $this->uiCore->getParams();
738751 foreach ( $formats as $format => $name ) {
739 - $result .= ' <option value="' . $format . '"' . ( $params['format'] == $format ? ' selected' : '' ) . '>' . $name . "</option>\n";
 752+ $result .= '<option value="' . $format . '"' . ( $params['format'] == $format ? ' selected' : '' ) . '>' . $name . "</option>\n";
740753 }
 754+
741755 $result .= "</select>";
742756 $result .= "</p>\n";
743757 $result .= '<fieldset><legend>' . wfMsg( 'smw_ask_otheroptions' ) . "</legend>\n";
@@ -744,7 +758,6 @@
745759 $result .= "</fieldset>\n";
746760
747761 // BEGIN: add javascript for updating formating options by ajax
748 - global $wgOut;
749762 $javascript = <<<END
750763 <script type="text/javascript">
751764 function updateOtherOptions(strURL) {
@@ -762,17 +775,19 @@
763776 }
764777
765778 /**
766 - * A method which decodes form data sent through form-elements generated by
767 - * its complement, getFormatSelectBox(). UIs may overload both to change form parameters.
 779+ * A method which decodes form data sent through form-elements generated
 780+ * by its complement, getFormatSelectBox(). UIs may overload both to
 781+ * change form parameters.
768782 *
769783 * @param WebRequest $wgRequest
770784 * @return array
771785 */
772786 protected function processFormatSelectBox( WebRequest $wgRequest ) {
773787 $query_val = $wgRequest->getVal( 'p' );
774 - if ( !empty( $query_val ) )
 788+
 789+ if ( !empty( $query_val ) ) {
775790 $params = SMWInfolink::decodeParameters( $query_val, false );
776 - else {
 791+ } else {
777792 $query_values = $wgRequest->getArray( 'p' );
778793
779794 if ( is_array( $query_values ) ) {
@@ -781,34 +796,35 @@
782797 }
783798 }
784799
785 - // p is used for any additional parameters in certain links.
786 - $params = SMWInfolink::decodeParameters( $query_values, false );
 800+ // p is used for any additional parameters in certain links.
 801+ $params = SMWInfolink::decodeParameters( $query_values, false );
787802 }
 803+
788804 return $params;
789805 }
790806
791807 /**
792808 * Generates form elements for a (web)requested format.
793809 *
794 - * Required by getFormatSelectBox() to recieve form elements from the web.
795 - * UIs may need to overload processFormatOptions(), getgetFormatSelectBox()
796 - * and getFormatSelectBox() to change behavior.
 810+ * Required by getFormatSelectBox() to recieve form elements from the Web.
 811+ * UIs may need to overload processFormatOptions(),
 812+ * getgetFormatSelectBox() and getFormatSelectBox() to change behavior.
797813 *
798814 * @param WebRequest $wgRequest
799 - * @return boolean Returns true if format options were requested and returned, else false
 815+ * @return boolean true if format options were requested and returned, else false
800816 */
801817 protected function processFormatOptions( $wgRequest ) {
802818 global $wgOut;
803819 if ( $wgRequest->getCheck( 'showformatoptions' ) ) {
804 - // handle Ajax action
805 - $format = $wgRequest->getVal( 'showformatoptions' );
806 - $params = $wgRequest->getArray( 'params' );
807 - $wgOut->disable();
808 - echo $this->showFormatOptions( $format, $params );
809 - return true;
810 - } else {
811 - return false;
812 - }
 820+ // handle Ajax action
 821+ $format = $wgRequest->getVal( 'showformatoptions' );
 822+ $params = $wgRequest->getArray( 'params' );
 823+ $wgOut->disable();
 824+ echo $this->showFormatOptions( $format, $params );
 825+ return true;
 826+ } else {
 827+ return false;
 828+ }
813829 }
814830
815831 /**
@@ -819,9 +835,9 @@
820836 * @return string
821837 */
822838 public function getPOStrings() {
823 - $string = "";
 839+ $string = '';
824840 $printouts = $this->uiCore->getPrintOuts();
825 - if ( !empty( $printouts ) ) {
 841+ if ( !empty( $printouts ) ) {
826842 foreach ( $printouts as $value ) {
827843 $string .= $value->getSerialisation() . "\n";
828844 }
@@ -830,32 +846,33 @@
831847 }
832848
833849 /**
834 - * Returns true if this page shows the navigationBar. Overload to change behavior.
 850+ * Returns true if this page shows the navigationBar. Overload to change
 851+ * behavior.
835852 *
836853 * @return boolean
837854 */
838855 protected function usesNavigationBar() {
839856 // hide if no results are found
840 - if ( $this->uiCore->getResultCount() == 0 ) return false;
841 - else return true;
 857+ return ( $this->uiCore->getResultCount() != 0 );
842858 }
843859
844860 }
845861
846862 /**
847 - * This class captures the core activities of what a semantic search page should do:
848 - * (take parameters, validate them and generate results, or errors, if any).
 863+ * This class captures the core activities of what a semantic search page should
 864+ * do: take parameters, validate them and generate results, or errors, if any.
849865 *
850 - * Query UIs may use this class to create a customised UI interface. In most cases,
851 - * one is likely to extend the SMWQueryUI class to build a Search Special page.
852 - * However in order to acces some core featues, one may directly access the methods
853 - * of this class.
 866+ * Query UIs may use this class to create a customised UI interface. In most
 867+ * cases, one is likely to extend the SMWQueryUI class to build a Search Special
 868+ * page. However in order to acces some core featues, one may directly access
 869+ * the methods of this class.
854870 *
855871 * This class does not define the format in which data should be passed through
856872 * the web, except those already defined by SMWInfolink.
857873 *
858874 * @author Devayon Das
859875 *
 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.
860877 */
861878 class SMWQueryUIHelper {
862879
@@ -866,19 +883,21 @@
867884 protected $queryString = '';
868885
869886 /**
870 - * Various parameters passed by the user which control the format, limit, offset.
 887+ * Various parameters passed by the user which control the format,
 888+ * limit, offset.
871889 * @var array of strings
872890 */
873891 protected $parameters = array();
874892
875893 /**
876 - * The additional columns to be displayed with results
 894+ * The additional columns to be displayed with results.
877895 * @var array of SMWPrintRequest
878896 */
879897 protected $printOuts = array(); // Properties to be printed along with results
880898
881899 /**
882 - * The The additional columns to be displayed with results in '?property' form
 900+ * The The additional columns to be displayed with results in
 901+ * '?property' form.
883902 *
884903 * @var array of strings
885904 */
@@ -928,7 +947,7 @@
929948 *
930949 * @var array of SpecialPage
931950 */
932 - protected static $uiPages = array(); // A list of Query UIs
 951+ protected static $uiPages = array();
933952
934953 /**
935954 * Although this constructor is publicly accessible, its use is discouraged.
@@ -953,27 +972,26 @@
954973 /**
955974 * Returns the limit of results defined. If not set, it returns 0.
956975 *
957 - * @return int
 976+ * @return integer
958977 */
959978 public function getLimit() {
960979 if ( array_key_exists( 'limit', $this->parameters ) ) {
961980 return $this->parameters['limit'];
962 - }
963 - else {
 981+ } else {
964982 return 0;
965983 }
966984 }
967985
968986 /**
969 - * Returns the offset of results. If it isnt defined, returns a default value of 20.
 987+ * Returns the offset of results. If it isnt defined, returns a default
 988+ * value of 20.
970989 *
971 - * @return int
 990+ * @return integer
972991 */
973992 public function getOffset() {
974993 if ( array_key_exists( 'offset', $this->parameters ) ) {
975994 return $this->parameters['offset'];
976 - }
977 - else {
 995+ } else {
978996 return 20;
979997 }
980998 }
@@ -986,8 +1004,7 @@
9871005 public function hasFurtherResults() {
9881006 if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { // The queryResult may not be set
9891007 return $this->queryResult->hasFurtherResults();
990 - }
991 - else {
 1008+ } else {
9921009 return false;
9931010 }
9941011 }
@@ -996,10 +1013,10 @@
9971014 * Returns a handle to the underlying Result object.
9981015 *
9991016 * @return SMWQueryResult
 1017+ * @todo Check if this method can be removed.
10001018 */
10011019 public function getResultObject() {
10021020 return $this->queryResult;
1003 - // TODO: see if this method can be removed.
10041021 }
10051022
10061023 /**
@@ -1015,17 +1032,17 @@
10161033 * Register a Semantic Search Special Page.
10171034 *
10181035 * This method can be used by any new Query UI to register itself.
1019 - * The corresponding method getUiList() would return the names of all lists
1020 - * Query UIs.
 1036+ * The corresponding method getUiList() would return the names of all
 1037+ * lists Query UIs.
10211038 *
10221039 * @see getUiList()
10231040 * @param SpecialPage $page
10241041 */
10251042 public static function addUI( SpecialPage &$page ) {
1026 - /*
1027 - * This way of registering, instead of using a global variable will cause
1028 - * SMWQueryUIHelper to AutoLoad, but the alternate would break encapsulation.
1029 - */
 1043+ /*
 1044+ * This way of registering, instead of using a global variable will cause
 1045+ * SMWQueryUIHelper to AutoLoad, but the alternate would break encapsulation.
 1046+ */
10301047 self::$uiPages[] = $page;
10311048 }
10321049
@@ -1040,8 +1057,8 @@
10411058 }
10421059
10431060 /**
1044 - * Sets up a query. If validation is enabled, then the query string is checked
1045 - * for errors.
 1061+ * Sets up a query. If validation is enabled, then the query string is
 1062+ * checked for errors.
10461063 *
10471064 * @param string $query_string The query
10481065 * @return array array of errors, if any.
@@ -1053,9 +1070,7 @@
10541071 if ( $enable_validation ) {
10551072 if ( $query_string == '' ) {
10561073 $errors[] = "No query has been specified"; // TODO i18n
1057 - }
1058 - else
1059 - {
 1074+ } else {
10601075 $query = SMWQueryProcessor::createQuery( $query_string, array() );
10611076 $errors = $query ->getErrors();
10621077 }
@@ -1070,12 +1085,13 @@
10711086
10721087 /**
10731088 *
1074 - * Sets up any extra properties which need to be displayed with results. Each
1075 - * string in printouts should be of the form "?property" or "property"
 1089+ * Sets up any extra properties which need to be displayed with results.
 1090+ * Each string in printouts should be of the form "?property" or
 1091+ * "property".
10761092 *
1077 - * When validation is enabled, the values in $print_outs are checked against
1078 - * properties which exist in the wiki, and a warning string (for each
1079 - * property) is returned. Returns an empty array otherwise.
 1093+ * When validation is enabled, the values in $print_outs are checked
 1094+ * against properties which exist in the wiki, and a warning string (for
 1095+ * each property) is returned. Returns an empty array otherwise.
10801096 *
10811097 * @param array $print_outs Array of strings
10821098 * @param boolean $enable_validation
@@ -1106,12 +1122,13 @@
11071123 /**
11081124 * Sets the parameters for the query.
11091125 *
1110 - * The structure of $params is defined partly by #ask
1111 - * and also by the Result Printer used. When validation is enabled, $params are checked
 1126+ * The structure of $params is defined partly by #ask and also by the
 1127+ * Result Printer used. When validation is enabled, $params are checked
11121128 * for conformance, and error messages, if any, are returned.
11131129 *
1114 - * Although it is not mandatory for any params to be set while calling this method,
1115 - * this method must be called so that default parameters are used.
 1130+ * Although it is not mandatory for any params to be set while calling
 1131+ * this method, this method must be called so that default parameters
 1132+ * are used.
11161133 *
11171134 * @global int $smwgQMaxInlineLimit
11181135 * @global array $smwgResultFormats
@@ -1123,40 +1140,39 @@
11241141 global $smwgQMaxInlineLimit, $smwgResultFormats;
11251142 $errors = array();
11261143
1127 - // checking for missing parameters and adding them
1128 - if ( !array_key_exists( 'format', $params ) or ! array_key_exists ( $params['format'], $smwgResultFormats ) )
1129 - $params[ 'format' ] = $this->defaultResultPrinter;
1130 - if ( !array_key_exists( 'limit', $params ) )
1131 - $params[ 'limit' ] = 20;
 1144+ // checking for missing parameters and adding them
 1145+ if ( !array_key_exists( 'format', $params ) or ! array_key_exists ( $params['format'], $smwgResultFormats ) ) {
 1146+ $params[ 'format' ] = $this->defaultResultPrinter;
 1147+ }
 1148+ if ( !array_key_exists( 'limit', $params ) ) {
 1149+ $params[ 'limit' ] = 20;
 1150+ }
11321151 $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit );
1133 - if ( !array_key_exists( 'offset', $params ) )
1134 - $params['offset'] = 0;
 1152+ if ( !array_key_exists( 'offset', $params ) ) {
 1153+ $params['offset'] = 0;
 1154+ }
11351155
1136 - if ( $enable_validation ) {
1137 - // validating the format
 1156+ if ( $enable_validation ) { // validating the format
11381157 if ( !array_key_exists( $params['format'], $smwgResultFormats ) ) {
11391158 $errors[] = "The chosen format " + $params['format'] + " does not exist for this wiki"; // TODO i18n
11401159 $this->errorsOccured = true;
1141 - }
1142 - else
1143 - { // validating parameters for result printer
 1160+ } else { // validating parameters for result printer
11441161 $printer = SMWQueryProcessor::getResultPrinter( $params[ 'format' ] );
11451162 $para_meters = $printer->getParameters();
11461163 if ( is_array( $para_meters ) ) {
1147 - $validator = new Validator();
1148 - $validator -> setParameters( $params, $para_meters );
1149 - $validator->validateParameters();
1150 - $validator_has_error = $validator->hasFatalError();
1151 - if ( $validator_has_error ) {
1152 - array_merge ( $errors, $validator->getErrorMessages () );
1153 - $this->errorsOccured = true;
1154 - }
 1164+ $validator = new Validator();
 1165+ $validator->setParameters( $params, $para_meters );
 1166+ $validator->validateParameters();
 1167+ if ( $validator->hasFatalError() ) {
 1168+ array_merge ( $errors, $validator->getErrorMessages () );
 1169+ $this->errorsOccured = true;
 1170+ }
11551171 }
11561172 }
11571173 }
11581174
1159 - $this -> parameters = $params;
1160 - $this -> errors = array_merge( $errors, $this->errors );
 1175+ $this->parameters = $params;
 1176+ $this->errors = array_merge( $errors, $this->errors );
11611177 return $errors;
11621178 }
11631179
@@ -1164,15 +1180,12 @@
11651181 * Processes the QueryString, Params, and PrintOuts.
11661182 *
11671183 * @todo Combine this method with execute() or remove it altogether.
1168 - *
11691184 */
11701185 public function extractParameters( $p ) {
11711186 if ( $this->context == self::SPECIAL_PAGE ) {
11721187 // assume setParams(), setPintouts and setQueryString have been called
11731188 $rawparams = array_merge( $this->parameters, array( $this->queryString ), $this->printOutStrings );
1174 - }
1175 - else // context is WIKI_LINK
1176 - {
 1189+ } else {// context is WIKI_LINK
11771190 $rawparams = SMWInfolink::decodeParameters( $p, true );
11781191 // calling setParams to fill in missing parameters
11791192 $this->setParams( $rawparams );
@@ -1185,17 +1198,20 @@
11861199 /**
11871200 * Executes the query.
11881201 *
1189 - * This method can be called once $queryString, $parameters, $printOuts are set
1190 - * either by using the setQueryString(), setParams() and setPrintOuts() followed by extractParameters(),
1191 - * or one of the static factory methods such as makeForInfoLink() or makeForUI().
 1202+ * This method can be called once $queryString, $parameters, $printOuts
 1203+ * are set either by using the setQueryString(), setParams() and
 1204+ * setPrintOuts() followed by extractParameters(), or one of the static
 1205+ * factory methods such as makeForInfoLink() or makeForUI().
11921206 *
11931207 * Errors, if any can be accessed from hasError() and getErrors().
11941208 */
11951209 public function execute() {
11961210 $errors = array();
1197 - $query = SMWQueryProcessor::createQuery( $this->queryString, $this->parameters, SMWQueryProcessor::SPECIAL_PAGE , $this->parameters['format'], $this->printOuts );
 1211+ $query = SMWQueryProcessor::createQuery( $this->queryString, $this->parameters,
 1212+ SMWQueryProcessor::SPECIAL_PAGE , $this->parameters['format'], $this->printOuts );
11981213 $res = smwfGetStore()->getQueryResult( $query );
11991214 $this->queryResult = $res;
 1215+
12001216 $errors = array_merge( $errors, $res->getErrors() );
12011217 if ( !empty( $errors ) ) {
12021218 $this->errorsOccured = true;
@@ -1213,7 +1229,7 @@
12141230 $desckey = false;
12151231 }
12161232
1217 - if ( ( $desckey ) && ( $query->getDescription() instanceof SMWConceptDescription ) &&
 1233+ if ( $desckey && ( $query->getDescription() instanceof SMWConceptDescription ) &&
12181234 ( !isset( $this->parameters[$desckey] ) || !isset( $this->parameters[$titlekey] ) ) ) {
12191235 $concept = $query->getDescription()->getConcept();
12201236
@@ -1222,6 +1238,7 @@
12231239 }
12241240
12251241 if ( !isset( $this->parameters[$desckey] ) ) {
 1242+ /// @bug The current SMWStore will never return SMWConceptValue (an SMWDataValue) here; it might return SMWDIConcept (an SMWDataItem)
12261243 $dv = end( smwfGetStore()->getPropertyValues( SMWWikiPageValue::makePageFromTitle( $concept ), new SMWDIProperty( '_CONC' ) ) );
12271244 if ( $dv instanceof SMWConceptValue ) {
12281245 $this->parameters[$desckey] = $dv->getDocu();
@@ -1232,32 +1249,34 @@
12331250 }
12341251
12351252 /**
1236 - * Returns the results in HTML, or in case of exports, a link to the result.
 1253+ * Returns the results in HTML, or in case of exports, a link to the
 1254+ * result.
12371255 *
12381256 * This method can only be called after execute() has been called.
12391257 *
1240 - * @return string of all the html generated
 1258+ * @return string of all the HTML generated
12411259 */
12421260 public function getHTMLResult() {
12431261 $result = '';
 1262+
12441263 $res = $this->queryResult;
1245 - $printer = SMWQueryProcessor::getResultPrinter( $this->parameters['format'], SMWQueryProcessor::SPECIAL_PAGE );
 1264+ $printer = SMWQueryProcessor::getResultPrinter( $this->parameters['format'],
 1265+ SMWQueryProcessor::SPECIAL_PAGE );
12461266 $result_mime = $printer->getMimeType( $res );
12471267
1248 - if ( $res->getCount() > 0 ) {
 1268+ if ( $res->getCount() > 0 ) {
 1269+ $query_result = $printer->getResult( $res, $this->parameters, SMW_OUTPUT_HTML );
12491270
1250 - $query_result = $printer->getResult( $res, $this->parameters, SMW_OUTPUT_HTML );
1251 -
1252 - if ( is_array( $query_result ) ) {
1253 - $result .= $query_result[0];
1254 - } else {
1255 - $result .= $query_result;
1256 - }
1257 -
 1271+ if ( is_array( $query_result ) ) {
 1272+ $result .= $query_result[0];
12581273 } else {
1259 - $result = wfMsg( 'smw_result_noresults' );
 1274+ $result .= $query_result;
12601275 }
1261 - return $result;
 1276+ } else {
 1277+ $result = wfMsg( 'smw_result_noresults' );
 1278+ }
 1279+
 1280+ return $result;
12621281 }
12631282
12641283 /**
@@ -1271,7 +1290,8 @@
12721291 $result .= '|' . $printout->getSerialisation() . "\n";
12731292 }
12741293 foreach ( $this->parameters as $param_name => $param_value ) {
1275 - $result .= '|' . htmlspecialchars( $param_name ) . '=' . htmlspecialchars( $param_value ) . "\n";
 1294+ $result .= '|' . htmlspecialchars( $param_name ) .
 1295+ '=' . htmlspecialchars( $param_value ) . "\n";
12761296 }
12771297 $result .= '}}';
12781298 return $result;
@@ -1294,45 +1314,48 @@
12951315 public function getResultCount() {
12961316 if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) {
12971317 return $this->queryResult->getCount();
 1318+ } else {
 1319+ return 0;
12981320 }
1299 - else return 0;
1300 -
13011321 }
13021322
13031323 /**
1304 - * Retuens the param array
 1324+ * Returns the parameter array.
13051325 *
13061326 * @return array
 1327+ * @todo Always avoid abbreviations (unless they are a special technical term used everywhere). Call this getParameters().
13071328 */
13081329 public function getParams() {
13091330 return $this->parameters;
13101331 }
13111332
13121333 /**
1313 - * Returns additional prinouts as an array of SMWPrintRequests
 1334+ * Returns additional prinouts as an array of SMWPrintRequests.
13141335 *
13151336 * @return array SMWPrintRequest or an empty array
13161337 */
13171338 public function getPrintOuts() {
1318 - if ( !empty( $this->printOuts ) ) {
1319 - if ( is_a( $this->printOuts[0], 'SMWPrintRequest' ) ) {
1320 - return $this->printOuts;
1321 - }
 1339+ if ( !empty( $this->printOuts ) &&
 1340+ is_a( $this->printOuts[0], 'SMWPrintRequest' ) ) {
 1341+ return $this->printOuts;
13221342 }
13231343 return array();
13241344 }
13251345
13261346 /**
1327 - * Constructs a new SMWQueryUIHelper when parameters are passed in the InfoLink style
 1347+ * Constructs a new SMWQueryUIHelper when parameters are passed in the
 1348+ * InfoLink style.
13281349 *
13291350 * Errors, if any can be accessed from hasError() and getErrors()
13301351 *
1331 - * @param string $p parametrs
 1352+ * @param string $p parameters
13321353 * @param boolean $enable_validation
13331354 * @return SMWQueryUIHelper
 1355+ *
 1356+ * @todo The above documentation contains an unclear sequence of words that do not form a sentence.
 1357+ * @todo Handle validation for infolink parameters
13341358 */
13351359 public static function makeForInfoLink( $p, $enable_validation = true ) {
1336 - // TODO handle validation for infolink parameters
13371360 $result = new SMWQueryUIHelper( self::WIKI_LINK );
13381361 $result->extractParameters( $p );
13391362 $result->execute();
@@ -1340,7 +1363,8 @@
13411364 }
13421365
13431366 /**
1344 - * Constructs a new SMWQueryUIHelper when arguments are extracted from the UI
 1367+ * Constructs a new SMWQueryUIHelper when arguments are extracted from
 1368+ * the UI.
13451369 *
13461370 * Errors, if any can be accessed from hasError() and getErrors()
13471371 *
@@ -1349,28 +1373,31 @@
13501374 * @param array $printouts array of '?property' strings
13511375 * @param boolean $enable_validation
13521376 * @return SMWQueryUIHelper
 1377+ *
 1378+ * @todo The above documentation contains an unclear sequence of words that do not form a sentence.
13531379 */
13541380 public static function makeForUI( $query, array $params, array $printouts, $enable_validation = true ) {
13551381 $result = new SMWQueryUIHelper( self::SPECIAL_PAGE );
13561382 $result->setParams( $params, $enable_validation );
13571383 $result->setPrintOuts( $printouts, $enable_validation );
13581384 $result->setQueryString( $query, $enable_validation );
1359 - $result->extractParameters( "" );
 1385+ $result->extractParameters( '' );
13601386 // $result->execute();
13611387 return $result;
13621388 }
13631389
13641390 /**
1365 - * Checks if $property exists in the wiki or not
 1391+ * Checks if $property exists in the wiki or not.
13661392 *
13671393 * @return bool
 1394+ * @todo Document parameter type and format.
13681395 */
13691396 protected static function validateProperty( $property ) {
13701397 /*
13711398 * Curently there isn't a simple, back-end agnost way of searching for properties from
13721399 * SMWStore. We hence we check if $property has a corresponding page describing it.
13731400 */
1374 - $prop = substr ( $property, 1 );// removing the leading '?' while checking.
 1401+ $prop = substr( $property, 1 ); // removing the leading '?' while checking.
13751402 $propertypage = Title::newFromText( $prop, SMW_NS_PROPERTY );
13761403 if ( is_a( $propertypage, 'Title' ) ) {
13771404 return( $propertypage->exists() );

Follow-up revisions

RevisionCommit summaryAuthorDate
r92142Fixed some bugs and todo's, follow-up to r92056devayon06:45, 14 July 2011
r94774bug regarding concepts (originally noted in r92056)devayon16:47, 17 August 2011