4 * Copyright (C) 2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
24 * Basic FoafViewPlugin for PHPWiki.
26 * Please note; this is <em>heavily</em> based on an example file distributed with XML_FOAF 0.2
28 * <kbd>foaf=uri</kbd> - Used to determine the URI of a FOAF file.
29 * <kbd>original=true</kbd> - Simply dumps contents of $foaf
31 * @author Daniel O'Connor <http://ahsonline.com.au/dod/FOAF.rdf>
32 * @author Davey Shafik <http://pear.php.net/user/davey>
35 * @bug XML_FOAF 0.2 has problems with named RDF nodes (ie, http://www.ahsonline.com.au/dod/FOAF.rdf).
36 * Davey knows, will be fixing this soon.
37 * @todo "Friends" component
38 * @todo Named URLs (DC metadata)
39 * @todo "View in FOAFNAUT/foafexplorer/other"
40 * @bug Full name !available incorrectly handled.
44 * FoafViewer: Parse an RDF FOAF file and extract information to render as HTML
45 * usage: <?plugin FoafViewer foaf=http://www.ahsonline.com.au/dod/rawksuga.rdf original=true?>
46 * author: Daniel O'Connor <http://www.ahsonline.com.au/dod/FOAF.rdf>
48 * phpwiki version based on version 0.0.2 by Daniel O'Connor
52 * - use the phpwiki internal user foaf data (stored by a UserPreferences extension)
53 * - fix the pear FOAF Parser or we'll write our own (based on our XmlParser)
55 class WikiPlugin_FoafViewer
58 // The handler is handled okay. The only problem is that it still
60 function _error_handler($error)
62 if (strstr($error->errstr, "Failed opening required 'XML/FOAF/Parser.php'"))
64 elseif (strstr($error->errstr, 'No such file or directory'))
71 return _("FoafViewer");
74 function getDescription()
76 return _("Parse an RDF FOAF file and extract information to render as HTML");
79 function getDefaultArguments()
81 return array('foaf' => false, // the URI to parse
87 function run($dbi, $argstr, &$request, $basepage)
90 /* ignore fatal on loading */
93 $ErrorManager->pushErrorHandler(new WikiMethodCb($this,'_error_handler'));
95 // Require the XML_FOAF_Parser class. This is a PEAR library not included with phpwiki.
96 // see doc/README.foaf
97 if (findFile('XML/FOAF/Parser.php', 'missing_ok'))
98 require_once 'XML/FOAF/Parser.php';
99 //$ErrorManager->popErrorHandler();
100 if (!class_exists('XML_FOAF_Parser'))
101 return $this->error(_("required PEAR library XML/FOAF/Parser.php not found in include_path"));
103 extract($this->getArgs($argstr, $request));
104 // Get our FOAF File from the foaf plugin argument or $_GET['foaf']
105 if (empty($foaf)) $foaf = $request->getArg('foaf');
106 $chooser = HTML::form(array('method' => 'get', 'action' => $request->getURLtoSelf()),
107 HTML::h4(_("FOAF File URI")),
108 HTML::input(array('id' => 'foaf', 'name' => 'foaf', 'type' => 'text', 'size' => '80', 'value' => $foaf)),
110 //HTML::p("Pretty HTML"),
111 HTML::input(array('id' => 'pretty', 'name' => 'pretty', 'type' => 'radio', 'checked' => 'checked'), _("Pretty HTML")),
112 //HTML::p("Original URL (Redirect)"),
113 HTML::input(array('id' => 'original', 'name' => 'original', 'type' => 'radio'), _("Original URL (Redirect)")),
115 HTML::input(array('type' => 'submit', 'value' => _("Parse FOAF")))
116 //HTML::label(array('for'=>'foaf'),"FOAF File URI"),
122 if (substr($foaf, 0, 7) != "http://") {
123 return $this->error(_("foaf must be a URI starting with http://"));
126 if (!empty($original)) {
127 $request->redirect($foaf);
129 $foaffile = url_get_contents($foaf);
132 return HTML(HTML::p("Resource isn't available: Something went wrong, probably a 404!"));
134 // Create new Parser object
135 $parser = new XML_FOAF_Parser;
136 // Parser FOAF into $foaffile
137 $parser->parseFromMem($foaffile);
138 $a = $parser->toArray();
140 $html = HTML(HTML::h1(@$a[0]["name"]),
145 HTML::tr(HTML::td(_("Title")),
146 HTML::td($a[0]["title"])) : null,
147 (@$a[0]["homepage"][0]) ?
148 $this->iterateHTML($a[0], "homepage", $a["dc"]) : null,
149 (@$a[0]["weblog"][0]) ?
150 $this->iterateHTML($a[0], "weblog", $a["dc"]) : null,
153 HTML::tr(HTML::td("Full Name"),
154 (@$a[0]["name"][0]) ?
155 HTML::td(@$a[0]["name"]) :
156 (@$a[0]["firstname"][0] && @$a[0]["surname"][0]) ?
157 HTML::td(@$a[0]["firstname"][0] . " " . @$a[0]["surname"][0])
160 HTML::tr(HTML::td("Full Name"),
161 (@$a[0]["name"][0]) ?
162 HTML::td(@$a[0]["name"]) : null
164 (@$a[0]["nick"][0]) ?
165 $this->iterateHTML($a[0], "nick", $a["dc"])
167 (@$a[0]["mboxsha1sum"][0]) ?
168 $this->iterateHTML($a[0], "mboxsha1sum", $a["dc"])
170 (@$a[0]["depiction"][0]) ?
171 $this->iterateHTML($a[0], "depiction", $a["dc"])
173 (@$a[0]["seealso"][0]) ?
174 $this->iterateHTML($a[0], "seealso", $a["dc"])
176 HTML::tr(HTML::td("Source"),
178 HTML::a(array('href' => @$foaf), "RDF")
185 $html->pushContent(HTML::hr(), $chooser);
193 * Renders array elements as HTML. May be used recursively.
195 * @param $array Source array
196 * @param $index Element Index to use.
199 * @todo Make sure it can look more than 1 layer deep
200 * @todo Pass in dublincore metadata
202 function iterateHTML($array, $index, $dc = NULL)
204 for ($i = 0; $i < count($array[$index]); $i++) {
205 //Cater for different types
206 $string = $array[$index][$i];
208 if ($index == "mboxsha1sum") {
209 $string = '<a href="http://beta.plink.org/profile/' . $array[$index][$i] . '">'
210 . '<img src="http://beta.plink.org/images/plink.png" alt="Plink - ' . $array[$index][$i] . '" /></a>';
211 } elseif ($index == "depiction") {
212 $string = '<img src="' . $array[$index][$i] . '" />';
213 } elseif ((substr($array[$index][$i], 0, 7) == "http://") || (substr($array[$index][$i], 0, 7) == "mailto:")) {
214 $string = '<a href="' . $array[$index][$i] . '"';
216 if (@$dc["description"][$array[$index][$i]]) {
217 $string .= ' title="' . $dc["description"][$array[$index][$i]] . '"';
220 if (@$dc["title"][$array[$index][$i]]) {
221 $string .= $dc["title"][$array[$index][$i]];
223 $string .= $array[$index][$i];
227 @$html .= "<tr><td>" . $index . "</td><td>" . $string . "</td></tr>";
230 return HTML::raw($html);
238 // c-hanging-comment-ender-p: nil
239 // indent-tabs-mode: nil