| Total Complexity | 102 |
| Total Lines | 571 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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() { |
||
| 212 | global $ID, $conf; |
||
| 213 | |||
| 214 | // check for type if unknown |
||
| 215 | if(!($_GET['type'] ?? "")) { |
||
| 216 | $userinfo = getDwUserInfo($ID, $this); |
||
| 217 | |||
| 218 | if($userinfo) { |
||
| 219 | $type = 'user'; |
||
| 220 | } elseif(isset($_GET['do']) && $_GET['do'] == 'index') { |
||
| 221 | $type = 'container'; |
||
| 222 | } else { |
||
| 223 | $type = 'post'; |
||
| 224 | } |
||
| 225 | |||
| 226 | } else { |
||
| 227 | $type = $_GET['type']; |
||
| 228 | } |
||
| 229 | |||
| 230 | return $type; |
||
| 231 | |||
| 232 | } |
||
| 233 | |||
| 234 | private function exportContainercontent($exporter) { |
||
| 235 | global $ID, $INFO, $conf; |
||
| 236 | |||
| 237 | if($ID == $conf['start']) { |
||
| 238 | $title = $conf['title']; |
||
| 239 | } elseif(isset($INFO['meta']['title'])) { |
||
| 240 | $title = $INFO['meta']['title']; |
||
| 241 | } else { |
||
| 242 | $title = $ID; |
||
| 243 | } |
||
| 244 | |||
| 245 | $exporter->setParameters( |
||
| 246 | 'Container: ' . $title, |
||
| 247 | getAbsUrl(), |
||
| 248 | getAbsUrl() . 'doku.php?', |
||
| 249 | 'utf-8', |
||
| 250 | $this->agentlink |
||
| 251 | ); |
||
| 252 | |||
| 253 | // create container object |
||
| 254 | $wikicontainer = new SIOCDokuWikiContainer( |
||
| 255 | $ID, |
||
| 256 | normalizeUri($exporter->siocURL('container', $ID)) |
||
| 257 | ); |
||
| 258 | |||
| 259 | /* container is type=wiki */ |
||
| 260 | if($ID == $conf['start']) { |
||
| 261 | $wikicontainer->isWiki(); |
||
| 262 | } |
||
| 263 | /* sioc:name */ |
||
| 264 | if($INFO['exists']) { |
||
| 265 | $wikicontainer->addTitle($INFO['meta']['title']); |
||
| 266 | } |
||
| 267 | /* has_parent */ |
||
| 268 | if($INFO['namespace']) { |
||
| 269 | $wikicontainer->addParent($INFO['namespace']); |
||
| 270 | } |
||
| 271 | |||
| 272 | // search next level entries (posts, sub containers) in container |
||
| 273 | require_once(DOKU_INC . 'inc/search.php'); |
||
| 274 | $dir = utf8_encodeFN(str_replace(':', '/', $ID)); |
||
| 275 | $entries = array(); |
||
| 276 | $posts = array(); |
||
| 277 | $containers = array(); |
||
| 278 | search($entries, $conf['datadir'], 'search_index', array('ns' => $ID), $dir); |
||
| 279 | foreach($entries as $entry) { |
||
| 280 | if($entry['type'] === 'f') { |
||
| 281 | // wikisite |
||
| 282 | $posts[] = $entry; |
||
| 283 | } elseif($entry['type'] === 'd') { |
||
| 284 | // sub container |
||
| 285 | $containers[] = $entry; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | // without sub content it can't be a container (so it does not exist as a container) |
||
| 290 | if(count($posts) + count($containers) == 0) { |
||
| 291 | $this->exit("HTTP/1.0 404 Not Found"); |
||
| 292 | } |
||
| 293 | |||
| 294 | if(count($posts) > 0) { |
||
| 295 | $wikicontainer->addArticles($posts); |
||
| 296 | } |
||
| 297 | if(count($containers) > 0) { |
||
| 298 | $wikicontainer->addContainers($containers); |
||
| 299 | } |
||
| 300 | |||
| 301 | //print_r($containers);die(); |
||
| 302 | |||
| 303 | // add container to exporter |
||
| 304 | $exporter->addObject($wikicontainer); |
||
| 305 | |||
| 306 | return $exporter; |
||
| 307 | } |
||
| 308 | |||
| 309 | /* -- private helpers --------------------------------------------------- */ |
||
| 310 | |||
| 311 | private function exportUsercontent($exporter) { |
||
| 345 | } |
||
| 346 | |||
| 347 | private function exportPostcontent($exporter) { |
||
| 348 | global $ID, $INFO, $REV, $conf; |
||
| 349 | |||
| 350 | $exporter->setParameters( |
||
| 476 | |||
| 477 | } |
||
| 478 | |||
| 479 | private function getDokuUrl($url = null) { |
||
| 481 | } |
||
| 482 | |||
| 483 | public function isRdfXmlRequest() { |
||
| 484 | // get accepted types |
||
| 485 | $http_accept = trim($_SERVER['HTTP_ACCEPT']); |
||
| 486 | |||
| 487 | // save accepted types in array |
||
| 488 | $accepted = explode(',', $http_accept); |
||
| 489 | |||
| 490 | /* |
||
| 491 | $debuginfo = implode(' // ', array(date('c',$_SERVER['REQUEST_TIME']), $_SERVER['HTTP_REFERER'], |
||
| 492 | $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_HOST'], $_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_ACCEPT'])); |
||
| 493 | global $conf; //print_r($conf); die(); |
||
| 494 | //die($debuginfo); |
||
| 495 | $debuglog = @fopen($conf['tmpdir'].DIRECTORY_SEPARATOR.'requests.log', 'ab'); |
||
| 496 | @fwrite($debuglog, $debuginfo."\n"); |
||
| 497 | @fclose($debuglog); |
||
| 498 | @chmod($conf['tmpdir'].DIRECTORY_SEPARATOR.'requests.log', 0777); |
||
| 499 | */ |
||
| 500 | |||
| 501 | // soft check, route to RDF when client requests it (don't check quality of request) |
||
| 502 | |||
| 503 | if($this->getConf('softck') && strpos($_SERVER['HTTP_ACCEPT'], 'application/rdf+xml') !== false) { |
||
| 504 | return true; |
||
| 505 | } |
||
| 506 | |||
| 507 | if(count($accepted) > 0) { |
||
| 508 | // hard check, only serve RDF if it is requested first or equal to first type |
||
| 509 | |||
| 510 | // extract accepting ratio |
||
| 511 | $test_accept = array(); |
||
| 512 | foreach($accepted as $format) { |
||
| 513 | $formatspec = explode(';', $format); |
||
| 514 | $k = trim($formatspec[0]); |
||
| 515 | if(count($formatspec) == 2) { |
||
| 516 | $test_accept[$k] = trim($formatspec[1]); |
||
| 517 | } else { |
||
| 518 | $test_accept[$k] = 'q=1.0'; |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | // sort by ratio |
||
| 523 | arsort($test_accept); |
||
| 524 | $accepted_order = array_keys($test_accept); |
||
| 525 | |||
| 526 | if($accepted_order[0] == 'application/rdf+xml' || |
||
| 527 | (array_key_exists('application/rdf+xml', $test_accept) |
||
| 528 | && $test_accept['application/rdf+xml'] == 'q=1.0') |
||
| 529 | ) { |
||
| 530 | return true; |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | // print_r($accepted_order);print_r($test_accept);die(); |
||
| 535 | |||
| 536 | return false; |
||
| 537 | |||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | */ |
||
| 542 | public function createRdfLink($event = null, $param = null) { |
||
| 543 | global $ID, $INFO, $conf; |
||
| 544 | |||
| 545 | // Test for hidden pages |
||
| 546 | |||
| 547 | if(isHiddenPage($ID)) { |
||
| 548 | return false; |
||
| 549 | } |
||
| 550 | |||
| 551 | // Get type of SIOC content |
||
| 552 | |||
| 553 | $sioc_type = $this->getContenttype(); |
||
| 554 | |||
| 555 | // Test for valid types |
||
| 556 | |||
| 557 | if(!(($sioc_type == 'post' && $INFO['exists']) || $sioc_type == 'user' || $sioc_type == 'container')) { |
||
| 558 | return false; |
||
| 559 | } |
||
| 560 | |||
| 561 | // Test for permission |
||
| 562 | |||
| 563 | if(!$INFO['perm']) { |
||
| 564 | // not enough rights to see the wiki page |
||
| 565 | return false; |
||
| 566 | } |
||
| 567 | |||
| 568 | $userinfo = getDwUserInfo($ID, $this); |
||
| 569 | |||
| 570 | // Create attributes for meta link |
||
| 571 | |||
| 572 | $metalink['type'] = 'application/rdf+xml'; |
||
| 573 | $metalink['rel'] = 'meta'; |
||
| 574 | |||
| 575 | switch($sioc_type) { |
||
| 576 | case 'container': |
||
| 577 | $title = htmlentities( |
||
| 578 | "Container '" . ($INFO['meta']['title'] ?? $ID) . "' (SIOC document as RDF/XML)" |
||
| 579 | ); |
||
| 580 | $queryAttr = array('type' => 'container'); |
||
| 581 | break; |
||
| 582 | |||
| 583 | case 'user': |
||
| 584 | $title = htmlentities("User account '" . $userinfo['name'] . "' (SIOC document as RDF/XML)"); |
||
| 585 | $queryAttr = array('type' => 'user'); |
||
| 586 | break; |
||
| 587 | |||
| 588 | case 'post': |
||
| 589 | default: |
||
| 590 | $title = htmlentities("Article '" . $INFO['meta']['title'] . "' (SIOC document as RDF/XML)"); |
||
| 591 | $queryAttr = array('type' => 'post'); |
||
| 592 | if(isset($_GET['rev']) && $_GET['rev'] === (int) $_GET['rev']) { |
||
| 593 | $queryAttr['rev'] = $_GET['rev']; |
||
| 594 | } |
||
| 595 | break; |
||
| 596 | } |
||
| 597 | |||
| 598 | $metalink['title'] = $title; |
||
| 599 | $metalink['href'] = normalizeUri(getAbsUrl(exportlink($ID, 'siocxml', $queryAttr, false, '&'))); |
||
| 600 | |||
| 601 | if($event !== null) { |
||
| 602 | $event->data['link'][] = $metalink; |
||
| 603 | |||
| 604 | // set canocial link for type URIs to prevent indexing double content |
||
| 605 | if($_GET['type']) { |
||
| 606 | $event->data['link'][] = array('rel' => 'canonical', 'href' => getAbsUrl(wl($ID))); |
||
| 607 | } |
||
| 608 | } |
||
| 609 | |||
| 610 | return $metalink; |
||
| 611 | } |
||
| 612 | |||
| 613 | public function pingService($data, $controller) { |
||
| 634 | } |
||
| 635 | } |
||
| 636 | |||
| 637 | private function getDate($date, $date_alt = null) { |
||
| 642 | } |
||
| 643 | |||
| 644 | } |
||
| 715 |