1<?php
2/**
3 * @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
4 */
5
6/**
7 * SIOC::WikiDokuArticle object
8 *
9 * Contains information about a wiki article
10 */
11class SIOCDokuWikiArticle extends SIOCObject {
12
13    private $type = 'sioct:WikiArticle';
14    private $id = null;
15    private $url = null;
16    private $subject = null;
17    private $creator = array();
18    private $contributors = array();
19    private $created = null;
20    private $modified = null;
21    private $links = array();
22    private $backlinks = array();
23    private $previous_version = null;
24    private $next_version = null;
25    private $latest_version = false; // show latest version
26    private $has_container = null;
27    private $has_space = null;
28    private $content = null;
29    private $content_encoded = null;
30    private $is_creator = false;
31
32    public function __construct($id, $url, $subject, $content) {
33        $this->id      = $id;
34        $this->url     = $url;
35        $this->subject = $subject;
36        $this->content = $content;
37    }
38
39    public function addCreated($created) {
40        $this->created = $created;
41    }
42
43    public function addModified($modified) {
44        $this->modified = $modified;
45    }
46
47    public function addCreator($creator) {
48        $this->creator = $creator;
49    }
50
51    public function addContributors($contributors) {
52        $this->contributors = $contributors;
53    }
54
55    public function isCreator() {
56        $this->is_creator = true;
57    }
58
59    public function addLinks($links) {
60        if(is_array($links) && count($links) > 0) {
61            $this->links = $links;
62        }
63    }
64
65    public function addBacklinks($links) {
66        $this->backlinks = $links;
67    }
68
69    //function addLinksExtern($links) { if (is_array($links) && count($links)>0) $this->ext_links = $links; }
70    public function addVersionPrevious($rev) {
71        $this->previous_version = $rev;
72    }
73
74    public function addVersionNext($rev) {
75        $this->next_version = $rev;
76    }
77
78    public function addVersionLatest() {
79        $this->latest_version = true;
80    }
81
82    public function addContentEncoded($encoded) {
83        $this->content_encoded = $encoded;
84    }
85
86    public function addContainer($id) {
87        $this->has_container = $id;
88    }
89
90    public function addSite($url) {
91        $this->has_space = $url;
92    }
93
94    public function getContent(&$exp): string {
95        $rdf = '<' . $this->type . " rdf:about=\"" . clean($this->url, true) . "\">\n";
96        if($this->subject) {
97            $rdf .= "    <dc:title>" . clean($this->subject) . "</dc:title>\n";
98            // if(strcmp($this->has_container, 'http://en.wikipedia.org')===0)
99            //    $rdf .= "    <foaf:primaryTopic rdf:resource=\"".clean('http://dbpedia.org/resource/'
100            //      .$this->subject)."\"/>\n";
101        }
102
103        $creator_name = null;
104
105        if(count($this->contributors) > 0) {
106            foreach($this->contributors as $cont_id => $cont_name) {
107                if(!isset($this->creator['sioc:modifier']) || ($this->creator['sioc:modifier'] != $cont_id)) {
108                    $rdf .= "    <sioc:has_modifier rdf:resource=\"" . normalizeUri($exp->siocURL('user', $cont_id))
109                        . "\" rdfs:label=\"" . clean($cont_name) . "\"/>\n";
110                }
111            }
112
113            if(isset($this->contributors[$this->creator['sioc:modifier']])) {
114                $creator_name = 'rdfs:label="' . clean($this->contributors[$this->creator['sioc:modifier']]) . '"';
115            }
116        }
117
118        if(is_array($this->creator)) {
119            // if ($this->creator['foaf:maker'])
120            //     $rdf .= "    <foaf:maker rdf:resource=\"".clean($this->creator['foaf:maker'])."\"/>\n";
121            if($this->creator['sioc:modifier']) {
122                if($this->is_creator === false) {
123                    $rdf .= "    <sioc:has_modifier rdf:resource=\""
124                        . normalizeUri($exp->siocURL('user', $this->creator['sioc:modifier'])) . "\" $creator_name/>\n";
125                }
126                if($this->is_creator === true) {
127                    $rdf .= "    <sioc:has_creator rdf:resource=\""
128                        . normalizeUri($exp->siocURL('user', $this->creator['sioc:modifier'])) . "\" $creator_name/>\n";
129                }
130            }
131        }
132
133        if($this->created) {
134            $rdf .= "    <dcterms:created>" . $this->created . "</dcterms:created>\n";
135        }
136
137        if($this->modified) {
138            $rdf .= "    <dcterms:modified>" . $this->modified . "</dcterms:modified>\n";
139        }
140
141        if($this->has_space) {
142            $rdf .= "    <sioc:has_space rdf:resource=\"" . clean($this->has_space, true) . "\" />\n";
143            // TODO: rdfs:label
144        }
145
146        if($this->has_container) {
147            $rdf .= "    <sioc:has_container rdf:resource=\""
148                . normalizeUri($exp->siocURL('container', $this->has_container)) . "\" />\n";
149            // TODO: rdfs:label
150        }
151
152        if($this->content) {
153            $rdf .= "    <sioc:content><![CDATA[" . pureContent($this->content) . "]]></sioc:content>\n";
154        }
155
156        if($this->content_encoded) {
157            $rdf .= "    <content:encoded><![CDATA[" . $this->content_encoded . "]]></content:encoded>\n";
158        }
159
160        /*
161        if(is_array($this->topics)) {
162            foreach($this->topics as $topic=>$url) {
163                $rdf .= "    <sioc:topic>\n";
164                $rdf .= "        <sioct:Category rdf:about=\"" . clean($url) ."\">\n";
165                $rdf .= "            <rdfs:seeAlso rdf:resource=\"" .
166                        clean('http://ws.sioc-project.org/mediawiki/mediawiki.php?wiki='.$url);
167                if ($this->api) $rdf .= clean("&api=" . $this->api);
168                $rdf .= "\"/>\n";
169                $rdf .= "        </sioct:Category>\n";
170                $rdf .= "    </sioc:topic>\n";
171            }
172        }
173        */
174
175        if(is_array($this->links) && count($this->links) > 0) {
176            foreach($this->links as $link_id => $link_exists) {
177                if($link_exists && !isHiddenPage($link_id)) {
178                    $rdf .= "    <sioc:links_to rdf:resource=\""
179                        . normalizeUri($exp->siocURL('post', $link_id)) . "\"/>\n";
180                    // TODO: rdfs:label
181                }
182            }
183        }
184
185        if(count($this->backlinks) > 0) {
186            foreach($this->backlinks as $link_id) {
187                if(!isHiddenPage($link_id)) {
188                    $rdf .= "    <dcterms:isReferencedBy rdf:resource=\""
189                        . normalizeUri($exp->siocURL('post', $link_id)) . "\"/>\n";
190                    // TODO: rdfs:label
191                }
192            }
193        }
194
195        /*
196        if(is_array($this->ext_links)) {
197            foreach($this->ext_links as $label=>$url) {
198                $rdf .= "    <sioc:links_to rdf:resource=\"" . clean($url) ."\"/>\n";
199            }
200        }
201        */
202
203        if($this->previous_version) {
204            $rdf .= "    <sioc:previous_version rdf:resource=\""
205                . normalizeUri(
206                    $exp->siocURL(
207                        'post', $this->id . $exp->_urlseparator . 'rev' . $exp->_urlequal
208                              . $this->previous_version
209                    )
210                ) . "\"/>\n";
211            // TODO: rdfs:label
212
213            /* If there is support for inference and transitivity the following is not needed */
214            $rdf .= "    <sioc:earlier_version rdf:resource=\""
215                . normalizeUri(
216                    $exp->siocURL(
217                        'post', $this->id . $exp->_urlseparator . 'rev' . $exp->_urlequal
218                              . $this->previous_version
219                    )
220                ) . "\"/>\n";
221            // TODO: rdfs:label
222
223        }
224
225        if($this->next_version) {
226            $rdf .= "    <sioc:next_version rdf:resource=\""
227                . normalizeUri(
228                    $exp->siocURL(
229                        'post', $this->id . $exp->_urlseparator . 'rev' . $exp->_urlequal
230                              . $this->next_version
231                    )
232                ) . "\"/>\n";
233            // TODO: rdfs:label
234
235            /* If there is support for inference and transitivity the following is not needed */
236            $rdf .= "    <sioc:later_version rdf:resource=\""
237                . normalizeUri(
238                    $exp->siocURL(
239                        'post', $this->id . $exp->_urlseparator . 'rev' . $exp->_urlequal
240                              . $this->next_version
241                    )
242                ) . "\"/>\n";
243            // TODO: rdfs:label
244        }
245
246        if($this->latest_version) {
247            $rdf .= "    <sioc:latest_version rdf:resource=\""
248                . normalizeUri($exp->siocURL('post', $this->id)) . "\"/>\n";
249            // TODO: rdfs:label
250        }
251
252        /*
253        if($this->has_discussion && (strpos($this->has_discussion, 'Talk:Talk:') == FALSE)) {
254                $rdf .= "    <sioc:has_discussion>\n";
255                $rdf .= "        <sioct:WikiArticle rdf:about=\"" . clean($this->has_discussion) ."\">\n";
256                $rdf .= "            <rdfs:seeAlso rdf:resource=\"" .
257                        clean('http://ws.sioc-project.org/mediawiki/mediawiki.php?wiki='.$this->has_discussion);
258                if ($this->api) $rdf .= clean("&api=" . $this->api);
259                $rdf .= "\"/>\n";
260                $rdf .= "        </sioct:WikiArticle>\n";
261                $rdf .= "    </sioc:has_discussion>\n";
262        }
263        */
264
265        /*
266        if($this->redirpage)
267        {
268            $rdf .= "    <owl:sameAs rdf:resource=\"" . clean($this->redirpage) ."\"/>\n";
269            $rdf .= "    <rdfs:seeAlso rdf:resource=\"" .
270                        clean('http://ws.sioc-project.org/mediawiki/mediawiki.php?wiki='.$this->redirpage);
271            if ($this->api) $rdf .= clean("&api=" . $this->api);
272            $rdf .= "\"/>\n";
273        }
274        */
275
276        $rdf .= "</" . $this->type . ">\n";
277        return $rdf;
278    }
279}
280
281/**
282 * SIOC::DokuWikiUser object
283 *
284 * Contains information about a wiki user
285 */
286class SIOCDokuWikiUser extends SIOCObject {
287
288    private $type = 'user';
289
290    private $id;
291    private $nick;
292    private $url;
293    private $name;
294    private $email;
295    private $sha1;
296    private $homepage;
297    private $foaf_uri;
298    private $role;
299    private $sioc_url;
300    private $foaf_url;
301
302    public function __construct($id, $url, $userid, $name, $email) {
303        $this->id   = $id;
304        $this->nick = $userid;
305        $this->name = $name;
306        //$this->email = $email;
307        $this->url = $url;
308
309        if(preg_match_all('/^.+@.+\..+$/Ui', $email, $check, PREG_SET_ORDER)) {
310            if(preg_match_all('/^mailto:(.+@.+\..+$)/Ui', $email, $matches, PREG_SET_ORDER)) {
311                $this->email = $email;
312                $this->sha1  = sha1($email);
313            } else {
314                $this->email = "mailto:" . $email;
315                $this->sha1  = sha1("mailto:" . $email);
316            }
317        }
318    }
319
320    public function getContent(&$exp): string {
321        $rdf = "<sioc:UserAccount rdf:about=\"" . clean($this->url, true) . "\">\n";
322        if($this->nick) {
323            $rdf .= "    <sioc:name>" . clean($this->nick) . "</sioc:name>\n";
324        }
325        if($this->email) {
326            if($exp->_export_email) {
327                $rdf .= "    <sioc:email rdf:resource=\"" . $this->email . "\"/>\n";
328            }
329            $rdf .= "    <sioc:email_sha1>" . $this->sha1 . "</sioc:email_sha1>\n";
330        }
331        if($this->role) {
332            $rdf .= "    <sioc:has_function>\n";
333            $rdf .= "        <sioc:Role>\n";
334            $rdf .= "            <sioc:name>" . $this->role . "</sioc:name>\n";
335            $rdf .= "        </sioc:Role>\n";
336            $rdf .= "    </sioc:has_function>\n";
337        }
338        $rdf .= "    <sioc:account_of>\n";
339        $rdf .= "        <foaf:Person>\n";
340        if($this->name) {
341            $rdf .= "            <foaf:name>" . clean($this->name) . "</foaf:name>\n";
342        }
343        if($this->email) {
344            $rdf .= "            <foaf:mbox_sha1sum>" . $this->sha1 . "</foaf:mbox_sha1sum>\n";
345        }
346        if($this->foaf_url) {
347            $rdf .= "            <rdfs:seeAlso rdf:resource=\"" . $this->foaf_url . "\"/>\n";
348        }
349        $rdf .= "        </foaf:Person>\n";
350        $rdf .= "    </sioc:account_of>\n";
351        //if($this->sioc_url) { $rdf .= "            <rdfs:seeAlso rdf:resource=\"". $this->sioc_url ."\"/>\n"; }
352        $rdf .= "</sioc:UserAccount>\n";
353
354        return $rdf;
355    }
356}
357
358/**
359 * SIOC::DokuWikiContainer object
360 *
361 * Contains information about a wiki container
362 */
363class SIOCDokuWikiContainer extends SIOCObject {
364
365    private $type = 'sioc:Container';
366
367    private $id = null;
368    private $url = null;
369    private $posts = array();
370    private $subcontainers = array();
371    private $has_parent = null;
372    private $title = null;
373
374    public function __construct($id, $url) {
375        $this->id  = $id;
376        $this->url = $url;
377    }
378
379    public function isWiki() {
380        $this->type = 'sioct:Wiki';
381    }
382
383    public function addArticles($posts) {
384        $this->posts = $posts;
385    }
386
387    public function addContainers($containers) {
388        $this->subcontainers = $containers;
389    }
390
391    public function addTitle($title) {
392        $this->title = $title;
393    }
394
395    public function addParent($id) {
396        $this->_has_parent = $id;
397    }
398
399    public function getContent(&$exp): string {
400        $rdf = '<' . $this->type . " rdf:about=\"" . normalizeUri(clean($this->url, true)) . "\" >\n";
401
402        if($this->title) {
403            $rdf .= "    <sioc:name>" . clean($this->title) . "</sioc:name>\n";
404        }
405
406        if($this->_has_parent) {
407            $rdf .= "    <sioc:has_parent rdf:resource=\""
408                . normalizeUri($exp->siocURL('container', $this->_has_parent)) . "\" />\n";
409            // TODO: rdfs:label
410        }
411
412        foreach($this->posts as $article) {
413            // TODO: test permission before?
414            $rdf .= "    <sioc:container_of rdf:resource=\""
415                . normalizeUri($exp->siocURL('post', $article['id'])) . "\"/>\n";
416            // TODO: inluding title/name
417        }
418
419        foreach($this->subcontainers as $container) {
420            $rdf .= "    <sioc:parent_of rdf:resource=\""
421                . normalizeUri($exp->siocURL('container', $container['id'])) . "\"/>\n";
422            // TODO: inluding title/name
423        }
424
425        $rdf .= "</" . $this->type . ">\n";
426        return $rdf;
427    }
428
429}
430
431