Yesterday I needed LIKE queries for properties, so I added it to SMW
(patch attached). It was surprisingly simple.

This patch adds a new comparator, %, to the already existing <, > and !.
So you can say [[has capital::%A%]] and it will return all pages that
have a property "has capital", with a value starting with "A".

Notes:
* The first % tells SMW that the following is to be interpreted as a
  parameter to LIKE. So if you want to get all pages with capitals that
  have an A in them, you'd have to say [[has capital::%%A%]]
* NOT LIKE would be trivial to add, I just didn't need it and couldn't
  decide on the character to use (maybe § or &?)
* Probably it should be possible to disable LIKE queries, as they could
  be quite expensive.
* I saw that there is some (planned?) support for LIKE queries already,
  but it looked like it was not applicable to {{#ask}}, so I didn't
  reuse it.

Thomas
--- a/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
+++ b/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -739,7 +739,7 @@ class SMWQueryParser {
 		if ($value == '*') { // printout statement
 			return;
 		}
-		$list = preg_split('/^(<|>|!)/',$value, 2, PREG_SPLIT_DELIM_CAPTURE);
+		$list = preg_split('/^(<|>|!|%)/',$value, 2, PREG_SPLIT_DELIM_CAPTURE);
 		$comparator = SMW_CMP_EQ;
 		if (count($list) == 3) { // initial comparator found ($list[1] should be empty)
 			switch ($list[1]) {
@@ -755,6 +755,10 @@ class SMWQueryParser {
 					$comparator = SMW_CMP_NEQ;
 					$value = $list[2];
 				break;
+				case '%':
+					$comparator = SMW_CMP_LIKE;
+					$value = $list[2];
+				break;
 				//default: not possible
 			}
 		}
--- a/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php
+++ b/extensions/SemanticMediaWiki/includes/storage/SMW_Description.php
@@ -11,6 +11,7 @@ define('SMW_CMP_EQ',1); // matches only datavalues that are equal to the given v
 define('SMW_CMP_LEQ',2); // matches only datavalues that are less or equal than the given value
 define('SMW_CMP_GEQ',3); // matches only datavalues that are greater or equal to the given value
 define('SMW_CMP_NEQ',4); // matches only datavalues that are unequal to the given value
+define('SMW_CMP_LIKE',5); // matches only datavalues that are LIKE the given value
 
 // print request
 define('SMW_PRINT_CATS', 0);  // print all direct cateories of the current element
@@ -392,6 +393,9 @@ class SMWValueDescription extends SMWDescription {
 				case SMW_CMP_NEQ: 
 					$comparator = '!'; // not supported yet?
 				break;
+				case SMW_CMP_LIKE: 
+					$comparator = '%'; // not supported yet?
+				break;
 				default: case SMW_CMP_EQ: 
 					$comparator = '';
 				break;
--- a/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
+++ b/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -1668,6 +1668,7 @@ class SMWSQLStore extends SMWStore {
 							case SMW_CMP_LEQ: $op = '<='; break;
 							case SMW_CMP_GEQ: $op = '>='; break;
 							case SMW_CMP_NEQ: $op = '!='; break;
+							case SMW_CMP_LIKE: $op = ' LIKE '; break;
 							case SMW_CMP_EQ: default: $op = '='; break;
 						}
 						if ($description->getDatavalue()->isNumeric()) {

Attachment: signature.asc
Description: Digital signature

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Semediawiki-devel mailing list
Semediawiki-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel

Reply via email to