r75210 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75209‎ | r75210 | r75211 >
Date:18:42, 22 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added optional support for strict comparators
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/SMW_Settings.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -398,7 +398,7 @@
399399 * @param string $comparator
400400 */
401401 static protected function prepareValue( &$value, &$comparator ) {
402 - global $smwgQComparators;
 402+ global $smwgQComparators, $smwStrictComparators;
403403
404404 $list = preg_split( '/^(' . $smwgQComparators . ')/u', $value, 2, PREG_SPLIT_DELIM_CAPTURE );
405405 $comparator = SMW_CMP_EQ;
@@ -408,10 +408,10 @@
409409
410410 switch ( $list[1] ) {
411411 case '<':
412 - $comparator = SMW_CMP_LEQ;
 412+ $comparator = $smwStrictComparators ? SMW_CMP_LESS : SMW_CMP_LEQ;
413413 break;
414414 case '>':
415 - $comparator = SMW_CMP_GEQ;
 415+ $comparator = $smwStrictComparators ? SMW_CMP_GRTR : SMW_CMP_GEQ;
416416 break;
417417 case '!':
418418 $comparator = SMW_CMP_NEQ;
@@ -422,6 +422,12 @@
423423 case '!~':
424424 $comparator = SMW_CMP_NLKE;
425425 break;
 426+ case '>=':
 427+ $comparator = SMW_CMP_GEQ;
 428+ break;
 429+ case '<=':
 430+ $comparator = SMW_CMP_LEQ;
 431+ break;
426432 // default: not possible
427433 }
428434 }
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php
@@ -386,16 +386,21 @@
387387
388388 public function getQueryString( $asvalue = false ) {
389389 if ( $this->m_datavalue !== null ) {
 390+ global $smwStrictComparators;
 391+
390392 switch ( $this->m_comparator ) {
391 - case SMW_CMP_LEQ: $comparator = '<'; break;
392 - case SMW_CMP_GEQ: $comparator = '>'; break;
 393+ case SMW_CMP_LEQ: $comparator = $smwStrictComparators ? '<=' : '<'; break;
 394+ case SMW_CMP_GEQ: $comparator = $smwStrictComparators ? '>=' : '>'; break;
393395 case SMW_CMP_NEQ: $comparator = '!'; break;
394396 case SMW_CMP_LIKE: $comparator = '~'; break;
395397 case SMW_CMP_NLKE: $comparator = '!~'; break;
 398+ case SMW_CMP_LESS: $comparator = '<'; break;
 399+ case SMW_CMP_GRTR: $comparator = '>'; break;
396400 default: case SMW_CMP_EQ:
397401 $comparator = '';
398402 break;
399403 }
 404+
400405 if ( $asvalue ) {
401406 return $comparator . $this->m_datavalue->getWikiValue();
402407 } else { // this only is possible for values of Type:Page
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php
@@ -500,6 +500,8 @@
501501 switch ( $description->getComparator() ) {
502502 case SMW_CMP_LEQ: $comp = '<='; break;
503503 case SMW_CMP_GEQ: $comp = '>='; break;
 504+ case SMW_CMP_LESS: $comp = '<'; break;
 505+ case SMW_CMP_GRTR: $comp = '>'; break;
504506 case SMW_CMP_NEQ: $comp = '!='; break;
505507 case SMW_CMP_LIKE: case SMW_CMP_NLKE:
506508 $comp = ' LIKE ';
@@ -507,7 +509,6 @@
508510 $value = str_replace( array( '%', '_', '*', '?' ), array( '\%', '\_', '%', '_' ), $value );
509511 break;
510512 }
511 -
512513 $query->where = "$query->alias.smw_sortkey$comp" . $this->m_dbs->addQuotes( $value );
513514 }
514515 }
@@ -767,8 +768,10 @@
768769
769770 switch ( $description->getComparator() ) {
770771 case SMW_CMP_EQ: $comparator = '='; break;
 772+ case SMW_CMP_LESS: $comparator = '<'; break;
 773+ case SMW_CMP_GRTR: $comparator = '>'; break;
771774 case SMW_CMP_LEQ: $comparator = '<='; break;
772 - case SMW_CMP_GEQ: $comparator = '>='; break;
 775+ case SMW_CMP_GEQ: $comparator = '>='; break;
773776 case SMW_CMP_NEQ: $comparator = '!='; break;
774777 case SMW_CMP_LIKE: case SMW_CMP_NLKE:
775778 $comparator = ' LIKE ';
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -54,6 +54,8 @@
5555 define( 'SMW_CMP_NEQ', 4 ); // Matches only datavalues that are unequal to the given value.
5656 define( 'SMW_CMP_LIKE', 5 ); // Matches only datavalues that are LIKE the given value.
5757 define( 'SMW_CMP_NLKE', 6 ); // Matches only datavalues that are not LIKE the given value.
 58+define( 'SMW_CMP_LESS', 7 ); // Matches only datavalues that are less than the given value.
 59+define( 'SMW_CMP_GRTR', 8 ); // Matches only datavalues that are greater than the given value.
5860
5961 // Constants for date formats (using binary encoding of nine bits: 3 positions x 3 interpretations).
6062 define( 'SMW_MDY', 785 ); // Month-Day-Year
Index: trunk/extensions/SemanticMediaWiki/SMW_Settings.php
@@ -198,19 +198,28 @@
199199 * List of comparator characters supported by queries, separated by '|', for use in a regex.
200200 *
201201 * Available entries:
202 - * < (smaller than)
203 - * < (greater than)
 202+ * < (smaller than) if $smwStrictComparators is false, it's actually smaller than or equal to
 203+ * > (greater than) if $smwStrictComparators is false, it's actually bigger than or equal to
204204 * ! (unequal to)
205205 * ~ (pattern with '*' as wildcard, only for Type:String)
206206 * !~ (not a pattern with '*' as wildcard, only for Type:String, need to be placed before ! and ~ to work correctly)
 207+ * <= (smaller than or equal to)
 208+ * >= (greater than or equal to)
207209 *
208210 * If unsupported comparators are used, they are treated as part of the queried value
209211 *
210212 * @var string
211213 */
212 -$smwgQComparators = '<|>|!~|!|~';
 214+$smwgQComparators = '<|>|!~|!|~|<=|>=';
213215
214216 ###
 217+# Sets whether the > and < comparators should be strict or not. If they are strict,
 218+# values that are equal will not be accepted.
 219+##
 220+$smwStrictComparators = false;
 221+##
 222+
 223+###
215224 # Further settings for queries. The following settings affect inline queries
216225 # and querying special pages. Essentially they should mirror the kind of
217226 # queries that should immediately be answered by the wiki, using whatever

Status & tagging log