r71560 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71559‎ | r71560 | r71561 >
Date:14:49, 24 August 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Documentation and style improvements
Modified paths:
  • /trunk/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticCompoundQueries/SCQ_QueryResult.php (modified) (history)
  • /trunk/extensions/SemanticCompoundQueries/SemanticCompoundQueries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticCompoundQueries/SCQ_QueryResult.php
@@ -1,35 +1,43 @@
22 <?php
33
4 -if ( !defined( 'MEDIAWIKI' ) ) die();
5 -
64 /**
75 * Subclass of SMWQueryResult - this class was mostly created in order to
86 * get around an inconvenient print-request-compatibility check in
97 * SMWQueryResult::addRow()
108 *
119 * @ingroup SemanticCompoundQueries
 10+ *
1211 * @author Yaron Koren
1312 */
1413 class SCQQueryResult extends SMWQueryResult {
1514
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+
2124 while ( $row = $this->getNext() ) {
2225 if ( $row[0] instanceof SMWResultArray ) {
2326 $content = $row[0]->getContent();
24 - $existing_page_names[] = $content[0]->getLongText( SMW_OUTPUT_WIKI );
 27+ $existingPageNames[] = $content[0]->getLongText( SMW_OUTPUT_WIKI );
2528 }
2629 }
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;
2933 $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 ) ) {
3237 $this->m_content[] = $row;
 38+ }
3339 }
 40+
3441 reset( $this->m_content );
3542 }
36 -}
 43+
 44+}
\ No newline at end of file
Index: trunk/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php
@@ -1,53 +1,35 @@
22 <?php
33
4 -if ( !defined( 'MEDIAWIKI' ) ) die();
5 -
64 /**
75 * Class that holds static functions for handling compound queries.
86 * This class inherits from Semantic MediaWiki's SMWQueryProcessor.
97 *
108 * @ingroup SemanticCompoundQueries
 9+ *
1110 * @author Yaron Koren
1211 */
1312 class SCQQueryProcessor extends SMWQueryProcessor {
1413
1514 /**
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
1920 */
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 ) {
4422 global $smwgQEnabled, $smwgIQRunningNumber;
 23+
4524 if ( $smwgQEnabled ) {
4625 $smwgIQRunningNumber++;
 26+
4727 $params = func_get_args();
48 - array_shift( $params ); // we already know the $parser ...
 28+ array_shift( $params ); // We already know the $parser.
 29+
4930 $other_params = array();
5031 $query_result = null;
5132 $results = array();
 33+
5234 foreach ( $params as $param ) {
5335 // very primitive heuristic - if the parameter
5436 // includes a square bracket, then it's a
@@ -55,67 +37,143 @@
5638 if ( strpos( $param, '[' ) !== false ) {
5739 $sub_params = self::getSubParams( $param );
5840 $next_result = self::getQueryResultFromFunctionParams( $sub_params, SMW_OUTPUT_WIKI );
 41+
5942 if ( method_exists( $next_result, 'getResults' ) ) { // SMW 1.5+
6043 $results = self::mergeSMWQueryResults( $results, $next_result->getResults() );
6144 } else {
62 - if ( $query_result == null )
 45+ if ( $query_result == null ) {
6346 $query_result = new SCQQueryResult( $next_result->getPrintRequests(), new SMWQuery() );
 47+ }
 48+
6449 $query_result->addResult( $next_result );
6550 }
6651 } else {
6752 $parts = explode( '=', $param, 2 );
 53+
6854 if ( count( $parts ) >= 2 ) {
6955 $other_params[strtolower( trim( $parts[0] ) )] = $parts[1]; // don't trim here, some params care for " "
7056 }
7157 }
7258 }
73 - if ( is_null( $query_result ) ) // SMW 1.5+
 59+
 60+ // SMW 1.5+
 61+ if ( is_null( $query_result ) ) {
7462 $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 );
7666 } else {
7767 wfLoadExtensionMessages( 'SemanticMediaWiki' );
7868 $result = smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
7969 }
 70+
8071 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;
81110 }
82111
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 ) {
84121 self::processFunctionParams( $rawparams, $querystring, $params, $printouts, $showmode );
85122 return self::getQueryResultFromQueryString( $querystring, $params, $printouts, SMW_OUTPUT_WIKI, $context );
86123 }
87124
88125 /**
89126 * Combine two arrays of SMWWikiPageValue objects into one
 127+ *
 128+ * @param array $result1
 129+ * @param array $result2
 130+ *
 131+ * @return array
90132 */
91 - static function mergeSMWQueryResults( $result1, $result2 ) {
 133+ protected static function mergeSMWQueryResults( $result1, $result2 ) {
92134 if ( $result1 == null ) {
93135 return $result2;
94136 }
 137+
95138 $existing_page_names = array();
 139+
96140 foreach ( $result1 as $r1 ) {
97141 $existing_page_names[] = $r1->getWikiValue();
98142 }
 143+
99144 foreach ( $result2 as $r2 ) {
100145 $page_name = $r2->getWikiValue();
 146+
101147 if ( ! in_array( $page_name, $existing_page_names ) ) {
102148 $result1[] = $r2;
103149 }
104150 }
 151+
105152 return $result1;
106153 }
107154
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 ) {
109165 wfProfileIn( 'SCQQueryProcessor::getQueryResultFromQueryString' );
 166+
110167 $query = self::createQuery( $querystring, $params, $context, null, $extraprintouts );
111168 $query_result = smwfGetStore()->getQueryResult( $query );
112169 $display_options = array();
 170+
113171 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.
116173 if ( $key == 'icon' ) {
117174 $icon_title = Title::newFromText( $value );
118175 $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
120178 if ( method_exists( 'ImagePage', 'getDisplayedFile' ) ) {
121179 $icon_url = $icon_image_page->getDisplayedFile()->getURL();
122180 $display_options['icon'] = $icon_url;
@@ -123,6 +181,7 @@
124182 } else {
125183 $display_options[$key] = $value;
126184 }
 185+
127186 if ( method_exists( $query_result, 'getResults' ) ) { // SMW 1.5+
128187 foreach ( $query_result->getResults() as $wiki_page ) {
129188 $wiki_page->display_options = $display_options;
@@ -133,19 +192,32 @@
134193 }
135194
136195 wfProfileOut( 'SCQQueryProcessor::getQueryResultFromQueryString' );
 196+
137197 return $query_result;
138198 }
139 -
140 - /*
 199+
 200+ /**
141201 * 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
143211 */
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 = '' ) {
145213 wfProfileIn( 'SCQQueryProcessor::getResultFromQueryResult' );
 214+
146215 $format = self::getResultFormat( $params );
147216 $printer = self::getResultPrinter( $format, $context, $res );
148217 $result = $printer->getResult( $res, $params, $outputmode );
 218+
149219 wfProfileOut( 'SCQQueryProcessor::getResultFromQueryResult' );
 220+
150221 return $result;
151222 }
152 -}
 223+
 224+}
