summaryrefslogtreecommitdiff
path: root/includes/Linker.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/Linker.php')
-rw-r--r--includes/Linker.php322
1 files changed, 163 insertions, 159 deletions
diff --git a/includes/Linker.php b/includes/Linker.php
index 0eabab2f..b12e2ad0 100644
--- a/includes/Linker.php
+++ b/includes/Linker.php
@@ -4,19 +4,15 @@
* These functions are used for primarily page content:
* links, embedded images, table of contents. Links are
* also used in the skin.
- * @package MediaWiki
- */
-
-/**
* For the moment, Skin is a descendent class of Linker.
* In the future, it should probably be further split
* so that ever other bit of the wiki doesn't have to
* go loading up Skin to get at it.
*
- * @package MediaWiki
+ * @addtogroup Skins
*/
class Linker {
- function Linker() {}
+ function __construct() {}
/**
* @deprecated
@@ -229,7 +225,7 @@ class Linker {
} else {
$threshold = $wgUser->getOption('stubthreshold') ;
if ( $threshold > 0 ) {
- $dbr =& wfGetDB( DB_SLAVE );
+ $dbr = wfGetDB( DB_SLAVE );
$s = $dbr->selectRow(
array( 'page' ),
array( 'page_len',
@@ -358,16 +354,8 @@ class Linker {
* the end of the link.
*/
function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
- $u = $nt->escapeLocalURL( $query );
-
- if ( '' == $text ) {
- $text = htmlspecialchars( $nt->getPrefixedText() );
- }
$style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
-
- list( $inside, $trail ) = Linker::splitTrail( $trail );
- $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
- return $s;
+ return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
}
/**
@@ -431,25 +419,19 @@ class Linker {
}
/** @todo document */
- function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
- $thumb = false, $manual_thumb = '', $page = null )
+ function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
+ $thumb = false, $manual_thumb = '', $valign = '' )
{
- global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
+ global $wgContLang, $wgUser, $wgThumbLimits;
$img = new Image( $nt );
- if ( ! is_null( $page ) ) {
- $img->selectPage( $page );
- }
-
if ( !$img->allowInlineDisplay() && $img->exists() ) {
return $this->makeKnownLinkObj( $nt );
}
- $url = $img->getViewURL();
$error = $prefix = $postfix = '';
-
- wfDebug( "makeImageLinkObj: '$width'x'$height', \"$label\"\n" );
+ $page = isset( $params['page'] ) ? $params['page'] : false;
if ( 'center' == $align )
{
@@ -458,6 +440,19 @@ class Linker {
$align = 'none';
}
+ if ( !isset( $params['width'] ) ) {
+ $params['width'] = $img->getWidth( $page );
+ if( $thumb || $framed ) {
+ $wopt = $wgUser->getOption( 'thumbsize' );
+
+ if( !isset( $wgThumbLimits[$wopt] ) ) {
+ $wopt = User::getDefaultOption( 'thumbsize' );
+ }
+
+ $params['width'] = min( $params['width'], $wgThumbLimits[$wopt] );
+ }
+ }
+
if ( $thumb || $framed ) {
# Create a thumbnail. Alignment depends on language
@@ -470,70 +465,39 @@ class Linker {
if ( $align == '' ) {
$align = $wgContLang->isRTL() ? 'left' : 'right';
}
-
-
- if ( $width === false ) {
- $wopt = $wgUser->getOption( 'thumbsize' );
-
- if( !isset( $wgThumbLimits[$wopt] ) ) {
- $wopt = User::getDefaultOption( 'thumbsize' );
- }
-
- $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
- }
-
- return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
+ return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $params, $framed, $manual_thumb ).$postfix;
}
- if ( $width && $img->exists() ) {
-
- # Create a resized image, without the additional thumbnail
- # features
-
- if ( $height == false )
- $height = -1;
- if ( $manual_thumb == '') {
- $thumb = $img->getThumbnail( $width, $height, $wgGenerateThumbnailOnParse );
- if ( $thumb ) {
- // In most cases, $width = $thumb->width or $height = $thumb->height.
- // If not, we're scaling the image larger than it can be scaled,
- // so we send to the browser a smaller thumbnail, and let the client do the scaling.
-
- if ($height != -1 && $width > $thumb->width * $height / $thumb->height) {
- // $height is the limiting factor, not $width
- // set $width to the largest it can be, such that the resulting
- // scaled height is at most $height
- $width = floor($thumb->width * $height / $thumb->height);
- }
- $height = round($thumb->height * $width / $thumb->width);
+ if ( $params['width'] && $img->exists() ) {
+ # Create a resized image, without the additional thumbnail features
+ $thumb = $img->transform( $params );
+ } else {
+ $thumb = false;
+ }
- wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" );
- $url = $thumb->getUrl();
- } else {
- $error = htmlspecialchars( $img->getLastError() );
- // Do client-side scaling...
- $height = intval( $img->getHeight() * $width / $img->getWidth() );
- }
- }
+ if ( $page ) {
+ $query = 'page=' . urlencode( $page );
} else {
- $width = $img->width;
- $height = $img->height;
+ $query = '';
+ }
+ $u = $nt->getLocalURL( $query );
+ $imgAttribs = array(
+ 'alt' => $alt,
+ 'longdesc' => $u
+ );
+ if ( $valign ) {
+ $imgAttribs['style'] = "vertical-align: $valign";
}
+ $linkAttribs = array(
+ 'href' => $u,
+ 'class' => 'image',
+ 'title' => $alt
+ );
- wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
- $u = $nt->escapeLocalURL();
- if ( $error ) {
- $s = $error;
- } elseif ( $url == '' ) {
+ if ( !$thumb ) {
$s = $this->makeBrokenImageLinkObj( $img->getTitle() );
- //$s .= "<br />{$alt}<br />{$url}<br />\n";
} else {
- $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
- '<img src="'.$url.'" alt="'.$alt.'" ' .
- ( $width
- ? ( 'width="'.$width.'" height="'.$height.'" ' )
- : '' ) .
- 'longdesc="'.$u.'" /></a>';
+ $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
}
if ( '' != $align ) {
$s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
@@ -545,86 +509,64 @@ class Linker {
* Make HTML for a thumbnail including image, border and caption
* $img is an Image object
*/
- function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
- global $wgStylePath, $wgContLang, $wgGenerateThumbnailOnParse;
+ function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "" ) {
+ global $wgStylePath, $wgContLang;
$thumbUrl = '';
$error = '';
- $width = $height = 0;
- if ( $img->exists() ) {
- $width = $img->getWidth();
- $height = $img->getHeight();
- }
- if ( 0 == $width || 0 == $height ) {
- $width = $height = 180;
- }
- if ( $boxwidth == 0 ) {
- $boxwidth = 180;
+ $page = isset( $params['page'] ) ? $params['page'] : false;
+
+ if ( empty( $params['width'] ) ) {
+ $params['width'] = 180;
}
- if ( $framed ) {
+ $thumb = false;
+ if ( $manual_thumb != '' ) {
+ # Use manually specified thumbnail
+ $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb );
+ if( $manual_title ) {
+ $manual_img = new Image( $manual_title );
+ $thumb = $manual_img->getUnscaledThumb();
+ }
+ } elseif ( $framed ) {
// Use image dimensions, don't scale
- $boxwidth = $width;
- $boxheight = $height;
- $thumbUrl = $img->getViewURL();
+ $thumb = $img->getUnscaledThumb( $page );
} else {
- if ( $boxheight === false )
- $boxheight = -1;
- if ( '' == $manual_thumb ) {
- $thumb = $img->getThumbnail( $boxwidth, $boxheight, $wgGenerateThumbnailOnParse );
- if ( $thumb ) {
- $thumbUrl = $thumb->getUrl();
- $boxwidth = $thumb->width;
- $boxheight = $thumb->height;
- } else {
- $error = $img->getLastError();
- }
- }
+ $thumb = $img->transform( $params );
}
- $oboxwidth = $boxwidth + 2;
- if ( $manual_thumb != '' ) # Use manually specified thumbnail
- {
- $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
- if( $manual_title ) {
- $manual_img = new Image( $manual_title );
- $thumbUrl = $manual_img->getViewURL();
- if ( $manual_img->exists() )
- {
- $width = $manual_img->getWidth();
- $height = $manual_img->getHeight();
- $boxwidth = $width ;
- $boxheight = $height ;
- $oboxwidth = $boxwidth + 2 ;
- }
- }
+ if ( $thumb ) {
+ $outerWidth = $thumb->getWidth() + 2;
+ } else {
+ $outerWidth = $params['width'] + 2;
}
- $u = $img->getEscapeLocalURL();
+ $query = $page ? 'page=' . urlencode( $page ) : '';
+ $u = $img->getTitle()->getLocalURL( $query );
$more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
$magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
$textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
- $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$oboxwidth}px;\">";
- if( $thumbUrl == '' ) {
- // Couldn't generate thumbnail? Scale the image client-side.
- $thumbUrl = $img->getViewURL();
- if( $boxheight == -1 ) {
- // Approximate...
- $boxheight = intval( $height * $boxwidth / $width );
- }
- }
- if ( $error ) {
- $s .= htmlspecialchars( $error );
+ $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
+ if ( !$thumb ) {
+ $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
$zoomicon = '';
} elseif( !$img->exists() ) {
$s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
$zoomicon = '';
} else {
- $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
- '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
- 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
- 'longdesc="'.$u.'" class="thumbimage" /></a>';
+ $imgAttribs = array(
+ 'alt' => $alt,
+ 'longdesc' => $u,
+ 'class' => 'thumbimage'
+ );
+ $linkAttribs = array(
+ 'href' => $u,
+ 'class' => 'internal',
+ 'title' => $alt
+ );
+
+ $s .= $thumb->toHtml( $imgAttribs, $linkAttribs );
if ( $framed ) {
$zoomicon="";
} else {
@@ -680,8 +622,6 @@ class Linker {
*
* @param $title Title object.
* @param $text String: pre-sanitized HTML
- * @param $nourl Boolean: Mask absolute URLs, so the parser doesn't
- * linkify them (it is currently not context-aware)
* @return string HTML
*
* @public
@@ -756,10 +696,10 @@ class Linker {
/**
* @param $userId Integer: user id in database.
* @param $userText String: user name in database.
+ * @param $redContribsWhenNoEdits Bool: return a red contribs link when the user had no edits and this is true.
* @return string HTML fragment with talk and/or block links
- * @private
*/
- function userToolLinks( $userId, $userText ) {
+ public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false ) {
global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
$talkable = !( $wgDisableAnonTalk && 0 == $userId );
$blockable = ( $wgSysopUserBans || 0 == $userId );
@@ -769,9 +709,15 @@ class Linker {
$items[] = $this->userTalkLink( $userId, $userText );
}
if( $userId ) {
+ // check if the user has an edit
+ if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
+ $style = "class='new'";
+ } else {
+ $style = '';
+ }
$contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
- $items[] = $this->makeKnownLinkObj( $contribsPage ,
- wfMsgHtml( 'contribslink' ) );
+
+ $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
}
if( $blockable && $wgUser->isAllowed( 'block' ) ) {
$items[] = $this->blockLink( $userId, $userText );
@@ -785,17 +731,22 @@ class Linker {
}
/**
+ * Alias for userToolLinks( $userId, $userText, true );
+ */
+ public function userToolLinksRedContribs( $userId, $userText ) {
+ return $this->userToolLinks( $userId, $userText, true );
+ }
+
+
+ /**
* @param $userId Integer: user id in database.
* @param $userText String: user name in database.
* @return string HTML fragment with user talk link
* @private
*/
function userTalkLink( $userId, $userText ) {
- global $wgLang;
- $talkname = $wgLang->getNsText( NS_TALK ); # use the shorter name
-
$userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
- $userTalkLink = $this->makeLinkObj( $userTalkPage, $talkname );
+ $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
return $userTalkLink;
}
@@ -860,7 +811,7 @@ class Linker {
* Since you can't set a default parameter for a reference, I've turned it
* temporarily to a value pass. Should be adjusted further. --brion
*
- * $param string $comment
+ * @param string $comment
* @param mixed $title Title object (to generate link to the section in autocomment) or null
* @param bool $local Whether section links should refer to local page
*/
@@ -1013,7 +964,7 @@ class Linker {
/** @todo document */
function tocList($toc) {
global $wgJsMimeType;
- $title = wfMsgForContent('toc') ;
+ $title = wfMsgHtml('toc') ;
return
'<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
. '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
@@ -1023,8 +974,8 @@ class Linker {
. "</ul>\n</td></tr></table>"
. '<script type="' . $wgJsMimeType . '">'
. ' if (window.showTocToggle) {'
- . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
- . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
+ . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
+ . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
. ' showTocToggle();'
. ' } '
. "</script>\n";
@@ -1134,7 +1085,7 @@ class Linker {
global $wgUser;
wfProfileIn( __METHOD__ );
- $sk =& $wgUser->getSkin();
+ $sk = $wgUser->getSkin();
$outText = '';
if ( count( $templates ) > 0 ) {
@@ -1182,10 +1133,14 @@ class Linker {
*/
public function formatSize( $size ) {
global $wgLang;
+ // For small sizes no decimal places necessary
+ $round = 0;
if( $size > 1024 ) {
$size = $size / 1024;
if( $size > 1024 ) {
$size = $size / 1024;
+ // For MB and bigger two decimal places are smarter
+ $round = 2;
if( $size > 1024 ) {
$size = $size / 1024;
$msg = 'size-gigabytes';
@@ -1198,10 +1153,59 @@ class Linker {
} else {
$msg = 'size-bytes';
}
- $size = round( $size, 0 );
+ $size = round( $size, $round );
return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
}
-
+
+ /**
+ * Given the id of an interface element, constructs the appropriate title
+ * and accesskey attributes from the system messages. (Note, this is usu-
+ * ally the id but isn't always, because sometimes the accesskey needs to
+ * go on a different element than the id, for reverse-compatibility, etc.)
+ *
+ * @param string $name Id of the element, minus prefixes.
+ * @return string title and accesskey attributes, ready to drop in an
+ * element (e.g., ' title="This does something [x]" accesskey="x"').
+ */
+ public function tooltipAndAccesskey($name) {
+ $out = '';
+
+ $tooltip = wfMsg('tooltip-'.$name);
+ if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
+ // Compatibility: formerly some tooltips had [alt-.] hardcoded
+ $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
+ $out .= ' title="'.htmlspecialchars($tooltip);
+ }
+ $accesskey = wfMsg('accesskey-'.$name);
+ if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
+ if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
+ else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
+ } elseif ($out) {
+ $out .= '"';
+ }
+ return $out;
+ }
+
+ /**
+ * Given the id of an interface element, constructs the appropriate title
+ * attribute from the system messages. (Note, this is usually the id but
+ * isn't always, because sometimes the accesskey needs to go on a different
+ * element than the id, for reverse-compatibility, etc.)
+ *
+ * @param string $name Id of the element, minus prefixes.
+ * @return string title attribute, ready to drop in an element
+ * (e.g., ' title="This does something"').
+ */
+ public function tooltip($name) {
+ $out = '';
+
+ $tooltip = wfMsg('tooltip-'.$name);
+ if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
+ $out = ' title="'.htmlspecialchars($tooltip).'"';
+ }
+
+ return $out;
+ }
}
?>