* Fixed unclosed <p> tag
[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 * @deprecated
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 /**
313 * Generate either a normal exists-style link or a stub link, depending
314 * on the given page size.
315 *
316 * @param int $size
317 * @param Title $nt
318 * @param string $text
319 * @param string $query
320 * @param string $trail
321 * @param string $prefix
322 * @return string HTML of link
323 */
324 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
325 global $wgUser;
326 $threshold = IntVal( $wgUser->getOption( 'stubthreshold' ) );
327 if( $size < $threshold ) {
328 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
329 } else {
330 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
331 }
332 }
333
334 /** @todo document */
335 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
336 $u = $nt->escapeLocalURL( $query );
337 if ( '' == $text ) {
338 $text = htmlspecialchars( $nt->getPrefixedText() );
339 }
340 list( $inside, $trail ) = Linker::splitTrail( $trail );
341 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
342 }
343
344 /** @todo document */
345 function fnamePart( $url ) {
346 $basename = strrchr( $url, '/' );
347 if ( false === $basename ) {
348 $basename = $url;
349 } else {
350 $basename = substr( $basename, 1 );
351 }
352 return htmlspecialchars( $basename );
353 }
354
355 /** Obsolete alias */
356 function makeImage( $url, $alt = '' ) {
357 return $this->makeExternalImage( $url, $alt );
358 }
359
360 /** @todo document */
361 function makeExternalImage( $url, $alt = '' ) {
362 global $wgOut;
363 if ( '' == $alt ) {
364 $alt = $this->fnamePart( $url );
365 }
366 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
367 return $s;
368 }
369
370 /** @todo document */
371 function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
372 $thumb = false, $manual_thumb = '' )
373 {
374 global $wgContLang, $wgUser, $wgThumbLimits;
375
376 $img = new Image( $nt );
377 $url = $img->getViewURL();
378 $prefix = $postfix = '';
379
380 wfDebug( "makeImageLinkObj: '$width'x'$height'\n" );
381
382 if ( 'center' == $align )
383 {
384 $prefix = '<div class="center">';
385 $postfix = '</div>';
386 $align = 'none';
387 }
388
389 if ( $thumb || $framed ) {
390
391 # Create a thumbnail. Alignment depends on language
392 # writing direction, # right aligned for left-to-right-
393 # languages ("Western languages"), left-aligned
394 # for right-to-left-languages ("Semitic languages")
395 #
396 # If thumbnail width has not been provided, it is set
397 # to the default user option as specified in Language*.php
398 if ( $align == '' ) {
399 $align = $wgContLang->isRTL() ? 'left' : 'right';
400 }
401
402
403 if ( $width === false ) {
404 $wopt = $wgUser->getOption( 'thumbsize' );
405
406 if( !isset( $wgThumbLimits[$wopt] ) ) {
407 $wopt = User::getDefaultOption( 'thumbsize' );
408 }
409
410 $width = $wgThumbLimits[$wopt];
411 }
412
413 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
414
415 } elseif ( $width ) {
416
417 # Create a resized image, without the additional thumbnail
418 # features
419
420 if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
421 $width = $img->getWidth() * $height / $img->getHeight();
422 }
423 if ( $manual_thumb == '') {
424 $thumb = $img->getThumbnail( $width );
425 if ( $thumb ) {
426 if( $width > $thumb->width ) {
427 // Requested a display size larger than the actual image;
428 // fake it up!
429 $height = floor($thumb->height * $width / $thumb->width);
430 wfDebug( "makeImageLinkObj: client-size height set to '$height'\n" );
431 } else {
432 $height = $thumb->height;
433 wfDebug( "makeImageLinkObj: thumb height set to '$height'\n" );
434 }
435 $url = $thumb->getUrl();
436 }
437 }
438 } else {
439 $width = $img->width;
440 $height = $img->height;
441 }
442
443 wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
444 $u = $nt->escapeLocalURL();
445 if ( $url == '' ) {
446 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
447 //$s .= "<br />{$alt}<br />{$url}<br />\n";
448 } else {
449 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
450 '<img src="'.$url.'" alt="'.$alt.'" ' .
451 ( $width
452 ? ( 'width="'.$width.'" height="'.$height.'" ' )
453 : '' ) .
454 'longdesc="'.$u.'" /></a>';
455 }
456 if ( '' != $align ) {
457 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
458 }
459 return str_replace("\n", ' ',$prefix.$s.$postfix);
460 }
461
462 /**
463 * Make HTML for a thumbnail including image, border and caption
464 * $img is an Image object
465 */
466 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
467 global $wgStylePath, $wgContLang;
468 $url = $img->getViewURL();
469
470 $width = $height = 0;
471 if ( $img->exists() )
472 {
473 $width = $img->getWidth();
474 $height = $img->getHeight();
475 }
476 if ( 0 == $width || 0 == $height )
477 {
478 $width = $height = 200;
479 }
480 if ( $boxwidth == 0 )
481 {
482 $boxwidth = 200;
483 }
484 if ( $framed )
485 {
486 // Use image dimensions, don't scale
487 $boxwidth = $width;
488 $oboxwidth = $boxwidth + 2;
489 $boxheight = $height;
490 $thumbUrl = $url;
491 } else {
492 $h = round( $height/($width/$boxwidth) );
493 $oboxwidth = $boxwidth + 2;
494 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
495 {
496 $boxwidth *= $boxheight/$h;
497 } else {
498 $boxheight = $h;
499 }
500 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
501 }
502
503 if ( $manual_thumb != '' ) # Use manually specified thumbnail
504 {
505 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
506 $manual_img = new Image( $manual_title );
507 $thumbUrl = $manual_img->getViewURL();
508 if ( $manual_img->exists() )
509 {
510 $width = $manual_img->getWidth();
511 $height = $manual_img->getHeight();
512 $boxwidth = $width ;
513 $boxheight = $height ;
514 $oboxwidth = $boxwidth + 2 ;
515 }
516 }
517
518 $u = $img->getEscapeLocalURL();
519
520 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
521 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
522 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
523
524 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
525 if ( $thumbUrl == '' ) {
526 $s .= $this->makeBrokenImageLinkObj( $img->getTitle );
527 $zoomicon = '';
528 } else {
529 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
530 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
531 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
532 'longdesc="'.$u.'" /></a>';
533 if ( $framed ) {
534 $zoomicon="";
535 } else {
536 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
537 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
538 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
539 'width="15" height="11" alt="'.$more.'" /></a></div>';
540 }
541 }
542 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
543 return str_replace("\n", ' ', $s);
544 }
545
546 /**
547 * Pass a title object, not a title string
548 */
549 function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
550 # Fail gracefully
551 if ( ! isset($nt) ) {
552 # wfDebugDieBacktrace();
553 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
554 }
555
556 $fname = 'Linker::makeBrokenImageLinkObj';
557 wfProfileIn( $fname );
558
559 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
560 if ( '' != $query ) {
561 $q .= "&$query";
562 }
563 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
564 $url = $uploadTitle->escapeLocalURL( $q );
565
566 if ( '' == $text ) {
567 $text = htmlspecialchars( $nt->getPrefixedText() );
568 }
569 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
570 list( $inside, $trail ) = Linker::splitTrail( $trail );
571 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
572
573 wfProfileOut( $fname );
574 return $s;
575 }
576
577 /** @todo document */
578 function makeMediaLink( $name, $url, $alt = '' ) {
579 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
580 return $this->makeMediaLinkObj( $nt, $alt );
581 }
582
583 /**
584 * Create a direct link to a given uploaded file.
585 *
586 * @param Title $title
587 * @param string $text pre-sanitized HTML
588 * @param bool $nourl Mask absolute URLs, so the parser doesn't
589 * linkify them (it is currently not context-aware)
590 * @return string HTML
591 *
592 * @access public
593 * @todo Handle invalid or missing images better.
594 */
595 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
596 if( is_null( $title ) ) {
597 ### HOTFIX. Instead of breaking, return empty string.
598 return $text;
599 } else {
600 $name = $title->getDBKey();
601 $img = new Image( $title );
602 if( $img->exists() ) {
603 $url = $img->getURL();
604 if( $nourl ) {
605 $url = str_replace( "http://", "http-noparse://", $url );
606 }
607 $class = 'internal';
608 } else {
609 $upload = Title::makeTitle( NS_SPECIAL, 'Upload' );
610 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
611 $class = 'new';
612 }
613 $alt = htmlspecialchars( $title->getText() );
614 if( $text == '' ) {
615 $text = $alt;
616 }
617 $u = htmlspecialchars( $url );
618 return "<a href=\"{$u}\" class='$class' title=\"{$alt}\">{$text}</a>";
619 }
620 }
621
622 /** @todo document */
623 function specialLink( $name, $key = '' ) {
624 global $wgContLang;
625
626 if ( '' == $key ) { $key = strtolower( $name ); }
627 $pn = $wgContLang->ucfirst( $name );
628 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
629 wfMsg( $key ) );
630 }
631
632 /** @todo document */
633 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
634 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
635 global $wgNoFollowLinks;
636 if( $wgNoFollowLinks ) {
637 $style .= ' rel="nofollow"';
638 }
639 $url = htmlspecialchars( $url );
640 if( $escape ) {
641 $text = htmlspecialchars( $text );
642 }
643 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
644 }
645
646 /**
647 * This function is called by all recent changes variants, by the page history,
648 * and by the user contributions list. It is responsible for formatting edit
649 * comments. It escapes any HTML in the comment, but adds some CSS to format
650 * auto-generated comments (from section editing) and formats [[wikilinks]].
651 *
652 * The &$title parameter must be a title OBJECT. It is used to generate a
653 * direct link to the section in the autocomment.
654 * @author Erik Moeller <moeller@scireview.de>
655 *
656 * Note: there's not always a title to pass to this function.
657 * Since you can't set a default parameter for a reference, I've turned it
658 * temporarily to a value pass. Should be adjusted further. --brion
659 */
660 function formatComment($comment, $title = NULL) {
661 $fname = 'Linker::formatComment';
662 wfProfileIn( $fname );
663
664 global $wgContLang;
665 $comment = str_replace( "\n", " ", $comment );
666 $comment = htmlspecialchars( $comment );
667
668 # The pattern for autogen comments is / * foo * /, which makes for
669 # some nasty regex.
670 # We look for all comments, match any text before and after the comment,
671 # add a separator where needed and format the comment itself with CSS
672 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
673 $pre=$match[1];
674 $auto=$match[2];
675 $post=$match[3];
676 $link='';
677 if($title) {
678 $section=$auto;
679
680 # This is hackish but should work in most cases.
681 $section=str_replace('[[','',$section);
682 $section=str_replace(']]','',$section);
683 $title->mFragment=$section;
684 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
685 }
686 $sep='-';
687 $auto=$link.$auto;
688 if($pre) { $auto = $sep.' '.$auto; }
689 if($post) { $auto .= ' '.$sep; }
690 $auto='<span class="autocomment">'.$auto.'</span>';
691 $comment=$pre.$auto.$post;
692 }
693
694 # format regular and media links - all other wiki formatting
695 # is ignored
696 $medians = $wgContLang->getNsText( NS_MEDIA ) . ':';
697 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
698 # Handle link renaming [[foo|text]] will show link as "text"
699 if( "" != $match[3] ) {
700 $text = $match[3];
701 } else {
702 $text = $match[1];
703 }
704 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
705 # Media link; trail not supported.
706 $linkRegexp = '/\[\[(.*?)\]\]/';
707 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
708 } else {
709 # Other kind of link
710 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
711 $trail = $submatch[1];
712 } else {
713 $trail = "";
714 }
715 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
716 if ($match[1][0] == ':')
717 $match[1] = substr($match[1], 1);
718 $thelink = $this->makeLink( $match[1], $text, "", $trail );
719 }
720 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
721 }
722 wfProfileOut( $fname );
723 return $comment;
724 }
725
726 /**
727 * Wrap a comment in standard punctuation and formatting if
728 * it's non-empty, otherwise return empty string.
729 *
730 * @param string $comment
731 * @param Title $title
732 * @return string
733 * @access public
734 */
735 function commentBlock( $comment, $title = NULL ) {
736 if( $comment == '' || $comment == '*' ) {
737 return '';
738 } else {
739 $formatted = $this->formatComment( $comment, $title );
740 return " <span class='comment'>($formatted)</span>";
741 }
742 }
743
744 /** @todo document */
745 function tocIndent() {
746 return "\n<ul>";
747 }
748
749 /** @todo document */
750 function tocUnindent($level) {
751 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
752 }
753
754 /**
755 * parameter level defines if we are on an indentation level
756 */
757 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
758 return "\n<li class='toclevel-$level'><a href=\"#" .
759 $anchor . '"><span class="tocnumber">' .
760 $tocnumber . '</span> <span class="toctext">' .
761 $tocline . '</span></a>';
762 }
763
764 /** @todo document */
765 function tocLineEnd() {
766 return "</li>\n";
767 }
768
769 /** @todo document */
770 function tocList($toc) {
771 global $wgJsMimeType;
772 return "<table id='toc' class='toc'><tr><td>"
773 . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
774 . $toc
775 . "</ul>\n</td></tr></table>\n"
776 . '<script type="'.$wgJsMimeType.'">'
777 . ' if (window.showTocToggle) {'
778 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
779 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
780 . ' showTocToggle();'
781 . ' } '
782 . "</script>\n";
783 }
784
785 /** @todo document */
786 function editSectionLinkForOther( $title, $section ) {
787 global $wgRequest;
788 global $wgContLang;
789
790 $title = Title::newFromText($title);
791 $editurl = '&section='.$section;
792 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
793
794 if( $wgContLang->isRTL() ) {
795 $farside = 'left';
796 $nearside = 'right';
797 } else {
798 $farside = 'right';
799 $nearside = 'left';
800 }
801 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
802
803 }
804
805 /** @todo document */
806 function editSectionLink( $nt, $section ) {
807 global $wgContLang;
808
809 $editurl = '&section='.$section;
810 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
811
812 if( $wgContLang->isRTL() ) {
813 $farside = 'left';
814 $nearside = 'right';
815 } else {
816 $farside = 'right';
817 $nearside = 'left';
818 }
819 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
820 }
821
822 /**
823 * Split a link trail, return the "inside" portion and the remainder of the trail
824 * as a two-element array
825 *
826 * @static
827 */
828 function splitTrail( $trail ) {
829 static $regex = false;
830 if ( $regex === false ) {
831 global $wgContLang;
832 $regex = $wgContLang->linkTrail();
833 }
834 $inside = '';
835 if ( '' != $trail ) {
836 if ( preg_match( $regex, $trail, $m ) ) {
837 $inside = $m[1];
838 $trail = $m[2];
839 }
840 }
841 return array( $inside, $trail );
842 }
843
844 }
845 ?>