(bug 4974) Don't follow redirected talk page on "new messages" link
[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 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
81 * it if you already have a title object handy. See makeLinkObj for further documentation.
82 *
83 * @param string $title The text of the title
84 * @param string $text Link text
85 * @param string $query Optional query part
86 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
87 * be included in the link text. Other characters will be appended after
88 * the end of the link.
89 */
90 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
91 wfProfileIn( 'Linker::makeLink' );
92 $nt = Title::newFromText( $title );
93 if ($nt) {
94 $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
95 } else {
96 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
97 $result = $text == "" ? $title : $text;
98 }
99
100 wfProfileOut( 'Linker::makeLink' );
101 return $result;
102 }
103
104 /**
105 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
106 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
107 *
108 * @param string $title The text of the title
109 * @param string $text Link text
110 * @param string $query Optional query part
111 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
112 * be included in the link text. Other characters will be appended after
113 * the end of the link.
114 */
115 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
116 $nt = Title::newFromText( $title );
117 if ($nt) {
118 return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
119 } else {
120 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
121 return $text == '' ? $title : $text;
122 }
123 }
124
125 /**
126 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
127 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
128 *
129 * @param string $title The text of the title
130 * @param string $text Link text
131 * @param string $query Optional query part
132 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
133 * be included in the link text. Other characters will be appended after
134 * the end of the link.
135 */
136 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
137 $nt = Title::newFromText( $title );
138 if ($nt) {
139 return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
140 } else {
141 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
142 return $text == '' ? $title : $text;
143 }
144 }
145
146 /**
147 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
148 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
149 *
150 * @param string $title The text of the title
151 * @param string $text Link text
152 * @param string $query Optional query part
153 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
154 * be included in the link text. Other characters will be appended after
155 * the end of the link.
156 */
157 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
158 $nt = Title::newFromText( $title );
159 if ($nt) {
160 return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
161 } else {
162 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
163 return $text == '' ? $title : $text;
164 }
165 }
166
167 /**
168 * Make a link for a title which may or may not be in the database. If you need to
169 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
170 * call to this will result in a DB query.
171 *
172 * @param string $title The text of the title
173 * @param string $text Link text
174 * @param string $query Optional query part
175 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
176 * be included in the link text. Other characters will be appended after
177 * the end of the link.
178 */
179 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
180 global $wgUser;
181 $fname = 'Linker::makeLinkObj';
182 wfProfileIn( $fname );
183
184 # Fail gracefully
185 if ( ! is_object($nt) ) {
186 # wfDebugDieBacktrace();
187 wfProfileOut( $fname );
188 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
189 }
190
191 $ns = $nt->getNamespace();
192 $dbkey = $nt->getDBkey();
193 if ( $nt->isExternal() ) {
194 $u = $nt->getFullURL();
195 $link = $nt->getPrefixedURL();
196 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
197 $style = $this->getExternalLinkAttributes( $link, $text, 'extiw' );
198
199 $inside = '';
200 if ( '' != $trail ) {
201 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
202 $inside = $m[1];
203 $trail = $m[2];
204 }
205 }
206
207 # Check for anchors, normalize the anchor
208
209 $parts = explode( '#', $u, 2 );
210 if ( count( $parts ) == 2 ) {
211 $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
212 $replacearray = array(
213 '%3A' => ':',
214 '%' => '.'
215 );
216 $u = $parts[0] . '#' .
217 str_replace( array_keys( $replacearray ),
218 array_values( $replacearray ),
219 $anchor );
220 }
221
222 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
223
224 wfProfileOut( $fname );
225 return $t;
226 } elseif ( $nt->isAlwaysKnown() ) {
227 # Image links, special page links and self-links with fragements are always known.
228 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
229 } else {
230 wfProfileIn( $fname.'-immediate' );
231 # Work out link colour immediately
232 $aid = $nt->getArticleID() ;
233 if ( 0 == $aid ) {
234 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
235 } else {
236 $threshold = $wgUser->getOption('stubthreshold') ;
237 if ( $threshold > 0 ) {
238 $dbr =& wfGetDB( DB_SLAVE );
239 $s = $dbr->selectRow(
240 array( 'page' ),
241 array( 'page_len',
242 'page_namespace',
243 'page_is_redirect' ),
244 array( 'page_id' => $aid ), $fname ) ;
245 if ( $s !== false ) {
246 $size = $s->page_len;
247 if ( $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) {
248 $size = $threshold*2 ; # Really big
249 }
250 } else {
251 $size = $threshold*2 ; # Really big
252 }
253 } else {
254 $size = 1 ;
255 }
256 if ( $size < $threshold ) {
257 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
258 } else {
259 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
260 }
261 }
262 wfProfileOut( $fname.'-immediate' );
263 }
264 wfProfileOut( $fname );
265 return $retVal;
266 }
267
268 /**
269 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
270 * it doesn't have to do a database query. It's also valid for interwiki titles and special
271 * pages.
272 *
273 * @param object Title of target page
274 * @param string Text to replace the title
275 * @param string Link target
276 * @param string Text after link
277 * @param string Text before link text
278 * @param string Extra attributes to the a-element
279 * @return the a-element
280 */
281 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
282
283 $fname = 'Linker::makeKnownLinkObj';
284 wfProfileIn( $fname );
285
286 if ( !is_object( $nt ) ) {
287 wfProfileOut( $fname );
288 return $text;
289 }
290
291 $u = $nt->escapeLocalURL( $query );
292 if ( '' != $nt->getFragment() ) {
293 if( $nt->getPrefixedDbkey() == '' ) {
294 $u = '';
295 if ( '' == $text ) {
296 $text = htmlspecialchars( $nt->getFragment() );
297 }
298 }
299 $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
300 $replacearray = array(
301 '%3A' => ':',
302 '%' => '.'
303 );
304 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
305 }
306 if ( '' == $text ) {
307 $text = htmlspecialchars( $nt->getPrefixedText() );
308 }
309 $style = $this->getInternalLinkAttributesObj( $nt, $text );
310
311 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
312
313 list( $inside, $trail ) = Linker::splitTrail( $trail );
314 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
315 wfProfileOut( $fname );
316 return $r;
317 }
318
319 /**
320 * Make a red link to the edit page of a given title.
321 *
322 * @param string $title The text of the title
323 * @param string $text Link text
324 * @param string $query Optional query part
325 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
326 * be included in the link text. Other characters will be appended after
327 * the end of the link.
328 */
329 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
330 # Fail gracefully
331 if ( ! isset($nt) ) {
332 # wfDebugDieBacktrace();
333 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
334 }
335
336 $fname = 'Linker::makeBrokenLinkObj';
337 wfProfileIn( $fname );
338
339 if ( '' == $query ) {
340 $q = 'action=edit';
341 } else {
342 $q = 'action=edit&'.$query;
343 }
344 $u = $nt->escapeLocalURL( $q );
345
346 if ( '' == $text ) {
347 $text = htmlspecialchars( $nt->getPrefixedText() );
348 }
349 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
350
351 list( $inside, $trail ) = Linker::splitTrail( $trail );
352 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
353
354 wfProfileOut( $fname );
355 return $s;
356 }
357
358 /**
359 * Make a brown link to a short article.
360 *
361 * @param string $title The text of the title
362 * @param string $text Link text
363 * @param string $query Optional query part
364 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
365 * be included in the link text. Other characters will be appended after
366 * the end of the link.
367 */
368 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
369 $link = $nt->getPrefixedURL();
370
371 $u = $nt->escapeLocalURL( $query );
372
373 if ( '' == $text ) {
374 $text = htmlspecialchars( $nt->getPrefixedText() );
375 }
376 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
377
378 list( $inside, $trail ) = Linker::splitTrail( $trail );
379 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
380 return $s;
381 }
382
383 /**
384 * Generate either a normal exists-style link or a stub link, depending
385 * on the given page size.
386 *
387 * @param int $size
388 * @param Title $nt
389 * @param string $text
390 * @param string $query
391 * @param string $trail
392 * @param string $prefix
393 * @return string HTML of link
394 */
395 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
396 global $wgUser;
397 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
398 if( $size < $threshold ) {
399 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
400 } else {
401 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
402 }
403 }
404
405 /**
406 * Make appropriate markup for a link to the current article. This is currently rendered
407 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
408 * despite $query not being used.
409 */
410 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
411 if ( '' == $text ) {
412 $text = htmlspecialchars( $nt->getPrefixedText() );
413 }
414 list( $inside, $trail ) = Linker::splitTrail( $trail );
415 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
416 }
417
418 /** @todo document */
419 function fnamePart( $url ) {
420 $basename = strrchr( $url, '/' );
421 if ( false === $basename ) {
422 $basename = $url;
423 } else {
424 $basename = substr( $basename, 1 );
425 }
426 return htmlspecialchars( $basename );
427 }
428
429 /** Obsolete alias */
430 function makeImage( $url, $alt = '' ) {
431 return $this->makeExternalImage( $url, $alt );
432 }
433
434 /** @todo document */
435 function makeExternalImage( $url, $alt = '' ) {
436 if ( '' == $alt ) {
437 $alt = $this->fnamePart( $url );
438 }
439 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
440 return $s;
441 }
442
443 /** @todo document */
444 function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
445 $thumb = false, $manual_thumb = '' )
446 {
447 global $wgContLang, $wgUser, $wgThumbLimits;
448
449 $img = new Image( $nt );
450 if ( !$img->allowInlineDisplay() && $img->exists() ) {
451 return $this->makeKnownLinkObj( $nt );
452 }
453
454 $url = $img->getViewURL();
455 $error = $prefix = $postfix = '';
456
457 wfDebug( "makeImageLinkObj: '$width'x'$height'\n" );
458
459 if ( 'center' == $align )
460 {
461 $prefix = '<div class="center">';
462 $postfix = '</div>';
463 $align = 'none';
464 }
465
466 if ( $thumb || $framed ) {
467
468 # Create a thumbnail. Alignment depends on language
469 # writing direction, # right aligned for left-to-right-
470 # languages ("Western languages"), left-aligned
471 # for right-to-left-languages ("Semitic languages")
472 #
473 # If thumbnail width has not been provided, it is set
474 # to the default user option as specified in Language*.php
475 if ( $align == '' ) {
476 $align = $wgContLang->isRTL() ? 'left' : 'right';
477 }
478
479
480 if ( $width === false ) {
481 $wopt = $wgUser->getOption( 'thumbsize' );
482
483 if( !isset( $wgThumbLimits[$wopt] ) ) {
484 $wopt = User::getDefaultOption( 'thumbsize' );
485 }
486
487 $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
488 }
489
490 return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
491 }
492
493 if ( $width && $img->exists() ) {
494
495 # Create a resized image, without the additional thumbnail
496 # features
497
498 if ( $height == false )
499 $height = -1;
500 if ( $manual_thumb == '') {
501 $thumb = $img->getThumbnail( $width, $height );
502 if ( $thumb ) {
503 // In most cases, $width = $thumb->width or $height = $thumb->height.
504 // If not, we're scaling the image larger than it can be scaled,
505 // so we send to the browser a smaller thumbnail, and let the client do the scaling.
506
507 if ($height != -1 && $width > $thumb->width * $height / $thumb->height) {
508 // $height is the limiting factor, not $width
509 // set $width to the largest it can be, such that the resulting
510 // scaled height is at most $height
511 $width = floor($thumb->width * $height / $thumb->height);
512 }
513 $height = round($thumb->height * $width / $thumb->width);
514
515 wfDebug( "makeImageLinkObj: client-size set to '$width x $height'\n" );
516 $url = $thumb->getUrl();
517 } else {
518 $error = htmlspecialchars( $img->getLastError() );
519 }
520 }
521 } else {
522 $width = $img->width;
523 $height = $img->height;
524 }
525
526 wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
527 $u = $nt->escapeLocalURL();
528 if ( $error ) {
529 $s = $error;
530 } elseif ( $url == '' ) {
531 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
532 //$s .= "<br />{$alt}<br />{$url}<br />\n";
533 } else {
534 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
535 '<img src="'.$url.'" alt="'.$alt.'" ' .
536 ( $width
537 ? ( 'width="'.$width.'" height="'.$height.'" ' )
538 : '' ) .
539 'longdesc="'.$u.'" /></a>';
540 }
541 if ( '' != $align ) {
542 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
543 }
544 return str_replace("\n", ' ',$prefix.$s.$postfix);
545 }
546
547 /**
548 * Make HTML for a thumbnail including image, border and caption
549 * $img is an Image object
550 */
551 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
552 global $wgStylePath, $wgContLang;
553 $url = $img->getViewURL();
554 $thumbUrl = '';
555 $error = '';
556
557 $width = $height = 0;
558 if ( $img->exists() ) {
559 $width = $img->getWidth();
560 $height = $img->getHeight();
561 }
562 if ( 0 == $width || 0 == $height ) {
563 $width = $height = 200;
564 }
565 if ( $boxwidth == 0 ) {
566 $boxwidth = 200;
567 }
568 if ( $framed ) {
569 // Use image dimensions, don't scale
570 $boxwidth = $width;
571 $boxheight = $height;
572 $thumbUrl = $url;
573 } else {
574 if ( $boxheight === false )
575 $boxheight = -1;
576 if ( '' == $manual_thumb ) {
577 $thumb = $img->getThumbnail( $boxwidth, $boxheight );
578 if ( $thumb ) {
579 $thumbUrl = $thumb->getUrl();
580 $boxwidth = $thumb->width;
581 $boxheight = $thumb->height;
582 } else {
583 $error = $img->getLastError();
584 }
585 }
586 }
587 $oboxwidth = $boxwidth + 2;
588
589 if ( $manual_thumb != '' ) # Use manually specified thumbnail
590 {
591 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
592 $manual_img = new Image( $manual_title );
593 $thumbUrl = $manual_img->getViewURL();
594 if ( $manual_img->exists() )
595 {
596 $width = $manual_img->getWidth();
597 $height = $manual_img->getHeight();
598 $boxwidth = $width ;
599 $boxheight = $height ;
600 $oboxwidth = $boxwidth + 2 ;
601 }
602 }
603
604 $u = $img->getEscapeLocalURL();
605
606 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
607 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
608 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
609
610 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
611 if( $thumbUrl == '' ) {
612 // Couldn't generate thumbnail? Scale the image client-side.
613 $thumbUrl = $url;
614 }
615 if ( $error ) {
616 $s .= htmlspecialchars( $error );
617 $zoomicon = '';
618 } elseif( !$img->exists() ) {
619 $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
620 $zoomicon = '';
621 } else {
622 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
623 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
624 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
625 'longdesc="'.$u.'" /></a>';
626 if ( $framed ) {
627 $zoomicon="";
628 } else {
629 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
630 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
631 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
632 'width="15" height="11" alt="'.$more.'" /></a></div>';
633 }
634 }
635 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
636 return str_replace("\n", ' ', $s);
637 }
638
639 /**
640 * Pass a title object, not a title string
641 */
642 function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
643 # Fail gracefully
644 if ( ! isset($nt) ) {
645 # wfDebugDieBacktrace();
646 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
647 }
648
649 $fname = 'Linker::makeBrokenImageLinkObj';
650 wfProfileIn( $fname );
651
652 $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
653 if ( '' != $query ) {
654 $q .= "&$query";
655 }
656 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
657 $url = $uploadTitle->escapeLocalURL( $q );
658
659 if ( '' == $text ) {
660 $text = htmlspecialchars( $nt->getPrefixedText() );
661 }
662 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
663 list( $inside, $trail ) = Linker::splitTrail( $trail );
664 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
665
666 wfProfileOut( $fname );
667 return $s;
668 }
669
670 /** @todo document */
671 function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
672 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
673 return $this->makeMediaLinkObj( $nt, $alt );
674 }
675
676 /**
677 * Create a direct link to a given uploaded file.
678 *
679 * @param Title $title
680 * @param string $text pre-sanitized HTML
681 * @param bool $nourl Mask absolute URLs, so the parser doesn't
682 * linkify them (it is currently not context-aware)
683 * @return string HTML
684 *
685 * @access public
686 * @todo Handle invalid or missing images better.
687 */
688 function makeMediaLinkObj( $title, $text = '' ) {
689 if( is_null( $title ) ) {
690 ### HOTFIX. Instead of breaking, return empty string.
691 return $text;
692 } else {
693 $name = $title->getDBKey();
694 $img = new Image( $title );
695 if( $img->exists() ) {
696 $url = $img->getURL();
697 $class = 'internal';
698 } else {
699 $upload = Title::makeTitle( NS_SPECIAL, 'Upload' );
700 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
701 $class = 'new';
702 }
703 $alt = htmlspecialchars( $title->getText() );
704 if( $text == '' ) {
705 $text = $alt;
706 }
707 $u = htmlspecialchars( $url );
708 return "<a href=\"{$u}\" class='$class' title=\"{$alt}\">{$text}</a>";
709 }
710 }
711
712 /** @todo document */
713 function specialLink( $name, $key = '' ) {
714 global $wgContLang;
715
716 if ( '' == $key ) { $key = strtolower( $name ); }
717 $pn = $wgContLang->ucfirst( $name );
718 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
719 wfMsg( $key ) );
720 }
721
722 /** @todo document */
723 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
724 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
725 global $wgNoFollowLinks;
726 if( $wgNoFollowLinks ) {
727 $style .= ' rel="nofollow"';
728 }
729 $url = htmlspecialchars( $url );
730 if( $escape ) {
731 $text = htmlspecialchars( $text );
732 }
733 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
734 }
735
736 /**
737 * This function is called by all recent changes variants, by the page history,
738 * and by the user contributions list. It is responsible for formatting edit
739 * comments. It escapes any HTML in the comment, but adds some CSS to format
740 * auto-generated comments (from section editing) and formats [[wikilinks]].
741 *
742 * The $title parameter must be a title OBJECT. It is used to generate a
743 * direct link to the section in the autocomment.
744 * @author Erik Moeller <moeller@scireview.de>
745 *
746 * Note: there's not always a title to pass to this function.
747 * Since you can't set a default parameter for a reference, I've turned it
748 * temporarily to a value pass. Should be adjusted further. --brion
749 */
750 function formatComment($comment, $title = NULL) {
751 $fname = 'Linker::formatComment';
752 wfProfileIn( $fname );
753
754 global $wgContLang;
755 $comment = str_replace( "\n", " ", $comment );
756 $comment = htmlspecialchars( $comment );
757
758 # The pattern for autogen comments is / * foo * /, which makes for
759 # some nasty regex.
760 # We look for all comments, match any text before and after the comment,
761 # add a separator where needed and format the comment itself with CSS
762 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
763 $pre=$match[1];
764 $auto=$match[2];
765 $post=$match[3];
766 $link='';
767 if( $title ) {
768 $section = $auto;
769
770 # This is hackish but should work in most cases.
771 $section = str_replace( '[[', '', $section );
772 $section = str_replace( ']]', '', $section );
773 $sectionTitle = wfClone( $title );
774 $sectionTitle->mFragment = $section;
775 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
776 }
777 $sep='-';
778 $auto=$link.$auto;
779 if($pre) { $auto = $sep.' '.$auto; }
780 if($post) { $auto .= ' '.$sep; }
781 $auto='<span class="autocomment">'.$auto.'</span>';
782 $comment=$pre.$auto.$post;
783 }
784
785 # format regular and media links - all other wiki formatting
786 # is ignored
787 $medians = $wgContLang->getNsText( NS_MEDIA ) . ':';
788 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
789 # Handle link renaming [[foo|text]] will show link as "text"
790 if( "" != $match[3] ) {
791 $text = $match[3];
792 } else {
793 $text = $match[1];
794 }
795 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
796 # Media link; trail not supported.
797 $linkRegexp = '/\[\[(.*?)\]\]/';
798 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
799 } else {
800 # Other kind of link
801 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
802 $trail = $submatch[1];
803 } else {
804 $trail = "";
805 }
806 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
807 if ($match[1][0] == ':')
808 $match[1] = substr($match[1], 1);
809 $thelink = $this->makeLink( $match[1], $text, "", $trail );
810 }
811 $comment = preg_replace( $linkRegexp, wfRegexReplacement( $thelink ), $comment, 1 );
812 }
813 wfProfileOut( $fname );
814 return $comment;
815 }
816
817 /**
818 * Wrap a comment in standard punctuation and formatting if
819 * it's non-empty, otherwise return empty string.
820 *
821 * @param string $comment
822 * @param Title $title
823 * @param bool $deleted
824 *
825 * @return string
826 */
827 function commentBlock( $comment, $title = NULL, $deleted = false ) {
828 // '*' used to be the comment inserted by the software way back
829 // in antiquity in case none was provided, here for backwards
830 // compatability, acc. to brion -ævar
831 if( $comment == '' || $comment == '*' ) {
832 return '';
833 } else {
834 if ( $deleted )
835 return " <span class='comment'>(...)</span>";
836 else {
837 $formatted = $this->formatComment( $comment, $title );
838 return " <span class='comment'>($formatted)</span>";
839 }
840 }
841 }
842
843 /** @todo document */
844 function tocIndent() {
845 return "\n<ul>";
846 }
847
848 /** @todo document */
849 function tocUnindent($level) {
850 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
851 }
852
853 /**
854 * parameter level defines if we are on an indentation level
855 */
856 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
857 return "\n<li class='toclevel-$level'><a href=\"#" .
858 $anchor . '"><span class="tocnumber">' .
859 $tocnumber . '</span> <span class="toctext">' .
860 $tocline . '</span></a>';
861 }
862
863 /** @todo document */
864 function tocLineEnd() {
865 return "</li>\n";
866 }
867
868 /** @todo document */
869 function tocList($toc) {
870 global $wgJsMimeType;
871 $title = wfMsgForContent('toc') ;
872 return
873 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
874 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
875 . $toc
876 . "</ul>\n</td></tr></table>\n"
877 . '<script type="' . $wgJsMimeType . '">'
878 . ' if (window.showTocToggle) {'
879 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
880 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
881 . ' showTocToggle();'
882 . ' } '
883 . "</script>\n";
884 }
885
886 /** @todo document */
887 function editSectionLinkForOther( $title, $section ) {
888 global $wgContLang;
889
890 $title = Title::newFromText( $title );
891 $editurl = '&section='.$section;
892 $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
893
894 if( $wgContLang->isRTL() ) {
895 $farside = 'left';
896 $nearside = 'right';
897 } else {
898 $farside = 'right';
899 $nearside = 'left';
900 }
901 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
902
903 }
904
905 /** @todo document */
906 function editSectionLink( $nt, $section ) {
907 global $wgContLang;
908
909 $editurl = '&section='.$section;
910 $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl );
911
912 if( $wgContLang->isRTL() ) {
913 $farside = 'left';
914 $nearside = 'right';
915 } else {
916 $farside = 'right';
917 $nearside = 'left';
918 }
919 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
920 }
921
922 /**
923 * Split a link trail, return the "inside" portion and the remainder of the trail
924 * as a two-element array
925 *
926 * @static
927 */
928 function splitTrail( $trail ) {
929 static $regex = false;
930 if ( $regex === false ) {
931 global $wgContLang;
932 $regex = $wgContLang->linkTrail();
933 }
934 $inside = '';
935 if ( '' != $trail ) {
936 if ( preg_match( $regex, $trail, $m ) ) {
937 $inside = $m[1];
938 $trail = $m[2];
939 }
940 }
941 return array( $inside, $trail );
942 }
943
944 }
945 ?>