| Total Complexity | 96 |
| Total Lines | 536 |
| 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): void { |
||
| 81 | //print_r(headers_list()); die(); |
||
| 82 | // test the requested action |
||
| 83 | $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'checkAction', $controller); |
||
| 84 | } |
||
| 85 | |||
| 86 | /* -- Event handlers ---------------------------------------------------- */ |
||
| 87 | |||
| 88 | public function checkAction($action, $controller) { |
||
| 111 | } |
||
| 112 | } |
||
| 113 | /* |
||
| 114 | else |
||
| 115 | { |
||
| 116 | print_r(array($action->data, $INFO['perm'], defined('DOKU_MEDIADETAIL'), $INFO['exists'], |
||
| 117 | getDwUserInfo($INFO['id'],$this), isHiddenPage($INFO['id']))); |
||
| 118 | die(); |
||
| 119 | } |
||
| 120 | */ |
||
| 121 | } |
||
| 122 | |||
| 123 | public function exportSioc() { |
||
| 124 | global $ID, $INFO, $conf, $REV, $auth; |
||
| 125 | |||
| 126 | // Test for hidden pages |
||
| 127 | |||
| 128 | if(isHiddenPage($ID)) { |
||
| 129 | $this->exit("HTTP/1.0 404 Not Found"); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Get type of SIOC content |
||
| 133 | |||
| 134 | $sioc_type = $this->getContenttype(); |
||
| 135 | |||
| 136 | // Test for valid types |
||
| 137 | |||
| 138 | if(!(($sioc_type == 'post' && $INFO['exists']) || $sioc_type == 'user' || $sioc_type == 'container')) { |
||
| 139 | $this->exit("HTTP/1.0 404 Not Found"); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Test for permission |
||
| 143 | |||
| 144 | if(!$INFO['perm']) { |
||
| 145 | // not enough rights to see the wiki page |
||
| 146 | $this->exit("HTTP/1.0 401 Unauthorized"); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Forward to URI with explicit type attribut |
||
| 150 | if(!isset($_GET['type'])) { |
||
| 151 | header('Location:' . $_SERVER['REQUEST_URI'] . '&type=' . $sioc_type, true, 302); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Include SIOC libs |
||
| 155 | |||
| 156 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sioc_inc.php'); |
||
| 157 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'sioc_dokuwiki.php'); |
||
| 158 | |||
| 159 | // Create exporter |
||
| 160 | |||
| 161 | $rdf = new SIOCExporter(); |
||
| 162 | $rdf->profile_url = normalizeUri( |
||
| 163 | getAbsUrl( |
||
| 164 | exportlink( |
||
|
|
|||
| 165 | $ID, 'siocxml', |
||
| 166 | array('type' => $sioc_type), false, '&' |
||
| 167 | ) |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | $rdf->setURLParameters('type', 'id', 'page', false); |
||
| 171 | |||
| 172 | // Create SIOC-RDF content |
||
| 173 | |||
| 174 | switch($sioc_type) { |
||
| 175 | case 'container': |
||
| 176 | $rdf = $this->exportContainercontent($rdf); |
||
| 177 | break; |
||
| 178 | |||
| 179 | case 'user': |
||
| 180 | $rdf = $this->exportUsercontent($rdf); |
||
| 181 | break; |
||
| 182 | |||
| 183 | case 'post': |
||
| 184 | default: |
||
| 185 | $rdf = $this->exportPostcontent($rdf); |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | |||
| 189 | // export |
||
| 190 | if($this->getConf('noindx')) { |
||
| 191 | header("X-Robots-Tag: noindex", true); |
||
| 192 | } |
||
| 193 | $rdf->export(); |
||
| 194 | |||
| 195 | //print_r(headers_list()); die(); |
||
| 196 | die(); |
||
| 197 | } |
||
| 198 | |||
| 199 | private function exit($headermsg) { |
||
| 200 | header($headermsg); |
||
| 201 | die(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /* -- public class methods ---------------------------------------------- */ |
||
| 205 | |||
| 206 | private function getContenttype() { |
||
| 207 | global $ID, $conf; |
||
| 208 | |||
| 209 | // check for type if unknown |
||
| 210 | if(!($_GET['type'] ?? "")) { |
||
| 211 | $userinfo = getDwUserInfo($ID, $this); |
||
| 212 | |||
| 213 | if($userinfo) { |
||
| 214 | $type = 'user'; |
||
| 215 | } elseif(isset($_GET['do']) && $_GET['do'] == 'index') { |
||
| 216 | $type = 'container'; |
||
| 217 | } else { |
||
| 218 | $type = 'post'; |
||
| 219 | } |
||
| 220 | |||
| 221 | } else { |
||
| 222 | $type = $_GET['type']; |
||
| 223 | } |
||
| 224 | |||
| 225 | return $type; |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 229 | private function exportContainercontent($exporter) { |
||
| 230 | global $ID, $INFO, $conf; |
||
| 231 | |||
| 232 | if($ID == $conf['start']) { |
||
| 233 | $title = $conf['title']; |
||
| 234 | } elseif(isset($INFO['meta']['title'])) { |
||
| 235 | $title = $INFO['meta']['title']; |
||
| 236 | } else { |
||
| 237 | $title = $ID; |
||
| 238 | } |
||
| 239 | |||
| 240 | $exporter->setParameters( |
||
| 241 | 'Container: ' . $title, |
||
| 242 | getAbsUrl(), |
||
| 243 | getAbsUrl() . 'doku.php?', |
||
| 244 | 'utf-8', |
||
| 245 | $this->agentlink |
||
| 246 | ); |
||
| 247 | |||
| 248 | // create container object |
||
| 249 | $wikicontainer = new SIOCDokuWikiContainer( |
||
| 250 | $ID, |
||
| 251 | normalizeUri($exporter->siocURL('container', $ID)) |
||
| 252 | ); |
||
| 253 | |||
| 254 | /* container is type=wiki */ |
||
| 255 | if($ID == $conf['start']) { |
||
| 256 | $wikicontainer->isWiki(); |
||
| 257 | } |
||
| 258 | /* sioc:name */ |
||
| 259 | if($INFO['exists']) { |
||
| 260 | $wikicontainer->addTitle($INFO['meta']['title']); |
||
| 261 | } |
||
| 262 | /* has_parent */ |
||
| 263 | if($INFO['namespace']) { |
||
| 264 | $wikicontainer->addParent($INFO['namespace']); |
||
| 265 | } |
||
| 266 | |||
| 267 | // search next level entries (posts, sub containers) in container |
||
| 268 | require_once(DOKU_INC . 'inc/search.php'); |
||
| 269 | $dir = utf8_encodeFN(str_replace(':', '/', $ID)); |
||
| 270 | $entries = array(); |
||
| 271 | $posts = array(); |
||
| 272 | $containers = array(); |
||
| 273 | search($entries, $conf['datadir'], 'search_index', array('ns' => $ID), $dir); |
||
| 274 | foreach($entries as $entry) { |
||
| 275 | if($entry['type'] === 'f') { |
||
| 276 | // wikisite |
||
| 277 | $posts[] = $entry; |
||
| 278 | } elseif($entry['type'] === 'd') { |
||
| 279 | // sub container |
||
| 280 | $containers[] = $entry; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | // without sub content it can't be a container (so it does not exist as a container) |
||
| 285 | if(count($posts) + count($containers) == 0) { |
||
| 286 | $this->exit("HTTP/1.0 404 Not Found"); |
||
| 287 | } |
||
| 288 | |||
| 289 | if(count($posts) > 0) { |
||
| 290 | $wikicontainer->addArticles($posts); |
||
| 291 | } |
||
| 292 | if(count($containers) > 0) { |
||
| 293 | $wikicontainer->addContainers($containers); |
||
| 294 | } |
||
| 295 | |||
| 296 | //print_r($containers);die(); |
||
| 297 | |||
| 298 | // add container to exporter |
||
| 299 | $exporter->addObject($wikicontainer); |
||
| 300 | |||
| 301 | return $exporter; |
||
| 302 | } |
||
| 303 | |||
| 304 | /* -- private helpers --------------------------------------------------- */ |
||
| 305 | |||
| 306 | private function exportUsercontent($exporter) { |
||
| 341 | } |
||
| 342 | |||
| 343 | private function exportPostcontent($exporter) { |
||
| 344 | global $ID, $INFO, $REV, $conf; |
||
| 345 | |||
| 346 | $exporter->setParameters( |
||
| 347 | 'Article: ' . $INFO['meta']['title'] . ($REV ? ' (rev ' . $REV . ')' : ''), |
||
| 348 | $this->getDokuUrl(), |
||
| 349 | $this->getDokuUrl() . 'doku.php?', |
||
| 350 | 'utf-8', |
||
| 351 | $this->agentlink |
||
| 352 | ); |
||
| 353 | |||
| 354 | // create user object |
||
| 355 | // $id, $uri, $name, $email, $homepage='', $foaf_uri='', $role=false, $nick='', $sioc_url='', $foaf_url='' |
||
| 356 | $dwuserpage_id = cleanID($this->getConf('userns')) . ($conf['useslash'] ? '/' : ':') . $INFO['editor']; |
||
| 357 | /* |
||
| 358 | if ($INFO['editor'] && $this->getConf('userns')) |
||
| 359 | $pageuser = new SIOCUser($INFO['editor'], |
||
| 360 | normalizeUri(getAbsUrl(exportlink($dwuserpage_id, 'siocxml', |
||
| 361 | array('type'=>'user'), false, '&'))), // user page |
||
| 362 | $INFO['meta']['contributor'][$INFO['editor']], |
||
| 363 | getDwUserInfo($dwuserpage_id,$this,'mail'), |
||
| 364 | '', // no homepage is saved for dokuwiki user |
||
| 365 | '#'.$INFO['editor'], // local uri |
||
| 366 | false, // no roles right now |
||
| 367 | '', // no nick name is saved for dokuwiki user |
||
| 368 | normalizeUri($exporter->siocURL('user', $dwuserpage_id)) |
||
| 369 | ); |
||
| 370 | */ |
||
| 371 | |||
| 372 | // create wiki page object |
||
| 373 | $wikipage = new SIOCDokuWikiArticle( |
||
| 374 | $ID, // id |
||
| 375 | normalizeUri( |
||
| 376 | $exporter->siocURL( |
||
| 377 | 'post', $ID . ($REV ? $exporter->_urlseparator . 'rev' |
||
| 378 | . $exporter->_urlequal . $REV : '') |
||
| 379 | ) |
||
| 380 | ), // url |
||
| 381 | $INFO['meta']['title'] . ($REV ? ' (rev ' . $REV . ')' : ''), // subject |
||
| 382 | rawWiki($ID, $REV) // body (content) |
||
| 383 | ); |
||
| 384 | /* encoded content */ |
||
| 385 | $wikipage->addContentEncoded(p_cached_output(wikiFN($ID, $REV), 'xhtml')); |
||
| 386 | /* created */ |
||
| 387 | if(isset($INFO['meta']['date']['created'])) { |
||
| 388 | $wikipage->addCreated(date('c', $INFO['meta']['date']['created'])); |
||
| 389 | } |
||
| 390 | /* or modified */ |
||
| 391 | if(isset($INFO['meta']['date']['modified'])) { |
||
| 392 | $wikipage->addModified(date('c', $INFO['meta']['date']['modified'])); |
||
| 393 | } |
||
| 394 | /* creator/modifier */ |
||
| 395 | if($INFO['editor'] && $this->getConf('userns')) { |
||
| 396 | $wikipage->addCreator(array('foaf:maker' => '#' . $INFO['editor'], 'sioc:modifier' => $dwuserpage_id)); |
||
| 397 | } |
||
| 398 | /* is creator */ |
||
| 399 | if(isset($INFO['meta']['date']['created'])) { |
||
| 400 | $wikipage->isCreator(); |
||
| 401 | } |
||
| 402 | /* intern wiki links */ |
||
| 403 | $wikipage->addLinks($INFO['meta']['relation']['references']); |
||
| 404 | |||
| 405 | // contributors - only for last revision b/c of wrong meta data for older revisions |
||
| 406 | if(!$REV && $this->getConf('userns') && isset($INFO['meta']['contributor'])) { |
||
| 407 | $cont_temp = array(); |
||
| 408 | $cont_ns = $this->getConf('userns') . ($conf['useslash'] ? '/' : ':'); |
||
| 409 | foreach($INFO['meta']['contributor'] as $cont_id => $cont_name) { |
||
| 410 | $cont_temp[$cont_ns . $cont_id] = $cont_name; |
||
| 411 | } |
||
| 412 | $wikipage->addContributors($cont_temp); |
||
| 413 | } |
||
| 414 | |||
| 415 | // backlinks - only for last revision |
||
| 416 | if(!$REV) { |
||
| 417 | require_once(DOKU_INC . 'inc/fulltext.php'); |
||
| 418 | $backlinks = ft_backlinks($ID); |
||
| 419 | if(count($backlinks) > 0) { |
||
| 420 | $wikipage->addBacklinks($backlinks); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | // TODO: addLinksExtern |
||
| 425 | |||
| 426 | /* previous and next revision */ |
||
| 427 | $changelog = new PageChangeLog($ID); |
||
| 428 | $pagerevs = $changelog->getRevisions(0, $conf['recent'] + 1); |
||
| 429 | $prevrev = false; |
||
| 430 | $nextrev = false; |
||
| 431 | if(!$REV) { |
||
| 432 | // latest revision, previous rev is on top in array |
||
| 433 | $prevrev = 0; |
||
| 434 | } else { |
||
| 435 | // other revision |
||
| 436 | $currentrev = array_search($REV, $pagerevs); |
||
| 437 | if($currentrev !== false) { |
||
| 438 | $prevrev = $currentrev + 1; |
||
| 439 | $nextrev = $currentrev - 1; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | if($prevrev !== false && $prevrev > -1 && page_exists($ID, $pagerevs[$prevrev])) { |
||
| 443 | /* previous revision*/ |
||
| 444 | $wikipage->addVersionPrevious($pagerevs[$prevrev]); |
||
| 445 | } |
||
| 446 | if($nextrev !== false && $nextrev > -1 && page_exists($ID, $pagerevs[$nextrev])) { |
||
| 447 | /* next revision*/ |
||
| 448 | $wikipage->addVersionNext($pagerevs[$nextrev]); |
||
| 449 | } |
||
| 450 | |||
| 451 | /* latest revision */ |
||
| 452 | if($REV) { |
||
| 453 | $wikipage->addVersionLatest(); |
||
| 454 | } |
||
| 455 | // TODO: topics |
||
| 456 | /* has_container */ |
||
| 457 | if($INFO['namespace']) { |
||
| 458 | $wikipage->addContainer($INFO['namespace']); |
||
| 459 | } |
||
| 460 | /* has_space */ |
||
| 461 | if($this->getConf('owners')) { |
||
| 462 | $wikipage->addSite($this->getConf('owners')); |
||
| 463 | } |
||
| 464 | // TODO: dc:contributor / has_modifier |
||
| 465 | // TODO: attachment (e.g. pictures in that dwns) |
||
| 466 | |||
| 467 | // add wiki page to exporter |
||
| 468 | $exporter->addObject($wikipage); |
||
| 469 | //if ($INFO['editor'] && $this->getConf('userns')) $exporter->addObject($pageuser); |
||
| 470 | |||
| 471 | return $exporter; |
||
| 472 | |||
| 473 | } |
||
| 474 | |||
| 475 | private function getDokuUrl($url = null) { |
||
| 476 | return getAbsUrl($url); |
||
| 477 | } |
||
| 478 | |||
| 479 | public function isRdfXmlRequest(): bool { |
||
| 533 | |||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | */ |
||
| 538 | public function createRdfLink($event = null, $param = null) { |
||
| 607 | } |
||
| 608 | } |
||
| 609 | |||
| 610 | if(!function_exists('getAbsUrl')) { |
||
| 611 | function getAbsUrl($url = null) { |
||
| 612 | if($url === null) { |
||
| 613 | $url = DOKU_BASE; |
||
| 679 |