Total Complexity | 106 |
Total Lines | 724 |
Duplicated Lines | 0 % |
Changes | 31 | ||
Bugs | 2 | Features | 0 |
Complex classes like ExportProfileData_Background 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 ExportProfileData_Background, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ExportProfileData_Background extends SMF_BackgroundTask |
||
20 | { |
||
21 | /** |
||
22 | * Some private variables to help the static functions in this class. |
||
23 | */ |
||
24 | private static $export_details = array(); |
||
25 | private static $real_modSettings = array(); |
||
26 | private static $xslt_info = array('stylesheet' => '', 'doctype' => ''); |
||
27 | |||
28 | /** |
||
29 | * Keep track of some other things on a per-instance basis. |
||
30 | */ |
||
31 | private $next_task = array(); |
||
32 | private $time_limit = 30; |
||
33 | |||
34 | /** |
||
35 | * This is the main dispatcher for the class. |
||
36 | * It calls the correct private function based on the information stored in |
||
37 | * the task details. |
||
38 | * |
||
39 | * @return bool Always returns true |
||
40 | */ |
||
41 | public function execute() |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * The workhorse of this class. Compiles profile data to XML files. |
||
110 | * |
||
111 | * @param array $member_info Minimal $user_info about the relevant member. |
||
112 | */ |
||
113 | protected function exportXml($member_info) |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Compiles profile data to HTML. |
||
420 | * |
||
421 | * Internally calls exportXml() and then uses an XSLT stylesheet to |
||
422 | * transform the XML files into HTML. |
||
423 | * |
||
424 | * @param array $member_info Minimal $user_info about the relevant member. |
||
425 | */ |
||
426 | protected function exportHtml($member_info) |
||
427 | { |
||
428 | global $modSettings, $context, $smcFunc, $sourcedir; |
||
429 | |||
430 | $context['export_last_page'] = $this->_details['last_page']; |
||
431 | $context['export_dlfilename'] = $this->_details['dlfilename']; |
||
432 | |||
433 | // Perform the export to XML. |
||
434 | $this->exportXml($member_info); |
||
435 | |||
436 | // Determine which files, if any, are ready to be transformed. |
||
437 | $export_dir_slash = $modSettings['export_dir'] . DIRECTORY_SEPARATOR; |
||
438 | $idhash = hash_hmac('sha1', $this->_details['uid'], get_auth_secret()); |
||
439 | $idhash_ext = $idhash . '.' . $this->_details['format_settings']['extension']; |
||
440 | |||
441 | $new_exportfiles = array(); |
||
442 | foreach (glob($export_dir_slash . '*_' . $idhash_ext) as $completed_file) |
||
443 | { |
||
444 | if (file_get_contents($completed_file, false, null, 0, 6) == '<?xml ') |
||
445 | $new_exportfiles[] = $completed_file; |
||
446 | } |
||
447 | if (empty($new_exportfiles)) |
||
448 | return; |
||
449 | |||
450 | // Get the XSLT stylesheet. |
||
451 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'Profile-Export.php'); |
||
452 | self::$xslt_info = get_xslt_stylesheet($this->_details['format'], $this->_details['uid']); |
||
453 | |||
454 | // Set up the XSLT processor. |
||
455 | $xslt = new DOMDocument(); |
||
456 | $xslt->loadXML(self::$xslt_info['stylesheet']); |
||
457 | $xsltproc = new XSLTProcessor(); |
||
458 | $xsltproc->importStylesheet($xslt); |
||
459 | |||
460 | $libxml_options = 0; |
||
461 | if (LIBXML_VERSION >= 20621 && defined('LIBXML_COMPACT')) |
||
462 | $libxml_options = $libxml_options | LIBXML_COMPACT; |
||
463 | if (LIBXML_VERSION >= 20700 && defined('LIBXML_PARSEHUGE')) |
||
464 | $libxml_options = $libxml_options | LIBXML_PARSEHUGE; |
||
465 | if (LIBXML_VERSION >= 20900 && defined('LIBXML_BIGLINES')) |
||
466 | $libxml_options = $libxml_options | LIBXML_BIGLINES; |
||
467 | |||
468 | // Transform the files to HTML. |
||
469 | $i = 0; |
||
470 | $num_files = count($new_exportfiles); |
||
471 | $max_transform_time = 0; |
||
472 | $xmldoc = new DOMDocument(); |
||
473 | foreach ($new_exportfiles as $exportfile) |
||
474 | { |
||
475 | if (function_exists('apache_reset_timeout')) |
||
476 | @apache_reset_timeout(); |
||
477 | |||
478 | $started = microtime(true); |
||
479 | $xmldoc->load($exportfile, $libxml_options); |
||
480 | $xsltproc->transformToURI($xmldoc, $exportfile); |
||
481 | $finished = microtime(true); |
||
482 | |||
483 | $max_transform_time = max($max_transform_time, $finished - $started); |
||
484 | |||
485 | // When deadlines loom, sometimes the best solution is procrastination. |
||
486 | if (++$i < $num_files && TIME_STARTED + $this->time_limit < $finished + $max_transform_time * 2) |
||
487 | { |
||
488 | // After all, there's always next time. |
||
489 | if (empty($this->next_task)) |
||
490 | { |
||
491 | $progressfile = $export_dir_slash . $idhash_ext . '.progress.json'; |
||
492 | |||
493 | $new_details = $this->_details; |
||
494 | $new_details['start'] = $smcFunc['json_decode'](file_get_contents($progressfile), true); |
||
495 | |||
496 | $this->next_task = array('$sourcedir/tasks/ExportProfileData.php', 'ExportProfileData_Background', $smcFunc['json_encode']($new_details), time() - MAX_CLAIM_THRESHOLD); |
||
497 | } |
||
498 | |||
499 | // So let's just relax and take a well deserved... |
||
500 | break; |
||
501 | } |
||
502 | } |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Compiles profile data to XML with embedded XSLT. |
||
507 | * |
||
508 | * Internally calls exportXml() and then embeds an XSLT stylesheet into |
||
509 | * the XML so that it can be processed by the client. |
||
510 | * |
||
511 | * @param array $member_info Minimal $user_info about the relevant member. |
||
512 | */ |
||
513 | protected function exportXmlXslt($member_info) |
||
514 | { |
||
515 | global $modSettings, $context, $smcFunc, $sourcedir; |
||
516 | |||
517 | $context['export_last_page'] = $this->_details['last_page']; |
||
518 | $context['export_dlfilename'] = $this->_details['dlfilename']; |
||
519 | |||
520 | // Embedded XSLT requires adding a special DTD and processing instruction in the main XML document. |
||
521 | add_integration_function('integrate_xml_data', 'ExportProfileData_Background::add_dtd', false); |
||
522 | |||
523 | // Perform the export to XML. |
||
524 | $this->exportXml($member_info); |
||
525 | |||
526 | // Make sure we have everything we need. |
||
527 | if (empty(self::$xslt_info['stylesheet'])) |
||
528 | { |
||
529 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'Profile-Export.php'); |
||
530 | self::$xslt_info = get_xslt_stylesheet($this->_details['format'], $this->_details['uid']); |
||
531 | } |
||
532 | if (empty($context['feed']['footer'])) |
||
533 | { |
||
534 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'News.php'); |
||
535 | buildXmlFeed('smf', array(), array_fill_keys(array('title', 'desc', 'source', 'self'), ''), 'profile'); |
||
536 | } |
||
537 | |||
538 | // Find any completed files that don't yet have the stylesheet embedded in them. |
||
539 | $export_dir_slash = $modSettings['export_dir'] . DIRECTORY_SEPARATOR; |
||
540 | $idhash = hash_hmac('sha1', $this->_details['uid'], get_auth_secret()); |
||
541 | $idhash_ext = $idhash . '.' . $this->_details['format_settings']['extension']; |
||
542 | |||
543 | $test_length = strlen(self::$xslt_info['stylesheet'] . $context['feed']['footer']); |
||
544 | |||
545 | $new_exportfiles = array(); |
||
546 | foreach (glob($export_dir_slash . '*_' . $idhash_ext) as $completed_file) |
||
547 | { |
||
548 | if (filesize($completed_file) < $test_length || file_get_contents($completed_file, false, null, $test_length * -1) !== self::$xslt_info['stylesheet'] . $context['feed']['footer']) |
||
549 | $new_exportfiles[] = $completed_file; |
||
550 | } |
||
551 | if (empty($new_exportfiles)) |
||
552 | return; |
||
553 | |||
554 | // Embedding the XSLT means writing to the file yet again. |
||
555 | foreach ($new_exportfiles as $exportfile) |
||
556 | { |
||
557 | $handle = fopen($exportfile, 'r+'); |
||
558 | if (is_resource($handle)) |
||
559 | { |
||
560 | flock($handle, LOCK_EX); |
||
561 | |||
562 | fseek($handle, strlen($context['feed']['footer']) * -1, SEEK_END); |
||
563 | |||
564 | $bytes_written = fwrite($handle, self::$xslt_info['stylesheet'] . $context['feed']['footer']); |
||
565 | |||
566 | // If we couldn't write everything, revert the changes. |
||
567 | if ($bytes_written > 0 && $bytes_written < strlen(self::$xslt_info['stylesheet'] . $context['feed']['footer'])) |
||
568 | { |
||
569 | fseek($handle, $bytes_written * -1, SEEK_END); |
||
570 | $pointer_pos = ftell($handle); |
||
571 | ftruncate($handle, $pointer_pos); |
||
572 | rewind($handle); |
||
573 | fseek($handle, 0, SEEK_END); |
||
574 | fwrite($handle, $context['feed']['footer']); |
||
575 | } |
||
576 | |||
577 | flock($handle, LOCK_UN); |
||
578 | fclose($handle); |
||
579 | } |
||
580 | } |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Adds a custom DOCTYPE definition and an XSLT processing instruction to |
||
585 | * the main XML file's header. |
||
586 | */ |
||
587 | public static function add_dtd(&$xml_data, &$feed_meta, &$namespaces, &$extraFeedTags, &$forceCdataKeys, &$nsKeys, $xml_format, $subaction, &$doctype) |
||
588 | { |
||
589 | global $sourcedir; |
||
590 | |||
591 | require_once($sourcedir . DIRECTORY_SEPARATOR . 'Profile-Export.php'); |
||
592 | self::$xslt_info = get_xslt_stylesheet(self::$export_details['format'], self::$export_details['uid']); |
||
593 | |||
594 | $doctype = self::$xslt_info['doctype']; |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * Adjusts some parse_bbc() parameters for the special case of exports. |
||
599 | */ |
||
600 | public static function pre_parsebbc(&$message, &$smileys, &$cache_id, &$parse_tags) |
||
601 | { |
||
602 | global $modSettings, $context, $user_info; |
||
603 | |||
604 | $cache_id = ''; |
||
605 | |||
606 | if (in_array(self::$export_details['format'], array('HTML', 'XML_XSLT'))) |
||
607 | { |
||
608 | foreach (array('smileys_url', 'attachmentThumbnails') as $var) |
||
609 | if (isset($modSettings[$var])) |
||
610 | self::$real_modSettings[$var] = $modSettings[$var]; |
||
611 | |||
612 | $modSettings['smileys_url'] = '.'; |
||
613 | $modSettings['attachmentThumbnails'] = false; |
||
614 | } |
||
615 | else |
||
616 | { |
||
617 | $smileys = false; |
||
618 | |||
619 | if (!isset($modSettings['disabledBBC'])) |
||
620 | $modSettings['disabledBBC'] = 'attach'; |
||
621 | else |
||
622 | { |
||
623 | self::$real_modSettings['disabledBBC'] = $modSettings['disabledBBC']; |
||
624 | |||
625 | if (strpos($modSettings['disabledBBC'], 'attach') === false) |
||
626 | $modSettings['disabledBBC'] = implode(',', array_merge(array_filter(explode(',', $modSettings['disabledBBC'])), array('attach'))); |
||
627 | } |
||
628 | } |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * Reverses changes made by pre_parsebbc() |
||
633 | */ |
||
634 | public static function post_parsebbc(&$message, &$smileys, &$cache_id, &$parse_tags) |
||
635 | { |
||
636 | global $modSettings, $context; |
||
637 | |||
638 | foreach (array('disabledBBC', 'smileys_url', 'attachmentThumbnails') as $var) |
||
639 | if (isset(self::$real_modSettings[$var])) |
||
640 | $modSettings[$var] = self::$real_modSettings[$var]; |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Adjusts certain BBCodes for the special case of exports. |
||
645 | */ |
||
646 | public static function bbc_codes(&$codes, &$no_autolink_tags) |
||
653 | } |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * Adjusts the attachment download URL for the special case of exports. |
||
658 | */ |
||
659 | public static function post_parseAttachBBC(&$attachContext) |
||
660 | { |
||
661 | global $scripturl, $context; |
||
662 | static $dltokens; |
||
663 | |||
664 | if (empty($dltokens[$context['xmlnews_uid']])) |
||
665 | { |
||
666 | $idhash = hash_hmac('sha1', $context['xmlnews_uid'], get_auth_secret()); |
||
667 | $dltokens[$context['xmlnews_uid']] = hash_hmac('sha1', $idhash, get_auth_secret()); |
||
668 | } |
||
669 | |||
670 | $attachContext['orig_href'] = $scripturl . '?action=profile;area=dlattach;u=' . $context['xmlnews_uid'] . ';attach=' . $attachContext['id'] . ';t=' . $dltokens[$context['xmlnews_uid']]; |
||
671 | $attachContext['href'] = rawurlencode($attachContext['id'] . ' - ' . html_entity_decode($attachContext['name'])); |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Adjusts the format of the HTML produced by the attach BBCode. |
||
676 | */ |
||
677 | public static function attach_bbc_validate(&$returnContext, $currentAttachment, $tag, $data, $disabled, $params) |
||
743 | } |
||
744 | } |
||
745 | } |
||
746 | |||
747 | ?> |