c65aa6d180d1e75489ea264a2b1ef15ec06fc625
[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( 'Skin::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 Skin::makeLink(): "'.$title."\"\n" );
90 $result = $text == "" ? $title : $text;
91 }
92
93 wfProfileOut( 'Skin::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 Skin::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 Skin::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 Skin::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 = 'Skin::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 return $t;
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 );
184 } else {
185 wfProfileIn( $fname.'-immediate' );
186 # Work out link colour immediately
187 $aid = $nt->getArticleID() ;
188 if ( 0 == $aid ) {
189 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
190 } else {
191 $threshold = $wgUser->getOption('stubthreshold') ;
192 if ( $threshold > 0 ) {
193 $dbr =& wfGetDB( DB_SLAVE );
194 $s = $dbr->selectRow(
195 array( 'page' ),
196 array( 'page_len',
197 'page_namespace',
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
204 }
205 } else {
206 $size = $threshold*2 ; # Really big
207 }
208 } else {
209 $size = 1 ;
210 }
211 if ( $size < $threshold ) {
212 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
213 } else {
214 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
215 }
216 }
217 wfProfileOut( $fname.'-immediate' );
218 }
219 wfProfileOut( $fname );
220 return $retVal;
221 }
222
223 /**
224 * Pass a title object, not a title string
225 */
226 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
227 global $wgOut, $wgTitle, $wgInputEncoding;
228
229 $fname = 'Skin::makeKnownLinkObj';
230 wfProfileIn( $fname );
231
232 if ( !is_object( $nt ) ) {
233 wfProfileOut( $fname );
234 return $text;
235 }
236
237 $u = $nt->escapeLocalURL( $query );
238 if ( '' != $nt->getFragment() ) {
239 if( $nt->getPrefixedDbkey() == '' ) {
240 $u = '';
241 if ( '' == $text ) {
242 $text = htmlspecialchars( $nt->getFragment() );
243 }
244 }
245 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT, $wgInputEncoding ) );
246 $replacearray = array(
247 '%3A' => ':',
248 '%' => '.'
249 );
250 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
251 }
252 if ( '' == $text ) {
253 $text = htmlspecialchars( $nt->getPrefixedText() );
254 }
255 $style = $this->getInternalLinkAttributesObj( $nt, $text );
256
257 list( $inside, $trail ) = Linker::splitTrail( $trail );
258 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
259 wfProfileOut( $fname );
260 return $r;
261 }
262
263 /**
264 * Pass a title object, not a title string
265 */
266 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
267 # Fail gracefully
268 if ( ! isset($nt) ) {
269 # wfDebugDieBacktrace();
270 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
271 }
272
273 $fname = 'Skin::makeBrokenLinkObj';
274 wfProfileIn( $fname );
275
276 if ( '' == $query ) {
277 $q = 'action=edit';
278 } else {
279 $q = 'action=edit&'.$query;
280 }
281 $u = $nt->escapeLocalURL( $q );
282
283 if ( '' == $text ) {
284 $text = htmlspecialchars( $nt->getPrefixedText() );
285 }
286 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
287
288 list( $inside, $trail ) = Linker::splitTrail( $trail );
289 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
290
291 wfProfileOut( $fname );
292 return $s;
293 }
294
295 /**
296 * Pass a title object, not a title string
297 */
298 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
299 $link = $nt->getPrefixedURL();
300
301 $u = $nt->escapeLocalURL( $query );
302
303 if ( '' == $text ) {
304 $text = htmlspecialchars( $nt->getPrefixedText() );
305 }
306 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
307
308 list( $inside, $trail ) = Linker::splitTrail( $trail );
309 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
310 return $s;
311 }
312
313 /** @todo document */
314 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
315 $u = $nt->escapeLocalURL( $query );
316 if ( '' == $text ) {
317 $text = htmlspecialchars( $nt->getPrefixedText() );
318 }
319 list( $inside, $trail ) = Linker::splitTrail( $trail );
320 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
321 }
322
323 /** @todo document */
324 function fnamePart( $url ) {
325 $basename = strrchr( $url, '/' );
326 if ( false === $basename ) {
327 $basename = $url;
328 } else {
329 $basename = substr( $basename, 1 );
330 }
331 return htmlspecialchars( $basename );
332 }
333
334 /** Obsolete alias */
335 function makeImage( $url, $alt = '' ) {
336 return $this->makeExternalImage( $url, $alt );
337 }
338
339 /** @todo document */
340 function makeExternalImage( $url, $alt = '' ) {
341 global $wgOut;
342 if ( '' == $alt ) {
343 $alt = $this->fnamePart( $url );
344 }
345 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
346 return $s;
347 }
348
349 /** @todo document */
350 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
351 $thumb = false, $manual_thumb = '' )
352 {
353 global $wgContLang, $wgUser, $wgThumbLimits;
354
355 $img = new Image( $nt );
356 $url = $img->getViewURL();
357 $prefix = $postfix = '';
358
359 if ( 'center' == $align )
360 {
361 $prefix = '<div class="center">';
362 $postfix = '</div>';
363 $align = 'none';
364 }
365
366 if ( $thumb || $framed ) {
367
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")
372 #
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';
377 }
378
379
380 if ( $width === false ) {
381 $wopt = $wgUser->getOption( 'thumbsize' );
382
383 if( !isset( $wgThumbLimits[$wopt] ) ) {
384 $wopt = User::getDefaultOption( 'thumbsize' );
385 }
386
387 $width = $wgThumbLimits[$wopt];
388 }
389
390 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
391
392 } elseif ( $width ) {
393
394 # Create a resized image, without the additional thumbnail
395 # features
396
397 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
398 $width = $img->getWidth() * $height / $img->getHeight();
399 }
400 if ( '' == $manual_thumb ) {
401 $url = $img->createThumb( $width );
402 }
403 }
404
405 $u = $nt->escapeLocalURL();
406 if ( $url == '' ) {
407 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
408 //$s .= "<br />{$alt}<br />{$url}<br />\n";
409 } else {
410 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
411 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
412 }
413 if ( '' != $align ) {
414 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
415 }
416 return str_replace("\n", ' ',$prefix.$s.$postfix);
417 }
418
419 /**
420 * Make HTML for a thumbnail including image, border and caption
421 * $img is an Image object
422 */
423 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
424 global $wgStylePath, $wgContLang;
425 $url = $img->getViewURL();
426
427 $width = $height = 0;
428 if ( $img->exists() )
429 {
430 $width = $img->getWidth();
431 $height = $img->getHeight();
432 }
433 if ( 0 == $width || 0 == $height )
434 {
435 $width = $height = 200;
436 }
437 if ( $boxwidth == 0 )
438 {
439 $boxwidth = 200;
440 }
441 if ( $framed )
442 {
443 // Use image dimensions, don't scale
444 $boxwidth = $width;
445 $oboxwidth = $boxwidth + 2;
446 $boxheight = $height;
447 $thumbUrl = $url;
448 } else {
449 $h = intval( $height/($width/$boxwidth) );
450 $oboxwidth = $boxwidth + 2;
451 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
452 {
453 $boxwidth *= $boxheight/$h;
454 } else {
455 $boxheight = $h;
456 }
457 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
458 }
459
460 if ( $manual_thumb != '' ) # Use manually specified thumbnail
461 {
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() )
466 {
467 $width = $manual_img->getWidth();
468 $height = $manual_img->getHeight();
469 $boxwidth = $width ;
470 $boxheight = $height ;
471 $oboxwidth = $boxwidth + 2 ;
472 }
473 }
474
475 $u = $img->getEscapeLocalURL();
476
477 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
478 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
479 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
480
481 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
482 if ( $thumbUrl == '' ) {
483 $s .= $this->makeBrokenImageLinkObj( $img->getTitle );
484 $zoomicon = '';
485 } else {
486 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
487 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
488 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
489 'longdesc="'.$u.'" /></a>';
490 if ( $framed ) {
491 $zoomicon="";
492 } else {
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>';
497 }
498 }
499 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
500 return str_replace("\n", ' ', $s);
501 }
502
503 /**
504 * Pass a title object, not a title string
505 */
506 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
507 # Fail gracefully
508 if ( ! isset($nt) ) {
509 # wfDebugDieBacktrace();
510 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
511 }
512
513 $fname = 'Skin::makeBrokenImageLinkObj';
514 wfProfileIn( $fname );
515
516 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
517 if ( '' != $query ) {
518 $q .= "&$query";
519 }
520 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
521 $url = $uploadTitle->escapeLocalURL( $q );
522
523 if ( '' == $text ) {
524 $text = htmlspecialchars( $nt->getPrefixedText() );
525 }
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}";
529
530 wfProfileOut( $fname );
531 return $s;
532 }
533
534 /** @todo document */
535 function makeMediaLink( $name, $url, $alt = '' ) {
536 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
537 return $this->makeMediaLinkObj( $nt, $alt );
538 }
539
540 /**
541 * Create a direct link to a given uploaded file.
542 *
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
548 *
549 * @access public
550 * @todo Handle invalid or missing images better.
551 */
552 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
553 if( is_null( $title ) ) {
554 ### HOTFIX. Instead of breaking, return empty string.
555 return $text;
556 } else {
557 $name = $title->getDBKey();
558 $img = new Image( $title );
559 $url = $img->getURL();
560 if( $nourl ) {
561 $url = str_replace( "http://", "http-noparse://", $url );
562 }
563 $alt = htmlspecialchars( $title->getText() );
564 if( $text == '' ) {
565 $text = $alt;
566 }
567 $u = htmlspecialchars( $url );
568 return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";
569 }
570 }
571
572 /** @todo document */
573 function specialLink( $name, $key = '' ) {
574 global $wgContLang;
575
576 if ( '' == $key ) { $key = strtolower( $name ); }
577 $pn = $wgContLang->ucfirst( $name );
578 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
579 wfMsg( $key ) );
580 }
581
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"';
588 }
589 $url = htmlspecialchars( $url );
590 if( $escape ) {
591 $text = htmlspecialchars( $text );
592 }
593 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
594 }
595
596 /**
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]].
601 *
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>
605 *
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
609 */
610 function formatComment($comment, $title = NULL) {
611 $fname = 'Skin::formatComment';
612 wfProfileIn( $fname );
613
614 global $wgContLang;
615 $comment = str_replace( "\n", " ", $comment );
616 $comment = htmlspecialchars( $comment );
617
618 # The pattern for autogen comments is / * foo * /, which makes for
619 # some nasty regex.
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)) {
623 $pre=$match[1];
624 $auto=$match[2];
625 $post=$match[3];
626 $link='';
627 if($title) {
628 $section=$auto;
629
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'));
635 }
636 $sep='-';
637 $auto=$link.$auto;
638 if($pre) { $auto = $sep.' '.$auto; }
639 if($post) { $auto .= ' '.$sep; }
640 $auto='<span class="autocomment">'.$auto.'</span>';
641 $comment=$pre.$auto.$post;
642 }
643
644 # format regular and media links - all other wiki formatting
645 # is ignored
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] ) {
650 $text = $match[3];
651 } else {
652 $text = $match[1];
653 }
654 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
655 # Media link; trail not supported.
656 $linkRegexp = '/\[\[(.*?)\]\]/';
657 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
658 } else {
659 # Other kind of link
660 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
661 $trail = $submatch[1];
662 } else {
663 $trail = "";
664 }
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 );
669 }
670 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
671 }
672 wfProfileOut( $fname );
673 return $comment;
674 }
675
676 /**
677 * Wrap a comment in standard punctuation and formatting if
678 * it's non-empty, otherwise return empty string.
679 *
680 * @param string $comment
681 * @param Title $title
682 * @return string
683 * @access public
684 */
685 function commentBlock( $comment, $title = NULL ) {
686 if( $comment == '' || $comment == '*' ) {
687 return '';
688 } else {
689 $formatted = $this->formatComment( $comment, $title );
690 return " <span class='comment'>($formatted)</span>";
691 }
692 }
693
694 /** @todo document */
695 function tocIndent() {
696 return "\n<ul>";
697 }
698
699 /** @todo document */
700 function tocUnindent($level) {
701 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
702 }
703
704 /**
705 * parameter level defines if we are on an indentation level
706 */
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>';
712 }
713
714 /** @todo document */
715 function tocLineEnd() {
716 return "</li>\n";
717 }
718
719 /** @todo document */
720 function tocList($toc) {
721 return "<table id='toc' class='toc'><tr><td>"
722 . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
723 . $toc
724 . "</ul>\n</td></tr></table>\n"
725 . '<script type="text/javascript">'
726 . ' if (window.showTocToggle) {'
727 . ' var tocShowText = "' . addslashes( wfMsg('showtoc') ) . '";'
728 . ' var tocHideText = "' . addslashes( wfMsg('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 ?>