Total Complexity | 102 |
Total Lines | 572 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 2 |
Complex classes like action_plugin_dokusioc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use action_plugin_dokusioc, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
71 | class action_plugin_dokusioc extends DokuWiki_Action_Plugin { |
||
72 | |||
73 | private $agentlink = 'http://eye48.com/go/dokusioc?v=0.1.2'; |
||
74 | |||
75 | /* -- Methods to manage plugin ------------------------------------------ */ |
||
76 | |||
77 | /** |
||
78 | * Register its handlers with the DokuWiki's event controller |
||
79 | */ |
||
80 | public function register(Doku_Event_Handler $controller) { |
||
81 | //print_r(headers_list()); die(); |
||
82 | |||
83 | // test the requested action |
||
84 | $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'checkAction', $controller); |
||
85 | // pingthesemanticweb.com |
||
86 | if($this->getConf('pingsw')) { |
||
87 | $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'pingService', $controller); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /* -- Event handlers ---------------------------------------------------- */ |
||
92 | |||
93 | public function checkAction($action, $controller) { |
||
116 | } |
||
117 | } |
||
118 | /* |
||
119 | else |
||
120 | { |
||
121 | print_r(array($action->data, $INFO['perm'], defined('DOKU_MEDIADETAIL'), $INFO['exists'], |
||
122 | getDwUserInfo($INFO['id'],$this), isHiddenPage($INFO['id']))); |
||
123 | die(); |
||
124 | } |
||
125 | */ |
||
126 | } |
||
127 | |||
128 | public function exportSioc() { |
||
129 | global $ID, $INFO, $conf, $REV, $auth; |
||
130 | |||
131 | // Test for hidden pages |
||
132 | |||
133 | if(isHiddenPage($ID)) { |
||
134 | $this->exit("HTTP/1.0 404 Not Found"); |
||
135 | } |
||
136 | |||
137 | // Get type of SIOC content |
||
138 | |||
139 | $sioc_type = $this->getContenttype(); |
||
140 | |||
141 | // Test for valid types |
||
142 | |||
143 | if(!(($sioc_type == 'post' && $INFO['exists']) || $sioc_type == 'user' || $sioc_type == 'container')) { |
||
144 | $this->exit("HTTP/1.0 404 Not Found"); |
||
145 | } |
||
146 | |||
147 | // Test for permission |
||
148 | |||
149 | if(!$INFO['perm']) { |
||
150 | // not enough rights to see the wiki page |
||
151 | $this->exit("HTTP/1.0 401 Unauthorized"); |
||
152 | } |
||
153 | |||
154 | // Forward to URI with explicit type attribut |
||
155 | if(!isset($_GET['type'])) { |
||
156 | header('Location:' . $_SERVER['REQUEST_URI'] . '&type=' . $sioc_type, true, 302); |
||
157 | } |
||
158 | |||
159 | // Include SIOC libs |
||
160 | |||
161 | require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sioc_inc.php'); |
||
162 | require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sioc_dokuwiki.php'); |
||
163 | |||
164 | // Create exporter |
||
165 | |||
166 | $rdf = new SIOCExporter(); |
||
167 | $rdf->profile_url = normalizeUri( |
||
168 | getAbsUrl( |
||
169 | exportlink( |
||
|
|||
170 | $ID, 'siocxml', |
||
171 | array('type' => $sioc_type), false, '&' |
||
172 | ) |
||
173 | ) |
||
174 | ); |
||
175 | $rdf->setURLParameters('type', 'id', 'page', false); |
||
176 | |||
177 | // Create SIOC-RDF content |
||
178 | |||
179 | switch($sioc_type) { |
||
180 | case 'container': |
||
181 | $rdf = $this->exportContainercontent($rdf); |
||
182 | break; |
||
183 | |||
184 | case 'user': |
||
185 | $rdf = $this->exportUsercontent($rdf); |
||
186 | break; |
||
187 | |||
188 | case 'post': |
||
189 | default: |
||
190 | $rdf = $this->exportPostcontent($rdf); |
||
191 | break; |
||
192 | } |
||
193 | |||
194 | // export |
||
195 | if($this->getConf('noindx')) { |
||
196 | header("X-Robots-Tag: noindex", true); |
||
197 | } |
||
198 | $rdf->export(); |
||
199 | |||
200 | //print_r(headers_list()); die(); |
||
201 | die(); |
||
202 | } |
||
203 | |||
204 | private function exit($headermsg) { |
||
205 | header($headermsg); |
||
206 | die(); |
||
207 | } |
||
208 | |||
209 | /* -- public class methods ---------------------------------------------- */ |
||
210 | |||
211 | private function getContenttype() { |
||
231 | |||
232 | } |
||
233 | |||
234 | private function exportContainercontent($exporter) { |
||
307 | } |
||
308 | |||
309 | /* -- private helpers --------------------------------------------------- */ |
||
310 | |||
311 | private function exportUsercontent($exporter) { |
||
346 | } |
||
347 | |||
348 | private function exportPostcontent($exporter) { |
||
349 | global $ID, $INFO, $REV, $conf; |
||
350 | |||
351 | $exporter->setParameters( |
||
352 | 'Article: ' . $INFO['meta']['title'] . ($REV ? ' (rev ' . $REV . ')' : ''), |
||
353 | $this->getDokuUrl(), |
||
354 | $this->getDokuUrl() . 'doku.php?', |
||
355 | 'utf-8', |
||
356 | $this->agentlink |
||
357 | ); |
||
358 | |||
359 | // create user object |
||
360 | // $id, $uri, $name, $email, $homepage='', $foaf_uri='', $role=false, $nick='', $sioc_url='', $foaf_url='' |
||
361 | $dwuserpage_id = cleanID($this->getConf('userns')) . ($conf['useslash'] ? '/' : ':') . $INFO['editor']; |
||
362 | /* |
||
363 | if ($INFO['editor'] && $this->getConf('userns')) |
||
364 | $pageuser = new SIOCUser($INFO['editor'], |
||
365 | normalizeUri(getAbsUrl(exportlink($dwuserpage_id, 'siocxml', |
||
366 | array('type'=>'user'), false, '&'))), // user page |
||
367 | $INFO['meta']['contributor'][$INFO['editor']], |
||
368 | getDwUserInfo($dwuserpage_id,$this,'mail'), |
||
369 | '', // no homepage is saved for dokuwiki user |
||
370 | '#'.$INFO['editor'], // local uri |
||
371 | false, // no roles right now |
||
372 | '', // no nick name is saved for dokuwiki user |
||
373 | normalizeUri($exporter->siocURL('user', $dwuserpage_id)) |
||
374 | ); |
||
375 | */ |
||
376 | |||
377 | // create wiki page object |
||
378 | $wikipage = new SIOCDokuWikiArticle( |
||
379 | $ID, // id |
||
380 | normalizeUri( |
||
381 | $exporter->siocURL( |
||
382 | 'post', $ID . ($REV ? $exporter->_urlseparator . 'rev' |
||
383 | . $exporter->_urlequal . $REV : '') |
||
384 | ) |
||
385 | ), // url |
||
386 | $INFO['meta']['title'] . ($REV ? ' (rev ' . $REV . ')' : ''), // subject |
||
387 | rawWiki($ID, $REV) // body (content) |
||
388 | ); |
||
389 | /* encoded content */ |
||
390 | $wikipage->addContentEncoded(p_cached_output(wikiFN($ID, $REV), 'xhtml')); |
||
391 | /* created */ |
||
392 | if(isset($INFO['meta']['date']['created'])) { |
||
393 | $wikipage->addCreated(date('c', $INFO['meta']['date']['created'])); |
||
394 | } |
||
395 | /* or modified */ |
||
396 | if(isset($INFO['meta']['date']['modified'])) { |
||
397 | $wikipage->addModified(date('c', $INFO['meta']['date']['modified'])); |
||
398 | } |
||
399 | /* creator/modifier */ |
||
400 | if($INFO['editor'] && $this->getConf('userns')) { |
||
401 | $wikipage->addCreator(array('foaf:maker' => '#' . $INFO['editor'], 'sioc:modifier' => $dwuserpage_id)); |
||
402 | } |
||
403 | /* is creator */ |
||
404 | if(isset($INFO['meta']['date']['created'])) { |
||
405 | $wikipage->isCreator(); |
||
406 | } |
||
407 | /* intern wiki links */ |
||
408 | $wikipage->addLinks($INFO['meta']['relation']['references']); |
||
409 | |||
410 | // contributors - only for last revision b/c of wrong meta data for older revisions |
||
411 | if(!$REV && $this->getConf('userns') && isset($INFO['meta']['contributor'])) { |
||
412 | $cont_temp = array(); |
||
413 | $cont_ns = $this->getConf('userns') . ($conf['useslash'] ? '/' : ':'); |
||
414 | foreach($INFO['meta']['contributor'] as $cont_id => $cont_name) { |
||
415 | $cont_temp[$cont_ns . $cont_id] = $cont_name; |
||
416 | } |
||
417 | $wikipage->addContributors($cont_temp); |
||
418 | } |
||
419 | |||
420 | // backlinks - only for last revision |
||
421 | if(!$REV) { |
||
422 | require_once(DOKU_INC . 'inc/fulltext.php'); |
||
423 | $backlinks = ft_backlinks($ID); |
||
424 | if(count($backlinks) > 0) { |
||
425 | $wikipage->addBacklinks($backlinks); |
||
426 | } |
||
427 | } |
||
428 | |||
429 | // TODO: addLinksExtern |
||
430 | |||
431 | /* previous and next revision */ |
||
432 | $changelog = new PageChangeLog($ID); |
||
433 | $pagerevs = $changelog->getRevisions(0, $conf['recent'] + 1); |
||
434 | $prevrev = false; |
||
435 | $nextrev = false; |
||
436 | if(!$REV) { |
||
437 | // latest revision, previous rev is on top in array |
||
438 | $prevrev = 0; |
||
439 | } else { |
||
440 | // other revision |
||
441 | $currentrev = array_search($REV, $pagerevs); |
||
442 | if($currentrev !== false) { |
||
443 | $prevrev = $currentrev + 1; |
||
444 | $nextrev = $currentrev - 1; |
||
445 | } |
||
446 | } |
||
447 | if($prevrev !== false && $prevrev > -1 && page_exists($ID, $pagerevs[$prevrev])) { |
||
448 | /* previous revision*/ |
||
449 | $wikipage->addVersionPrevious($pagerevs[$prevrev]); |
||
450 | } |
||
451 | if($nextrev !== false && $nextrev > -1 && page_exists($ID, $pagerevs[$nextrev])) { |
||
452 | /* next revision*/ |
||
453 | $wikipage->addVersionNext($pagerevs[$nextrev]); |
||
454 | } |
||
455 | |||
456 | /* latest revision */ |
||
457 | if($REV) { |
||
458 | $wikipage->addVersionLatest(); |
||
459 | } |
||
460 | // TODO: topics |
||
461 | /* has_container */ |
||
462 | if($INFO['namespace']) { |
||
463 | $wikipage->addContainer($INFO['namespace']); |
||
464 | } |
||
465 | /* has_space */ |
||
466 | if($this->getConf('owners')) { |
||
467 | $wikipage->addSite($this->getConf('owners')); |
||
468 | } |
||
469 | // TODO: dc:contributor / has_modifier |
||
470 | // TODO: attachment (e.g. pictures in that dwns) |
||
471 | |||
472 | // add wiki page to exporter |
||
473 | $exporter->addObject($wikipage); |
||
474 | //if ($INFO['editor'] && $this->getConf('userns')) $exporter->addObject($pageuser); |
||
475 | |||
476 | return $exporter; |
||
477 | |||
478 | } |
||
479 | |||
480 | private function getDokuUrl($url = null) { |
||
481 | return getAbsUrl($url); |
||
482 | } |
||
483 | |||
484 | public function isRdfXmlRequest() { |
||
485 | // get accepted types |
||
486 | $http_accept = trim($_SERVER['HTTP_ACCEPT']); |
||
487 | |||
488 | // save accepted types in array |
||
489 | $accepted = explode(',', $http_accept); |
||
490 | |||
491 | /* |
||
492 | $debuginfo = implode(' // ', array(date('c',$_SERVER['REQUEST_TIME']), $_SERVER['HTTP_REFERER'], |
||
493 | $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_HOST'], $_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_ACCEPT'])); |
||
494 | global $conf; //print_r($conf); die(); |
||
495 | //die($debuginfo); |
||
496 | $debuglog = @fopen($conf['tmpdir'].DIRECTORY_SEPARATOR.'requests.log', 'ab'); |
||
497 | @fwrite($debuglog, $debuginfo."\n"); |
||
498 | @fclose($debuglog); |
||
499 | @chmod($conf['tmpdir'].DIRECTORY_SEPARATOR.'requests.log', 0777); |
||
500 | */ |
||
501 | |||
502 | // soft check, route to RDF when client requests it (don't check quality of request) |
||
503 | |||
504 | if($this->getConf('softck') && strpos($_SERVER['HTTP_ACCEPT'], 'application/rdf+xml') !== false) { |
||
505 | return true; |
||
506 | } |
||
507 | |||
508 | if(count($accepted) > 0) { |
||
509 | // hard check, only serve RDF if it is requested first or equal to first type |
||
510 | |||
511 | // extract accepting ratio |
||
512 | $test_accept = array(); |
||
513 | foreach($accepted as $format) { |
||
514 | $formatspec = explode(';', $format); |
||
515 | $k = trim($formatspec[0]); |
||
516 | if(count($formatspec) == 2) { |
||
517 | $test_accept[$k] = trim($formatspec[1]); |
||
518 | } else { |
||
519 | $test_accept[$k] = 'q=1.0'; |
||
520 | } |
||
521 | } |
||
522 | |||
523 | // sort by ratio |
||
524 | arsort($test_accept); |
||
525 | $accepted_order = array_keys($test_accept); |
||
526 | |||
527 | if($accepted_order[0] == 'application/rdf+xml' || |
||
528 | (array_key_exists('application/rdf+xml', $test_accept) |
||
529 | && $test_accept['application/rdf+xml'] == 'q=1.0') |
||
530 | ) { |
||
531 | return true; |
||
532 | } |
||
533 | } |
||
534 | |||
535 | // print_r($accepted_order);print_r($test_accept);die(); |
||
536 | |||
537 | return false; |
||
538 | |||
539 | } |
||
540 | |||
541 | /** |
||
542 | */ |
||
543 | public function createRdfLink($event = null, $param = null) { |
||
544 | global $ID, $INFO, $conf; |
||
545 | |||
546 | // Test for hidden pages |
||
547 | |||
548 | if(isHiddenPage($ID)) { |
||
549 | return false; |
||
550 | } |
||
551 | |||
552 | // Get type of SIOC content |
||
553 | |||
554 | $sioc_type = $this->getContenttype(); |
||
555 | |||
556 | // Test for valid types |
||
557 | |||
558 | if(!(($sioc_type == 'post' && $INFO['exists']) || $sioc_type == 'user' || $sioc_type == 'container')) { |
||
559 | return false; |
||
560 | } |
||
561 | |||
562 | // Test for permission |
||
563 | |||
564 | if(!$INFO['perm']) { |
||
565 | // not enough rights to see the wiki page |
||
566 | return false; |
||
567 | } |
||
568 | |||
569 | $userinfo = getDwUserInfo($ID, $this); |
||
570 | |||
571 | // Create attributes for meta link |
||
572 | |||
573 | $metalink['type'] = 'application/rdf+xml'; |
||
574 | $metalink['rel'] = 'meta'; |
||
575 | |||
576 | switch($sioc_type) { |
||
577 | case 'container': |
||
578 | $title = htmlentities( |
||
579 | "Container '" . ($INFO['meta']['title'] ?? $ID) . "' (SIOC document as RDF/XML)" |
||
580 | ); |
||
581 | $queryAttr = array('type' => 'container'); |
||
582 | break; |
||
583 | |||
584 | case 'user': |
||
585 | $title = htmlentities("User account '" . $userinfo['name'] . "' (SIOC document as RDF/XML)"); |
||
586 | $queryAttr = array('type' => 'user'); |
||
587 | break; |
||
588 | |||
589 | case 'post': |
||
590 | default: |
||
591 | $title = htmlentities("Article '" . $INFO['meta']['title'] . "' (SIOC document as RDF/XML)"); |
||
592 | $queryAttr = array('type' => 'post'); |
||
593 | if(isset($_GET['rev']) && $_GET['rev'] === (int) $_GET['rev']) { |
||
594 | $queryAttr['rev'] = $_GET['rev']; |
||
595 | } |
||
596 | break; |
||
597 | } |
||
598 | |||
599 | $metalink['title'] = $title; |
||
600 | $metalink['href'] = normalizeUri(getAbsUrl(exportlink($ID, 'siocxml', $queryAttr, false, '&'))); |
||
601 | |||
602 | if($event !== null) { |
||
603 | $event->data['link'][] = $metalink; |
||
604 | |||
605 | // set canocial link for type URIs to prevent indexing double content |
||
606 | if($_GET['type'] ?? "") { |
||
607 | $event->data['link'][] = array('rel' => 'canonical', 'href' => getAbsUrl(wl($ID))); |
||
608 | } |
||
609 | } |
||
610 | |||
611 | return $metalink; |
||
612 | } |
||
613 | |||
614 | public function pingService($data, $controller) { |
||
635 | } |
||
636 | } |
||
637 | |||
638 | private function getDate($date, $date_alt = null) { |
||
643 | } |
||
644 | |||
645 | } |
||
646 | |||
647 | if(!function_exists('getAbsUrl')) { |
||
716 |