/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 193 lines · 96 code · 32 blank · 65 comment · 19 complexity · bc400045715556ad6434b903083e5d7e MD5 · raw file

  1. <?php
  2. /**
  3. * Class that holds static functions for handling compound queries.
  4. * This class inherits from Semantic MediaWiki's SMWQueryProcessor.
  5. *
  6. * @ingroup SemanticCompoundQueries
  7. *
  8. * @author Yaron Koren
  9. */
  10. class SCQQueryProcessor extends SMWQueryProcessor {
  11. /**
  12. * Handler for the #compound_query parser function.
  13. *
  14. * @param Parser $parser
  15. *
  16. * @return string
  17. */
  18. public static function doCompoundQuery( Parser &$parser ) {
  19. global $smwgQEnabled, $smwgIQRunningNumber;
  20. if ( !$smwgQEnabled ) {
  21. return smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
  22. }
  23. $smwgIQRunningNumber++;
  24. $params = func_get_args();
  25. array_shift( $params ); // We already know the $parser.
  26. $other_params = array();
  27. $results = array();
  28. foreach ( $params as $param ) {
  29. // Very primitive heuristic - if the parameter
  30. // includes a square bracket, then it's a
  31. // sub-query; otherwise it's a regular parameter.
  32. if ( strpos( $param, '[' ) !== false ) {
  33. $sub_params = self::getSubParams( $param );
  34. $next_result = self::getQueryResultFromFunctionParams( $sub_params, SMW_OUTPUT_WIKI );
  35. $results = self::mergeSMWQueryResults( $results, $next_result->getResults() );
  36. } else {
  37. $parts = explode( '=', $param, 2 );
  38. if ( count( $parts ) >= 2 ) {
  39. $other_params[strtolower( trim( $parts[0] ) )] = $parts[1]; // don't trim here, some params care for " "
  40. }
  41. }
  42. }
  43. $query_result = new SCQQueryResult( $next_result->getPrintRequests(), new SMWQuery(), $results, smwfGetStore() );
  44. return self::getResultFromQueryResult( $query_result, $other_params, SMW_OUTPUT_WIKI );
  45. }
  46. /**
  47. * An alternative to explode() - that function won't work here,
  48. * because we don't want to split the string on all semicolons, just
  49. * the ones that aren't contained within square brackets
  50. *
  51. * @param string $param
  52. *
  53. * @return array
  54. */
  55. protected static function getSubParams( $param ) {
  56. $sub_params = array();
  57. $sub_param = '';
  58. $uncompleted_square_brackets = 0;
  59. for ( $i = 0; $i < strlen( $param ); $i++ ) {
  60. $c = $param[$i];
  61. if ( ( $c == ';' ) && ( $uncompleted_square_brackets <= 0 ) ) {
  62. $sub_params[] = trim( $sub_param );
  63. $sub_param = '';
  64. } else {
  65. $sub_param .= $c;
  66. if ( $c == '[' ) {
  67. $uncompleted_square_brackets++;
  68. }
  69. elseif ( $c == ']' ) {
  70. $uncompleted_square_brackets--;
  71. }
  72. }
  73. }
  74. $sub_params[] = trim( $sub_param );
  75. return $sub_params;
  76. }
  77. /**
  78. * @param $rawparams
  79. * @param $outputmode
  80. * @param $context
  81. * @param $showmode
  82. *
  83. * @return SMWQueryResult
  84. */
  85. protected static function getQueryResultFromFunctionParams( $rawparams, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $showmode = false ) {
  86. self::processFunctionParams( $rawparams, $querystring, $params, $printouts, $showmode );
  87. return self::getQueryResultFromQueryString( $querystring, $params, $printouts, SMW_OUTPUT_WIKI, $context );
  88. }
  89. /**
  90. * Combine two arrays of SMWWikiPageValue objects into one
  91. *
  92. * @param array $result1
  93. * @param array $result2
  94. *
  95. * @return array
  96. */
  97. protected static function mergeSMWQueryResults( $result1, $result2 ) {
  98. if ( $result1 == null ) {
  99. return $result2;
  100. }
  101. $existing_page_names = array();
  102. foreach ( $result1 as $r1 ) {
  103. // SMW 1.6+
  104. if ( $r1 instanceof SMWDIWikiPage ) {
  105. $existing_page_names[] = $r1->getDBkey();
  106. } else {
  107. $existing_page_names[] = $r1->getWikiValue();
  108. }
  109. }
  110. foreach ( $result2 as $r2 ) {
  111. if ( $r1 instanceof SMWDIWikiPage ) {
  112. $page_name = $r2->getDBkey();
  113. } else {
  114. $page_name = $r2->getWikiValue();
  115. }
  116. if ( ! in_array( $page_name, $existing_page_names ) ) {
  117. $result1[] = $r2;
  118. }
  119. }
  120. return $result1;
  121. }
  122. /**
  123. * @param $querystring
  124. * @param array $params
  125. * @param $extraprintouts
  126. * @param $outputmode
  127. * @param $context
  128. *
  129. * @return SMWQueryResult
  130. */
  131. protected static function getQueryResultFromQueryString( $querystring, array $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY ) {
  132. wfProfileIn( 'SCQQueryProcessor::getQueryResultFromQueryString' );
  133. $query = self::createQuery( $querystring, $params, $context, null, $extraprintouts );
  134. $query_result = smwfGetStore()->getQueryResult( $query );
  135. foreach ( $query_result->getResults() as $wiki_page ) {
  136. $wiki_page->display_options = $params;
  137. }
  138. wfProfileOut( 'SCQQueryProcessor::getQueryResultFromQueryString' );
  139. return $query_result;
  140. }
  141. /**
  142. * Matches getResultFromQueryResult() from SMWQueryProcessor,
  143. * except that formats of type 'debug' and 'count' aren't handled.
  144. *
  145. * @param SCQQueryResult $res
  146. * @param array $params
  147. * @param $outputmode
  148. * @param $context
  149. * @param string $format
  150. *
  151. * @return string
  152. */
  153. protected static function getResultFromQueryResult( SCQQueryResult $res, array $params, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '' ) {
  154. wfProfileIn( 'SCQQueryProcessor::getResultFromQueryResult' );
  155. $format = self::getResultFormat( $params );
  156. $printer = self::getResultPrinter( $format, $context, $res );
  157. $result = $printer->getResult( $res, $params, $outputmode );
  158. wfProfileOut( 'SCQQueryProcessor::getResultFromQueryResult' );
  159. return $result;
  160. }
  161. }