* Replace wfMungeToUtf8 and do_html_entity_decode with a single function
[lhc/web/wiklou.git] / includes / Linker.php
1 <?php
2 /**
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.
7 * @package MediaWiki
8 */
9
10 /**
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.
15 *
16 * @package MediaWiki
17 */
18 class Linker {
19
20 function Linker() {}
21
22 /**
23 * OBSOLETE
24 */
25 function postParseLinkColour( $s = NULL ) {
26 return NULL;
27 }
28
29 /** @todo document */
30 function getExternalLinkAttributes( $link, $text, $class='' ) {
31 global $wgContLang;
32
33 $same = ($link == $text);
34 $link = urldecode( $link );
35 $link = $wgContLang->checkTitleEncoding( $link );
36 $link = preg_replace( '/[\\x00-\\x1f_]/', ' ', $link );
37 $link = htmlspecialchars( $link );
38
39 $r = ($class != '') ? " class='$class'" : " class='external'";
40
41 $r .= " title=\"{$link}\"";
42 return $r;
43 }
44
45 /** @todo document */
46 function getInternalLinkAttributes( $link, $text, $broken = false ) {
47 $link = urldecode( $link );
48 $link = str_replace( '_', ' ', $link );
49 $link = htmlspecialchars( $link );
50
51 if( $broken == 'stub' ) {
52 $r = ' class="stub"';
53 } else if ( $broken == 'yes' ) {
54 $r = ' class="new"';
55 } else {
56 $r = '';
57 }
58
59 $r .= " title=\"{$link}\"";
60 return $r;
61 }
62
63 /**
64 * @param bool $broken
65 */
66 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
67 if( $broken == 'stub' ) {
68 $r = ' class="stub"';
69 } else if ( $broken == 'yes' ) {
70 $r = ' class="new"';
71 } else {
72 $r = '';
73 }
74
75 $r .= ' title="' . $nt->getEscapedText() . '"';
76 return $r;
77 }
78
79 /**
80 * Note: This function MUST call getArticleID() on the link,
81 * otherwise the cache won't get updated properly. See LINKCACHE.DOC.
82 */
83 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
84 wfProfileIn( 'Linker::makeLink' );
85 $nt = Title::newFromText( $title );
86 if ($nt) {
87 $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
88 } else {
89 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
90 $result = $text == "" ? $title : $text;
91 }
92
93 wfProfileOut( 'Linker::makeLink' );
94 return $result;
95 }
96
97 /** @todo document */
98 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
99 $nt = Title::newFromText( $title );
100 if ($nt) {
101 return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
102 } else {
103 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
104 return $text == '' ? $title : $text;
105 }
106 }
107
108 /** @todo document */
109 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
110 $nt = Title::newFromText( $title );
111 if ($nt) {
112 return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
113 } else {
114 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
115 return $text == '' ? $title : $text;
116 }
117 }
118
119 /** @todo document */
120 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
121 $nt = Title::newFromText( $title );
122 if ($nt) {
123 return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
124 } else {
125 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
126 return $text == '' ? $title : $text;
127 }
128 }
129
130 /**
131 * Pass a title object, not a title string
132 */
133 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
134 global $wgOut, $wgUser;
135 $fname = 'Linker::makeLinkObj';
136 wfProfileIn( $fname );
137
138 # Fail gracefully
139 if ( ! is_object($nt) ) {
140 # wfDebugDieBacktrace();
141 wfProfileOut( $fname );
142 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
143 }
144
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' );
152
153 $inside = '';
154 if ( '' != $trail ) {
155 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
156 $inside = $m[1];
157 $trail = $m[2];
158 }
159 }
160
161 # Check for anchors, normalize the anchor
162
163 $parts = explode( '#', $u, 2 );
164 if ( count( $parts ) == 2 ) {
165 $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
166 $replacearray = array(
167 '%3A' => ':',
168 '%' => '.'
169 );
170 $u = $parts[0] . '#' .
171 str_replace( array_keys( $replacearray ),
172 array_values( $replacearray ),
173 $anchor );
174 }
175
176 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
177
178 wfProfileOut( $fname );
179 return $t;
180 } elseif ( $nt->isAlwaysKnown() ) {
181 # Image links, special page links and self-links with fragements are always known.
182 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
183 } else {
184 wfProfileIn( $fname.'-immediate' );
185 # Work out link colour immediately
186 $aid = $nt->getArticleID() ;
187 if ( 0 == $aid ) {
188 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
189 } else {
190 $threshold = $wgUser->getOption('stubthreshold') ;
191 if ( $threshold > 0 ) {
192 $dbr =& wfGetDB( DB_SLAVE );
193 $s = $dbr->selectRow(
194 array( 'page' ),
195 array( 'page_len',
196 'page_namespace',
197 'page_is_redirect' ),
198 array( 'page_id' => $aid ), $fname ) ;
199 if ( $s !== false ) {
200 $size = $s->page_len;
201 if ( $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) {
202 $size = $threshold*2 ; # Really big
203 }
204 } else {
205 $size = $threshold*2 ; # Really big
206 }
207 } else {
208 $size = 1 ;
209 }
210 if ( $size < $threshold ) {
211 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
212 } else {
213 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
214 }
215 }
216 wfProfileOut( $fname.'-immediate' );
217 }
218 wfProfileOut( $fname );
219 return $retVal;
220 }
221
222 /**
223 * Pass a title object, not a title string
224 */
225 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
226 global $wgOut, $wgTitle;
227
228 $fname = 'Linker::makeKnownLinkObj';
229 wfProfileIn( $fname );
230
231 if ( !is_object( $nt ) ) {
232 wfProfileOut( $fname );
233 return $text;
234 }
235
236 $u = $nt->escapeLocalURL( $query );
237 if ( '' != $nt->getFragment() ) {
238 if( $nt->getPrefixedDbkey() == '' ) {
239 $u = '';
240 if ( '' == $text ) {
241 $text = htmlspecialchars( $nt->getFragment() );
242 }
243 }
244 $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
245 $replacearray = array(
246 '%3A' => ':',
247 '%' => '.'
248 );
249 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
250 }
251 if ( '' == $text ) {
252 $text = htmlspecialchars( $nt->getPrefixedText() );
253 }
254 $style = $this->getInternalLinkAttributesObj( $nt, $text );
255
256 list( $inside, $trail ) = Linker::splitTrail( $trail );
257 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
258 wfProfileOut( $fname );
259 return $r;
260 }
261
262 /**
263 * Pass a title object, not a title string
264 */
265 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
266 # Fail gracefully
267 if ( ! isset($nt) ) {
268 # wfDebugDieBacktrace();
269 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
270 }
271
272 $fname = 'Linker::makeBrokenLinkObj';
273 wfProfileIn( $fname );
274
275 if ( '' == $query ) {
276 $q = 'action=edit';
277 } else {
278 $q = 'action=edit&'.$query;
279 }
280 $u = $nt->escapeLocalURL( $q );
281
282 if ( '' == $text ) {
283 $text = htmlspecialchars( $nt->getPrefixedText() );
284 }
285 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
286
287 list( $inside, $trail ) = Linker::splitTrail( $trail );
288 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
289
290 wfProfileOut( $fname );
291 return $s;
292 }
293
294 /**
295 * Pass a title object, not a title string
296 */
297 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
298 $link = $nt->getPrefixedURL();
299
300 $u = $nt->escapeLocalURL( $query );
301
302 if ( '' == $text ) {
303 $text = htmlspecialchars( $nt->getPrefixedText() );
304 }
305 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
306
307 list( $inside, $trail ) = Linker::splitTrail( $trail );
308 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
309 return $s;
310 }
311
312 /** @todo document */
313 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
314 $u = $nt->escapeLocalURL( $query );
315 if ( '' == $text ) {
316 $text = htmlspecialchars( $nt->getPrefixedText() );
317 }
318 list( $inside, $trail ) = Linker::splitTrail( $trail );
319 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
320 }
321
322 /** @todo document */
323 function fnamePart( $url ) {
324 $basename = strrchr( $url, '/' );
325 if ( false === $basename ) {
326 $basename = $url;
327 } else {
328 $basename = substr( $basename, 1 );
329 }
330 return htmlspecialchars( $basename );
331 }
332
333 /** Obsolete alias */
334 function makeImage( $url, $alt = '' ) {
335 return $this->makeExternalImage( $url, $alt );
336 }
337
338 /** @todo document */
339 function makeExternalImage( $url, $alt = '' ) {
340 global $wgOut;
341 if ( '' == $alt ) {
342 $alt = $this->fnamePart( $url );
343 }
344 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
345 return $s;
346 }
347
348 /** @todo document */
349 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
350 $thumb = false, $manual_thumb = '' )
351 {
352 global $wgContLang, $wgUser, $wgThumbLimits;
353
354 $img = new Image( $nt );
355 $url = $img->getViewURL();
356 $prefix = $postfix = '';
357
358 if ( 'center' == $align )
359 {
360 $prefix = '<div class="center">';
361 $postfix = '</div>';
362 $align = 'none';
363 }
364
365 if ( $thumb || $framed ) {
366
367 # Create a thumbnail. Alignment depends on language
368 # writing direction, # right aligned for left-to-right-
369 # languages ("Western languages"), left-aligned
370 # for right-to-left-languages ("Semitic languages")
371 #
372 # If thumbnail width has not been provided, it is set
373 # to the default user option as specified in Language*.php
374 if ( $align == '' ) {
375 $align = $wgContLang->isRTL() ? 'left' : 'right';
376 }
377
378
379 if ( $width === false ) {
380 $wopt = $wgUser->getOption( 'thumbsize' );
381
382 if( !isset( $wgThumbLimits[$wopt] ) ) {
383 $wopt = User::getDefaultOption( 'thumbsize' );
384 }
385
386 $width = $wgThumbLimits[$wopt];
387 }
388
389 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
390
391 } elseif ( $width ) {
392
393 # Create a resized image, without the additional thumbnail
394 # features
395
396 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
397 $width = $img->getWidth() * $height / $img->getHeight();
398 }
399 if ( '' == $manual_thumb ) {
400 $url = $img->createThumb( $width );
401 }
402 }
403
404 $u = $nt->escapeLocalURL();
405 if ( $url == '' ) {
406 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
407 //$s .= "<br />{$alt}<br />{$url}<br />\n";
408 } else {
409 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
410 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
411 }
412 if ( '' != $align ) {
413 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
414 }
415 return str_replace("\n", ' ',$prefix.$s.$postfix);
416 }
417
418 /**
419 * Make HTML for a thumbnail including image, border and caption
420 * $img is an Image object
421 */
422 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
423 global $wgStylePath, $wgContLang;
424 $url = $img->getViewURL();
425
426 $width = $height = 0;
427 if ( $img->exists() )
428 {
429 $width = $img->getWidth();
430 $height = $img->getHeight();
431 }
432 if ( 0 == $width || 0 == $height )
433 {
434 $width = $height = 200;
435 }
436 if ( $boxwidth == 0 )
437 {
438 $boxwidth = 200;
439 }
440 if ( $framed )
441 {
442 // Use image dimensions, don't scale
443 $boxwidth = $width;
444 $oboxwidth = $boxwidth + 2;
445 $boxheight = $height;
446 $thumbUrl = $url;
447 } else {
448 $h = intval( $height/($width/$boxwidth) );
449 $oboxwidth = $boxwidth + 2;
450 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
451 {
452 $boxwidth *= $boxheight/$h;
453 } else {
454 $boxheight = $h;
455 }
456 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
457 }
458
459 if ( $manual_thumb != '' ) # Use manually specified thumbnail
460 {
461 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
462 $manual_img = new Image( $manual_title );
463 $thumbUrl = $manual_img->getViewURL();
464 if ( $manual_img->exists() )
465 {
466 $width = $manual_img->getWidth();
467 $height = $manual_img->getHeight();
468 $boxwidth = $width ;
469 $boxheight = $height ;
470 $oboxwidth = $boxwidth + 2 ;
471 }
472 }
473
474 $u = $img->getEscapeLocalURL();
475
476 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
477 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
478 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
479
480 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
481 if ( $thumbUrl == '' ) {
482 $s .= $this->makeBrokenImageLinkObj( $img->getTitle );
483 $zoomicon = '';
484 } else {
485 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
486 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
487 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
488 'longdesc="'.$u.'" /></a>';
489 if ( $framed ) {
490 $zoomicon="";
491 } else {
492 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
493 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
494 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
495 'width="15" height="11" alt="'.$more.'" /></a></div>';
496 }
497 }
498 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
499 return str_replace("\n", ' ', $s);
500 }
501
502 /**
503 * Pass a title object, not a title string
504 */
505 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
506 # Fail gracefully
507 if ( ! isset($nt) ) {
508 # wfDebugDieBacktrace();
509 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
510 }
511
512 $fname = 'Linker::makeBrokenImageLinkObj';
513 wfProfileIn( $fname );
514
515 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
516 if ( '' != $query ) {
517 $q .= "&$query";
518 }
519 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
520 $url = $uploadTitle->escapeLocalURL( $q );
521
522 if ( '' == $text ) {
523 $text = htmlspecialchars( $nt->getPrefixedText() );
524 }
525 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
526 list( $inside, $trail ) = Linker::splitTrail( $trail );
527 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
528
529 wfProfileOut( $fname );
530 return $s;
531 }
532
533 /** @todo document */
534 function makeMediaLink( $name, $url, $alt = '' ) {
535 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
536 return $this->makeMediaLinkObj( $nt, $alt );
537 }
538
539 /**
540 * Create a direct link to a given uploaded file.
541 *
542 * @param Title $title
543 * @param string $text pre-sanitized HTML
544 * @param bool $nourl Mask absolute URLs, so the parser doesn't
545 * linkify them (it is currently not context-aware)
546 * @return string HTML
547 *
548 * @access public
549 * @todo Handle invalid or missing images better.
550 */
551 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
552 if( is_null( $title ) ) {
553 ### HOTFIX. Instead of breaking, return empty string.
554 return $text;
555 } else {
556 $name = $title->getDBKey();
557 $img = new Image( $title );
558 $url = $img->getURL();
559 if( $nourl ) {
560 $url = str_replace( "http://", "http-noparse://", $url );
561 }
562 $alt = htmlspecialchars( $title->getText() );
563 if( $text == '' ) {
564 $text = $alt;
565 }
566 $u = htmlspecialchars( $url );
567 return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";
568 }
569 }
570
571 /** @todo document */
572 function specialLink( $name, $key = '' ) {
573 global $wgContLang;
574
575 if ( '' == $key ) { $key = strtolower( $name ); }
576 $pn = $wgContLang->ucfirst( $name );
577 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
578 wfMsg( $key ) );
579 }
580
581 /** @todo document */
582 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
583 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
584 global $wgNoFollowLinks;
585 if( $wgNoFollowLinks ) {
586 $style .= ' rel="nofollow"';
587 }
588 $url = htmlspecialchars( $url );
589 if( $escape ) {
590 $text = htmlspecialchars( $text );
591 }
592 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
593 }
594
595 /**
596 * This function is called by all recent changes variants, by the page history,
597 * and by the user contributions list. It is responsible for formatting edit
598 * comments. It escapes any HTML in the comment, but adds some CSS to format
599 * auto-generated comments (from section editing) and formats [[wikilinks]].
600 *
601 * The &$title parameter must be a title OBJECT. It is used to generate a
602 * direct link to the section in the autocomment.
603 * @author Erik Moeller <moeller@scireview.de>
604 *
605 * Note: there's not always a title to pass to this function.
606 * Since you can't set a default parameter for a reference, I've turned it
607 * temporarily to a value pass. Should be adjusted further. --brion
608 */
609 function formatComment($comment, $title = NULL) {
610 $fname = 'Linker::formatComment';
611 wfProfileIn( $fname );
612
613 global $wgContLang;
614 $comment = str_replace( "\n", " ", $comment );
615 $comment = htmlspecialchars( $comment );
616
617 # The pattern for autogen comments is / * foo * /, which makes for
618 # some nasty regex.
619 # We look for all comments, match any text before and after the comment,
620 # add a separator where needed and format the comment itself with CSS
621 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
622 $pre=$match[1];
623 $auto=$match[2];
624 $post=$match[3];
625 $link='';
626 if($title) {
627 $section=$auto;
628
629 # This is hackish but should work in most cases.
630 $section=str_replace('[[','',$section);
631 $section=str_replace(']]','',$section);
632 $title->mFragment=$section;
633 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
634 }
635 $sep='-';
636 $auto=$link.$auto;
637 if($pre) { $auto = $sep.' '.$auto; }
638 if($post) { $auto .= ' '.$sep; }
639 $auto='<span class="autocomment">'.$auto.'</span>';
640 $comment=$pre.$auto.$post;
641 }
642
643 # format regular and media links - all other wiki formatting
644 # is ignored
645 $medians = $wgContLang->getNsText( NS_MEDIA ) . ':';
646 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
647 # Handle link renaming [[foo|text]] will show link as "text"
648 if( "" != $match[3] ) {
649 $text = $match[3];
650 } else {
651 $text = $match[1];
652 }
653 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
654 # Media link; trail not supported.
655 $linkRegexp = '/\[\[(.*?)\]\]/';
656 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
657 } else {
658 # Other kind of link
659 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
660 $trail = $submatch[1];
661 } else {
662 $trail = "";
663 }
664 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
665 if ($match[1][0] == ':')
666 $match[1] = substr($match[1], 1);
667 $thelink = $this->makeLink( $match[1], $text, "", $trail );
668 }
669 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
670 }
671 wfProfileOut( $fname );
672 return $comment;
673 }
674
675 /**
676 * Wrap a comment in standard punctuation and formatting if
677 * it's non-empty, otherwise return empty string.
678 *
679 * @param string $comment
680 * @param Title $title
681 * @return string
682 * @access public
683 */
684 function commentBlock( $comment, $title = NULL ) {
685 if( $comment == '' || $comment == '*' ) {
686 return '';
687 } else {
688 $formatted = $this->formatComment( $comment, $title );
689 return " <span class='comment'>($formatted)</span>";
690 }
691 }
692
693 /** @todo document */
694 function tocIndent() {
695 return "\n<ul>";
696 }
697
698 /** @todo document */
699 function tocUnindent($level) {
700 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
701 }
702
703 /**
704 * parameter level defines if we are on an indentation level
705 */
706 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
707 return "\n<li class='toclevel-$level'><a href=\"#" .
708 $anchor . '"><span class="tocnumber">' .
709 $tocnumber . '</span> <span class="toctext">' .
710 $tocline . '</span></a>';
711 }
712
713 /** @todo document */
714 function tocLineEnd() {
715 return "</li>\n";
716 }
717
718 /** @todo document */
719 function tocList($toc) {
720 global $wgJsMimeType;
721 return "<table id='toc' class='toc'><tr><td>"
722 . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
723 . $toc
724 . "</ul>\n</td></tr></table>\n"
725 . '<script type="'.$wgJsMimeType.'">'
726 . ' if (window.showTocToggle) {'
727 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
728 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
729 . ' showTocToggle();'
730 . ' } '
731 . "</script>\n";
732 }
733
734 /** @todo document */
735 function editSectionLinkForOther( $title, $section ) {
736 global $wgRequest;
737 global $wgContLang;
738
739 $title = Title::newFromText($title);
740 $editurl = '&section='.$section;
741 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
742
743 if( $wgContLang->isRTL() ) {
744 $farside = 'left';
745 $nearside = 'right';
746 } else {
747 $farside = 'right';
748 $nearside = 'left';
749 }
750 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
751
752 }
753
754 /** @todo document */
755 function editSectionLink( $nt, $section ) {
756 global $wgRequest;
757 global $wgContLang;
758
759 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
760 # Section edit links would be out of sync on an old page.
761 # But, if we're diffing to the current page, they'll be
762 # correct.
763 return '';
764 }
765
766 $editurl = '&section='.$section;
767 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
768
769 if( $wgContLang->isRTL() ) {
770 $farside = 'left';
771 $nearside = 'right';
772 } else {
773 $farside = 'right';
774 $nearside = 'left';
775 }
776 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
777 }
778
779 /**
780 * Split a link trail, return the "inside" portion and the remainder of the trail
781 * as a two-element array
782 *
783 * @static
784 */
785 function splitTrail( $trail ) {
786 static $regex = false;
787 if ( $regex === false ) {
788 global $wgContLang;
789 $regex = $wgContLang->linkTrail();
790 }
791 $inside = '';
792 if ( '' != $trail ) {
793 if ( preg_match( $regex, $trail, $m ) ) {
794 $inside = $m[1];
795 $trail = $m[2];
796 }
797 }
798 return array( $inside, $trail );
799 }
800
801 }
802 ?>