]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FoafViewer.php
Disallow refernces in calls if the declaration is a reference
[SourceForge/phpwiki.git] / lib / plugin / FoafViewer.php
1 <?php
2 rcs_id('$Id: FoafViewer.php,v 1.3 2004-06-16 10:38:59 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.3 $");
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         // see doc/README.foaf
80         if (findFile('XML/FOAF/Parser.php','missing_ok'))
81             require_once 'XML/FOAF/Parser.php';
82         //$ErrorManager->popErrorHandler();
83         if (!class_exists('XML_FOAF_Parser'))
84             return $this->error(_("required pear library XML/FOAF/Parser.php not found in include_path"));
85
86         extract($this->getArgs($argstr, $request));
87         // Get our FOAF File from the foaf plugin argument or $_GET['foaf']
88         if (empty($foaf)) $foaf = $request->getArg('foaf');
89         $chooser = HTML::form(array('method'=>'get','action'=>$request->getURLtoSelf()),
90                               HTML::h4(_("FOAF File URI")),
91                               HTML::input(array('id'=>'foaf','name'=>'foaf','type'=>'text','size'=>'80','value'=>$foaf)),
92                               HTML::br(),
93                               //HTML::p("Pretty HTML"),
94                               HTML::input(array('id'=>'pretty','name'=>'pretty','type'=>'radio','checked'=>'checked'),_("Pretty HTML")),
95                               //HTML::p("Original URL (Redirect)"),
96                               HTML::input(array('id'=>'original','name'=>'original','type'=>'radio'),_("Original URL (Redirect)")),
97                               HTML::br(),
98                               HTML::input(array('type'=>'submit','value'=>_("Parse FOAF")))
99                               //HTML::label(array('for'=>'foaf'),"FOAF File URI"),
100                               );
101         if (empty($foaf)) {
102             return $chooser;
103         }
104         else {
105             //Error Checking
106             if (substr($foaf,0,7) != "http://") {
107                 return $this->error(_("foaf must be a URI starting with http://"));
108             }
109             // Start of output
110             if (!empty($original)) {
111                 $request->redirect($foaf);
112             }
113             else {
114                 $foaffile = url_get_contents($foaf);
115                 if (!$foaffile) {
116                     //TODO: get errormsg
117                     return HTML(HTML::p("Resource isn't available: Something went wrong, probably a 404!"));
118                 }
119                 // Create new Parser object
120                 $parser = new XML_FOAF_Parser;
121                 // Parser FOAF into $foaffile
122                 $parser->parseFromMem($foaffile);
123                 $a = $parser->toArray();
124                         
125                 $html = HTML(HTML::h1(@$a[0]["name"]),
126                             HTML::table(
127                                         HTML::thead(),
128                                         HTML::tbody(
129                                                     @$a[0]["title"] ? 
130                                                         HTML::tr(HTML::td(_("Title")),
131                                                                  HTML::td($a[0]["title"])) : null,
132                                                     (@$a[0]["homepage"][0]) ? 
133                                                         $this->iterateHTML($a[0],"homepage",$a["dc"]) : null,
134                                                     (@$a[0]["weblog"][0]) ?
135                                                         $this->iterateHTML($a[0],"weblog",$a["dc"]) : null,
136                                                     //This seems broken?
137                                                     /*
138                                                      HTML::tr(HTML::td("Full Name"),
139                                                                        (@$a[0]["name"][0]) ?                                    
140                                                                        HTML::td(@$a[0]["name"]) :
141                                                                        (@$a[0]["firstname"][0] && @$a[0]["surname"][0]) ?
142                                                                        HTML::td(@$a[0]["firstname"][0] . " " . @$a[0]["surname"][0])
143                                                                        : null
144                                                     */
145                                                     HTML::tr(HTML::td("Full Name"),
146                                                              (@$a[0]["name"][0]) ?                                      
147                                                              HTML::td(@$a[0]["name"]) : null
148                                                              ),
149                                                     (@$a[0]["nick"][0]) ?
150                                                     $this->iterateHTML($a[0],"nick",$a["dc"])
151                                                     : null,
152                                                     (@$a[0]["mboxsha1sum"][0]) ?
153                                                     $this->iterateHTML($a[0],"mboxsha1sum",$a["dc"])
154                                                     : null,
155                                                     (@$a[0]["depiction"][0]) ?
156                                                     $this->iterateHTML($a[0],"depiction",$a["dc"])
157                                                     : null,
158                                                     (@$a[0]["seealso"][0]) ?
159                                                     $this->iterateHTML($a[0],"seealso",$a["dc"])
160                                                     : null,
161                                                     HTML::tr(HTML::td("Source"),
162                                                              HTML::td(
163                                                                       HTML::a(array('href'=>@$foaf),"RDF")
164                                                                       )
165                                                              )
166                                                     )
167                                         )
168                              );
169                 if (DEBUG) {
170                     $html->pushContent(HTML::hr(),$chooser);
171                 }
172                 return $html;
173             }
174         }
175     }
176
177     /**
178      * Renders array elements as HTML. May be used recursively.
179      *
180      * @param $array Source array
181      * @param $index Element Index to use.
182      * @todo Make sure it can look more than 1 layer deep
183      * @todo Pass in dublincore metadata
184      */
185     function iterateHTML($array,$index,$dc=NULL) {
186         for ($i=0;$i<count($array[$index]);$i++) {
187             //Cater for different types
188             $string = $array[$index][$i];
189
190             if ($index == "mboxsha1sum") {
191                 $string = '<a href="http://beta.plink.org/profile/' . $array[$index][$i] . '">'
192                     .'<img src="http://beta.plink.org/images/plink.png" alt="Plink - ' . $array[$index][$i] . '" /></a>';
193             }
194             else if ($index == "depiction") { 
195                 $string = '<img src="' . $array[$index][$i] . '" />';
196             }
197             else if ((substr($array[$index][$i],0,7) == "http://") || (substr($array[$index][$i],0,7) == "mailto:")) { 
198                 $string = '<a href="' . $array[$index][$i] . '"';
199                                 
200                 if (@$dc["description"][$array[$index][$i]]) {
201                     $string .= ' title="' . $dc["description"][$array[$index][$i]] . '"';
202                 }
203                 $string .= '>';
204                 if (@$dc["title"][$array[$index][$i]]) {
205                     $string .=  $dc["title"][$array[$index][$i]];
206                 }
207                 else {
208                     $string .= $array[$index][$i];
209                 }                               
210                 $string .= '</a>';
211             }
212             @$html .= "<tr><td>" . $index . "</td><td>" . $string . "</td></tr>";
213         }
214         
215         return HTML::raw($html);
216     }
217 }
218
219
220 // $Log: not supported by cvs2svn $
221 // Revision 1.2  2004/06/13 13:54:25  rurban
222 // Catch fatals on the four dump calls (as file and zip, as html and mimified)
223 // FoafViewer: Check against external requirements, instead of fatal.
224 // Change output for xhtmldumps: using file:// urls to the local fs.
225 // Catch SOAP fatal by checking for GOOGLE_LICENSE_KEY
226 // Import GOOGLE_LICENSE_KEY and FORTUNE_DIR from config.ini.
227 //
228 // Revision 1.1  2004/06/08 21:38:21  rurban
229 // based on dans version 0.0.2 - simplified
230 //
231
232 // For emacs users
233 // Local Variables:
234 // mode: php
235 // tab-width: 8
236 // c-basic-offset: 4
237 // c-hanging-comment-ender-p: nil
238 // indent-tabs-mode: nil
239 // End:
240 ?>