]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SemanticRelations.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / SemanticRelations.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2005 Reini Urban
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * SemanticRelations - Display the list of relations and attributes of given page(s).
25  * Relations are stored in the link table.
26  * Attributes as simple page meta-data.
27  *
28  * @author: Reini Urban
29  * @see WikiPlugin_SemanticSearch
30  */
31 class WikiPlugin_SemanticRelations
32 extends WikiPlugin
33 {
34     function getName() {
35         return _("SemanticRelations");
36     }
37     function getDescription() {
38         return _("Display the list of relations and attributes on this page.");
39     }
40     function getDefaultArguments() {
41         return array(
42                      'page'       => "[pagename]", // which pages (glob allowed), default: current
43                      'relations'  => '', // which relations. default all
44                      'attributes' => '', // which attributes. default all
45                      'units'      => '', // ?
46                      'noheader'   => false,
47                      'nohelp'     => false
48                      );
49     }
50     function run ($dbi, $argstr, &$request, $basepage) {
51         global $WikiTheme;
52         $args = $this->getArgs($argstr, $request);
53         extract($args);
54         if (empty($page))
55             $page = $request->getArg('pagename');
56         $relhtml = HTML();
57         if ($args['relations'] != '') {
58             $relfilter = explode(",", $args['relations']);
59         } else
60             $relfilter = array();
61         if ($args['attributes'] != '') {
62             $attfilter = explode(",", $args['attributes']);
63         } else
64             $attfilter = array();
65         foreach (explodePageList($page) as $pagename) {
66             $p = $dbi->getPage($pagename);
67             if ($args['relations'] != '0') {
68               $links = $p->getRelations(); // iter of pagelinks
69               // TODO: merge same relations together located_in::here, located_in::there
70               while ($object = $links->next()) {
71                 if ($related = $object->get('linkrelation')) { // a page name
72                     if ($relfilter and !in_array($related, $relfilter)) {
73                              continue;
74                     }
75                     $rellink = WikiLink($related, false, $related);
76                     $rellink->setAttr('class', $rellink->getAttr('class').' relation');
77                     $relhtml->pushContent
78                         ($pagename . " ",
79                          // Link to a special "Relation:" InterWiki link?
80                          $rellink,
81                          HTML::span(array('class'=>'relation-symbol'), "::"), // use spaces?
82                          WikiLink($object->_pagename),
83                          " ",
84                          // Link to SemanticSearch
85                          $WikiTheme->makeActionButton(array('relation' => $related,
86                                                             's'   => $object->_pagename),
87                                                       '+',
88                                                       _("SemanticSearch")),
89                          (count($relfilter) > 3 ? HTML::br() : " "));
90                 }
91               }
92               if (!empty($relhtml->_content) and !$noheader)
93                   $relhtml = HTML(HTML::hr(),
94                                   HTML::h3(fmt("Semantic relations for %s", $pagename)),
95                                   $relhtml);
96             }
97             $atthtml = HTML();
98             if ($args['attributes'] != '0') {
99               if ($attributes = $p->get('attributes')) { // a hash of unique pairs
100                 foreach ($attributes as $att => $val) {
101                     if ($attfilter and !in_array($att, $attfilter)) continue;
102                     $rellink = WikiLink($att, false, $att);
103                     $rellink->setAttr('class', $rellink->getAttr('class').' relation');
104                     $searchlink = $WikiTheme->makeActionButton
105                         (array('attribute' => $att,
106                                's'         => $val),
107                          $val,
108                          _("SemanticSearch"));
109                     $searchlink->setAttr('class', $searchlink->getAttr('class').' attribute');
110                     if (!$noheader)
111                         $atthtml->pushContent("$pagename  ");
112                     $atthtml->pushContent(HTML::span(array('class' => 'attribute '.$att),
113                                                      $rellink,
114                                                      HTML::span(array('class'=>'relation-symbol'),
115                                                                 ":="),
116                                                      $searchlink),
117                                           (count($attfilter) > 3 ? HTML::br() : " "));
118                 }
119                 if (!$noheader)
120                     $relhtml = HTML($relhtml,
121                                     HTML::hr(),
122                                     HTML::h3(fmt("Attributes of %s", $pagename)),
123                                     $atthtml);
124                 else
125                     $relhtml = HTML($relhtml, $atthtml);
126              }
127            }
128         }
129         if ($nohelp) return $relhtml;
130         return HTML($relhtml,
131                     HTML::hr(),
132                     WikiLink(_("Help/SemanticRelations"), false,
133                              HTML::em(_("Help/SemanticRelations"))),
134                     " - ",
135                     HTML::em(_("Find out how to add relations and attributes to pages.")));
136     }
137 };
138
139 // Local Variables:
140 // mode: php
141 // tab-width: 8
142 // c-basic-offset: 4
143 // c-hanging-comment-ender-p: nil
144 // indent-tabs-mode: nil
145 // End:
146 ?>