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