]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FoafViewer.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / plugin / FoafViewer.php
1 <?php
2 // rcs_id('$Id$');
3 /*
4  * Copyright (C) 2004 $ThePhpWikiProgrammingTeam
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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24 * Basic FoafViewPlugin for PHPWiki.
25 *
26 * Please note; this is <em>heavily</em> based on an example file distributed with XML_FOAF 0.2
27 * HTTP GET vars:
28 * <kbd>foaf=uri</kbd> - Used to determine the URI of a FOAF file.
29 * <kbd>original=true</kbd> - Simply dumps contents of $foaf
30 *
31 * @author Daniel O'Connor <http://ahsonline.com.au/dod/FOAF.rdf>
32 * @author Davey Shafik <http://pear.php.net/user/davey>
33 * @date 2004-06-07
34 * @version 0.0.2
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.
41 */
42
43 /**
44  * FoafViewer:  Parse an RDF FOAF file and extract information to render as HTML
45  * usage:   &lt;?plugin FoafViewer foaf=http://www.ahsonline.com.au/dod/rawksuga.rdf original=true?&gt;
46  * author:  Daniel O'Connor <http://www.ahsonline.com.au/dod/FOAF.rdf>
47  *
48  * phpwiki version based on version 0.0.2 by Daniel O'Connor
49  *
50  * TODO:
51  *  - use a template.
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)
54  */
55 class WikiPlugin_FoafViewer
56 extends WikiPlugin
57 {
58     // The handler is handled okay. The only problem is that it still
59     // throws a fatal.
60     function _error_handler($error) {
61         if (strstr($error->errstr,"Failed opening required 'XML/FOAF/Parser.php'"))
62             return true;
63         elseif (strstr($error->errstr,'No such file or directory'))
64             return true;
65         return false;
66     }
67
68     function getName() {
69         return _("FoafViewer");
70     }
71
72     function getDescription() {
73         return _("Parse an RDF FOAF file and extract information to render as HTML");
74     }
75
76     function getDefaultArguments() {
77         return array( 'foaf'     => false, // the URI to parse
78                       //'userid'   => false,
79                       'original' => false
80                       );
81     }
82
83     function run($dbi, $argstr, &$request, $basepage) {
84
85         /* ignore fatal on loading */
86         /*
87         global $ErrorManager;
88         $ErrorManager->pushErrorHandler(new WikiMethodCb($this,'_error_handler'));
89         */
90         // Require the XML_FOAF_Parser class. This is a pear library not included with phpwiki.
91         // see doc/README.foaf
92         if (findFile('XML/FOAF/Parser.php','missing_ok'))
93             require_once 'XML/FOAF/Parser.php';
94         //$ErrorManager->popErrorHandler();
95         if (!class_exists('XML_FOAF_Parser'))
96             return $this->error(_("required pear library XML/FOAF/Parser.php not found in include_path"));
97
98         extract($this->getArgs($argstr, $request));
99         // Get our FOAF File from the foaf plugin argument or $_GET['foaf']
100         if (empty($foaf)) $foaf = $request->getArg('foaf');
101         $chooser = HTML::form(array('method'=>'get','action'=>$request->getURLtoSelf()),
102                               HTML::h4(_("FOAF File URI")),
103                               HTML::input(array('id'=>'foaf','name'=>'foaf','type'=>'text','size'=>'80','value'=>$foaf)),
104                               HTML::br(),
105                               //HTML::p("Pretty HTML"),
106                               HTML::input(array('id'=>'pretty','name'=>'pretty','type'=>'radio','checked'=>'checked'),_("Pretty HTML")),
107                               //HTML::p("Original URL (Redirect)"),
108                               HTML::input(array('id'=>'original','name'=>'original','type'=>'radio'),_("Original URL (Redirect)")),
109                               HTML::br(),
110                               HTML::input(array('type'=>'submit','value'=>_("Parse FOAF")))
111                               //HTML::label(array('for'=>'foaf'),"FOAF File URI"),
112                               );
113         if (empty($foaf)) {
114             return $chooser;
115         }
116         else {
117             //Error Checking
118             if (substr($foaf,0,7) != "http://") {
119                 return $this->error(_("foaf must be a URI starting with http://"));
120             }
121             // Start of output
122                if (!empty($original)) {
123                 $request->redirect($foaf);
124             }
125             else {
126                 $foaffile = url_get_contents($foaf);
127                 if (!$foaffile) {
128                     //TODO: get errormsg
129                     return HTML(HTML::p("Resource isn't available: Something went wrong, probably a 404!"));
130                 }
131                 // Create new Parser object
132                 $parser = new XML_FOAF_Parser;
133                 // Parser FOAF into $foaffile
134                 $parser->parseFromMem($foaffile);
135                 $a = $parser->toArray();
136
137                 $html = HTML(HTML::h1(@$a[0]["name"]),
138                             HTML::table(
139                                         HTML::thead(),
140                                         HTML::tbody(
141                                                     @$a[0]["title"] ?
142                                                         HTML::tr(HTML::td(_("Title")),
143                                                                  HTML::td($a[0]["title"])) : null,
144                                                     (@$a[0]["homepage"][0]) ?
145                                                         $this->iterateHTML($a[0],"homepage",$a["dc"]) : null,
146                                                     (@$a[0]["weblog"][0]) ?
147                                                         $this->iterateHTML($a[0],"weblog",$a["dc"]) : null,
148                                                     //This seems broken?
149                                                     /*
150                                                      HTML::tr(HTML::td("Full Name"),
151                                                                        (@$a[0]["name"][0]) ?
152                                                                        HTML::td(@$a[0]["name"]) :
153                                                                        (@$a[0]["firstname"][0] && @$a[0]["surname"][0]) ?
154                                                                        HTML::td(@$a[0]["firstname"][0] . " " . @$a[0]["surname"][0])
155                                                                        : null
156                                                     */
157                                                     HTML::tr(HTML::td("Full Name"),
158                                                              (@$a[0]["name"][0]) ?
159                                                              HTML::td(@$a[0]["name"]) : null
160                                                              ),
161                                                     (@$a[0]["nick"][0]) ?
162                                                     $this->iterateHTML($a[0],"nick",$a["dc"])
163                                                     : null,
164                                                     (@$a[0]["mboxsha1sum"][0]) ?
165                                                     $this->iterateHTML($a[0],"mboxsha1sum",$a["dc"])
166                                                     : null,
167                                                     (@$a[0]["depiction"][0]) ?
168                                                     $this->iterateHTML($a[0],"depiction",$a["dc"])
169                                                     : null,
170                                                     (@$a[0]["seealso"][0]) ?
171                                                     $this->iterateHTML($a[0],"seealso",$a["dc"])
172                                                     : null,
173                                                     HTML::tr(HTML::td("Source"),
174                                                              HTML::td(
175                                                                       HTML::a(array('href'=>@$foaf),"RDF")
176                                                                       )
177                                                              )
178                                                     )
179                                         )
180                              );
181                 if (DEBUG) {
182                     $html->pushContent(HTML::hr(),$chooser);
183                 }
184                 return $html;
185             }
186         }
187     }
188
189     /**
190      * Renders array elements as HTML. May be used recursively.
191      *
192      * @param $array Source array
193      * @param $index Element Index to use.
194      * @todo Make sure it can look more than 1 layer deep
195      * @todo Pass in dublincore metadata
196      */
197     function iterateHTML($array,$index,$dc=NULL) {
198         for ($i=0;$i<count($array[$index]);$i++) {
199             //Cater for different types
200             $string = $array[$index][$i];
201
202             if ($index == "mboxsha1sum") {
203                 $string = '<a href="http://beta.plink.org/profile/' . $array[$index][$i] . '">'
204                     .'<img src="http://beta.plink.org/images/plink.png" alt="Plink - ' . $array[$index][$i] . '" /></a>';
205             }
206             else if ($index == "depiction") {
207                 $string = '<img src="' . $array[$index][$i] . '" />';
208             }
209             else if ((substr($array[$index][$i],0,7) == "http://") || (substr($array[$index][$i],0,7) == "mailto:")) {
210                 $string = '<a href="' . $array[$index][$i] . '"';
211
212                 if (@$dc["description"][$array[$index][$i]]) {
213                     $string .= ' title="' . $dc["description"][$array[$index][$i]] . '"';
214                 }
215                 $string .= '>';
216                 if (@$dc["title"][$array[$index][$i]]) {
217                     $string .=  $dc["title"][$array[$index][$i]];
218                 }
219                 else {
220                     $string .= $array[$index][$i];
221                 }
222                 $string .= '</a>';
223             }
224             @$html .= "<tr><td>" . $index . "</td><td>" . $string . "</td></tr>";
225         }
226
227         return HTML::raw($html);
228     }
229 }
230
231 // For emacs users
232 // Local Variables:
233 // mode: php
234 // tab-width: 8
235 // c-basic-offset: 4
236 // c-hanging-comment-ender-p: nil
237 // indent-tabs-mode: nil
238 // End:
239 ?>