Here is a patch to add printing of reverse relations in results. 

syntax is [[somerelation::-*|header]] (where '|header' is optional).

this will fetch all articles that have a relation to the current row

so: <ask>[[Category:Something]] [[foo::-*]]</ask> will return a table of all 
pages in category Something and in the next column all pages that refer to the 
current row's page with relation 'foo'.

diff -u -r /tmp/SemanticMediaWiki/includes/SMW_InlineQueries.php 
SemanticMediaWiki/includes/SMW_InlineQueries.php
--- /tmp/SemanticMediaWiki/includes/SMW_InlineQueries.php       2007-01-07 
10:39:35.000000000 +0200
+++ SemanticMediaWiki/includes/SMW_InlineQueries.php    2007-01-07 
12:10:36.000000000 +0200
@@ -118,6 +118,7 @@
 define('SMW_IQ_PRINT_ATTS', 2);  // print all attribute values of a certain 
attribute
 define('SMW_IQ_PRINT_RSEL', 3);  // print certain relation objects selected by 
the query
 define('SMW_IQ_PRINT_ASEL', 4);  // print certain attribute values selected by 
the query
+define('SMW_IQ_PRINT_REV_RELS', 5);  // print all reverse relations objects of 
a certain relation
 
 
 /**
@@ -422,6 +423,15 @@
                                                        'subject_id=' . 
$row['page_id'] . ' AND relation_title=' . 
$this->dbr->addQuotes($print_data[2]),
                                                        
'SMW::InlineQuery::Print' , $sql_params); 
                                return new 
SMWRelationIterator($this,$this->dbr,$res,$linked);
+                       case SMW_IQ_PRINT_REV_RELS:
+                               if ($smwgIQSortingEnabled)
+                                       $sql_params['ORDER BY'] = 
"subject_title $this->mOrder";
+                               $where = 'object_title=' . 
$this->dbr->addQuotes($row['page_title']) . ' AND relation_title=' . 
$this->dbr->addQuotes($print_data[2]);
+                               $res = $this->dbr->select( 
$this->dbr->tableName('smw_relations'),
+                                                       'DISTINCT 
subject_title,subject_namespace',
+                                                       $where,
+                                                       
'SMW::InlineQuery::Print' , $sql_params); 
+                               return new 
SMWRevRelationIterator($this,$this->dbr,$res,$linked);
                        case SMW_IQ_PRINT_ATTS:
                                if ($smwgIQSortingEnabled) {
                                        if ($print_data[3]->isNumeric()) {
@@ -572,7 +582,7 @@
                $result->mTables = $this->dbr->tableName('page') . " AS 
$pagetable";
 
                foreach ($queryparts as $q) {
-                       $qparts = preg_split("/(::|:=[><]?|^$cat_sep)/", $q, 2, 
PREG_SPLIT_DELIM_CAPTURE);
+                       $qparts = preg_split("/(::[-]?|:=[><]?|^$cat_sep)/", 
$q, 2, PREG_SPLIT_DELIM_CAPTURE);
                        if (count($qparts)<=2) { // $q was not something like 
"xxx:=yyy", probably a fixed subject
                                $qparts = array('','',$q); // this saves a lot 
of code below ;-)
                        }
@@ -594,6 +604,9 @@
                                        } elseif ( '::' == $op ) {
                                                $result->mPrint['R:' . 
$qparts[0]] = 
array($this->makeTitleString($wgContLang->getNsText(SMW_NS_RELATION) . ':' . 
$qparts[0],$label,true),
                                                
SMW_IQ_PRINT_RELS,smwfNormalTitleDBKey($qparts[0]));
+                                       } elseif ( '::-' == $op ) {
+                                               $result->mPrint['R:' . 
$qparts[0]] = 
array($this->makeTitleString($wgContLang->getNsText(SMW_NS_RELATION) . ':' . 
$qparts[0],$label,true),
+                                               
SMW_IQ_PRINT_REV_RELS,smwfNormalTitleDBKey($qparts[0]));
                                        } elseif ( ':=' == $op ) {
                                                $av = 
SMWDataValue::newAttributeValue($qparts[0]);
                                                $unit = mb_substr($qparts[2],1);
@@ -1001,6 +1014,32 @@
 }
 
 /**
+ * Iterator for relation objects, returned by some SQL query.
+ */
+class SMWRevRelationIterator {
+       private $mRes;
+       private $mDB;
+       private $mIQ;
+       private $mLinked;
+
+       public function SMWRevRelationIterator($iq, $db, $res, $linked) {
+               $this->mIQ = $iq;
+               $this->mDB = $db;
+               $this->mRes = $res;
+               $this->mLinked = $linked;
+       }
+
+       public function getNext() {
+               global $wgContLang;
+               $row = $this->mDB->fetchRow($this->mRes);
+               if ($row) {
+                       return 
array($this->mIQ->makeTitleString($wgContLang->getNsText($row['subject_namespace'])
 . ':' . $row['subject_title'],NULL,$this->mLinked));
+               } else  return false;
+       }
+}
+
+
+/**
  * Iterator wrapping a single string
  */
 class SMWFixedIterator {
diff -u -r /tmp/SemanticMediaWiki/includes/SMW_QueryPrinters.php 
SemanticMediaWiki/includes/SMW_QueryPrinters.php
--- /tmp/SemanticMediaWiki/includes/SMW_QueryPrinters.php       2007-01-07 
10:39:34.000000000 +0200
+++ SemanticMediaWiki/includes/SMW_QueryPrinters.php    2007-01-07 
11:55:10.000000000 +0200
@@ -307,4 +307,4 @@
        }
 }
 
-?>
\ No newline at end of file
+?>


-- 
===================================
Ittay Dror, 
Chief architect, 
R&D, Qlusters Inc.
[EMAIL PROTECTED]
+972-3-6081994 Fax: +972-3-6081841

www.openqrm.org - Data Center Provisioning

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Semediawiki-user mailing list
Semediawiki-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/semediawiki-user

Reply via email to