Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like GedcomRecord 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 GedcomRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class GedcomRecord { |
||
| 27 | const RECORD_TYPE = 'UNKNOWN'; |
||
| 28 | const URL_PREFIX = 'gedrecord.php?pid='; |
||
| 29 | |||
| 30 | /** @var string The record identifier */ |
||
| 31 | protected $xref; |
||
| 32 | |||
| 33 | /** @var Tree The family tree to which this record belongs */ |
||
| 34 | protected $tree; |
||
| 35 | |||
| 36 | /** @var string GEDCOM data (before any pending edits) */ |
||
| 37 | protected $gedcom; |
||
| 38 | |||
| 39 | /** @var string|null GEDCOM data (after any pending edits) */ |
||
| 40 | protected $pending; |
||
| 41 | |||
| 42 | /** @var Fact[] facts extracted from $gedcom/$pending */ |
||
| 43 | protected $facts; |
||
| 44 | |||
| 45 | /** @var bool Can we display details of this record to Auth::PRIV_PRIVATE */ |
||
| 46 | private $disp_public; |
||
| 47 | |||
| 48 | /** @var bool Can we display details of this record to Auth::PRIV_USER */ |
||
| 49 | private $disp_user; |
||
| 50 | |||
| 51 | /** @var bool Can we display details of this record to Auth::PRIV_NONE */ |
||
| 52 | private $disp_none; |
||
| 53 | |||
| 54 | /** @var string[][] All the names of this individual */ |
||
| 55 | protected $_getAllNames; |
||
| 56 | |||
| 57 | /** @var int Cached result */ |
||
| 58 | protected $_getPrimaryName; |
||
| 59 | |||
| 60 | /** @var int Cached result */ |
||
| 61 | protected $_getSecondaryName; |
||
| 62 | |||
| 63 | /** @var GedcomRecord[][] Allow getInstance() to return references to existing objects */ |
||
| 64 | protected static $gedcom_record_cache; |
||
| 65 | |||
| 66 | /** @var \stdClass[][] Fetch all pending edits in one database query */ |
||
| 67 | private static $pending_record_cache; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Create a GedcomRecord object from raw GEDCOM data. |
||
| 71 | * |
||
| 72 | * @param string $xref |
||
| 73 | * @param string $gedcom an empty string for new/pending records |
||
| 74 | * @param string|null $pending null for a record with no pending edits, |
||
| 75 | * empty string for records with pending deletions |
||
| 76 | * @param Tree $tree |
||
| 77 | */ |
||
| 78 | public function __construct($xref, $gedcom, $pending, $tree) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Split the record into facts |
||
| 89 | */ |
||
| 90 | private function parseFacts() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get an instance of a GedcomRecord object. For single records, |
||
| 125 | * we just receive the XREF. For bulk records (such as lists |
||
| 126 | * and search results) we can receive the GEDCOM data as well. |
||
| 127 | * |
||
| 128 | * @param string $xref |
||
| 129 | * @param Tree $tree |
||
| 130 | * @param string|null $gedcom |
||
| 131 | * |
||
| 132 | * @throws \Exception |
||
| 133 | * |
||
| 134 | * @return GedcomRecord|Individual|Family|Source|Repository|Media|Note|null |
||
| 135 | */ |
||
| 136 | public static function getInstance($xref, Tree $tree, $gedcom = null) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Fetch data from the database |
||
| 232 | * |
||
| 233 | * @param string $xref |
||
| 234 | * @param int $tree_id |
||
| 235 | * |
||
| 236 | * @return null|string |
||
| 237 | */ |
||
| 238 | protected static function fetchGedcomRecord($xref, $tree_id) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get the XREF for this record |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function getXref() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the tree to which this record belongs |
||
| 285 | * |
||
| 286 | * @return Tree |
||
| 287 | */ |
||
| 288 | public function getTree() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Application code should access data via Fact objects. |
||
| 294 | * This function exists to support old code. |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getGedcom() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Does this record have a pending change? |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function isPendingAddtion() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Does this record have a pending deletion? |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function isPendingDeletion() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Generate a URL to this record, suitable for use in HTML, etc. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getHtmlUrl() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Generate a URL to this record, suitable for use in javascript, HTTP headers, etc. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | public function getRawUrl() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Generate a URL to this record. |
||
| 344 | * |
||
| 345 | * @param string $separator |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | private function getLinkUrl($separator) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Work out whether this record can be shown to a user with a given access level |
||
| 355 | * |
||
| 356 | * @param int $access_level |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | private function canShowRecord($access_level) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Each object type may have its own special rules, and re-implement this function. |
||
| 399 | * |
||
| 400 | * @param int $access_level |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | protected function canShowByType($access_level) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Can the details of this record be shown? |
||
| 418 | * |
||
| 419 | * @param int|null $access_level |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public function canShow($access_level = null) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Can the name of this record be shown? |
||
| 461 | * |
||
| 462 | * @param int|null $access_level |
||
| 463 | * |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function canShowName($access_level = null) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Can we edit this record? |
||
| 476 | * |
||
| 477 | * @return bool |
||
| 478 | */ |
||
| 479 | public function canEdit() { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Remove private data from the raw gedcom record. |
||
| 485 | * Return both the visible and invisible data. We need the invisible data when editing. |
||
| 486 | * |
||
| 487 | * @param int $access_level |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | public function privatizeGedcom($access_level) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Generate a private version of this record |
||
| 516 | * |
||
| 517 | * @param int $access_level |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | protected function createPrivateGedcomRecord($access_level) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Convert a name record into sortable and full/display versions. This default |
||
| 527 | * should be OK for simple record types. INDI/FAM records will need to redefine it. |
||
| 528 | * |
||
| 529 | * @param string $type |
||
| 530 | * @param string $value |
||
| 531 | * @param string $gedcom |
||
| 532 | */ |
||
| 533 | protected function addName($type, $value, $gedcom) { |
||
| 534 | $this->_getAllNames[] = [ |
||
| 535 | 'type' => $type, |
||
| 536 | 'sort' => preg_replace_callback('/([0-9]+)/', function ($matches) { |
||
| 537 | return str_pad($matches[0], 10, '0', STR_PAD_LEFT); |
||
| 538 | }, $value), |
||
| 539 | 'full' => '<span dir="auto">' . Html::escape($value) . '</span>', // This is used for display |
||
| 540 | 'fullNN' => $value, // This goes into the database |
||
| 541 | ]; |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get all the names of a record, including ROMN, FONE and _HEB alternatives. |
||
| 546 | * Records without a name (e.g. FAM) will need to redefine this function. |
||
| 547 | * Parameters: the level 1 fact containing the name. |
||
| 548 | * Return value: an array of name structures, each containing |
||
| 549 | * ['type'] = the gedcom fact, e.g. NAME, TITL, FONE, _HEB, etc. |
||
| 550 | * ['full'] = the name as specified in the record, e.g. 'Vincent van Gogh' or 'John Unknown' |
||
| 551 | * ['sort'] = a sortable version of the name (not for display), e.g. 'Gogh, Vincent' or '@N.N., John' |
||
| 552 | * |
||
| 553 | * @param int $level |
||
| 554 | * @param string $fact_type |
||
| 555 | * @param Fact[] $facts |
||
| 556 | */ |
||
| 557 | protected function extractNamesFromFacts($level, $fact_type, $facts) { |
||
| 558 | $sublevel = $level + 1; |
||
| 559 | $subsublevel = $sublevel + 1; |
||
| 560 | foreach ($facts as $fact) { |
||
| 561 | if (preg_match_all("/^{$level} ({$fact_type}) (.+)((\n[{$sublevel}-9].+)*)/m", $fact->getGedcom(), $matches, PREG_SET_ORDER)) { |
||
| 562 | foreach ($matches as $match) { |
||
| 563 | // Treat 1 NAME / 2 TYPE married the same as _MARNM |
||
| 564 | if ($match[1] == 'NAME' && strpos($match[3], "\n2 TYPE married") !== false) { |
||
| 565 | $this->addName('_MARNM', $match[2], $fact->getGedcom()); |
||
| 566 | } else { |
||
| 567 | $this->addName($match[1], $match[2], $fact->getGedcom()); |
||
| 568 | } |
||
| 569 | if ($match[3] && preg_match_all("/^{$sublevel} (ROMN|FONE|_\w+) (.+)((\n[{$subsublevel}-9].+)*)/m", $match[3], $submatches, PREG_SET_ORDER)) { |
||
| 570 | foreach ($submatches as $submatch) { |
||
| 571 | $this->addName($submatch[1], $submatch[2], $match[3]); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Default for "other" object types |
||
| 581 | */ |
||
| 582 | public function extractNames() { |
||
| 583 | $this->addName(static::RECORD_TYPE, $this->getFallBackName(), null); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Derived classes should redefine this function, otherwise the object will have no name |
||
| 588 | * |
||
| 589 | * @return string[][] |
||
| 590 | */ |
||
| 591 | public function getAllNames() { |
||
| 592 | if ($this->_getAllNames === null) { |
||
| 593 | $this->_getAllNames = []; |
||
| 594 | if ($this->canShowName()) { |
||
| 595 | // Ask the record to extract its names |
||
| 596 | $this->extractNames(); |
||
| 597 | // No name found? Use a fallback. |
||
| 598 | if (!$this->_getAllNames) { |
||
| 599 | $this->addName(static::RECORD_TYPE, $this->getFallBackName(), null); |
||
| 600 | } |
||
| 601 | } else { |
||
| 602 | $this->addName(static::RECORD_TYPE, I18N::translate('Private'), null); |
||
| 603 | } |
||
| 604 | } |
||
| 605 | |||
| 606 | return $this->_getAllNames; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * If this object has no name, what do we call it? |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public function getFallBackName() { |
||
| 615 | return Html::escape($this->getXref()); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Which of the (possibly several) names of this record is the primary one. |
||
| 620 | * |
||
| 621 | * @return int |
||
| 622 | */ |
||
| 623 | public function getPrimaryName() { |
||
| 624 | static $language_script; |
||
| 625 | |||
| 626 | if ($language_script === null) { |
||
| 627 | $language_script = I18N::languageScript(WT_LOCALE); |
||
| 628 | } |
||
| 629 | |||
| 630 | if ($this->_getPrimaryName === null) { |
||
| 631 | // Generally, the first name is the primary one.... |
||
| 632 | $this->_getPrimaryName = 0; |
||
| 633 | // ...except when the language/name use different character sets |
||
| 634 | if (count($this->getAllNames()) > 1) { |
||
| 635 | foreach ($this->getAllNames() as $n => $name) { |
||
| 636 | if (I18N::textScript($name['sort']) === $language_script) { |
||
| 637 | $this->_getPrimaryName = $n; |
||
| 638 | break; |
||
| 639 | } |
||
| 640 | } |
||
| 641 | } |
||
| 642 | } |
||
| 643 | |||
| 644 | return $this->_getPrimaryName; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Which of the (possibly several) names of this record is the secondary one. |
||
| 649 | * |
||
| 650 | * @return int |
||
| 651 | */ |
||
| 652 | public function getSecondaryName() { |
||
| 653 | if (is_null($this->_getSecondaryName)) { |
||
| 654 | // Generally, the primary and secondary names are the same |
||
| 655 | $this->_getSecondaryName = $this->getPrimaryName(); |
||
| 656 | // ....except when there are names with different character sets |
||
| 657 | $all_names = $this->getAllNames(); |
||
| 658 | if (count($all_names) > 1) { |
||
| 659 | $primary_script = I18N::textScript($all_names[$this->getPrimaryName()]['sort']); |
||
| 660 | foreach ($all_names as $n => $name) { |
||
| 661 | if ($n != $this->getPrimaryName() && $name['type'] != '_MARNM' && I18N::textScript($name['sort']) != $primary_script) { |
||
| 662 | $this->_getSecondaryName = $n; |
||
| 663 | break; |
||
| 664 | } |
||
| 665 | } |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | return $this->_getSecondaryName; |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Allow the choice of primary name to be overidden, e.g. in a search result |
||
| 674 | * |
||
| 675 | * @param int $n |
||
| 676 | */ |
||
| 677 | public function setPrimaryName($n) { |
||
| 678 | $this->_getPrimaryName = $n; |
||
| 679 | $this->_getSecondaryName = null; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Allow native PHP functions such as array_unique() to work with objects |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function __toString() { |
||
| 688 | return $this->xref . '@' . $this->tree->getTreeId(); |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Static helper function to sort an array of objects by name |
||
| 693 | * Records whose names cannot be displayed are sorted at the end. |
||
| 694 | * |
||
| 695 | * @param GedcomRecord $x |
||
| 696 | * @param GedcomRecord $y |
||
| 697 | * |
||
| 698 | * @return int |
||
| 699 | */ |
||
| 700 | public static function compare(GedcomRecord $x, GedcomRecord $y) { |
||
| 701 | if ($x->canShowName()) { |
||
| 702 | if ($y->canShowName()) { |
||
| 703 | return I18N::strcasecmp($x->getSortName(), $y->getSortName()); |
||
| 704 | } else { |
||
| 705 | return -1; // only $y is private |
||
| 706 | } |
||
| 707 | } else { |
||
| 708 | if ($y->canShowName()) { |
||
| 709 | return 1; // only $x is private |
||
| 710 | } else { |
||
| 711 | return 0; // both $x and $y private |
||
| 712 | } |
||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get variants of the name |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | public function getFullName() { |
||
| 722 | if ($this->canShowName()) { |
||
| 723 | $tmp = $this->getAllNames(); |
||
| 724 | |||
| 725 | return $tmp[$this->getPrimaryName()]['full']; |
||
| 726 | } else { |
||
| 727 | return I18N::translate('Private'); |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get a sortable version of the name. Do not display this! |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | public function getSortName() { |
||
| 737 | // The sortable name is never displayed, no need to call canShowName() |
||
| 738 | $tmp = $this->getAllNames(); |
||
| 739 | |||
| 740 | return $tmp[$this->getPrimaryName()]['sort']; |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Get the full name in an alternative character set |
||
| 745 | * |
||
| 746 | * @return null|string |
||
| 747 | */ |
||
| 748 | public function getAddName() { |
||
| 749 | if ($this->canShowName() && $this->getPrimaryName() != $this->getSecondaryName()) { |
||
| 750 | $all_names = $this->getAllNames(); |
||
| 751 | |||
| 752 | return $all_names[$this->getSecondaryName()]['full']; |
||
| 753 | } else { |
||
| 754 | return null; |
||
| 755 | } |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Format this object for display in a list |
||
| 760 | * If $find is set, then we are displaying items from a selection list. |
||
| 761 | * $name allows us to use something other than the record name. |
||
| 762 | * |
||
| 763 | * @param string $tag |
||
| 764 | * @param bool $find |
||
| 765 | * @param null $name |
||
| 766 | * |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | public function formatList($tag = 'li', $find = false, $name = null) { |
||
| 770 | if (is_null($name)) { |
||
| 771 | $name = $this->getFullName(); |
||
| 772 | } |
||
| 773 | $html = '<a href="' . $this->getHtmlUrl() . '"'; |
||
| 774 | if ($find) { |
||
| 775 | $html .= ' onclick="pasteid(\'' . $this->getXref() . '\', \'' . htmlentities($name) . '\');"'; |
||
| 776 | } |
||
| 777 | $html .= ' class="list_item"><b>' . $name . '</b>'; |
||
| 778 | $html .= $this->formatListDetails(); |
||
| 779 | $html = '<' . $tag . '>' . $html . '</a></' . $tag . '>'; |
||
| 780 | |||
| 781 | return $html; |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * This function should be redefined in derived classes to show any major |
||
| 786 | * identifying characteristics of this record. |
||
| 787 | * |
||
| 788 | * @return string |
||
| 789 | */ |
||
| 790 | public function formatListDetails() { |
||
| 791 | return ''; |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Extract/format the first fact from a list of facts. |
||
| 796 | * |
||
| 797 | * @param string $facts |
||
| 798 | * @param int $style |
||
| 799 | * |
||
| 800 | * @return string |
||
| 801 | */ |
||
| 802 | public function formatFirstMajorFact($facts, $style) { |
||
| 803 | foreach ($this->getFacts($facts, true) as $event) { |
||
| 804 | // Only display if it has a date or place (or both) |
||
| 805 | if ($event->getDate()->isOK() && !$event->getPlace()->isEmpty()) { |
||
| 806 | $joiner = ' — '; |
||
| 807 | } else { |
||
| 808 | $joiner = ''; |
||
| 809 | } |
||
| 810 | if ($event->getDate()->isOK() || !$event->getPlace()->isEmpty()) { |
||
| 811 | switch ($style) { |
||
| 812 | View Code Duplication | case 1: |
|
| 813 | return '<br><em>' . $event->getLabel() . ' ' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</em>'; |
||
| 814 | View Code Duplication | case 2: |
|
| 815 | return '<dl><dt class="label">' . $event->getLabel() . '</dt><dd class="field">' . FunctionsPrint::formatFactDate($event, $this, false, false) . $joiner . FunctionsPrint::formatFactPlace($event) . '</dd></dl>'; |
||
| 816 | } |
||
| 817 | } |
||
| 818 | } |
||
| 819 | |||
| 820 | return ''; |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Find individuals linked to this record. |
||
| 825 | * |
||
| 826 | * @param string $link |
||
| 827 | * |
||
| 828 | * @return Individual[] |
||
| 829 | */ |
||
| 830 | View Code Duplication | public function linkedIndividuals($link) { |
|
| 831 | $rows = Database::prepare( |
||
| 832 | "SELECT i_id AS xref, i_gedcom AS gedcom" . |
||
| 833 | " FROM `##individuals`" . |
||
| 834 | " JOIN `##link` ON i_file = l_file AND i_id = l_from" . |
||
| 835 | " LEFT JOIN `##name` ON i_file = n_file AND i_id = n_id AND n_num = 0" . |
||
| 836 | " WHERE i_file = :tree_id AND l_type = :link AND l_to = :xref" . |
||
| 837 | " ORDER BY n_sort COLLATE :collation" |
||
| 838 | )->execute([ |
||
| 839 | 'tree_id' => $this->tree->getTreeId(), |
||
| 840 | 'link' => $link, |
||
| 841 | 'xref' => $this->xref, |
||
| 842 | 'collation' => I18N::collation(), |
||
| 843 | ])->fetchAll(); |
||
| 844 | |||
| 845 | $list = []; |
||
| 846 | foreach ($rows as $row) { |
||
| 847 | $record = Individual::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 848 | if ($record->canShowName()) { |
||
| 849 | $list[] = $record; |
||
| 850 | } |
||
| 851 | } |
||
| 852 | |||
| 853 | return $list; |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Find families linked to this record. |
||
| 858 | * |
||
| 859 | * @param string $link |
||
| 860 | * |
||
| 861 | * @return Family[] |
||
| 862 | */ |
||
| 863 | View Code Duplication | public function linkedFamilies($link) { |
|
| 864 | $rows = Database::prepare( |
||
| 865 | "SELECT f_id AS xref, f_gedcom AS gedcom" . |
||
| 866 | " FROM `##families`" . |
||
| 867 | " JOIN `##link` ON f_file = l_file AND f_id = l_from" . |
||
| 868 | " LEFT JOIN `##name` ON f_file = n_file AND f_id = n_id AND n_num = 0" . |
||
| 869 | " WHERE f_file = :tree_id AND l_type = :link AND l_to = :xref" |
||
| 870 | )->execute([ |
||
| 871 | 'tree_id' => $this->tree->getTreeId(), |
||
| 872 | 'link' => $link, |
||
| 873 | 'xref' => $this->xref, |
||
| 874 | ])->fetchAll(); |
||
| 875 | |||
| 876 | $list = []; |
||
| 877 | foreach ($rows as $row) { |
||
| 878 | $record = Family::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 879 | if ($record->canShowName()) { |
||
| 880 | $list[] = $record; |
||
| 881 | } |
||
| 882 | } |
||
| 883 | |||
| 884 | return $list; |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Find sources linked to this record. |
||
| 889 | * |
||
| 890 | * @param string $link |
||
| 891 | * |
||
| 892 | * @return Source[] |
||
| 893 | */ |
||
| 894 | View Code Duplication | public function linkedSources($link) { |
|
| 895 | $rows = Database::prepare( |
||
| 896 | "SELECT s_id AS xref, s_gedcom AS gedcom" . |
||
| 897 | " FROM `##sources`" . |
||
| 898 | " JOIN `##link` ON s_file = l_file AND s_id = l_from" . |
||
| 899 | " WHERE s_file = :tree_id AND l_type = :link AND l_to = :xref" . |
||
| 900 | " ORDER BY s_name COLLATE :collation" |
||
| 901 | )->execute([ |
||
| 902 | 'tree_id' => $this->tree->getTreeId(), |
||
| 903 | 'link' => $link, |
||
| 904 | 'xref' => $this->xref, |
||
| 905 | 'collation' => I18N::collation(), |
||
| 906 | ])->fetchAll(); |
||
| 907 | |||
| 908 | $list = []; |
||
| 909 | foreach ($rows as $row) { |
||
| 910 | $record = Source::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 911 | if ($record->canShowName()) { |
||
| 912 | $list[] = $record; |
||
| 913 | } |
||
| 914 | } |
||
| 915 | |||
| 916 | return $list; |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Find media objects linked to this record. |
||
| 921 | * |
||
| 922 | * @param string $link |
||
| 923 | * |
||
| 924 | * @return Media[] |
||
| 925 | */ |
||
| 926 | public function linkedMedia($link) { |
||
| 927 | $rows = Database::prepare( |
||
| 928 | "SELECT m_id AS xref, m_gedcom AS gedcom" . |
||
| 929 | " FROM `##media`" . |
||
| 930 | " JOIN `##link` ON m_file = l_file AND m_id = l_from" . |
||
| 931 | " WHERE m_file = :tree_id AND l_type = :link AND l_to = :xref" |
||
| 932 | )->execute([ |
||
| 933 | 'tree_id' => $this->tree->getTreeId(), |
||
| 934 | 'link' => $link, |
||
| 935 | 'xref' => $this->xref, |
||
| 936 | ])->fetchAll(); |
||
| 937 | |||
| 938 | $list = []; |
||
| 939 | foreach ($rows as $row) { |
||
| 940 | $record = Media::getInstance($row->xref, $this->tree, $row->gedcom); |
||
| 941 | if ($record->canShowName()) { |
||
| 942 | $list[] = $record; |
||
| 943 | } |
||
| 944 | } |
||
| 945 | |||
| 946 | return $list; |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Find notes linked to this record. |
||
| 951 | * |
||
| 952 | * @param string $link |
||
| 953 | * |
||
| 954 | * @return Note[] |
||
| 955 | */ |
||
| 956 | View Code Duplication | public function linkedNotes($link) { |
|
| 981 | |||
| 982 | /** |
||
| 983 | * Find repositories linked to this record. |
||
| 984 | * |
||
| 985 | * @param string $link |
||
| 986 | * |
||
| 987 | * @return Repository[] |
||
| 988 | */ |
||
| 989 | View Code Duplication | public function linkedRepositories($link) { |
|
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Get all attributes (e.g. DATE or PLAC) from an event (e.g. BIRT or MARR). |
||
| 1017 | * This is used to display multiple events on the individual/family lists. |
||
| 1018 | * Multiple events can exist because of uncertainty in dates, dates in different |
||
| 1019 | * calendars, place-names in both latin and hebrew character sets, etc. |
||
| 1020 | * It also allows us to combine dates/places from different events in the summaries. |
||
| 1021 | * |
||
| 1022 | * @param string $event_type |
||
| 1023 | * |
||
| 1024 | * @return Date[] |
||
| 1025 | */ |
||
| 1026 | public function getAllEventDates($event_type) { |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Get all the places for a particular type of event |
||
| 1039 | * |
||
| 1040 | * @param string $event_type |
||
| 1041 | * |
||
| 1042 | * @return Place[] |
||
| 1043 | */ |
||
| 1044 | public function getAllEventPlaces($event_type) { |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Get the first (i.e. prefered) Fact for the given fact type |
||
| 1059 | * |
||
| 1060 | * @param string $tag |
||
| 1061 | * |
||
| 1062 | * @return Fact|null |
||
| 1063 | */ |
||
| 1064 | public function getFirstFact($tag) { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * The facts and events for this record. |
||
| 1076 | * |
||
| 1077 | * @param string $filter |
||
| 1078 | * @param bool $sort |
||
| 1079 | * @param int|null $access_level |
||
| 1080 | * @param bool $override Include private records, to allow us to implement $SHOW_PRIVATE_RELATIONSHIPS and $SHOW_LIVING_NAMES. |
||
| 1081 | * |
||
| 1082 | * @return Fact[] |
||
| 1083 | */ |
||
| 1084 | public function getFacts($filter = null, $sort = false, $access_level = null, $override = false) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Get the last-change timestamp for this record, either as a formatted string |
||
| 1106 | * (for display) or as a unix timestamp (for sorting) |
||
| 1107 | * |
||
| 1108 | * @param bool $sorting |
||
| 1109 | * |
||
| 1110 | * @return string |
||
| 1111 | */ |
||
| 1112 | public function lastChangeTimestamp($sorting = false) { |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Get the last-change user for this record |
||
| 1142 | * |
||
| 1143 | * @return string |
||
| 1144 | */ |
||
| 1145 | public function lastChangeUser() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Add a new fact to this record |
||
| 1162 | * |
||
| 1163 | * @param string $gedcom |
||
| 1164 | * @param bool $update_chan |
||
| 1165 | */ |
||
| 1166 | public function createFact($gedcom, $update_chan) { |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Delete a fact from this record |
||
| 1172 | * |
||
| 1173 | * @param string $fact_id |
||
| 1174 | * @param bool $update_chan |
||
| 1175 | */ |
||
| 1176 | public function deleteFact($fact_id, $update_chan) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Replace a fact with a new gedcom data. |
||
| 1182 | * |
||
| 1183 | * @param string $fact_id |
||
| 1184 | * @param string $gedcom |
||
| 1185 | * @param bool $update_chan |
||
| 1186 | * |
||
| 1187 | * @throws \Exception |
||
| 1188 | */ |
||
| 1189 | public function updateFact($fact_id, $gedcom, $update_chan) { |
||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Update this record |
||
| 1257 | * |
||
| 1258 | * @param string $gedcom |
||
| 1259 | * @param bool $update_chan |
||
| 1260 | */ |
||
| 1261 | public function updateRecord($gedcom, $update_chan) { |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Delete this record |
||
| 1300 | */ |
||
| 1301 | public function deleteRecord() { |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Remove all links from this record to $xref |
||
| 1328 | * |
||
| 1329 | * @param string $xref |
||
| 1330 | * @param bool $update_chan |
||
| 1331 | */ |
||
| 1332 | public function removeLinks($xref, $update_chan) { |
||
| 1349 | } |
||
| 1350 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: