BUG#1860 - Anchors of interwiki links did not get normalized
[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 var $linktrail ; # linktrail regexp
20 var $postParseLinkColour = false;
21
22 /** @todo document */
23 function Linker() {
24 global $wgContLang;
25 $this->linktrail = $wgContLang->linkTrail();
26 }
27
28 /**
29 * Get/set accessor for delayed link colouring
30 */
31 function postParseLinkColour( $setting = NULL ) {
32 return wfSetVar( $this->postParseLinkColour, $setting );
33 }
34
35 /** @todo document */
36 function getExternalLinkAttributes( $link, $text, $class='' ) {
37 global $wgContLang;
38
39 $same = ($link == $text);
40 $link = urldecode( $link );
41 $link = $wgContLang->checkTitleEncoding( $link );
42 $link = preg_replace( '/[\\x00-\\x1f_]/', ' ', $link );
43 $link = htmlspecialchars( $link );
44
45 $r = ($class != '') ? " class='$class'" : " class='external'";
46
47 $r .= " title=\"{$link}\"";
48 return $r;
49 }
50
51 /** @todo document */
52 function getInternalLinkAttributes( $link, $text, $broken = false ) {
53 $link = urldecode( $link );
54 $link = str_replace( '_', ' ', $link );
55 $link = htmlspecialchars( $link );
56
57 if( $broken == 'stub' ) {
58 $r = ' class="stub"';
59 } else if ( $broken == 'yes' ) {
60 $r = ' class="new"';
61 } else {
62 $r = '';
63 }
64
65 $r .= " title=\"{$link}\"";
66 return $r;
67 }
68
69 /**
70 * @param bool $broken
71 */
72 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
73 if( $broken == 'stub' ) {
74 $r = ' class="stub"';
75 } else if ( $broken == 'yes' ) {
76 $r = ' class="new"';
77 } else {
78 $r = '';
79 }
80
81 $r .= ' title="' . $nt->getEscapedText() . '"';
82 return $r;
83 }
84
85 /**
86 * Note: This function MUST call getArticleID() on the link,
87 * otherwise the cache won't get updated properly. See LINKCACHE.DOC.
88 */
89 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
90 wfProfileIn( 'Skin::makeLink' );
91 $nt = Title::newFromText( $title );
92 if ($nt) {
93 $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
94 } else {
95 wfDebug( 'Invalid title passed to Skin::makeLink(): "'.$title."\"\n" );
96 $result = $text == "" ? $title : $text;
97 }
98
99 wfProfileOut( 'Skin::makeLink' );
100 return $result;
101 }
102
103 /** @todo document */
104 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
105 $nt = Title::newFromText( $title );
106 if ($nt) {
107 return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
108 } else {
109 wfDebug( 'Invalid title passed to Skin::makeKnownLink(): "'.$title."\"\n" );
110 return $text == '' ? $title : $text;
111 }
112 }
113
114 /** @todo document */
115 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
116 $nt = Title::newFromText( $title );
117 if ($nt) {
118 return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
119 } else {
120 wfDebug( 'Invalid title passed to Skin::makeBrokenLink(): "'.$title."\"\n" );
121 return $text == '' ? $title : $text;
122 }
123 }
124
125 /** @todo document */
126 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
127 $nt = Title::newFromText( $title );
128 if ($nt) {
129 return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
130 } else {
131 wfDebug( 'Invalid title passed to Skin::makeStubLink(): "'.$title."\"\n" );
132 return $text == '' ? $title : $text;
133 }
134 }
135
136 /**
137 * Pass a title object, not a title string
138 */
139 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
140 global $wgOut, $wgUser, $wgLinkHolders, $wgInputEncoding;
141 $fname = 'Skin::makeLinkObj';
142 wfProfileIn( $fname );
143
144 # Fail gracefully
145 if ( ! isset($nt) ) {
146 # wfDebugDieBacktrace();
147 wfProfileOut( $fname );
148 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
149 }
150
151 $ns = $nt->getNamespace();
152 $dbkey = $nt->getDBkey();
153 if ( $nt->isExternal() ) {
154 $u = $nt->getFullURL();
155 $link = $nt->getPrefixedURL();
156 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
157 $style = $this->getExternalLinkAttributes( $link, $text, 'extiw' );
158
159 $inside = '';
160 if ( '' != $trail ) {
161 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
162 $inside = $m[1];
163 $trail = $m[2];
164 }
165 }
166
167 # Check for anchors, normalize the anchor
168
169 $parts = explode( '#', $u, 2 );
170 if ( count( $parts ) == 2 ) {
171 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $parts[1] ),
172 ENT_COMPAT,
173 $wgInputEncoding ) );
174 $replacearray = array(
175 '%3A' => ':',
176 '%' => '.'
177 );
178 $u = $parts[0] . '#' .
179 str_replace( array_keys( $replacearray ),
180 array_values( $replacearray ),
181 $anchor );
182 }
183
184 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
185
186 if( $this->postParseLinkColour ) {
187 # There's no existence check, but this will prevent
188 # interwiki links from being parsed as external links.
189 global $wgInterwikiLinkHolders;
190 $nr = array_push($wgInterwikiLinkHolders, $t);
191 $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
192 } else {
193 return $t;
194 }
195 } elseif ( 0 == $ns && "" == $dbkey ) {
196 # A self-link with a fragment; skip existence check.
197 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
198 } elseif ( ( NS_SPECIAL == $ns ) || ( NS_IMAGE == $ns ) ) {
199 # These are always shown as existing, currently.
200 # Special pages don't exist in the database; images may
201 # occasionally be present when there is no description
202 # page per se, so we always shown them.
203 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
204 } elseif ( $this->postParseLinkColour ) {
205 wfProfileIn( $fname.'-postparse' );
206 # Insert a placeholder, and we'll work out the existence checks
207 # in a big lump later.
208 $inside = '';
209 if ( '' != $trail ) {
210 if ( preg_match( $this->linktrail, $trail, $m ) ) {
211 $inside = $m[1];
212 $trail = $m[2];
213 }
214 }
215
216 # These get picked up by Parser::replaceLinkHolders()
217 $nr = array_push( $wgLinkHolders['namespaces'], $nt->getNamespace() );
218 $wgLinkHolders['dbkeys'][] = $dbkey;
219 $wgLinkHolders['queries'][] = $query;
220 $wgLinkHolders['texts'][] = $prefix.$text.$inside;
221 $wgLinkHolders['titles'][] =& $nt;
222
223 $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
224 wfProfileOut( $fname.'-postparse' );
225 } else {
226 wfProfileIn( $fname.'-immediate' );
227 # Work out link colour immediately
228 $aid = $nt->getArticleID() ;
229 if ( 0 == $aid ) {
230 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
231 } else {
232 $threshold = $wgUser->getOption('stubthreshold') ;
233 if ( $threshold > 0 ) {
234 $dbr =& wfGetDB( DB_SLAVE );
235 $s = $dbr->selectRow(
236 array( 'page' ),
237 array( 'page_len',
238 'page_namespace',
239 'page_is_redirect' ),
240 array( 'page_id' => $aid ), $fname ) ;
241 if ( $s !== false ) {
242 $size = $s->page_len;
243 if ( $s->page_is_redirect OR $s->page_namespace != NS_MAIN ) {
244 $size = $threshold*2 ; # Really big
245 }
246 } else {
247 $size = $threshold*2 ; # Really big
248 }
249 } else {
250 $size = 1 ;
251 }
252 if ( $size < $threshold ) {
253 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
254 } else {
255 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
256 }
257 }
258 wfProfileOut( $fname.'-immediate' );
259 }
260 wfProfileOut( $fname );
261 return $retVal;
262 }
263
264 /**
265 * Pass a title object, not a title string
266 */
267 function makeKnownLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
268 global $wgOut, $wgTitle, $wgInputEncoding;
269
270 $fname = 'Skin::makeKnownLinkObj';
271 wfProfileIn( $fname );
272
273 if ( !is_object( $nt ) ) {
274 wfProfileIn( $fname );
275 return $text;
276 }
277
278 $u = $nt->escapeLocalURL( $query );
279 if ( '' != $nt->getFragment() ) {
280 if( $nt->getPrefixedDbkey() == '' ) {
281 $u = '';
282 if ( '' == $text ) {
283 $text = htmlspecialchars( $nt->getFragment() );
284 }
285 }
286 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT, $wgInputEncoding ) );
287 $replacearray = array(
288 '%3A' => ':',
289 '%' => '.'
290 );
291 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
292 }
293 if ( '' == $text ) {
294 $text = htmlspecialchars( $nt->getPrefixedText() );
295 }
296 $style = $this->getInternalLinkAttributesObj( $nt, $text );
297
298 $inside = '';
299 if ( '' != $trail ) {
300 if ( preg_match( $this->linktrail, $trail, $m ) ) {
301 $inside = $m[1];
302 $trail = $m[2];
303 }
304 }
305 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
306 wfProfileOut( $fname );
307 return $r;
308 }
309
310 /**
311 * Pass a title object, not a title string
312 */
313 function makeBrokenLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
314 # Fail gracefully
315 if ( ! isset($nt) ) {
316 # wfDebugDieBacktrace();
317 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
318 }
319
320 $fname = 'Skin::makeBrokenLinkObj';
321 wfProfileIn( $fname );
322
323 if ( '' == $query ) {
324 $q = 'action=edit';
325 } else {
326 $q = 'action=edit&'.$query;
327 }
328 $u = $nt->escapeLocalURL( $q );
329
330 if ( '' == $text ) {
331 $text = htmlspecialchars( $nt->getPrefixedText() );
332 }
333 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
334
335 $inside = '';
336 if ( '' != $trail ) {
337 if ( preg_match( $this->linktrail, $trail, $m ) ) {
338 $inside = $m[1];
339 $trail = $m[2];
340 }
341 }
342 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
343
344 wfProfileOut( $fname );
345 return $s;
346 }
347
348 /**
349 * Pass a title object, not a title string
350 */
351 function makeStubLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
352 $link = $nt->getPrefixedURL();
353
354 $u = $nt->escapeLocalURL( $query );
355
356 if ( '' == $text ) {
357 $text = htmlspecialchars( $nt->getPrefixedText() );
358 }
359 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
360
361 $inside = '';
362 if ( '' != $trail ) {
363 if ( preg_match( $this->linktrail, $trail, $m ) ) {
364 $inside = $m[1];
365 $trail = $m[2];
366 }
367 }
368 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
369 return $s;
370 }
371
372 /** @todo document */
373 function makeSelfLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
374 $u = $nt->escapeLocalURL( $query );
375 if ( '' == $text ) {
376 $text = htmlspecialchars( $nt->getPrefixedText() );
377 }
378 $inside = '';
379 if ( '' != $trail ) {
380 if ( preg_match( $this->linktrail, $trail, $m ) ) {
381 $inside = $m[1];
382 $trail = $m[2];
383 }
384 }
385 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
386 }
387
388 /** @todo document */
389 function fnamePart( $url ) {
390 $basename = strrchr( $url, '/' );
391 if ( false === $basename ) {
392 $basename = $url;
393 } else {
394 $basename = substr( $basename, 1 );
395 }
396 return htmlspecialchars( $basename );
397 }
398
399 /** @todo document */
400 function makeImage( $url, $alt = '' ) {
401 global $wgOut;
402 if ( '' == $alt ) {
403 $alt = $this->fnamePart( $url );
404 }
405 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
406 return $s;
407 }
408
409 /** @todo document */
410 function makeImageLink( $name, $url, $alt = '' ) {
411 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
412 return $this->makeImageLinkObj( $nt, $alt );
413 }
414
415 /** @todo document */
416 function makeImageLinkObj( $nt, $alt = '' ) {
417 global $wgContLang, $wgUseImageResize;
418 $img = new Image( $nt );
419 $url = $img->getViewURL();
420
421 $align = '';
422 $prefix = $postfix = '';
423
424 # Check if the alt text is of the form "options|alt text"
425 # Options are:
426 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
427 # * left no resizing, just left align. label is used for alt= only
428 # * right same, but right aligned
429 # * none same, but not aligned
430 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
431 # * center center the image
432 # * framed Keep original image size, no magnify-button.
433
434 $part = explode( '|', $alt);
435
436 $mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
437 $mwLeft =& MagicWord::get( MAG_IMG_LEFT );
438 $mwRight =& MagicWord::get( MAG_IMG_RIGHT );
439 $mwNone =& MagicWord::get( MAG_IMG_NONE );
440 $mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
441 $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
442 $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
443 $alt = '';
444
445 $height = $framed = $thumb = false;
446 $manual_thumb = "" ;
447
448 foreach( $part as $key => $val ) {
449 $val_parts = explode ( "=" , $val , 2 ) ;
450 $left_part = array_shift ( $val_parts ) ;
451 if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
452 $thumb=true;
453 } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
454 # use manually specified thumbnail
455 $thumb=true;
456 $manual_thumb = array_shift ( $val_parts ) ;
457 } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
458 # remember to set an alignment, don't render immediately
459 $align = 'right';
460 } elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
461 # remember to set an alignment, don't render immediately
462 $align = 'left';
463 } elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
464 # remember to set an alignment, don't render immediately
465 $align = 'center';
466 } elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
467 # remember to set an alignment, don't render immediately
468 $align = 'none';
469 } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
470 # $match is the image width in pixels
471 if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
472 $width = intval( $m[1] );
473 $height = intval( $m[2] );
474 } else {
475 $width = intval($match);
476 }
477 } elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
478 $framed=true;
479 } else {
480 $alt = $val;
481 }
482 }
483 if ( 'center' == $align )
484 {
485 $prefix = '<div class="center">';
486 $postfix = '</div>';
487 $align = 'none';
488 }
489
490 if ( $thumb || $framed ) {
491
492 # Create a thumbnail. Alignment depends on language
493 # writing direction, # right aligned for left-to-right-
494 # languages ("Western languages"), left-aligned
495 # for right-to-left-languages ("Semitic languages")
496 #
497 # If thumbnail width has not been provided, it is set
498 # here to 180 pixels
499 if ( $align == '' ) {
500 $align = $wgContLang->isRTL() ? 'left' : 'right';
501 }
502 if ( ! isset($width) ) {
503 $width = 180;
504 }
505 return $prefix.$this->makeThumbLinkObj( $img, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
506
507 } elseif ( isset($width) ) {
508
509 # Create a resized image, without the additional thumbnail
510 # features
511
512 if ( ( ! $height === false )
513 && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
514 $width = $img->getWidth() * $height / $img->getHeight();
515 }
516 if ( '' == $manual_thumb ) $url = $img->createThumb( $width );
517 }
518
519 # FIXME: This is a gross hack using a global.
520 # Replace link color holders in the caption text so the
521 # text portion can be placed int the alt/title attributes.
522 global $wgParser;
523 $wgParser->replaceLinkHolders( $alt );
524
525 $alt = Sanitizer::stripAllTags( $alt );
526
527 $u = $nt->escapeLocalURL();
528 if ( $url == '' ) {
529 $s = wfMsg( 'missingimage', $img->getName() );
530 $s .= "<br />{$alt}<br />{$url}<br />\n";
531 } else {
532 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
533 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
534 }
535 if ( '' != $align ) {
536 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
537 }
538 return str_replace("\n", ' ',$prefix.$s.$postfix);
539 }
540
541 /**
542 * Make HTML for a thumbnail including image, border and caption
543 * $img is an Image object
544 */
545 function makeThumbLinkObj( $img, $label = '', $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
546 global $wgStylePath, $wgContLang;
547 # $image = Title::makeTitleSafe( NS_IMAGE, $name );
548 $url = $img->getViewURL();
549
550 #$label = htmlspecialchars( $label );
551 $alt = Sanitizer::stripAllTags( $label );
552
553 $width = $height = 0;
554 if ( $img->exists() )
555 {
556 $width = $img->getWidth();
557 $height = $img->getHeight();
558 }
559 if ( 0 == $width || 0 == $height )
560 {
561 $width = $height = 200;
562 }
563 if ( $boxwidth == 0 )
564 {
565 $boxwidth = 200;
566 }
567 if ( $framed )
568 {
569 // Use image dimensions, don't scale
570 $boxwidth = $width;
571 $oboxwidth = $boxwidth + 2;
572 $boxheight = $height;
573 $thumbUrl = $url;
574 } else {
575 $h = intval( $height/($width/$boxwidth) );
576 $oboxwidth = $boxwidth + 2;
577 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
578 {
579 $boxwidth *= $boxheight/$h;
580 } else {
581 $boxheight = $h;
582 }
583 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
584 }
585
586 if ( $manual_thumb != '' ) # Use manually specified thumbnail
587 {
588 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
589 $manual_img = new Image( $manual_title );
590 $thumbUrl = $manual_img->getViewURL();
591 if ( $manual_img->exists() )
592 {
593 $width = $manual_img->getWidth();
594 $height = $manual_img->getHeight();
595 $boxwidth = $width ;
596 $boxheight = $height ;
597 $oboxwidth = $boxwidth + 2 ;
598 }
599 }
600
601 $u = $img->getEscapeLocalURL();
602
603 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
604 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
605 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
606
607 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
608 if ( $thumbUrl == '' ) {
609 $s .= wfMsg( 'missingimage', $img->getName() );
610 $zoomicon = '';
611 } else {
612 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
613 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
614 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
615 'longdesc="'.$u.'" /></a>';
616 if ( $framed ) {
617 $zoomicon="";
618 } else {
619 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
620 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
621 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
622 'width="15" height="11" alt="'.$more.'" /></a></div>';
623 }
624 }
625 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
626 return str_replace("\n", ' ', $s);
627 }
628
629 /** @todo document */
630 function makeMediaLink( $name, $url, $alt = '' ) {
631 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
632 return $this->makeMediaLinkObj( $nt, $alt );
633 }
634
635 /**
636 * Create a direct link to a given uploaded file.
637 *
638 * @param Title $title
639 * @param string $text pre-sanitized HTML
640 * @param bool $nourl Mask absolute URLs, so the parser doesn't
641 * linkify them (it is currently not context-aware)
642 * @return string HTML
643 *
644 * @access public
645 * @todo Handle invalid or missing images better.
646 */
647 function makeMediaLinkObj( $title, $text = '', $nourl=false ) {
648 if( is_null( $title ) ) {
649 ### HOTFIX. Instead of breaking, return empty string.
650 return $text;
651 } else {
652 $name = $title->getDBKey();
653 $img = new Image( $title );
654 $url = $img->getURL();
655 if( $nourl ) {
656 $url = str_replace( "http://", "http-noparse://", $url );
657 }
658 $alt = htmlspecialchars( $title->getText() );
659 if( $text == '' ) {
660 $text = $alt;
661 }
662 $u = htmlspecialchars( $url );
663 return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";
664 }
665 }
666
667 /** @todo document */
668 function specialLink( $name, $key = '' ) {
669 global $wgContLang;
670
671 if ( '' == $key ) { $key = strtolower( $name ); }
672 $pn = $wgContLang->ucfirst( $name );
673 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
674 wfMsg( $key ) );
675 }
676
677 /** @todo document */
678 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
679 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
680 global $wgNoFollowLinks;
681 if( $wgNoFollowLinks ) {
682 $style .= ' rel="nofollow"';
683 }
684 $url = htmlspecialchars( $url );
685 if( $escape ) {
686 $text = htmlspecialchars( $text );
687 }
688 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
689 }
690
691 /**
692 * This function is called by all recent changes variants, by the page history,
693 * and by the user contributions list. It is responsible for formatting edit
694 * comments. It escapes any HTML in the comment, but adds some CSS to format
695 * auto-generated comments (from section editing) and formats [[wikilinks]].
696 *
697 * The &$title parameter must be a title OBJECT. It is used to generate a
698 * direct link to the section in the autocomment.
699 * @author Erik Moeller <moeller@scireview.de>
700 *
701 * Note: there's not always a title to pass to this function.
702 * Since you can't set a default parameter for a reference, I've turned it
703 * temporarily to a value pass. Should be adjusted further. --brion
704 */
705 function formatComment($comment, $title = NULL) {
706 $fname = 'Skin::formatComment';
707 wfProfileIn( $fname );
708
709 global $wgContLang;
710 $comment = str_replace( "\n", " ", $comment );
711 $comment = htmlspecialchars( $comment );
712
713 # The pattern for autogen comments is / * foo * /, which makes for
714 # some nasty regex.
715 # We look for all comments, match any text before and after the comment,
716 # add a separator where needed and format the comment itself with CSS
717 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
718 $pre=$match[1];
719 $auto=$match[2];
720 $post=$match[3];
721 $link='';
722 if($title) {
723 $section=$auto;
724
725 # This is hackish but should work in most cases.
726 $section=str_replace('[[','',$section);
727 $section=str_replace(']]','',$section);
728 $title->mFragment=$section;
729 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
730 }
731 $sep='-';
732 $auto=$link.$auto;
733 if($pre) { $auto = $sep.' '.$auto; }
734 if($post) { $auto .= ' '.$sep; }
735 $auto='<span class="autocomment">'.$auto.'</span>';
736 $comment=$pre.$auto.$post;
737 }
738
739 # format regular and media links - all other wiki formatting
740 # is ignored
741 $medians = $wgContLang->getNsText( NS_MEDIA ) . ':';
742 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
743 # Handle link renaming [[foo|text]] will show link as "text"
744 if( "" != $match[3] ) {
745 $text = $match[3];
746 } else {
747 $text = $match[1];
748 }
749 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
750 # Media link; trail not supported.
751 $linkRegexp = '/\[\[(.*?)\]\]/';
752 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
753 } else {
754 # Other kind of link
755 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
756 $trail = $submatch[1];
757 } else {
758 $trail = "";
759 }
760 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
761 if ($match[1][0] == ':')
762 $match[1] = substr($match[1], 1);
763 $thelink = $this->makeLink( $match[1], $text, "", $trail );
764 }
765 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
766 }
767 wfProfileOut( $fname );
768 return $comment;
769 }
770
771 /**
772 * Wrap a comment in standard punctuation and formatting if
773 * it's non-empty, otherwise return empty string.
774 *
775 * @param string $comment
776 * @param Title $title
777 * @return string
778 * @access public
779 */
780 function commentBlock( $comment, $title = NULL ) {
781 if( $comment == '' || $comment == '*' ) {
782 return '';
783 } else {
784 $formatted = $this->formatComment( $comment, $title );
785 return " <span class='comment'>($formatted)</span>";
786 }
787 }
788
789 /** @todo document */
790 function tocIndent() {
791 return "\n<ul>";
792 }
793
794 /** @todo document */
795 function tocUnindent($level) {
796 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
797 }
798
799 /**
800 * parameter level defines if we are on an indentation level
801 */
802 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
803 return "\n<li class='toclevel-$level'><a href=\"#" .
804 $anchor . '"><span class="tocnumber">' .
805 $tocnumber . '</span> <span class="toctext">' .
806 $tocline . '</span></a>';
807 }
808
809 /** @todo document */
810 function tocLineEnd() {
811 return "</li>\n";
812 }
813
814 /** @todo document */
815 function tocList($toc) {
816 return "<div id='toc'>\n"
817 . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
818 . $toc
819 . "</ul>\n</div>\n"
820 . '<script type="text/javascript">'
821 . ' if (window.showTocToggle) {'
822 . ' var tocShowText = "' . addslashes( wfMsg('showtoc') ) . '";'
823 . ' var tocHideText = "' . addslashes( wfMsg('hidetoc') ) . '"; '
824 . ' showTocToggle();'
825 . ' } '
826 . '</script>'
827 . "<div class='visualClear'></div>\n";
828 }
829
830 /** @todo document */
831 function editSectionLinkForOther( $title, $section ) {
832 global $wgRequest;
833 global $wgContLang;
834
835 $title = Title::newFromText($title);
836 $editurl = '&section='.$section;
837 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
838
839 if( $wgContLang->isRTL() ) {
840 $farside = 'left';
841 $nearside = 'right';
842 } else {
843 $farside = 'right';
844 $nearside = 'left';
845 }
846 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
847
848 }
849
850 /** @todo document */
851 function editSectionLink( $nt, $section ) {
852 global $wgRequest;
853 global $wgContLang;
854
855 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
856 # Section edit links would be out of sync on an old page.
857 # But, if we're diffing to the current page, they'll be
858 # correct.
859 return '';
860 }
861
862 $editurl = '&section='.$section;
863 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
864
865 if( $wgContLang->isRTL() ) {
866 $farside = 'left';
867 $nearside = 'right';
868 } else {
869 $farside = 'right';
870 $nearside = 'left';
871 }
872 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
873 }
874 }
875 ?>