hi,
I wrote yesterday a small patch for SMW. It allows to sort query results
randomly.
Creative Commons needed this feature for its Casestudies project.

Just write "|sort=random" in the {{#ask}} params.

It's compatible with other sorting, which means you can sort ascending by
one property and then sort randomly by an other property.
example:
{{#ask: [[test::+]][[check::+]]
|? |? test |? check
|sort = test, check
|order = asc, rand
|limit = 3
}}

It uses the MySQL RAND() function [1], which may not be very efficient on a
large number of row [2].
I hope this will be useful. Feel free to add this patch to the SMW
development repository if you want.

Steren

[1] :
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
[2] : http://forums.mysql.com/read.php?24,163940,163940
Index: includes/storage/SMW_SQLStore2_Queries.php
===================================================================
--- includes/storage/SMW_SQLStore2_Queries.php	(révision 38017)
+++ includes/storage/SMW_SQLStore2_Queries.php	(copie de travail)
@@ -699,7 +699,7 @@
 	 * Get a SQL option array for the given query and preprocessed query object at given id.
 	 */
 	protected function getSQLOptions($query,$rootid) {
-		global $smwgQSortingSupport;
+		global $smwgQSortingSupport, $smwgQRandSortingSupport;
 		$result = array( 'LIMIT' => $query->getLimit() + 1, 'OFFSET' => $query->getOffset() );
 		// build ORDER BY options using discovered sorting fields:
 		if ($smwgQSortingSupport) {
@@ -711,11 +711,16 @@
 					} else {
 						$result['ORDER BY'] .= ', ';
 					}
-					$result['ORDER BY'] .= $qobj->sortfields[$propkey] . " $order ";
+                    if ('RAND()' == $order){
+					    $result['ORDER BY'] .= " $order ";
+                    } else {
+                    	$result['ORDER BY'] .= $qobj->sortfields[$propkey] . " $order ";
+                    }
+
 				}
 			}
 		}
 		return $result;
 	}
 
-}
\ No newline at end of file
+}
Index: includes/SMW_QueryProcessor.php
===================================================================
--- includes/SMW_QueryProcessor.php	(révision 38017)
+++ includes/SMW_QueryProcessor.php	(copie de travail)
@@ -125,10 +125,12 @@
 			$orders = explode( ',', $params['order'] );
 			foreach ($orders as $key => $order) { // normalise
 				$order = strtolower(trim($order));
-				if ( ('descending' != $order) && ('reverse' != $order) && ('desc' != $order) ) {
+				if ( ('descending' == $order) || ('reverse' == $order) || ('desc' == $order) ) {
+					$orders[$key] = 'DESC';
+				} elseif ( ('random' == $order) || ('rand' == $order) ) {
+					$orders[$key] = 'RAND()';
+				} else {
 					$orders[$key] = 'ASC';
-				} else {
-					$orders[$key] = 'DESC';
 				}
 			}
 		} else {
Index: includes/SMW_Settings.php
===================================================================
--- includes/SMW_Settings.php	(révision 38017)
+++ includes/SMW_Settings.php	(copie de travail)
@@ -126,6 +126,7 @@
   //$smwgQEqualitySupport = SMW_EQ_FULL; // Evaluate #redirects as equality between page names in all cases
   //$smwgQEqualitySupport = SMW_EQ_NONE; // Never evaluate #redirects as equality between page names
 $smwgQSortingSupport    = true; // (De)activate sorting of results.
+$smwgQRandSortingSupport= true; // (De)activate random sorting of results.
 $smwgQDefaultNamespaces = NULL; // Which namespaces should be searched by default?
                                 // (value NULL switches off default restrictions on searching -- this is faster)
                                 // Example with namespaces: $smwgQDefaultNamespaces = array(NS_MAIN, NS_IMAGE);
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Semediawiki-devel mailing list
Semediawiki-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel

Reply via email to