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