From d34b2090c00d68baee95b67b16b72238c12eeb63 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Sun, 3 Sep 2017 09:35:11 +0200 Subject: [PATCH] Harden test for redirects. - Fix: Do not crash when testing redirect target on empty page. --- includes/Target.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/includes/Target.php b/includes/Target.php index 9d61426..451c6e1 100644 --- a/includes/Target.php +++ b/includes/Target.php @@ -73,7 +73,6 @@ class Target { * @param String &$title Title of the target page */ public function __construct( $namespace, $title, Config &$config ) { - // print "\n>>>namespace=$namespace;title=$title<<<\n"; $this->title = \Title::makeTitleSafe( $namespace, $title ); $this->titleValue = $this->title->getTitleValue(); $this->config = $config; @@ -95,7 +94,7 @@ class Target { } public function getPrefixedTitleText() { - return $this->getNsPrefix() . $this->getTitleText(); + return $this->title->getPrefixedText(); } /** @@ -204,11 +203,8 @@ class Target { // If checking for redirects is enabled and the target page does // indeed redirect to the current page, return the page title as-is // (unlinked). - if ( $this->config->checkRedirect ) { - $redirectTitle = $this->getContent()->getUltimateRedirectTarget(); - if ( $redirectTitle && $redirectTitle->equals( $source->getTitle() ) ) { - return false; - } + if ( $this->config->checkRedirect && $this->redirectsTo( $source ) ) { + return false; }; // If the magic word __NOAUTOLINKTARGET__ is enabled and the target // page does indeed contain this magic word, return the page title @@ -229,4 +225,16 @@ class Target { public function isSameTitle( Source $source) { return $this->title->equals( $source->getTitle() ); } + + /** + * Checks whether this target redirects to the source. + * @param Source $source Source page. + * @return bool True if the target redirects to the source. + */ + public function redirectsTo( $source ) { + if ( $this->getContent() ) { + $redirectTitle = $this->getContent()->getUltimateRedirectTarget(); + return $redirectTitle && $redirectTitle->equals( $source->getTitle() ); + } + } }