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