getPagesWithSameLastName($requestedPage); if (sizeof($pagesWithSameName) == 0) { return [null, null]; } return self::getBestEndPageIdFromPages($pagesWithSameName, $requestedPage); } /** * @param MarkupPath $missingPage * @return array with the best page and the type of redirect */ public static function process(MarkupPath $missingPage): array { $return = [null, null]; $minimalScoreForARedirect = SiteConfig::getConfValue(self::CONF_MINIMAL_SCORE_FOR_REDIRECT, self::CONF_MINIMAL_SCORE_FOR_REDIRECT_DEFAULT); list($bestPage, $bestScore) = self::getBestEndPageId($missingPage); if ($bestPage != null) { $redirectType = RouterRedirection::REDIRECT_NOTFOUND_METHOD; if ($minimalScoreForARedirect != 0 && $bestScore >= $minimalScoreForARedirect) { try { Aliases::createForPage($bestPage) ->addAlias($missingPage, AliasType::REDIRECT) ->sendToWriteStore() ->setReadStore(MetadataDbStore::getOrCreateFromResource($bestPage)) ->sendToWriteStore(); } catch (ExceptionBadArgument|ExceptionCompile $e) { LogUtility::error("Error while creating an alias",LogUtility::SUPPORT_CANONICAL,$e); return $return; } $redirectType = RouterRedirection::REDIRECT_PERMANENT_METHOD; } $return = array( $bestPage, $redirectType ); } return $return; } /** * @param MarkupPath[] $candidatePagesWithSameLastName * @param MarkupPath $requestedPage * @return array */ public static function getBestEndPageIdFromPages(array $candidatePagesWithSameLastName, MarkupPath $requestedPage): array { // Default value $bestScore = 0; $bestPage = $candidatePagesWithSameLastName[0]; // The name of the dokuwiki id $requestedPageNames = $requestedPage->getPathObject()->getNames(); // Loop foreach ($candidatePagesWithSameLastName as $candidatePage) { try { if ($candidatePage->getWikiId() === $requestedPage->getWikiId()) { // when the index is not up to date continue; } } catch (ExceptionBadArgument $e) { // should not happen but yeah } $candidatePageNames = $candidatePage->getPathObject()->getNames(); $score = 0; foreach ($candidatePageNames as $candidatePageName) { if (in_array($candidatePageName, $requestedPageNames)) { $score++; } } if ($score > $bestScore) { $bestScore = $score; $bestPage = $candidatePage; } else if ($score === $bestScore) { /** * Best backlink count */ $candidateBacklinksCount = sizeof($candidatePage->getBacklinks()); $bestPageBacklinksCount = sizeof($bestPage->getBacklinks()); if ($candidateBacklinksCount > $bestPageBacklinksCount) { $bestPage = $candidatePage; } } } return array( $bestPage, $bestScore ); } }