3 * Split off some of the internal bits from Skin.php.
4 * These functions are used for primarily page content:
5 * links, embedded images, table of contents. Links are
6 * also used in the skin.
11 * For the moment, Skin is a descendent class of Linker.
12 * In the future, it should probably be further split
13 * so that ever other bit of the wiki doesn't have to
14 * go loading up Skin to get at it.
25 function postParseLinkColour( $s = NULL ) {
30 function getExternalLinkAttributes( $link, $text, $class='' ) {
33 $same = ($link == $text);
34 $link = urldecode( $link );
35 $link = $wgContLang->checkTitleEncoding( $link );
36 $link = preg_replace( '/[\\x00-\\x1f_]/', ' ', $link );
37 $link = htmlspecialchars( $link );
39 $r = ($class != '') ?
" class='$class'" : " class='external'";
41 $r .= " title=\"{$link}\"";
46 function getInternalLinkAttributes( $link, $text, $broken = false ) {
47 $link = urldecode( $link );
48 $link = str_replace( '_', ' ', $link );
49 $link = htmlspecialchars( $link );
51 if( $broken == 'stub' ) {
53 } else if ( $broken == 'yes' ) {
59 $r .= " title=\"{$link}\"";
66 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
67 if( $broken == 'stub' ) {
69 } else if ( $broken == 'yes' ) {
75 $r .= ' title="' . $nt->getEscapedText() . '"';
80 * Note: This function MUST call getArticleID() on the link,
81 * otherwise the cache won't get updated properly. See LINKCACHE.DOC.
83 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
84 wfProfileIn( 'Skin::makeLink' );
85 $nt = Title
::newFromText( $title );
87 $result = $this->makeLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
89 wfDebug( 'Invalid title passed to Skin::makeLink(): "'.$title."\"\n" );
90 $result = $text == "" ?
$title : $text;
93 wfProfileOut( 'Skin::makeLink' );
98 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
99 $nt = Title
::newFromText( $title );
101 return $this->makeKnownLinkObj( Title
::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
103 wfDebug( 'Invalid title passed to Skin::makeKnownLink(): "'.$title."\"\n" );
104 return $text == '' ?
$title : $text;
108 /** @todo document */
109 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
110 $nt = Title
::newFromText( $title );
112 return $this->makeBrokenLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
114 wfDebug( 'Invalid title passed to Skin::makeBrokenLink(): "'.$title."\"\n" );
115 return $text == '' ?
$title : $text;
119 /** @todo document */
120 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
121 $nt = Title
::newFromText( $title );
123 return $this->makeStubLinkObj( Title
::newFromText( $title ), $text, $query, $trail );
125 wfDebug( 'Invalid title passed to Skin::makeStubLink(): "'.$title."\"\n" );
126 return $text == '' ?
$title : $text;
131 * Pass a title object, not a title string
133 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
134 global $wgOut, $wgUser, $wgInputEncoding;
135 $fname = 'Skin::makeLinkObj';
136 wfProfileIn( $fname );
139 if ( ! is_object($nt) ) {
140 # wfDebugDieBacktrace();
141 wfProfileOut( $fname );
142 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
145 $ns = $nt->getNamespace();
146 $dbkey = $nt->getDBkey();
147 if ( $nt->isExternal() ) {
148 $u = $nt->getFullURL();
149 $link = $nt->getPrefixedURL();
150 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
151 $style = $this->getExternalLinkAttributes( $link, $text, 'extiw' );
154 if ( '' != $trail ) {
155 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
161 # Check for anchors, normalize the anchor
163 $parts = explode( '#', $u, 2 );
164 if ( count( $parts ) == 2 ) {
165 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $parts[1] ),
167 $wgInputEncoding ) );
168 $replacearray = array(
172 $u = $parts[0] . '#' .
173 str_replace( array_keys( $replacearray ),
174 array_values( $replacearray ),
178 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
181 } elseif ( $nt->isAlwaysKnown() ) {
182 # Image links, special page links and self-links with fragements are always known.
183 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
185 wfProfileIn( $fname.'-immediate' );
186 # Work out link colour immediately
187 $aid = $nt->getArticleID() ;
189 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
191 $threshold = $wgUser->getOption('stubthreshold') ;
192 if ( $threshold > 0 ) {
193 $dbr =& wfGetDB( DB_SLAVE
);
194 $s = $dbr->selectRow(
198 'page_is_redirect' ),
199 array( 'page_id' => $aid ), $fname ) ;
200 if ( $s !== false ) {
201 $size = $s->page_len
;
202 if ( $s->page_is_redirect
OR $s->page_namespace
!= NS_MAIN
) {
203 $size = $threshold*2 ; # Really big
206 $size = $threshold*2 ; # Really big
211 if ( $size < $threshold ) {
212 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
214 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
217 wfProfileOut( $fname.'-immediate' );
219 wfProfileOut( $fname );
224 * Pass a title object, not a title string
226 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
227 global $wgOut, $wgTitle, $wgInputEncoding;
229 $fname = 'Skin::makeKnownLinkObj';
230 wfProfileIn( $fname );
232 if ( !is_object( $nt ) ) {
233 wfProfileOut( $fname );
237 $u = $nt->escapeLocalURL( $query );
238 if ( '' != $nt->getFragment() ) {
239 if( $nt->getPrefixedDbkey() == '' ) {
242 $text = htmlspecialchars( $nt->getFragment() );
245 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT
, $wgInputEncoding ) );
246 $replacearray = array(
250 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
253 $text = htmlspecialchars( $nt->getPrefixedText() );
255 $style = $this->getInternalLinkAttributesObj( $nt, $text );
257 list( $inside, $trail ) = Linker
::splitTrail( $trail );
258 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
259 wfProfileOut( $fname );
264 * Pass a title object, not a title string
266 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
268 if ( ! isset($nt) ) {
269 # wfDebugDieBacktrace();
270 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
273 $fname = 'Skin::makeBrokenLinkObj';
274 wfProfileIn( $fname );
276 if ( '' == $query ) {
279 $q = 'action=edit&'.$query;
281 $u = $nt->escapeLocalURL( $q );
284 $text = htmlspecialchars( $nt->getPrefixedText() );
286 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
288 list( $inside, $trail ) = Linker
::splitTrail( $trail );
289 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
291 wfProfileOut( $fname );
296 * Pass a title object, not a title string
298 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
299 $link = $nt->getPrefixedURL();
301 $u = $nt->escapeLocalURL( $query );
304 $text = htmlspecialchars( $nt->getPrefixedText() );
306 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
308 list( $inside, $trail ) = Linker
::splitTrail( $trail );
309 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
313 /** @todo document */
314 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
315 $u = $nt->escapeLocalURL( $query );
317 $text = htmlspecialchars( $nt->getPrefixedText() );
319 list( $inside, $trail ) = Linker
::splitTrail( $trail );
320 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
323 /** @todo document */
324 function fnamePart( $url ) {
325 $basename = strrchr( $url, '/' );
326 if ( false === $basename ) {
329 $basename = substr( $basename, 1 );
331 return htmlspecialchars( $basename );
334 /** Obsolete alias */
335 function makeImage( $url, $alt = '' ) {
336 return $this->makeExternalImage( $url, $alt );
339 /** @todo document */
340 function makeExternalImage( $url, $alt = '' ) {
343 $alt = $this->fnamePart( $url );
345 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
349 /** @todo document */
350 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
351 $thumb = false, $manual_thumb = '' )
353 global $wgContLang, $wgUser, $wgThumbLimits;
355 $img = new Image( $nt );
356 $url = $img->getViewURL();
357 $prefix = $postfix = '';
359 if ( 'center' == $align )
361 $prefix = '<div class="center">';
366 if ( $thumb ||
$framed ) {
368 # Create a thumbnail. Alignment depends on language
369 # writing direction, # right aligned for left-to-right-
370 # languages ("Western languages"), left-aligned
371 # for right-to-left-languages ("Semitic languages")
373 # If thumbnail width has not been provided, it is set
374 # to the default user option as specified in Language*.php
375 if ( $align == '' ) {
376 $align = $wgContLang->isRTL() ?
'left' : 'right';
380 if ( $width === false ) {
381 $wopt = $wgUser->getOption( 'thumbsize' );
383 if( !isset( $wgThumbLimits[$wopt] ) ) {
384 $wopt = User
::getDefaultOption( 'thumbsize' );
387 $width = $wgThumbLimits[$wopt];
390 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
392 } elseif ( $width ) {
394 # Create a resized image, without the additional thumbnail
397 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
398 $width = $img->getWidth() * $height / $img->getHeight();
400 if ( '' == $manual_thumb ) {
401 $url = $img->createThumb( $width );
405 $u = $nt->escapeLocalURL();
407 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
408 //$s .= "<br />{$alt}<br />{$url}<br />\n";
410 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
411 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
413 if ( '' != $align ) {
414 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
416 return str_replace("\n", ' ',$prefix.$s.$postfix);
420 * Make HTML for a thumbnail including image, border and caption
421 * $img is an Image object
423 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
424 global $wgStylePath, $wgContLang;
425 $url = $img->getViewURL();
427 $width = $height = 0;
428 if ( $img->exists() )
430 $width = $img->getWidth();
431 $height = $img->getHeight();
433 if ( 0 == $width ||
0 == $height )
435 $width = $height = 200;
437 if ( $boxwidth == 0 )
443 // Use image dimensions, don't scale
445 $oboxwidth = $boxwidth +
2;
446 $boxheight = $height;
449 $h = intval( $height/($width/$boxwidth) );
450 $oboxwidth = $boxwidth +
2;
451 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
453 $boxwidth *= $boxheight/$h;
457 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
460 if ( $manual_thumb != '' ) # Use manually specified thumbnail
462 $manual_title = Title
::makeTitleSafe( NS_IMAGE
, $manual_thumb ); #new Title ( $manual_thumb ) ;
463 $manual_img = new Image( $manual_title );
464 $thumbUrl = $manual_img->getViewURL();
465 if ( $manual_img->exists() )
467 $width = $manual_img->getWidth();
468 $height = $manual_img->getHeight();
470 $boxheight = $height ;
471 $oboxwidth = $boxwidth +
2 ;
475 $u = $img->getEscapeLocalURL();
477 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
478 $magnifyalign = $wgContLang->isRTL() ?
'left' : 'right';
479 $textalign = $wgContLang->isRTL() ?
' style="text-align:right"' : '';
481 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
482 if ( $thumbUrl == '' ) {
483 $s .= $this->makeBrokenImageLinkObj( $img->getTitle
);
486 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
487 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
488 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
489 'longdesc="'.$u.'" /></a>';
493 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
494 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
495 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
496 'width="15" height="11" alt="'.$more.'" /></a></div>';
499 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
500 return str_replace("\n", ' ', $s);
504 * Pass a title object, not a title string
506 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
508 if ( ! isset($nt) ) {
509 # wfDebugDieBacktrace();
510 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
513 $fname = 'Skin::makeBrokenImageLinkObj';
514 wfProfileIn( $fname );
516 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
517 if ( '' != $query ) {
520 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
521 $url = $uploadTitle->escapeLocalURL( $q );
524 $text = htmlspecialchars( $nt->getPrefixedText() );
526 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
527 list( $inside, $trail ) = Linker
::splitTrail( $trail );
528 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
530 wfProfileOut( $fname );
534 /** @todo document */
535 function makeMediaLink( $name, $url, $alt = '' ) {
536 $nt = Title
::makeTitleSafe( NS_IMAGE
, $name );
537 return $this->makeMediaLinkObj( $nt, $alt );
541 * Create a direct link to a given uploaded file.
543 * @param Title $title
544 * @param string $text pre-sanitized HTML
545 * @param bool $nourl Mask absolute URLs, so the parser doesn't
546 * linkify them (it is currently not context-aware)
547 * @return string HTML
550 * @todo Handle invalid or missing images better.
552 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
553 if( is_null( $title ) ) {
554 ### HOTFIX. Instead of breaking, return empty string.
557 $name = $title->getDBKey();
558 $img = new Image( $title );
559 $url = $img->getURL();
561 $url = str_replace( "http://", "http-noparse://", $url );
563 $alt = htmlspecialchars( $title->getText() );
567 $u = htmlspecialchars( $url );
568 return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";
572 /** @todo document */
573 function specialLink( $name, $key = '' ) {
576 if ( '' == $key ) { $key = strtolower( $name ); }
577 $pn = $wgContLang->ucfirst( $name );
578 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
582 /** @todo document */
583 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
584 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
585 global $wgNoFollowLinks;
586 if( $wgNoFollowLinks ) {
587 $style .= ' rel="nofollow"';
589 $url = htmlspecialchars( $url );
591 $text = htmlspecialchars( $text );
593 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
597 * This function is called by all recent changes variants, by the page history,
598 * and by the user contributions list. It is responsible for formatting edit
599 * comments. It escapes any HTML in the comment, but adds some CSS to format
600 * auto-generated comments (from section editing) and formats [[wikilinks]].
602 * The &$title parameter must be a title OBJECT. It is used to generate a
603 * direct link to the section in the autocomment.
604 * @author Erik Moeller <moeller@scireview.de>
606 * Note: there's not always a title to pass to this function.
607 * Since you can't set a default parameter for a reference, I've turned it
608 * temporarily to a value pass. Should be adjusted further. --brion
610 function formatComment($comment, $title = NULL) {
611 $fname = 'Skin::formatComment';
612 wfProfileIn( $fname );
615 $comment = str_replace( "\n", " ", $comment );
616 $comment = htmlspecialchars( $comment );
618 # The pattern for autogen comments is / * foo * /, which makes for
620 # We look for all comments, match any text before and after the comment,
621 # add a separator where needed and format the comment itself with CSS
622 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
630 # This is hackish but should work in most cases.
631 $section=str_replace('[[','',$section);
632 $section=str_replace(']]','',$section);
633 $title->mFragment
=$section;
634 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
638 if($pre) { $auto = $sep.' '.$auto; }
639 if($post) { $auto .= ' '.$sep; }
640 $auto='<span class="autocomment">'.$auto.'</span>';
641 $comment=$pre.$auto.$post;
644 # format regular and media links - all other wiki formatting
646 $medians = $wgContLang->getNsText( NS_MEDIA
) . ':';
647 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
648 # Handle link renaming [[foo|text]] will show link as "text"
649 if( "" != $match[3] ) {
654 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
655 # Media link; trail not supported.
656 $linkRegexp = '/\[\[(.*?)\]\]/';
657 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
660 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
661 $trail = $submatch[1];
665 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
666 if ($match[1][0] == ':')
667 $match[1] = substr($match[1], 1);
668 $thelink = $this->makeLink( $match[1], $text, "", $trail );
670 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
672 wfProfileOut( $fname );
677 * Wrap a comment in standard punctuation and formatting if
678 * it's non-empty, otherwise return empty string.
680 * @param string $comment
681 * @param Title $title
685 function commentBlock( $comment, $title = NULL ) {
686 if( $comment == '' ||
$comment == '*' ) {
689 $formatted = $this->formatComment( $comment, $title );
690 return " <span class='comment'>($formatted)</span>";
694 /** @todo document */
695 function tocIndent() {
699 /** @todo document */
700 function tocUnindent($level) {
701 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ?
$level : 0 );
705 * parameter level defines if we are on an indentation level
707 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
708 return "\n<li class='toclevel-$level'><a href=\"#" .
709 $anchor . '"><span class="tocnumber">' .
710 $tocnumber . '</span> <span class="toctext">' .
711 $tocline . '</span></a>';
714 /** @todo document */
715 function tocLineEnd() {
719 /** @todo document */
720 function tocList($toc) {
721 global $wgJsMimeType;
722 return "<table id='toc' class='toc'><tr><td>"
723 . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
725 . "</ul>\n</td></tr></table>\n"
726 . '<script type="'.$wgJsMimeType.'">'
727 . ' if (window.showTocToggle) {'
728 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
729 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
730 . ' showTocToggle();'
735 /** @todo document */
736 function editSectionLinkForOther( $title, $section ) {
740 $title = Title
::newFromText($title);
741 $editurl = '§ion='.$section;
742 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
744 if( $wgContLang->isRTL() ) {
751 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
755 /** @todo document */
756 function editSectionLink( $nt, $section ) {
760 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
761 # Section edit links would be out of sync on an old page.
762 # But, if we're diffing to the current page, they'll be
767 $editurl = '§ion='.$section;
768 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
770 if( $wgContLang->isRTL() ) {
777 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
781 * Split a link trail, return the "inside" portion and the remainder of the trail
782 * as a two-element array
786 function splitTrail( $trail ) {
787 static $regex = false;
788 if ( $regex === false ) {
790 $regex = $wgContLang->linkTrail();
793 if ( '' != $trail ) {
794 if ( preg_match( $regex, $trail, $m ) ) {
799 return array( $inside, $trail );