From cf792072ddfda4c85a732417fc615cb1897d6e2e Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Mon, 2 Jun 2014 18:55:02 +0200 Subject: [PATCH] Prevent creating indirect links to self. Added code to check if a target page redirects to the current page, and prevents linking to that target page. Note that this does not catch situations where a page title exists twice, once with first capital letter and once with first lowercase letter (should happen on test systems only). --- LinkTitles.body.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 35eb95e..b61a10c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -183,10 +183,26 @@ $targetPage = WikiPage::factory($targetTitle); $targetText = $targetPage->getText(); - // Only proceed if we're not operating on the very same page - if ( ! ( $myTitle->equals($targetTitle) || - $noAutoLinkTarget->match($targetText) ) ) { + // To prevent linking to pages that redirect to the current page, + // obtain the title that the target page redirects to. Will be null + // if there is no redirect. + $redirectTitle = $targetPage->getRedirectTarget(); + if ( $redirectTitle ) { + // If the target page redirects to the current page, exit the + // function. + if ( $redirectTitle->equals($myTitle) ) { + continue; + } + } + if ( $noAutoLinkTarget->match($targetText) ) { + continue; + } + + // Only proceed if we're not operating on the very same page, if the + // target page does not have the __NOAUTOLINKTARGET__ magic word in + // it, and if the target page does not redirect to the current page. + if ( ! $myTitle->equals($targetTitle) ) { // split the string by [[...]] groups // credits to inhan @ StackOverflow for suggesting preg_split // see http://stackoverflow.com/questions/10672286