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