From d03c63eb737da8a7c7c7dc28b37c4ca133cb4bac Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sun, 18 Nov 2007 11:10:26 +0000 Subject: [PATCH] Inserted two hooks for extension-defined colour codes, and simplified the way link colours are passed. Deprecated makeStubLink and makeStubLinkObj --- includes/Linker.php | 72 ++++++++++++++++++++++++++++++--------------- includes/Parser.php | 9 ++---- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/includes/Linker.php b/includes/Linker.php index 94f6b05c10..9917c88aa1 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -52,15 +52,13 @@ class Linker { } /** @todo document */ - function getInternalLinkAttributes( $link, $text, $broken = false ) { + function getInternalLinkAttributes( $link, $text, $class = false ) { $link = urldecode( $link ); $link = str_replace( '_', ' ', $link ); $link = htmlspecialchars( $link ); - if( $broken == 'stub' ) { - $r = ' class="stub"'; - } else if ( $broken == 'yes' ) { - $r = ' class="new"'; + if( $class ) { + $r = ' class="'.$class.'"'; } else { $r = ''; } @@ -74,11 +72,9 @@ class Linker { * @param $text String: FIXME * @param $broken Boolean: FIXME, default 'false'. */ - function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) { - if( $broken == 'stub' ) { - $r = ' class="stub"'; - } else if ( $broken == 'yes' ) { - $r = ' class="new"'; + function getInternalLinkAttributesObj( &$nt, $text, $class = false ) { + if( $class ) { + $r = ' class="'.$class.'"'; } else { $r = ''; } @@ -155,6 +151,8 @@ class Linker { } /** + * @deprecated use makeColouredLinkObj + * * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeStubLinkObj for further documentation. * @@ -240,7 +238,7 @@ class Linker { if ( 0 == $aid ) { $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix ); } else { - $stub = false; + $colour = 1; if ( $nt->isContentPage() ) { $threshold = $wgUser->getOption('stubthreshold'); if ( $threshold > 0 ) { @@ -252,13 +250,11 @@ class Linker { array( 'page_id' => $aid ), __METHOD__ ) ; $stub = ( $s !== false && !$s->page_is_redirect && $s->page_len < $threshold ); + $colour = $stub ? 2 : 1; + wfRunHooks( 'GetLinkColour', array( $dbr, $aid, &$colour ) ); } } - if ( $stub ) { - $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix ); - } else { - $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix ); - } + $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix ); } wfProfileOut( __METHOD__.'-immediate' ); } @@ -345,7 +341,7 @@ class Linker { if ( '' == $text ) { $text = htmlspecialchars( $nt->getPrefixedText() ); } - $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" ); + $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new' ); list( $inside, $trail ) = Linker::splitTrail( $trail ); $s = "{$prefix}{$text}{$inside}{$trail}"; @@ -355,6 +351,8 @@ class Linker { } /** + * @deprecated use makeColouredLinkObj + * * Make a brown link to a short article. * * @param $title String: the text of the title @@ -365,7 +363,36 @@ class Linker { * the end of the link. */ function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) { - $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' ); + makeColouredLinkObj( $nt, 2, $text, $query, $trail, $prefix ); + } + + /** + * Make a coloured link. + * + * @param $title String: the text of the title + * @param $colour Integer: colour of the link + * @param $text String: link text + * @param $query String: optional query part + * @param $trail String: optional trail. Alphabetic characters at the start of this string will + * be included in the link text. Other characters will be appended after + * the end of the link. + */ + function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) { + + // convert colour into class name + $colourcode = array( + 0 => 'new', + 1 => '', + 2 => 'stub' + ); + // allow alternative colour code + wfRunHooks( 'GetLinkColourCode', array( &$colourcode ) ); + + $class = $colourcode[$colour]; + + if($class){ + $style = $this->getInternalLinkAttributesObj( $nt, $text, $class ); + } else $style = ''; return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style ); } @@ -384,11 +411,8 @@ class Linker { function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) { global $wgUser; $threshold = intval( $wgUser->getOption( 'stubthreshold' ) ); - if( $size < $threshold ) { - return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix ); - } else { - return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix ); - } + $colour = ( $size < $threshold ) ? 2 : 1; + return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix ); } /** @@ -700,7 +724,7 @@ class Linker { if( $query != '' ) $q .= '&' . $query; list( $inside, $trail ) = self::splitTrail( $trail ); - $style = $this->getInternalLinkAttributesObj( $title, $text, 'yes' ); + $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' ); wfProfileOut( __METHOD__ ); return '' . $prefix . $text . $inside . '' . $trail; diff --git a/includes/Parser.php b/includes/Parser.php index 46687e61ec..c7b5462cb0 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -4174,6 +4174,7 @@ class Parser $s->page_is_redirect || !Namespace::isContent( $s->page_namespace ) ) ? 1 : 2 ); + wfRunHooks( 'GetLinkColour', array( $dbr, $s->page_id, &$colours[$pdbk] ) ); } } wfProfileOut( $fname.'-check' ); @@ -4321,12 +4322,8 @@ class Parser $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title, $this->mLinkHolders['texts'][$key], $this->mLinkHolders['queries'][$key] ); - } elseif ( $colours[$pdbk] == 1 ) { - $replacePairs[$searchkey] = $sk->makeKnownLinkObj( $title, - $this->mLinkHolders['texts'][$key], - $this->mLinkHolders['queries'][$key] ); - } elseif ( $colours[$pdbk] == 2 ) { - $replacePairs[$searchkey] = $sk->makeStubLinkObj( $title, + } else { + $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk], $this->mLinkHolders['texts'][$key], $this->mLinkHolders['queries'][$key] ); } -- 2.20.1