$this->linkAttribs( $target, $customAttribs, $options )
);
if( is_null( $text ) ) {
- $text = $this->linkText( $target, $options );
+ $text = $this->linkText( $target );
}
$ret = Xml::openElement( 'a', $attribs )
private function linkUrl( $target, $query, $options ) {
wfProfileIn( __METHOD__ );
# If it's a broken link, add the appropriate query pieces, unless
- # there's already an action specified.
- if( in_array( 'broken', $options ) and empty( $query['action'] ) ) {
+ # there's already an action specified, or unless 'edit' makes no sense
+ # (i.e., for a nonexistent special page).
+ if( in_array( 'broken', $options ) and empty( $query['action'] )
+ and $target->getNamespace() != NS_SPECIAL ) {
$query['action'] = 'edit';
$query['redlink'] = '1';
}
global $wgUser;
$defaults = array();
- # First get a default title attribute.
- if( in_array( 'known', $options ) ) {
- $defaults['title'] = $target->getPrefixedText();
- } else {
- $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
- }
-
if( !in_array( 'noclasses', $options ) ) {
+ wfProfileIn( __METHOD__ . '-getClasses' );
# Now build the classes.
$classes = array();
if( $classes != array() ) {
$defaults['class'] = implode( ' ', $classes );
}
+ wfProfileOut( __METHOD__ . '-getClasses' );
+ }
+
+ # Get a default title attribute.
+ if( in_array( 'known', $options ) ) {
+ $defaults['title'] = $target->getPrefixedText();
+ } else {
+ $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
}
# Finally, merge the custom attribs with the default ones, and iterate
return $ret;
}
- private function linkText( $target, $options ) {
+ private function linkText( $target ) {
# If the target is just a fragment, with no title, we return the frag-
# ment text. Otherwise, we return the title text itself.
if( $target->getPrefixedText() === '' and $target->getFragment() !== '' ) {
global $wgUser;
wfProfileIn( __METHOD__ );
- if ( $nt->isExternal() ) {
- $u = $nt->getFullURL();
- $link = $nt->getPrefixedURL();
- if ( '' == $text ) { $text = $nt->getPrefixedText(); }
- $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
-
- $inside = '';
- if ( '' != $trail ) {
- $m = array();
- if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
- $inside = $m[1];
- $trail = $m[2];
- }
- }
- $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
-
- wfProfileOut( __METHOD__ );
- return $t;
- } elseif ( $nt->isAlwaysKnown() ) {
- # Image links, special page links and self-links with fragments are always known.
- $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
- } else {
- wfProfileIn( __METHOD__.'-immediate' );
+ $query = wfCgiToArray( $query );
+ list( $inside, $trail ) = Linker::splitTrail( $trail );
+ if( $text === '' ) {
+ $text = $this->linkText( $nt );
+ }
- # Handles links to special pages which do not exist in the database:
- if( $nt->getNamespace() == NS_SPECIAL ) {
- if( SpecialPage::exists( $nt->getDBkey() ) ) {
- $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
- } else {
- $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
- }
- wfProfileOut( __METHOD__.'-immediate' );
- wfProfileOut( __METHOD__ );
- return $retVal;
- }
+ $ret = $this->link( $nt, "$prefix$text$inside", array(), $query,
+ 'noclasses' ) . $trail;
- # Work out link colour immediately
- $aid = $nt->getArticleID() ;
- if ( 0 == $aid ) {
- $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
- } else {
- $colour = '';
- if ( $nt->isContentPage() ) {
- $threshold = $wgUser->getOption('stubthreshold');
- $colour = $this->getLinkColour( $nt, $threshold );
- }
- $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
- }
- wfProfileOut( __METHOD__.'-immediate' );
- }
wfProfileOut( __METHOD__ );
- return $retVal;
+ return $ret;
}
/**
function makeKnownLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
wfProfileIn( __METHOD__ );
- $nt = $this->normaliseSpecialPage( $title );
-
- $u = $nt->escapeLocalURL( $query );
- if ( $nt->getFragment() != '' ) {
- if( $nt->getPrefixedDbkey() == '' ) {
- $u = '';
- if ( '' == $text ) {
- $text = htmlspecialchars( $nt->getFragment() );
- }
- }
- $u .= $nt->getFragmentForURL();
- }
if ( $text == '' ) {
- $text = htmlspecialchars( $nt->getPrefixedText() );
- }
- if ( $style == '' ) {
- $style = $this->getInternalLinkAttributesObj( $nt, $text );
+ $text = $this->linkText( $title );
}
+ $attribs = Sanitizer::mergeAttributes(
+ Xml::explodeAttributes( $aprops ),
+ Xml::explodeAttributes( $style )
+ );
+ $query = wfCgiToArray( $query );
+ list( $inside, $trail ) = Linker::splitTrail( $trail );
- if ( $aprops !== '' ) $aprops = " $aprops";
+ $ret = $this->link( $title, "$prefix$text$inside", $attribs, $query,
+ array( 'known', 'noclasses' ) ) . $trail;
- list( $inside, $trail ) = Linker::splitTrail( $trail );
- $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
wfProfileOut( __METHOD__ );
- return $r;
+ return $ret;
}
/**
function makeBrokenLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
wfProfileIn( __METHOD__ );
- $nt = $this->normaliseSpecialPage( $title );
-
- if( $nt->getNamespace() == NS_SPECIAL ) {
- $q = $query;
- } else if ( '' == $query ) {
- $q = 'action=edit&redlink=1';
- } else {
- $q = 'action=edit&redlink=1&'.$query;
- }
- $u = $nt->escapeLocalURL( $q );
- if( $nt->getFragmentForURL() !== '' ) {
- # Might seem pointless to have a fragment on a redlink, but let's
- # be obedient.
- $u .= $nt->getFragmentForURL();
- }
-
- $titleText = $nt->getPrefixedText();
- if ( '' == $text ) {
- $text = htmlspecialchars( $titleText );
- }
- $titleAttr = wfMsg( 'red-link-title', $titleText );
- $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new', $titleAttr );
list( $inside, $trail ) = Linker::splitTrail( $trail );
+ if( $text === '' ) {
+ $text = $this->linkText( $title );
+ }
+ $nt = $this->normaliseSpecialPage( $title );
wfRunHooks( 'BrokenLink', array( &$this, $nt, $query, &$u, &$style, &$prefix, &$text, &$inside, &$trail ) );
- $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
+ $s = $this->link( $title, "$prefix$text$inside", array(),
+ wfCgiToArray( $query ), array( 'broken', 'noclasses' ) ) . $trail;
wfProfileOut( __METHOD__ );
return $s;