Пример #1
0
function wfMatchFamily($args)
{
    global $wgUser, $wgAjaxCachePolicy;
    $wgAjaxCachePolicy->setPolicy(0);
    $status = GE_SUCCESS;
    $result = '';
    $gedcomId = GedcomUtil::getGedcomId($args);
    if (!$wgUser->isLoggedIn()) {
        $status = GE_NOT_LOGGED_IN;
    } else {
        if ($wgUser->isBlocked() || wfReadOnly()) {
            $status = GE_NOT_AUTHORIZED;
        } else {
            if (!$args || !$gedcomId || !preg_match('/^<gedcom [^>]*id="([^"]+)"/', $args, $idMatches) || !preg_match('/^<gedcom [^>]*match="([^"]+)"/', $args, $titleMatches)) {
                $status = GE_INVALID_ARG;
            } else {
                // calc family match scores
                $id = $idMatches[1];
                $matchTitle = Title::newFromText($titleMatches[1], NS_FAMILY);
                $matchTitleString = $matchTitle->getText();
                $gedcomData = GedcomUtil::getGedcomDataMap($args);
                $gedcomTitleString = $gedcomData[$id]['title'];
                if (strpos($gedcomTitleString, 'Person:') === 0 || strpos($gedcomTitleString, 'Family:') === 0) {
                    $gedcomTitleString = substr($gedcomTitleString, 7);
                }
                $compare = new CompareForm('Family', array($gedcomTitleString, $matchTitleString), $gedcomData, $args);
                list($compareData, $compareChildren, $maxChildren) = $compare->readCompareData();
                $dataLabels = $compare->getDataLabels();
                list($compareClass, $husbandScores, $wifeScores, $totalScores) = $compare->scoreCompareData($dataLabels, $compareData);
                // matched if not multiple husbands/wives, total score > threshold, husband + wife scores > threshold, and all gedcom children matched
                $gedcomHusbandCount = count($compareData[$gedcomTitleString]['husbandTitle']);
                $gedcomWifeCount = count($compareData[$gedcomTitleString]['wifeTitle']);
                $matchHusbandCount = count($compareData[$matchTitleString]['husbandTitle']);
                $matchWifeCount = count($compareData[$matchTitleString]['wifeTitle']);
                $matched = $gedcomHusbandCount <= 1 && $gedcomWifeCount <= 1 && $matchHusbandCount <= 1 && $matchWifeCount <= 1 && $totalScores[1] >= 4 && ($gedcomHusbandCount == 0 || $matchHusbandCount == 0 || $husbandScores[1] > 0) && ($gedcomWifeCount == 0 || $matchWifeCount == 0 || $wifeScores[1] > 0);
                //wfDebug("MATCHCOMPARE gedcomTitle=$gedcomTitleString matchTitle=$matchTitleString ghc=$gedcomHusbandCount gwc=$gedcomWifeCount mhc=$matchHusbandCount mwc=$matchWifeCount".
                //			" totalScore={$totalScores[1]} husbandScore={$husbandScores[1]} wifeScore={$wifeScores[1]} matched=$matched\n");
                if ($matched) {
                    $unmatchedGedcomChild = false;
                    $unmatchedWikiChild = false;
                    for ($i = 0; $i < $maxChildren; $i++) {
                        if (count(@$compareChildren[$gedcomTitleString][$i]['childTitle']) == 1 && count(@$compareChildren[$matchTitleString][$i]['childTitle']) != 1) {
                            $unmatchedGedcomChild = true;
                        } else {
                            if (count(@$compareChildren[$gedcomTitleString][$i]['childTitle']) != 1 && count(@$compareChildren[$matchTitleString][$i]['childTitle']) == 1) {
                                $unmatchedWikiChild = true;
                            }
                        }
                    }
                    if ($unmatchedGedcomChild && $unmatchedWikiChild) {
                        // could be a mismatched child
                        $matched = false;
                        //wfDebug("MATCHCOMPARE failed on children\n");
                    }
                }
                if ($matched) {
                    // save matches and return related matches
                    $keys = array();
                    $keys[] = $id;
                    $matches = array();
                    $matches[] = $matchTitle->getPrefixedText();
                    $merged = array();
                    $merged[] = 'false';
                    if ($gedcomHusbandCount == 1 && $matchHusbandCount == 1) {
                        $key = GedcomUtil::getKeyFromTitle($compareData[$gedcomTitleString]['husbandTitle'][0]);
                        if ($key) {
                            // not already merged
                            $keys[] = $key;
                            $matches[] = 'Person:' . $compareData[$matchTitleString]['husbandTitle'][0];
                            $merged[] = 'false';
                        }
                    }
                    if ($gedcomWifeCount == 1 && $matchWifeCount == 1) {
                        $key = GedcomUtil::getKeyFromTitle($compareData[$gedcomTitleString]['wifeTitle'][0]);
                        if ($key) {
                            $keys[] = $key;
                            $matches[] = 'Person:' . $compareData[$matchTitleString]['wifeTitle'][0];
                            $merged[] = 'false';
                        }
                    }
                    for ($i = 0; $i < $maxChildren; $i++) {
                        if (count(@$compareChildren[$gedcomTitleString][$i]['childTitle']) == 1 && count(@$compareChildren[$matchTitleString][$i]['childTitle']) == 1) {
                            $key = GedcomUtil::getKeyFromTitle($compareChildren[$gedcomTitleString][$i]['childTitle'][0]);
                            if ($key) {
                                $keys[] = $key;
                                $matches[] = 'Person:' . $compareChildren[$matchTitleString][$i]['childTitle'][0];
                                $merged[] = 'false';
                            }
                        }
                    }
                    $status = fgUpdateMatches($gedcomId, $keys, $matches, $merged);
                    if ($status == GE_SUCCESS) {
                        $result = fgGetRelatedMatches($keys, $matches);
                    }
                } else {
                    // save as potential match and return
                    $keys = array($id);
                    $matches = array($matchTitleString);
                    $status = fgUpdatePotentialMatches($gedcomId, $keys, $matches);
                    if ($status == GE_SUCCESS) {
                        $result = '<match id="' . StructuredData::escapeXml($id) . '" matches="' . StructuredData::escapeXml($matchTitleString) . '"/>';
                    }
                }
            }
        }
    }
    return "<matchfamily status=\"{$status}\">{$result}</matchfamily>";
}