Split link generation, table of contents, and image functions from the
[lhc/web/wiklou.git] / includes / Linker.php
1 <?php
2
3 /**
4 * Split off some of the internal bits from Skin.php.
5 * These functions are used for primarily page content:
6 * links, embedded images, table of contents. Links are
7 * also used in the skin.
8 *
9 * For the moment, Skin is a descendent class of Linker.
10 * In the future, it should probably be further split
11 * so that ever other bit of the wiki doesn't have to
12 * go loading up Skin to get at it.
13 *
14 * @package MediaWiki
15 */
16
17 class Linker {
18 var $linktrail ; # linktrail regexp
19 var $postParseLinkColour = false;
20
21 function Linker() {
22 global $wgContLang;
23 $this->linktrail = $wgContLang->linkTrail();
24
25 # Cache option lookups done very frequently
26 $options = array( 'highlightbroken', 'hover' );
27 foreach( $options as $opt ) {
28 global $wgUser;
29 $this->mOptions[$opt] = $wgUser->getOption( $opt );
30 }
31 }
32
33 /**
34 * Get/set accessor for delayed link colouring
35 */
36 function postParseLinkColour( $setting = NULL ) {
37 return wfSetVar( $this->postParseLinkColour, $setting );
38 }
39
40 function getExternalLinkAttributes( $link, $text, $class='' ) {
41 global $wgContLang;
42
43 $same = ($link == $text);
44 $link = urldecode( $link );
45 $link = $wgContLang->checkTitleEncoding( $link );
46 $link = str_replace( '_', ' ', $link );
47 $link = htmlspecialchars( $link );
48
49 $r = ($class != '') ? " class='$class'" : " class='external'";
50
51 if( !$same && $this->mOptions['hover'] ) {
52 $r .= " title=\"{$link}\"";
53 }
54 return $r;
55 }
56
57 function getInternalLinkAttributes( $link, $text, $broken = false ) {
58 $link = urldecode( $link );
59 $link = str_replace( '_', ' ', $link );
60 $link = htmlspecialchars( $link );
61
62 if( $broken == 'stub' ) {
63 $r = ' class="stub"';
64 } else if ( $broken == 'yes' ) {
65 $r = ' class="new"';
66 } else {
67 $r = '';
68 }
69
70 if( $this->mOptions['hover'] ) {
71 $r .= " title=\"{$link}\"";
72 }
73 return $r;
74 }
75
76 /**
77 * @param bool $broken
78 */
79 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
80 if( $broken == 'stub' ) {
81 $r = ' class="stub"';
82 } else if ( $broken == 'yes' ) {
83 $r = ' class="new"';
84 } else {
85 $r = '';
86 }
87
88 if( $this->mOptions['hover'] ) {
89 $r .= ' title="' . $nt->getEscapedText() . '"';
90 }
91 return $r;
92 }
93
94 /**
95 * Note: This function MUST call getArticleID() on the link,
96 * otherwise the cache won't get updated properly. See LINKCACHE.DOC.
97 */
98 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
99 wfProfileIn( 'Skin::makeLink' );
100 $nt = Title::newFromText( $title );
101 if ($nt) {
102 $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
103 } else {
104 wfDebug( 'Invalid title passed to Skin::makeLink(): "'.$title."\"\n" );
105 $result = $text == "" ? $title : $text;
106 }
107
108 wfProfileOut( 'Skin::makeLink' );
109 return $result;
110 }
111
112 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
113 $nt = Title::newFromText( $title );
114 if ($nt) {
115 return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
116 } else {
117 wfDebug( 'Invalid title passed to Skin::makeKnownLink(): "'.$title."\"\n" );
118 return $text == '' ? $title : $text;
119 }
120 }
121
122 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
123 $nt = Title::newFromText( $title );
124 if ($nt) {
125 return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
126 } else {
127 wfDebug( 'Invalid title passed to Skin::makeBrokenLink(): "'.$title."\"\n" );
128 return $text == '' ? $title : $text;
129 }
130 }
131
132 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
133 $nt = Title::newFromText( $title );
134 if ($nt) {
135 return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
136 } else {
137 wfDebug( 'Invalid title passed to Skin::makeStubLink(): "'.$title."\"\n" );
138 return $text == '' ? $title : $text;
139 }
140 }
141
142 /**
143 * Pass a title object, not a title string
144 */
145 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
146 global $wgOut, $wgUser, $wgLinkHolders;
147 $fname = 'Skin::makeLinkObj';
148 wfProfileIn( $fname );
149
150 # Fail gracefully
151 if ( ! isset($nt) ) {
152 # wfDebugDieBacktrace();
153 wfProfileOut( $fname );
154 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
155 }
156
157 $ns = $nt->getNamespace();
158 $dbkey = $nt->getDBkey();
159 if ( $nt->isExternal() ) {
160 $u = $nt->getFullURL();
161 $link = $nt->getPrefixedURL();
162 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
163 $style = $this->getExternalLinkAttributes( $link, $text, 'extiw' );
164
165 $inside = '';
166 if ( '' != $trail ) {
167 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
168 $inside = $m[1];
169 $trail = $m[2];
170 }
171 }
172 # Assume $this->postParseLinkColour(). This prevents
173 # interwiki links from being parsed as external links.
174 global $wgInterwikiLinkHolders;
175 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
176 $nr = array_push($wgInterwikiLinkHolders, $t);
177 $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
178 } elseif ( 0 == $ns && "" == $dbkey ) {
179 # A self-link with a fragment; skip existence check.
180 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
181 } elseif ( ( NS_SPECIAL == $ns ) || ( NS_IMAGE == $ns ) ) {
182 # These are always shown as existing, currently.
183 # Special pages don't exist in the database; images may
184 # occasionally be present when there is no description
185 # page per se, so we always shown them.
186 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
187 } elseif ( $this->postParseLinkColour ) {
188 wfProfileIn( $fname.'-postparse' );
189 # Insert a placeholder, and we'll work out the existence checks
190 # in a big lump later.
191 $inside = '';
192 if ( '' != $trail ) {
193 if ( preg_match( $this->linktrail, $trail, $m ) ) {
194 $inside = $m[1];
195 $trail = $m[2];
196 }
197 }
198
199 # These get picked up by Parser::replaceLinkHolders()
200 $nr = array_push( $wgLinkHolders['namespaces'], $nt->getNamespace() );
201 $wgLinkHolders['dbkeys'][] = $dbkey;
202 $wgLinkHolders['queries'][] = $query;
203 $wgLinkHolders['texts'][] = $prefix.$text.$inside;
204 $wgLinkHolders['titles'][] =& $nt;
205
206 $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
207 wfProfileOut( $fname.'-postparse' );
208 } else {
209 wfProfileIn( $fname.'-immediate' );
210 # Work out link colour immediately
211 $aid = $nt->getArticleID() ;
212 if ( 0 == $aid ) {
213 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
214 } else {
215 $threshold = $wgUser->getOption('stubthreshold') ;
216 if ( $threshold > 0 ) {
217 $dbr =& wfGetDB( DB_SLAVE );
218 $s = $dbr->selectRow( 'cur', array( 'LENGTH(cur_text) AS x', 'cur_namespace',
219 'cur_is_redirect' ), array( 'cur_id' => $aid ), $fname ) ;
220 if ( $s !== false ) {
221 $size = $s->x;
222 if ( $s->cur_is_redirect OR $s->cur_namespace != 0 ) {
223 $size = $threshold*2 ; # Really big
224 }
225 } else {
226 $size = $threshold*2 ; # Really big
227 }
228 } else {
229 $size = 1 ;
230 }
231 if ( $size < $threshold ) {
232 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
233 } else {
234 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
235 }
236 }
237 wfProfileOut( $fname.'-immediate' );
238 }
239 wfProfileOut( $fname );
240 return $retVal;
241 }
242
243 /**
244 * Pass a title object, not a title string
245 */
246 function makeKnownLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
247 global $wgOut, $wgTitle, $wgInputEncoding;
248
249 $fname = 'Skin::makeKnownLinkObj';
250 wfProfileIn( $fname );
251
252 if ( !is_object( $nt ) ) {
253 wfProfileIn( $fname );
254 return $text;
255 }
256
257 $u = $nt->escapeLocalURL( $query );
258 if ( '' != $nt->getFragment() ) {
259 if( $nt->getPrefixedDbkey() == '' ) {
260 $u = '';
261 if ( '' == $text ) {
262 $text = htmlspecialchars( $nt->getFragment() );
263 }
264 }
265 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT, $wgInputEncoding ) );
266 $replacearray = array(
267 '%3A' => ':',
268 '%' => '.'
269 );
270 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
271 }
272 if ( '' == $text ) {
273 $text = htmlspecialchars( $nt->getPrefixedText() );
274 }
275 $style = $this->getInternalLinkAttributesObj( $nt, $text );
276
277 $inside = '';
278 if ( '' != $trail ) {
279 if ( preg_match( $this->linktrail, $trail, $m ) ) {
280 $inside = $m[1];
281 $trail = $m[2];
282 }
283 }
284 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
285 wfProfileOut( $fname );
286 return $r;
287 }
288
289 /**
290 * Pass a title object, not a title string
291 */
292 function makeBrokenLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
293 # Fail gracefully
294 if ( ! isset($nt) ) {
295 # wfDebugDieBacktrace();
296 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
297 }
298
299 $fname = 'Skin::makeBrokenLinkObj';
300 wfProfileIn( $fname );
301
302 if ( '' == $query ) {
303 $q = 'action=edit';
304 } else {
305 $q = 'action=edit&'.$query;
306 }
307 $u = $nt->escapeLocalURL( $q );
308
309 if ( '' == $text ) {
310 $text = htmlspecialchars( $nt->getPrefixedText() );
311 }
312 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
313
314 $inside = '';
315 if ( '' != $trail ) {
316 if ( preg_match( $this->linktrail, $trail, $m ) ) {
317 $inside = $m[1];
318 $trail = $m[2];
319 }
320 }
321 if ( $this->mOptions['highlightbroken'] ) {
322 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
323 } else {
324 $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>?</a>{$trail}";
325 }
326
327 wfProfileOut( $fname );
328 return $s;
329 }
330
331 /**
332 * Pass a title object, not a title string
333 */
334 function makeStubLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
335 $link = $nt->getPrefixedURL();
336
337 $u = $nt->escapeLocalURL( $query );
338
339 if ( '' == $text ) {
340 $text = htmlspecialchars( $nt->getPrefixedText() );
341 }
342 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
343
344 $inside = '';
345 if ( '' != $trail ) {
346 if ( preg_match( $this->linktrail, $trail, $m ) ) {
347 $inside = $m[1];
348 $trail = $m[2];
349 }
350 }
351 if ( $this->mOptions['highlightbroken'] ) {
352 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
353 } else {
354 $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>!</a>{$trail}";
355 }
356 return $s;
357 }
358
359 function makeSelfLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
360 $u = $nt->escapeLocalURL( $query );
361 if ( '' == $text ) {
362 $text = htmlspecialchars( $nt->getPrefixedText() );
363 }
364 $inside = '';
365 if ( '' != $trail ) {
366 if ( preg_match( $this->linktrail, $trail, $m ) ) {
367 $inside = $m[1];
368 $trail = $m[2];
369 }
370 }
371 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
372 }
373
374 function fnamePart( $url ) {
375 $basename = strrchr( $url, '/' );
376 if ( false === $basename ) {
377 $basename = $url;
378 } else {
379 $basename = substr( $basename, 1 );
380 }
381 return htmlspecialchars( $basename );
382 }
383
384 function makeImage( $url, $alt = '' ) {
385 global $wgOut;
386 if ( '' == $alt ) {
387 $alt = $this->fnamePart( $url );
388 }
389 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
390 return $s;
391 }
392
393 function makeImageLink( $name, $url, $alt = '' ) {
394 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
395 return $this->makeImageLinkObj( $nt, $alt );
396 }
397
398 function makeImageLinkObj( $nt, $alt = '' ) {
399 global $wgContLang, $wgUseImageResize;
400 $img = Image::newFromTitle( $nt );
401 $url = $img->getViewURL();
402
403 $align = '';
404 $prefix = $postfix = '';
405
406 # Check if the alt text is of the form "options|alt text"
407 # Options are:
408 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
409 # * left no resizing, just left align. label is used for alt= only
410 # * right same, but right aligned
411 # * none same, but not aligned
412 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
413 # * center center the image
414 # * framed Keep original image size, no magnify-button.
415
416 $part = explode( '|', $alt);
417
418 $mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
419 $mwLeft =& MagicWord::get( MAG_IMG_LEFT );
420 $mwRight =& MagicWord::get( MAG_IMG_RIGHT );
421 $mwNone =& MagicWord::get( MAG_IMG_NONE );
422 $mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
423 $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
424 $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
425 $alt = '';
426
427 $height = $framed = $thumb = false;
428 $manual_thumb = "" ;
429
430 foreach( $part as $key => $val ) {
431 $val_parts = explode ( "=" , $val , 2 ) ;
432 $left_part = array_shift ( $val_parts ) ;
433 if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
434 $thumb=true;
435 } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
436 # use manually specified thumbnail
437 $thumb=true;
438 $manual_thumb = array_shift ( $val_parts ) ;
439 } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
440 # remember to set an alignment, don't render immediately
441 $align = 'right';
442 } elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
443 # remember to set an alignment, don't render immediately
444 $align = 'left';
445 } elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
446 # remember to set an alignment, don't render immediately
447 $align = 'center';
448 } elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
449 # remember to set an alignment, don't render immediately
450 $align = 'none';
451 } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
452 # $match is the image width in pixels
453 if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
454 $width = intval( $m[1] );
455 $height = intval( $m[2] );
456 } else {
457 $width = intval($match);
458 }
459 } elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
460 $framed=true;
461 } else {
462 $alt = $val;
463 }
464 }
465 if ( 'center' == $align )
466 {
467 $prefix = '<div class="center">';
468 $postfix = '</div>';
469 $align = 'none';
470 }
471
472 if ( $thumb || $framed ) {
473
474 # Create a thumbnail. Alignment depends on language
475 # writing direction, # right aligned for left-to-right-
476 # languages ("Western languages"), left-aligned
477 # for right-to-left-languages ("Semitic languages")
478 #
479 # If thumbnail width has not been provided, it is set
480 # here to 180 pixels
481 if ( $align == '' ) {
482 $align = $wgContLang->isRTL() ? 'left' : 'right';
483 }
484 if ( ! isset($width) ) {
485 $width = 180;
486 }
487 return $prefix.$this->makeThumbLinkObj( $img, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
488
489 } elseif ( isset($width) ) {
490
491 # Create a resized image, without the additional thumbnail
492 # features
493
494 if ( ( ! $height === false )
495 && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
496 $width = $img->getWidth() * $height / $img->getHeight();
497 }
498 if ( '' == $manual_thumb ) $url = $img->createThumb( $width );
499 }
500
501 $alt = preg_replace( '/<[^>]*>/', '', $alt );
502 $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
503 $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
504
505 $u = $nt->escapeLocalURL();
506 if ( $url == '' ) {
507 $s = wfMsg( 'missingimage', $img->getName() );
508 $s .= "<br>{$alt}<br>{$url}<br>\n";
509 } else {
510 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
511 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
512 }
513 if ( '' != $align ) {
514 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
515 }
516 return str_replace("\n", ' ',$prefix.$s.$postfix);
517 }
518
519 /**
520 * Make HTML for a thumbnail including image, border and caption
521 * $img is an Image object
522 */
523 function makeThumbLinkObj( $img, $label = '', $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
524 global $wgStylePath, $wgContLang;
525 # $image = Title::makeTitleSafe( NS_IMAGE, $name );
526 $url = $img->getViewURL();
527
528 #$label = htmlspecialchars( $label );
529 $alt = preg_replace( '/<[^>]*>/', '', $label);
530 $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
531 $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
532
533 $width = $height = 0;
534 if ( $img->exists() )
535 {
536 $width = $img->getWidth();
537 $height = $img->getHeight();
538 }
539 if ( 0 == $width || 0 == $height )
540 {
541 $width = $height = 200;
542 }
543 if ( $boxwidth == 0 )
544 {
545 $boxwidth = 200;
546 }
547 if ( $framed )
548 {
549 // Use image dimensions, don't scale
550 $boxwidth = $width;
551 $oboxwidth = $boxwidth + 2;
552 $boxheight = $height;
553 $thumbUrl = $url;
554 } else {
555 $h = intval( $height/($width/$boxwidth) );
556 $oboxwidth = $boxwidth + 2;
557 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
558 {
559 $boxwidth *= $boxheight/$h;
560 } else {
561 $boxheight = $h;
562 }
563 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
564 }
565
566 if ( $manual_thumb != '' ) # Use manually specified thumbnail
567 {
568 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
569 $manual_img = Image::newFromTitle( $manual_title );
570 $thumbUrl = $manual_img->getViewURL();
571 if ( $manual_img->exists() )
572 {
573 $width = $manual_img->getWidth();
574 $height = $manual_img->getHeight();
575 $boxwidth = $width ;
576 $boxheight = $height ;
577 $oboxwidth = $boxwidth + 2 ;
578 }
579 }
580
581 $u = $img->getEscapeLocalURL();
582
583 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
584 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
585 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
586
587 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
588 if ( $thumbUrl == '' ) {
589 $s .= wfMsg( 'missingimage', $img->getName() );
590 $zoomicon = '';
591 } else {
592 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
593 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
594 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
595 'longdesc="'.$u.'" /></a>';
596 if ( $framed ) {
597 $zoomicon="";
598 } else {
599 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
600 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
601 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
602 'width="15" height="11" alt="'.$more.'" /></a></div>';
603 }
604 }
605 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
606 return str_replace("\n", ' ', $s);
607 }
608
609 function makeMediaLink( $name, $url, $alt = '' ) {
610 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
611 return $this->makeMediaLinkObj( $nt, $alt );
612 }
613
614 function makeMediaLinkObj( $nt, $alt = '', $nourl=false ) {
615 if ( ! isset( $nt ) )
616 {
617 ### HOTFIX. Instead of breaking, return empty string.
618 $s = $alt;
619 } else {
620 $name = $nt->getDBKey();
621 $img = Image::newFromTitle( $nt );
622 $url = $img->getURL();
623 # $nourl can be set by the parser
624 # this is a hack to mask absolute URLs, so the parser doesn't
625 # linkify them (it is currently not context-aware)
626 # 2004-10-25
627 if ($nourl) { $url=str_replace("http://","http-noparse://",$url) ; }
628 if ( empty( $alt ) ) {
629 $alt = preg_replace( '/\.(.+?)^/', '', $name );
630 }
631 $u = htmlspecialchars( $url );
632 $s = "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$alt}</a>";
633 }
634 return $s;
635 }
636
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 function makeExternalLink( $url, $text, $escape = true ) {
647 $style = $this->getExternalLinkAttributes( $url, $text );
648 $url = htmlspecialchars( $url );
649 if( $escape ) {
650 $text = htmlspecialchars( $text );
651 }
652 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
653 }
654
655 /**
656 * This function is called by all recent changes variants, by the page history,
657 * and by the user contributions list. It is responsible for formatting edit
658 * comments. It escapes any HTML in the comment, but adds some CSS to format
659 * auto-generated comments (from section editing) and formats [[wikilinks]].
660 *
661 * The &$title parameter must be a title OBJECT. It is used to generate a
662 * direct link to the section in the autocomment.
663 * @author Erik Moeller <moeller@scireview.de>
664 *
665 * Note: there's not always a title to pass to this function.
666 * Since you can't set a default parameter for a reference, I've turned it
667 * temporarily to a value pass. Should be adjusted further. --brion
668 */
669 function formatComment($comment, $title = NULL) {
670 $fname = 'Skin::formatComment';
671 wfProfileIn( $fname );
672
673 global $wgContLang;
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(Namespace::getMedia()).':';
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 function tocIndent($level) {
735 return str_repeat( '<div class="tocindent">'."\n", $level>0 ? $level : 0 );
736 }
737
738 function tocUnindent($level) {
739 return str_repeat( "</div>\n", $level>0 ? $level : 0 );
740 }
741
742 /**
743 * parameter level defines if we are on an indentation level
744 */
745 function tocLine( $anchor, $tocline, $level ) {
746 $link = '<a href="#'.$anchor.'">'.$tocline.'</a><br />';
747 if($level) {
748 return $link."\n";
749 } else {
750 return '<div class="tocline">'.$link."</div>\n";
751 }
752
753 }
754
755 function tocTable($toc) {
756 # note to CSS fanatics: putting this in a div does not work -- div won't auto-expand
757 # try min-width & co when somebody gets a chance
758 $hideline = ' <script type="text/javascript">showTocToggle("' . addslashes( wfMsg('showtoc') ) . '","' . addslashes( wfMsg('hidetoc') ) . '")</script>';
759 return
760 '<table border="0" id="toc"><tr id="toctitle"><td align="center">'."\n".
761 '<b>'.wfMsgForContent('toc').'</b>' .
762 $hideline .
763 '</td></tr><tr id="tocinside"><td>'."\n".
764 $toc."</td></tr></table>\n";
765 }
766
767 /**
768 * These two do not check for permissions: check $wgTitle->userCanEdit
769 * before calling them
770 */
771 function editSectionScriptForOther( $title, $section, $head ) {
772 $ttl = Title::newFromText( $title );
773 $url = $ttl->escapeLocalURL( 'action=edit&section='.$section );
774 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
775 }
776
777 function editSectionScript( $nt, $section, $head ) {
778 global $wgRequest;
779 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
780 return $head;
781 }
782 $url = $nt->escapeLocalURL( 'action=edit&section='.$section );
783 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
784 }
785
786 function editSectionLinkForOther( $title, $section ) {
787 global $wgRequest;
788 global $wgContLang;
789
790 $title = Title::newFromText($title);
791 $editurl = '&section='.$section;
792 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
793
794 if( $wgContLang->isRTL() ) {
795 $farside = 'left';
796 $nearside = 'right';
797 } else {
798 $farside = 'right';
799 $nearside = 'left';
800 }
801 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
802
803 }
804
805 function editSectionLink( $nt, $section ) {
806 global $wgRequest;
807 global $wgContLang;
808
809 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
810 # Section edit links would be out of sync on an old page.
811 # But, if we're diffing to the current page, they'll be
812 # correct.
813 return '';
814 }
815
816 $editurl = '&section='.$section;
817 $url = $this->makeKnownLink($nt->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 /**
831 * @access public
832 */
833 function suppressUrlExpansion() {
834 return false;
835 }
836
837 }
838
839 ?>