From: ThomasV Date: Thu, 22 Nov 2007 15:54:18 +0000 (+0000) Subject: * got rid of magic codes for colours.* added hook for link recolouring by extensions... X-Git-Tag: 1.31.0-rc.0~50750 X-Git-Url: http://git.cyclocoop.org//%22%22.str_replace%28%27%22%27%2C?a=commitdiff_plain;h=abf601e0fc2f0c102b585c1982e487910c167053;p=lhc%2Fweb%2Fwiklou.git * got rid of magic codes for colours.* added hook for link recolouring by extensions* merged redundant stub tests in one single function, getLinkColour (Linker.php) * deprecated makeStubLink and makeStubLinkObj --- diff --git a/docs/hooks.txt b/docs/hooks.txt index d9adf9603d..36d57aa266 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -504,6 +504,10 @@ $title: Title object of page $url: string value as output (out parameter, can modify) $query: query options passed to Title::getInternalURL() +'GetLinkColours': modify the CSS class of an array of page links +$linkcolour_ids: array of prefixed DB keys of the pages linked to, indexed by page_id. +&$colours: (output) array of CSS classes, indexed by prefixed DB keys + 'GetLocalURL': modify local URLs as output into page links $title: Title object of page $url: string value as output (out parameter, can modify) diff --git a/includes/Linker.php b/includes/Linker.php index 9e38a41f42..4599b1bb1d 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -52,19 +52,11 @@ class Linker { } /** @todo document */ - function getInternalLinkAttributes( $link, $text, $broken = false ) { + function getInternalLinkAttributes( $link, $text, $class='' ) { $link = urldecode( $link ); $link = str_replace( '_', ' ', $link ); $link = htmlspecialchars( $link ); - - if( $broken == 'stub' ) { - $r = ' class="stub"'; - } else if ( $broken == 'yes' ) { - $r = ' class="new"'; - } else { - $r = ''; - } - + $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : ''; $r .= " title=\"{$link}\""; return $r; } @@ -72,21 +64,34 @@ class Linker { /** * @param $nt Title object. * @param $text String: FIXME - * @param $broken Boolean: FIXME, default 'false'. + * @param $class String: CSS class of the link, default ''. */ - function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) { - if( $broken == 'stub' ) { - $r = ' class="stub"'; - } else if ( $broken == 'yes' ) { - $r = ' class="new"'; - } else { - $r = ''; - } - + function getInternalLinkAttributesObj( &$nt, $text, $class='' ) { + $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : ''; $r .= ' title="' . $nt->getEscapedText() . '"'; return $r; } + /** + * Return the CSS colour of a known link + * + * @param mixed $s + * @param integer $id + * @param integer $threshold + */ + function getLinkColour( $s, $threshold ) { + if( $threshold > 0 && $s!=false ) { + $colour = ( $s->page_len >= $threshold || + $s->page_is_redirect || + !Namespace::isContent( $s->page_namespace ) + ? '' : 'stub' ); + } + else { + $colour = ''; + } + return $colour; + } + /** * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call * it if you already have a title object handy. See makeLinkObj for further documentation. @@ -155,6 +160,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 +247,7 @@ class Linker { if ( 0 == $aid ) { $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix ); } else { - $stub = false; + $colour = ''; if ( $nt->isContentPage() ) { $threshold = $wgUser->getOption('stubthreshold'); if ( $threshold > 0 ) { @@ -250,15 +257,10 @@ class Linker { array( 'page_len', 'page_is_redirect' ), array( 'page_id' => $aid ), __METHOD__ ) ; - $stub = ( $s !== false && !$s->page_is_redirect && - $s->page_len < $threshold ); + $colour=$this->getLinkColour( $s, $threshold ); } } - 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 +347,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 +357,8 @@ class Linker { } /** + * @deprecated use makeColouredLinkObj + * * Make a brown link to a short article. * * @param $title String: the text of the title @@ -365,7 +369,25 @@ class Linker { * the end of the link. */ function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) { - $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' ); + makeColouredLinkObj( $nt, 'stub', $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 = '' ) { + + if($colour != ''){ + $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour ); + } else $style = ''; return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style ); } @@ -384,11 +406,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 ) ? 'stub' : ''; + return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix ); } /** @@ -709,7 +728,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 e07157bfcd..06a1c9c806 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -4315,10 +4315,7 @@ class Parser /** * Replace link placeholders with actual links, in the buffer * Placeholders created in Skin::makeLinkObj() - * Returns an array of links found, indexed by PDBK: - * 0 - broken - * 1 - normal link - * 2 - stub + * Returns an array of link CSS classes, indexed by PDBK. * $options is a bit field, RLH_FOR_UPDATE to select for update */ function replaceLinkHolders( &$text, $options = 0 ) { @@ -4330,6 +4327,7 @@ class Parser $pdbks = array(); $colours = array(); + $linkcolour_ids = array(); $sk = $this->mOptions->getSkin(); $linkCache =& LinkCache::singleton(); @@ -4358,14 +4356,14 @@ class Parser # Check if it's a static known link, e.g. interwiki if ( $title->isAlwaysKnown() ) { - $colours[$pdbk] = 1; + $colours[$pdbk] = ''; } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) { - $colours[$pdbk] = 1; + $colours[$pdbk] = ''; $this->mOutput->addLink( $title, $id ); } elseif ( $linkCache->isBadLink( $pdbk ) ) { - $colours[$pdbk] = 0; + $colours[$pdbk] = 'new'; } elseif ( $title->getNamespace() == NS_SPECIAL && !SpecialPage::exists( $pdbk ) ) { - $colours[$pdbk] = 0; + $colours[$pdbk] = 'new'; } else { # Not in the link cache, add it to the query if ( !isset( $current ) ) { @@ -4395,20 +4393,17 @@ class Parser # Fetch data and form into an associative array # non-existent = broken - # 1 = known - # 2 = stub while ( $s = $dbr->fetchObject($res) ) { $title = Title::makeTitle( $s->page_namespace, $s->page_title ); $pdbk = $title->getPrefixedDBkey(); $linkCache->addGoodLinkObj( $s->page_id, $title ); $this->mOutput->addLink( $title, $s->page_id ); - - $colours[$pdbk] = ( $threshold == 0 || ( - $s->page_len >= $threshold || # always true if $threshold <= 0 - $s->page_is_redirect || - !Namespace::isContent( $s->page_namespace ) ) - ? 1 : 2 ); + $colours[$pdbk] = $sk->getLinkColour( $s, $threshold ); + //add id to the extension todolist + $linkcolour_ids[$s->page_id] = $pdbk; } + //pass an array of page_ids to an extension + wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) ); } wfProfileOut( $fname.'-check' ); @@ -4504,18 +4499,10 @@ class Parser // set pdbk and colour $pdbks[$key] = $varPdbk; - if ( $threshold > 0 ) { - $size = $s->page_len; - if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) { - $colours[$varPdbk] = 1; - } else { - $colours[$varPdbk] = 2; - } - } - else { - $colours[$varPdbk] = 1; - } + $colours[$varPdbk] = $sk->getLinkColour( $s, $threshold ); + $linkcolour_ids[$s->page_id] = $pdbk; } + wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) ); } // check if the object is a variant of a category @@ -4548,19 +4535,15 @@ class Parser $pdbk = $pdbks[$key]; $searchkey = ""; $title = $this->mLinkHolders['titles'][$key]; - if ( empty( $colours[$pdbk] ) ) { + if ( !isset( $colours[$pdbk] ) ) { $linkCache->addBadLinkObj( $title ); - $colours[$pdbk] = 0; + $colours[$pdbk] = 'new'; $this->mOutput->addLink( $title, 0 ); $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] ); }