\ No newline at end of file
Index: trunk/extensions/SemanticCompoundQueries/SemanticCompoundQueries.php
@@ -1,12 +1,19 @@
22 <?php
33 /**
4 - * Initialization file for SemanticCompoundQueries
 4+ * Initialization file for the SemanticCompoundQueries extension.
55 *
6 - * @file
 6+ * @file SemanticCompoundQueries.php
77 * @ingroup SemanticCompoundQueries
 8+ *
89 * @author Yaron Koren
910 */
1011
 12+/**
 13+ * This documenation group collects source code files belonging to SemanticCompoundQueries.
 14+ *
 15+ * @defgroup SemanticCompoundQueries SemanticCompoundQueries
 16+ */
 17+
1118 if ( !defined( 'MEDIAWIKI' ) ) die();
1219
1320 define( 'SCQ_VERSION', '0.2.5' );
@@ -15,7 +22,7 @@
1623 'path' => __FILE__,
1724 'name' => 'Semantic Compound Queries',
1825 'version' => SCQ_VERSION,
19 - 'author' => 'Yaron Koren',
 26+ 'author' => array( 'Yaron Koren' ),
2027 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Compound_Queries',
2128 'descriptionmsg' => 'semanticcompoundqueries-desc',
2229 );
@@ -29,7 +36,7 @@
3037 $wgAutoloadClasses['SCQQueryProcessor'] = dirname( __FILE__ ) . '/SCQ_QueryProcessor.php';
3138 $wgAutoloadClasses['SCQQueryResult'] = dirname( __FILE__ ) . '/SCQ_QueryResult.php';
3239
33 -function scqgRegisterParser( &$parser ) {
 40+function scqgRegisterParser( Parser &$parser ) {
3441 $parser->setFunctionHook( 'compound_query', array( 'SCQQueryProcessor', 'doCompoundQuery' ) );
3542 return true; // always return true, in order not to stop MW's hook processing!
3643 }

Status & tagging log