]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SemanticRelations.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / SemanticRelations.php
1 <?php
2
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 getDescription()
35     {
36         return _("Display the list of relations and attributes on this page.");
37     }
38
39     function getDefaultArguments()
40     {
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
51     /**
52      * @param WikiDB $dbi
53      * @param string $argstr
54      * @param WikiRequest $request
55      * @param string $basepage
56      * @return mixed
57      */
58     function run($dbi, $argstr, &$request, $basepage)
59     {
60         global $WikiTheme;
61         $args = $this->getArgs($argstr, $request);
62         extract($args);
63         if (empty($page))
64             $page = $request->getArg('pagename');
65         $relhtml = HTML();
66         if ($args['relations'] != '') {
67             $relfilter = explode(",", $args['relations']);
68         } else
69             $relfilter = array();
70         if ($args['attributes'] != '') {
71             $attfilter = explode(",", $args['attributes']);
72         } else
73             $attfilter = array();
74         foreach (explodePageList($page) as $pagename) {
75             $p = $dbi->getPage($pagename);
76             if ($args['relations'] != '0') {
77                 $links = $p->getRelations(); // iter of pagelinks
78                 // TODO: merge same relations together located_in::here, located_in::there
79                 while ($object = $links->next()) {
80                     if ($related = $object->get('linkrelation')) { // a page name
81                         if ($relfilter and !in_array($related, $relfilter)) {
82                             continue;
83                         }
84                         $rellink = WikiLink($related, false, $related);
85                         $rellink->setAttr('class', $rellink->getAttr('class') . ' relation');
86                         $relhtml->pushContent
87                         ($pagename . " ",
88                             // Link to a special "Relation:" InterWiki link?
89                             $rellink,
90                             HTML::span(array('class' => 'relation-symbol'), "::"), // use spaces?
91                             WikiLink($object->_pagename),
92                             " ",
93                             // Link to SemanticSearch
94                             $WikiTheme->makeActionButton(array('relation' => $related,
95                                     's' => $object->_pagename),
96                                 '+',
97                                 _("SemanticSearch")),
98                             (count($relfilter) > 3 ? HTML::br() : " "));
99                     }
100                 }
101                 if (!empty($relhtml->_content) and !$noheader)
102                     $relhtml = HTML(HTML::hr(),
103                         HTML::h3(fmt("Semantic relations for %s", $pagename)),
104                         $relhtml);
105             }
106             $atthtml = HTML();
107             if ($args['attributes'] != '0') {
108                 if ($attributes = $p->get('attributes')) { // a hash of unique pairs
109                     foreach ($attributes as $att => $val) {
110                         if ($attfilter and !in_array($att, $attfilter)) continue;
111                         $rellink = WikiLink($att, false, $att);
112                         $rellink->setAttr('class', $rellink->getAttr('class') . ' relation');
113                         $searchlink = $WikiTheme->makeActionButton
114                         (array('attribute' => $att,
115                                 's' => $val),
116                             $val,
117                             _("SemanticSearch"));
118                         $searchlink->setAttr('class', $searchlink->getAttr('class') . ' attribute');
119                         if (!$noheader)
120                             $atthtml->pushContent("$pagename  ");
121                         $atthtml->pushContent(HTML::span(array('class' => 'attribute ' . $att),
122                                 $rellink,
123                                 HTML::span(array('class' => 'relation-symbol'),
124                                     ":="),
125                                 $searchlink),
126                             (count($attfilter) > 3 ? HTML::br() : " "));
127                     }
128                     if (!$noheader)
129                         $relhtml = HTML($relhtml,
130                             HTML::hr(),
131                             HTML::h3(fmt("Attributes of %s", $pagename)),
132                             $atthtml);
133                     else
134                         $relhtml = HTML($relhtml, $atthtml);
135                 }
136             }
137         }
138         if ($nohelp) return $relhtml;
139         return HTML($relhtml,
140             HTML::hr(),
141             WikiLink(__("Help").":".__("SemanticRelations"), false,
142                 HTML::em(_("Help")."/"._("SemanticRelations"))),
143             " - ",
144             HTML::em(_("Find out how to add relations and attributes to pages.")));
145     }
146 }
147
148 // Local Variables:
149 // mode: php
150 // tab-width: 8
151 // c-basic-offset: 4
152 // c-hanging-comment-ender-p: nil
153 // indent-tabs-mode: nil
154 // End: