Index: trunk/extensions/SemanticCompoundQueries/SCQ_QueryResult.php |
— | — | @@ -1,35 +1,43 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -if ( !defined( 'MEDIAWIKI' ) ) die(); |
5 | | - |
6 | 4 | /** |
7 | 5 | * Subclass of SMWQueryResult - this class was mostly created in order to |
8 | 6 | * get around an inconvenient print-request-compatibility check in |
9 | 7 | * SMWQueryResult::addRow() |
10 | 8 | * |
11 | 9 | * @ingroup SemanticCompoundQueries |
| 10 | + * |
12 | 11 | * @author Yaron Koren |
13 | 12 | */ |
14 | 13 | class SCQQueryResult extends SMWQueryResult { |
15 | 14 | |
16 | | - function addResult( $new_result ) { |
17 | | - // create an array of the pages already in this query result, |
18 | | - // so that we can check against it to make sure that pages |
19 | | - // aren't included twice |
20 | | - $existing_page_names = array(); |
| 15 | + /** |
| 16 | + * Adds a result, and ensures it's uniqueness by building a |
| 17 | + * list of pages already in the query result first. |
| 18 | + * |
| 19 | + * @param SMWQueryResult $new_result |
| 20 | + */ |
| 21 | + public function addResult( SMWQueryResult $newResult ) { |
| 22 | + $existingPageNames = array(); |
| 23 | + |
21 | 24 | while ( $row = $this->getNext() ) { |
22 | 25 | if ( $row[0] instanceof SMWResultArray ) { |
23 | 26 | $content = $row[0]->getContent(); |
24 | | - $existing_page_names[] = $content[0]->getLongText( SMW_OUTPUT_WIKI ); |
| 27 | + $existingPageNames[] = $content[0]->getLongText( SMW_OUTPUT_WIKI ); |
25 | 28 | } |
26 | 29 | } |
27 | | - while ( ( $row = $new_result->getNext() ) !== false ) { |
28 | | - $row[0]->display_options = $new_result->display_options; |
| 30 | + |
| 31 | + while ( ( $row = $newResult->getNext() ) !== false ) { |
| 32 | + $row[0]->display_options = $newResult->display_options; |
29 | 33 | $content = $row[0]->getContent(); |
30 | | - $page_name = $content[0]->getLongText( SMW_OUTPUT_WIKI ); |
31 | | - if ( ! in_array( $page_name, $existing_page_names ) ) |
| 34 | + $pageName = $content[0]->getLongText( SMW_OUTPUT_WIKI ); |
| 35 | + |
| 36 | + if ( !in_array( $pageName, $existingPageNames ) ) { |
32 | 37 | $this->m_content[] = $row; |
| 38 | + } |
33 | 39 | } |
| 40 | + |
34 | 41 | reset( $this->m_content ); |
35 | 42 | } |
36 | | -} |
| 43 | + |
| 44 | +} |
\ No newline at end of file |
Index: trunk/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php |
— | — | @@ -1,53 +1,35 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -if ( !defined( 'MEDIAWIKI' ) ) die(); |
5 | | - |
6 | 4 | /** |
7 | 5 | * Class that holds static functions for handling compound queries. |
8 | 6 | * This class inherits from Semantic MediaWiki's SMWQueryProcessor. |
9 | 7 | * |
10 | 8 | * @ingroup SemanticCompoundQueries |
| 9 | + * |
11 | 10 | * @author Yaron Koren |
12 | 11 | */ |
13 | 12 | class SCQQueryProcessor extends SMWQueryProcessor { |
14 | 13 | |
15 | 14 | /** |
16 | | - * An alternative to explode() - that function won't work here, |
17 | | - * because we don't want to split the string on all semicolons, just |
18 | | - * the ones that aren't contained within square brackets |
| 15 | + * Handler for the #compound_query parser function. |
| 16 | + * |
| 17 | + * @param Parser $parser |
| 18 | + * |
| 19 | + * @return string |
19 | 20 | */ |
20 | | - public static function getSubParams( $param ) { |
21 | | - $sub_params = array(); |
22 | | - $sub_param = ""; |
23 | | - $uncompleted_square_brackets = 0; |
24 | | - for ( $i = 0; $i < strlen( $param ); $i++ ) { |
25 | | - $c = $param[$i]; |
26 | | - if ( ( $c == ';' ) && ( $uncompleted_square_brackets <= 0 ) ) { |
27 | | - $sub_params[] = trim( $sub_param ); |
28 | | - $sub_param = ""; |
29 | | - } else { |
30 | | - $sub_param .= $c; |
31 | | - if ( $c == '[' ) |
32 | | - $uncompleted_square_brackets++; |
33 | | - elseif ( $c == ']' ) |
34 | | - $uncompleted_square_brackets--; |
35 | | - } |
36 | | - } |
37 | | - $sub_params[] = trim( $sub_param ); |
38 | | - return $sub_params; |
39 | | - } |
40 | | - |
41 | | - /** |
42 | | - */ |
43 | | - public static function doCompoundQuery( &$parser ) { |
| 21 | + public static function doCompoundQuery( Parser &$parser ) { |
44 | 22 | global $smwgQEnabled, $smwgIQRunningNumber; |
| 23 | + |
45 | 24 | if ( $smwgQEnabled ) { |
46 | 25 | $smwgIQRunningNumber++; |
| 26 | + |
47 | 27 | $params = func_get_args(); |
48 | | - array_shift( $params ); // we already know the $parser ... |
| 28 | + array_shift( $params ); // We already know the $parser. |
| 29 | + |
49 | 30 | $other_params = array(); |
50 | 31 | $query_result = null; |
51 | 32 | $results = array(); |
| 33 | + |
52 | 34 | foreach ( $params as $param ) { |
53 | 35 | // very primitive heuristic - if the parameter |
54 | 36 | // includes a square bracket, then it's a |
— | — | @@ -55,67 +37,143 @@ |
56 | 38 | if ( strpos( $param, '[' ) !== false ) { |
57 | 39 | $sub_params = self::getSubParams( $param ); |
58 | 40 | $next_result = self::getQueryResultFromFunctionParams( $sub_params, SMW_OUTPUT_WIKI ); |
| 41 | + |
59 | 42 | if ( method_exists( $next_result, 'getResults' ) ) { // SMW 1.5+ |
60 | 43 | $results = self::mergeSMWQueryResults( $results, $next_result->getResults() ); |
61 | 44 | } else { |
62 | | - if ( $query_result == null ) |
| 45 | + if ( $query_result == null ) { |
63 | 46 | $query_result = new SCQQueryResult( $next_result->getPrintRequests(), new SMWQuery() ); |
| 47 | + } |
| 48 | + |
64 | 49 | $query_result->addResult( $next_result ); |
65 | 50 | } |
66 | 51 | } else { |
67 | 52 | $parts = explode( '=', $param, 2 ); |
| 53 | + |
68 | 54 | if ( count( $parts ) >= 2 ) { |
69 | 55 | $other_params[strtolower( trim( $parts[0] ) )] = $parts[1]; // don't trim here, some params care for " " |
70 | 56 | } |
71 | 57 | } |
72 | 58 | } |
73 | | - if ( is_null( $query_result ) ) // SMW 1.5+ |
| 59 | + |
| 60 | + // SMW 1.5+ |
| 61 | + if ( is_null( $query_result ) ) { |
74 | 62 | $query_result = new SCQQueryResult( $next_result->getPrintRequests(), new SMWQuery(), $results, smwfGetStore() ); |
75 | | - $result = SCQQueryProcessor::getResultFromQueryResult( $query_result, $other_params, null, SMW_OUTPUT_WIKI ); |
| 63 | + } |
| 64 | + |
| 65 | + $result = self::getResultFromQueryResult( $query_result, $other_params, SMW_OUTPUT_WIKI ); |
76 | 66 | } else { |
77 | 67 | wfLoadExtensionMessages( 'SemanticMediaWiki' ); |
78 | 68 | $result = smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) ); |
79 | 69 | } |
| 70 | + |
80 | 71 | return $result; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * An alternative to explode() - that function won't work here, |
| 76 | + * because we don't want to split the string on all semicolons, just |
| 77 | + * the ones that aren't contained within square brackets |
| 78 | + * |
| 79 | + * @param string $param |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + protected static function getSubParams( $param ) { |
| 84 | + $sub_params = array(); |
| 85 | + $sub_param = ''; |
| 86 | + $uncompleted_square_brackets = 0; |
| 87 | + |
| 88 | + for ( $i = 0; $i < strlen( $param ); $i++ ) { |
| 89 | + $c = $param[$i]; |
| 90 | + |
| 91 | + if ( ( $c == ';' ) && ( $uncompleted_square_brackets <= 0 ) ) { |
| 92 | + $sub_params[] = trim( $sub_param ); |
| 93 | + $sub_param = ''; |
| 94 | + } else { |
| 95 | + $sub_param .= $c; |
| 96 | + |
| 97 | + if ( $c == '[' ) { |
| 98 | + $uncompleted_square_brackets++; |
| 99 | + } |
| 100 | + |
| 101 | + elseif ( $c == ']' ) { |
| 102 | + $uncompleted_square_brackets--; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + $sub_params[] = trim( $sub_param ); |
| 108 | + |
| 109 | + return $sub_params; |
81 | 110 | } |
82 | 111 | |
83 | | - static function getQueryResultFromFunctionParams( $rawparams, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $showmode = false ) { |
| 112 | + /** |
| 113 | + * @param $rawparams |
| 114 | + * @param $outputmode |
| 115 | + * @param $context |
| 116 | + * @param $showmode |
| 117 | + * |
| 118 | + * @return SMWQueryResult |
| 119 | + */ |
| 120 | + protected static function getQueryResultFromFunctionParams( $rawparams, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $showmode = false ) { |
84 | 121 | self::processFunctionParams( $rawparams, $querystring, $params, $printouts, $showmode ); |
85 | 122 | return self::getQueryResultFromQueryString( $querystring, $params, $printouts, SMW_OUTPUT_WIKI, $context ); |
86 | 123 | } |
87 | 124 | |
88 | 125 | /** |
89 | 126 | * Combine two arrays of SMWWikiPageValue objects into one |
| 127 | + * |
| 128 | + * @param array $result1 |
| 129 | + * @param array $result2 |
| 130 | + * |
| 131 | + * @return array |
90 | 132 | */ |
91 | | - static function mergeSMWQueryResults( $result1, $result2 ) { |
| 133 | + protected static function mergeSMWQueryResults( $result1, $result2 ) { |
92 | 134 | if ( $result1 == null ) { |
93 | 135 | return $result2; |
94 | 136 | } |
| 137 | + |
95 | 138 | $existing_page_names = array(); |
| 139 | + |
96 | 140 | foreach ( $result1 as $r1 ) { |
97 | 141 | $existing_page_names[] = $r1->getWikiValue(); |
98 | 142 | } |
| 143 | + |
99 | 144 | foreach ( $result2 as $r2 ) { |
100 | 145 | $page_name = $r2->getWikiValue(); |
| 146 | + |
101 | 147 | if ( ! in_array( $page_name, $existing_page_names ) ) { |
102 | 148 | $result1[] = $r2; |
103 | 149 | } |
104 | 150 | } |
| 151 | + |
105 | 152 | return $result1; |
106 | 153 | } |
107 | 154 | |
108 | | - static function getQueryResultFromQueryString( $querystring, $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY ) { |
| 155 | + /** |
| 156 | + * @param $querystring |
| 157 | + * @param array $params |
| 158 | + * @param $extraprintouts |
| 159 | + * @param $outputmode |
| 160 | + * @param $context |
| 161 | + * |
| 162 | + * @return SMWQueryResult |
| 163 | + */ |
| 164 | + protected static function getQueryResultFromQueryString( $querystring, array $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY ) { |
109 | 165 | wfProfileIn( 'SCQQueryProcessor::getQueryResultFromQueryString' ); |
| 166 | + |
110 | 167 | $query = self::createQuery( $querystring, $params, $context, null, $extraprintouts ); |
111 | 168 | $query_result = smwfGetStore()->getQueryResult( $query ); |
112 | 169 | $display_options = array(); |
| 170 | + |
113 | 171 | foreach ( $params as $key => $value ) { |
114 | | - // special handling for 'icon' field, since it requires |
115 | | - // conversion of a name to a URL |
| 172 | + // Special handling for 'icon' field, since it requires conversion of a name to a URL. |
116 | 173 | if ( $key == 'icon' ) { |
117 | 174 | $icon_title = Title::newFromText( $value ); |
118 | 175 | $icon_image_page = new ImagePage( $icon_title ); |
119 | | - // method was only added in MW 1.13 |
| 176 | + |
| 177 | + // Method was only added in MW 1.13 |
120 | 178 | if ( method_exists( 'ImagePage', 'getDisplayedFile' ) ) { |
121 | 179 | $icon_url = $icon_image_page->getDisplayedFile()->getURL(); |
122 | 180 | $display_options['icon'] = $icon_url; |
— | — | @@ -123,6 +181,7 @@ |
124 | 182 | } else { |
125 | 183 | $display_options[$key] = $value; |
126 | 184 | } |
| 185 | + |
127 | 186 | if ( method_exists( $query_result, 'getResults' ) ) { // SMW 1.5+ |
128 | 187 | foreach ( $query_result->getResults() as $wiki_page ) { |
129 | 188 | $wiki_page->display_options = $display_options; |
— | — | @@ -133,19 +192,32 @@ |
134 | 193 | } |
135 | 194 | |
136 | 195 | wfProfileOut( 'SCQQueryProcessor::getQueryResultFromQueryString' ); |
| 196 | + |
137 | 197 | return $query_result; |
138 | 198 | } |
139 | | - |
140 | | - /* |
| 199 | + |
| 200 | + /** |
141 | 201 | * Matches getResultFromQueryResult() from SMWQueryProcessor, |
142 | | - * except that formats of type 'debug' and 'count' aren't handled |
| 202 | + * except that formats of type 'debug' and 'count' aren't handled. |
| 203 | + * |
| 204 | + * @param SCQQueryResult $res |
| 205 | + * @param array $params |
| 206 | + * @param $outputmode |
| 207 | + * @param $context |
| 208 | + * @param string $format |
| 209 | + * |
| 210 | + * @return string |
143 | 211 | */ |
144 | | - static function getResultFromQueryResult( $res, $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '' ) { |
| 212 | + protected static function getResultFromQueryResult( SCQQueryResult $res, array $params, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '' ) { |
145 | 213 | wfProfileIn( 'SCQQueryProcessor::getResultFromQueryResult' ); |
| 214 | + |
146 | 215 | $format = self::getResultFormat( $params ); |
147 | 216 | $printer = self::getResultPrinter( $format, $context, $res ); |
148 | 217 | $result = $printer->getResult( $res, $params, $outputmode ); |
| 218 | + |
149 | 219 | wfProfileOut( 'SCQQueryProcessor::getResultFromQueryResult' ); |
| 220 | + |
150 | 221 | return $result; |
151 | 222 | } |
152 | | -} |
| 223 | + |
| 224 | +} |
\ No newline at end of file |
Index: trunk/extensions/SemanticCompoundQueries/SemanticCompoundQueries.php |
— | — | @@ -1,12 +1,19 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Initialization file for SemanticCompoundQueries |
| 4 | + * Initialization file for the SemanticCompoundQueries extension. |
5 | 5 | * |
6 | | - * @file |
| 6 | + * @file SemanticCompoundQueries.php |
7 | 7 | * @ingroup SemanticCompoundQueries |
| 8 | + * |
8 | 9 | * @author Yaron Koren |
9 | 10 | */ |
10 | 11 | |
| 12 | +/** |
| 13 | + * This documenation group collects source code files belonging to SemanticCompoundQueries. |
| 14 | + * |
| 15 | + * @defgroup SemanticCompoundQueries SemanticCompoundQueries |
| 16 | + */ |
| 17 | + |
11 | 18 | if ( !defined( 'MEDIAWIKI' ) ) die(); |
12 | 19 | |
13 | 20 | define( 'SCQ_VERSION', '0.2.5' ); |
— | — | @@ -15,7 +22,7 @@ |
16 | 23 | 'path' => __FILE__, |
17 | 24 | 'name' => 'Semantic Compound Queries', |
18 | 25 | 'version' => SCQ_VERSION, |
19 | | - 'author' => 'Yaron Koren', |
| 26 | + 'author' => array( 'Yaron Koren' ), |
20 | 27 | 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Compound_Queries', |
21 | 28 | 'descriptionmsg' => 'semanticcompoundqueries-desc', |
22 | 29 | ); |
— | — | @@ -29,7 +36,7 @@ |
30 | 37 | $wgAutoloadClasses['SCQQueryProcessor'] = dirname( __FILE__ ) . '/SCQ_QueryProcessor.php'; |
31 | 38 | $wgAutoloadClasses['SCQQueryResult'] = dirname( __FILE__ ) . '/SCQ_QueryResult.php'; |
32 | 39 | |
33 | | -function scqgRegisterParser( &$parser ) { |
| 40 | +function scqgRegisterParser( Parser &$parser ) { |
34 | 41 | $parser->setFunctionHook( 'compound_query', array( 'SCQQueryProcessor', 'doCompoundQuery' ) ); |
35 | 42 | return true; // always return true, in order not to stop MW's hook processing! |
36 | 43 | } |