Complete TOC recode: proper HTML list; CSS for layout; JS recode; hidden TOC stays...
[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 = preg_replace( '/[\\x00-\\x1f_]/', ' ', $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 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
173 if( $this->postParseLinkColour ) {
174 # There's no existence check, but this will prevent
175 # interwiki links from being parsed as external links.
176 global $wgInterwikiLinkHolders;
177 $nr = array_push($wgInterwikiLinkHolders, $t);
178 $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
179 } else {
180 return $t;
181 }
182 } elseif ( 0 == $ns && "" == $dbkey ) {
183 # A self-link with a fragment; skip existence check.
184 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
185 } elseif ( ( NS_SPECIAL == $ns ) || ( NS_IMAGE == $ns ) ) {
186 # These are always shown as existing, currently.
187 # Special pages don't exist in the database; images may
188 # occasionally be present when there is no description
189 # page per se, so we always shown them.
190 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
191 } elseif ( $this->postParseLinkColour ) {
192 wfProfileIn( $fname.'-postparse' );
193 # Insert a placeholder, and we'll work out the existence checks
194 # in a big lump later.
195 $inside = '';
196 if ( '' != $trail ) {
197 if ( preg_match( $this->linktrail, $trail, $m ) ) {
198 $inside = $m[1];
199 $trail = $m[2];
200 }
201 }
202
203 # These get picked up by Parser::replaceLinkHolders()
204 $nr = array_push( $wgLinkHolders['namespaces'], $nt->getNamespace() );
205 $wgLinkHolders['dbkeys'][] = $dbkey;
206 $wgLinkHolders['queries'][] = $query;
207 $wgLinkHolders['texts'][] = $prefix.$text.$inside;
208 $wgLinkHolders['titles'][] =& $nt;
209
210 $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
211 wfProfileOut( $fname.'-postparse' );
212 } else {
213 wfProfileIn( $fname.'-immediate' );
214 # Work out link colour immediately
215 $aid = $nt->getArticleID() ;
216 if ( 0 == $aid ) {
217 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
218 } else {
219 $threshold = $wgUser->getOption('stubthreshold') ;
220 if ( $threshold > 0 ) {
221 $dbr =& wfGetDB( DB_SLAVE );
222 $s = $dbr->selectRow( 'cur', array( 'LENGTH(cur_text) AS x', 'cur_namespace',
223 'cur_is_redirect' ), array( 'cur_id' => $aid ), $fname ) ;
224 if ( $s !== false ) {
225 $size = $s->x;
226 if ( $s->cur_is_redirect OR $s->cur_namespace != NS_MAIN ) {
227 $size = $threshold*2 ; # Really big
228 }
229 } else {
230 $size = $threshold*2 ; # Really big
231 }
232 } else {
233 $size = 1 ;
234 }
235 if ( $size < $threshold ) {
236 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
237 } else {
238 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
239 }
240 }
241 wfProfileOut( $fname.'-immediate' );
242 }
243 wfProfileOut( $fname );
244 return $retVal;
245 }
246
247 /**
248 * Pass a title object, not a title string
249 */
250 function makeKnownLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
251 global $wgOut, $wgTitle, $wgInputEncoding;
252
253 $fname = 'Skin::makeKnownLinkObj';
254 wfProfileIn( $fname );
255
256 if ( !is_object( $nt ) ) {
257 wfProfileIn( $fname );
258 return $text;
259 }
260
261 $u = $nt->escapeLocalURL( $query );
262 if ( '' != $nt->getFragment() ) {
263 if( $nt->getPrefixedDbkey() == '' ) {
264 $u = '';
265 if ( '' == $text ) {
266 $text = htmlspecialchars( $nt->getFragment() );
267 }
268 }
269 $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT, $wgInputEncoding ) );
270 $replacearray = array(
271 '%3A' => ':',
272 '%' => '.'
273 );
274 $u .= '#' . str_replace(array_keys($replacearray),array_values($replacearray),$anchor);
275 }
276 if ( '' == $text ) {
277 $text = htmlspecialchars( $nt->getPrefixedText() );
278 }
279 $style = $this->getInternalLinkAttributesObj( $nt, $text );
280
281 $inside = '';
282 if ( '' != $trail ) {
283 if ( preg_match( $this->linktrail, $trail, $m ) ) {
284 $inside = $m[1];
285 $trail = $m[2];
286 }
287 }
288 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
289 wfProfileOut( $fname );
290 return $r;
291 }
292
293 /**
294 * Pass a title object, not a title string
295 */
296 function makeBrokenLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
297 # Fail gracefully
298 if ( ! isset($nt) ) {
299 # wfDebugDieBacktrace();
300 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
301 }
302
303 $fname = 'Skin::makeBrokenLinkObj';
304 wfProfileIn( $fname );
305
306 if ( '' == $query ) {
307 $q = 'action=edit';
308 } else {
309 $q = 'action=edit&'.$query;
310 }
311 $u = $nt->escapeLocalURL( $q );
312
313 if ( '' == $text ) {
314 $text = htmlspecialchars( $nt->getPrefixedText() );
315 }
316 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
317
318 $inside = '';
319 if ( '' != $trail ) {
320 if ( preg_match( $this->linktrail, $trail, $m ) ) {
321 $inside = $m[1];
322 $trail = $m[2];
323 }
324 }
325 if ( $this->mOptions['highlightbroken'] ) {
326 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
327 } else {
328 $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>?</a>{$trail}";
329 }
330
331 wfProfileOut( $fname );
332 return $s;
333 }
334
335 /**
336 * Pass a title object, not a title string
337 */
338 function makeStubLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
339 $link = $nt->getPrefixedURL();
340
341 $u = $nt->escapeLocalURL( $query );
342
343 if ( '' == $text ) {
344 $text = htmlspecialchars( $nt->getPrefixedText() );
345 }
346 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
347
348 $inside = '';
349 if ( '' != $trail ) {
350 if ( preg_match( $this->linktrail, $trail, $m ) ) {
351 $inside = $m[1];
352 $trail = $m[2];
353 }
354 }
355 if ( $this->mOptions['highlightbroken'] ) {
356 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
357 } else {
358 $s = "{$prefix}{$text}{$inside}<a href=\"{$u}\"{$style}>!</a>{$trail}";
359 }
360 return $s;
361 }
362
363 function makeSelfLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
364 $u = $nt->escapeLocalURL( $query );
365 if ( '' == $text ) {
366 $text = htmlspecialchars( $nt->getPrefixedText() );
367 }
368 $inside = '';
369 if ( '' != $trail ) {
370 if ( preg_match( $this->linktrail, $trail, $m ) ) {
371 $inside = $m[1];
372 $trail = $m[2];
373 }
374 }
375 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
376 }
377
378 function fnamePart( $url ) {
379 $basename = strrchr( $url, '/' );
380 if ( false === $basename ) {
381 $basename = $url;
382 } else {
383 $basename = substr( $basename, 1 );
384 }
385 return htmlspecialchars( $basename );
386 }
387
388 function makeImage( $url, $alt = '' ) {
389 global $wgOut;
390 if ( '' == $alt ) {
391 $alt = $this->fnamePart( $url );
392 }
393 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
394 return $s;
395 }
396
397 function makeImageLink( $name, $url, $alt = '' ) {
398 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
399 return $this->makeImageLinkObj( $nt, $alt );
400 }
401
402 function makeImageLinkObj( $nt, $alt = '' ) {
403 global $wgContLang, $wgUseImageResize;
404 $img = Image::newFromTitle( $nt );
405 $url = $img->getViewURL();
406
407 $align = '';
408 $prefix = $postfix = '';
409
410 # Check if the alt text is of the form "options|alt text"
411 # Options are:
412 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
413 # * left no resizing, just left align. label is used for alt= only
414 # * right same, but right aligned
415 # * none same, but not aligned
416 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
417 # * center center the image
418 # * framed Keep original image size, no magnify-button.
419
420 $part = explode( '|', $alt);
421
422 $mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
423 $mwLeft =& MagicWord::get( MAG_IMG_LEFT );
424 $mwRight =& MagicWord::get( MAG_IMG_RIGHT );
425 $mwNone =& MagicWord::get( MAG_IMG_NONE );
426 $mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
427 $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
428 $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
429 $alt = '';
430
431 $height = $framed = $thumb = false;
432 $manual_thumb = "" ;
433
434 foreach( $part as $key => $val ) {
435 $val_parts = explode ( "=" , $val , 2 ) ;
436 $left_part = array_shift ( $val_parts ) ;
437 if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
438 $thumb=true;
439 } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
440 # use manually specified thumbnail
441 $thumb=true;
442 $manual_thumb = array_shift ( $val_parts ) ;
443 } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
444 # remember to set an alignment, don't render immediately
445 $align = 'right';
446 } elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
447 # remember to set an alignment, don't render immediately
448 $align = 'left';
449 } elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
450 # remember to set an alignment, don't render immediately
451 $align = 'center';
452 } elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
453 # remember to set an alignment, don't render immediately
454 $align = 'none';
455 } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
456 # $match is the image width in pixels
457 if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
458 $width = intval( $m[1] );
459 $height = intval( $m[2] );
460 } else {
461 $width = intval($match);
462 }
463 } elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
464 $framed=true;
465 } else {
466 $alt = $val;
467 }
468 }
469 if ( 'center' == $align )
470 {
471 $prefix = '<div class="center">';
472 $postfix = '</div>';
473 $align = 'none';
474 }
475
476 if ( $thumb || $framed ) {
477
478 # Create a thumbnail. Alignment depends on language
479 # writing direction, # right aligned for left-to-right-
480 # languages ("Western languages"), left-aligned
481 # for right-to-left-languages ("Semitic languages")
482 #
483 # If thumbnail width has not been provided, it is set
484 # here to 180 pixels
485 if ( $align == '' ) {
486 $align = $wgContLang->isRTL() ? 'left' : 'right';
487 }
488 if ( ! isset($width) ) {
489 $width = 180;
490 }
491 return $prefix.$this->makeThumbLinkObj( $img, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
492
493 } elseif ( isset($width) ) {
494
495 # Create a resized image, without the additional thumbnail
496 # features
497
498 if ( ( ! $height === false )
499 && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
500 $width = $img->getWidth() * $height / $img->getHeight();
501 }
502 if ( '' == $manual_thumb ) $url = $img->createThumb( $width );
503 }
504
505 $alt = preg_replace( '/<[^>]*>/', '', $alt );
506 $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
507 $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
508
509 $u = $nt->escapeLocalURL();
510 if ( $url == '' ) {
511 $s = wfMsg( 'missingimage', $img->getName() );
512 $s .= "<br />{$alt}<br />{$url}<br />\n";
513 } else {
514 $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
515 '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
516 }
517 if ( '' != $align ) {
518 $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
519 }
520 return str_replace("\n", ' ',$prefix.$s.$postfix);
521 }
522
523 /**
524 * Make HTML for a thumbnail including image, border and caption
525 * $img is an Image object
526 */
527 function makeThumbLinkObj( $img, $label = '', $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
528 global $wgStylePath, $wgContLang;
529 # $image = Title::makeTitleSafe( NS_IMAGE, $name );
530 $url = $img->getViewURL();
531
532 #$label = htmlspecialchars( $label );
533 $alt = preg_replace( '/<[^>]*>/', '', $label);
534 $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
535 $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
536
537 $width = $height = 0;
538 if ( $img->exists() )
539 {
540 $width = $img->getWidth();
541 $height = $img->getHeight();
542 }
543 if ( 0 == $width || 0 == $height )
544 {
545 $width = $height = 200;
546 }
547 if ( $boxwidth == 0 )
548 {
549 $boxwidth = 200;
550 }
551 if ( $framed )
552 {
553 // Use image dimensions, don't scale
554 $boxwidth = $width;
555 $oboxwidth = $boxwidth + 2;
556 $boxheight = $height;
557 $thumbUrl = $url;
558 } else {
559 $h = intval( $height/($width/$boxwidth) );
560 $oboxwidth = $boxwidth + 2;
561 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
562 {
563 $boxwidth *= $boxheight/$h;
564 } else {
565 $boxheight = $h;
566 }
567 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
568 }
569
570 if ( $manual_thumb != '' ) # Use manually specified thumbnail
571 {
572 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
573 $manual_img = Image::newFromTitle( $manual_title );
574 $thumbUrl = $manual_img->getViewURL();
575 if ( $manual_img->exists() )
576 {
577 $width = $manual_img->getWidth();
578 $height = $manual_img->getHeight();
579 $boxwidth = $width ;
580 $boxheight = $height ;
581 $oboxwidth = $boxwidth + 2 ;
582 }
583 }
584
585 $u = $img->getEscapeLocalURL();
586
587 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
588 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
589 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
590
591 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
592 if ( $thumbUrl == '' ) {
593 $s .= wfMsg( 'missingimage', $img->getName() );
594 $zoomicon = '';
595 } else {
596 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
597 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
598 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
599 'longdesc="'.$u.'" /></a>';
600 if ( $framed ) {
601 $zoomicon="";
602 } else {
603 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
604 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
605 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
606 'width="15" height="11" alt="'.$more.'" /></a></div>';
607 }
608 }
609 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
610 return str_replace("\n", ' ', $s);
611 }
612
613 function makeMediaLink( $name, $url, $alt = '' ) {
614 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
615 return $this->makeMediaLinkObj( $nt, $alt );
616 }
617
618 function makeMediaLinkObj( $nt, $alt = '', $nourl=false ) {
619 if ( ! isset( $nt ) )
620 {
621 ### HOTFIX. Instead of breaking, return empty string.
622 $s = $alt;
623 } else {
624 $name = $nt->getDBKey();
625 $img = Image::newFromTitle( $nt );
626 $url = $img->getURL();
627 # $nourl can be set by the parser
628 # this is a hack to mask absolute URLs, so the parser doesn't
629 # linkify them (it is currently not context-aware)
630 # 2004-10-25
631 if ($nourl) { $url=str_replace("http://","http-noparse://",$url) ; }
632 if ( empty( $alt ) ) {
633 $alt = preg_replace( '/\.(.+?)^/', '', $name );
634 }
635 $u = htmlspecialchars( $url );
636 $s = "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$alt}</a>";
637 }
638 return $s;
639 }
640
641 function specialLink( $name, $key = '' ) {
642 global $wgContLang;
643
644 if ( '' == $key ) { $key = strtolower( $name ); }
645 $pn = $wgContLang->ucfirst( $name );
646 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
647 wfMsg( $key ) );
648 }
649
650 function makeExternalLink( $url, $text, $escape = true ) {
651 $style = $this->getExternalLinkAttributes( $url, $text );
652 $url = htmlspecialchars( $url );
653 if( $escape ) {
654 $text = htmlspecialchars( $text );
655 }
656 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
657 }
658
659 /**
660 * This function is called by all recent changes variants, by the page history,
661 * and by the user contributions list. It is responsible for formatting edit
662 * comments. It escapes any HTML in the comment, but adds some CSS to format
663 * auto-generated comments (from section editing) and formats [[wikilinks]].
664 *
665 * The &$title parameter must be a title OBJECT. It is used to generate a
666 * direct link to the section in the autocomment.
667 * @author Erik Moeller <moeller@scireview.de>
668 *
669 * Note: there's not always a title to pass to this function.
670 * Since you can't set a default parameter for a reference, I've turned it
671 * temporarily to a value pass. Should be adjusted further. --brion
672 */
673 function formatComment($comment, $title = NULL) {
674 $fname = 'Skin::formatComment';
675 wfProfileIn( $fname );
676
677 global $wgContLang;
678 $comment = htmlspecialchars( $comment );
679
680 # The pattern for autogen comments is / * foo * /, which makes for
681 # some nasty regex.
682 # We look for all comments, match any text before and after the comment,
683 # add a separator where needed and format the comment itself with CSS
684 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
685 $pre=$match[1];
686 $auto=$match[2];
687 $post=$match[3];
688 $link='';
689 if($title) {
690 $section=$auto;
691
692 # This is hackish but should work in most cases.
693 $section=str_replace('[[','',$section);
694 $section=str_replace(']]','',$section);
695 $title->mFragment=$section;
696 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
697 }
698 $sep='-';
699 $auto=$link.$auto;
700 if($pre) { $auto = $sep.' '.$auto; }
701 if($post) { $auto .= ' '.$sep; }
702 $auto='<span class="autocomment">'.$auto.'</span>';
703 $comment=$pre.$auto.$post;
704 }
705
706 # format regular and media links - all other wiki formatting
707 # is ignored
708 $medians = $wgContLang->getNsText(Namespace::getMedia()).':';
709 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
710 # Handle link renaming [[foo|text]] will show link as "text"
711 if( "" != $match[3] ) {
712 $text = $match[3];
713 } else {
714 $text = $match[1];
715 }
716 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
717 # Media link; trail not supported.
718 $linkRegexp = '/\[\[(.*?)\]\]/';
719 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
720 } else {
721 # Other kind of link
722 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
723 $trail = $submatch[1];
724 } else {
725 $trail = "";
726 }
727 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
728 if ($match[1][0] == ':')
729 $match[1] = substr($match[1], 1);
730 $thelink = $this->makeLink( $match[1], $text, "", $trail );
731 }
732 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
733 }
734 wfProfileOut( $fname );
735 return $comment;
736 }
737
738 function tocIndent() {
739 return "\n<ul>";
740 }
741
742 function tocUnindent($level) {
743 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
744 }
745
746 /**
747 * parameter level defines if we are on an indentation level
748 */
749 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
750 return "\n<li class='toclevel-$level'><a href=\"#" . $anchor . '"><span class="tocnumber">' . $tocnumber . '</span> <span class="toctext">' . $tocline . '</span></a>';
751 }
752
753 function tocLineEnd()
754 {
755 return "</li>\n";
756 }
757
758 function tocList($toc) {
759 return "<div id='toc'>\n"
760 . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
761 . $toc
762 . "</ul>\n</div>\n"
763 . '<script type="text/javascript">'
764 . ' if (window.showTocToggle) {'
765 . ' var tocShowText = "' . addslashes( wfMsg('showtoc') ) . '";'
766 . ' var tocHideText = "' . addslashes( wfMsg('hidetoc') ) . '"; '
767 . ' showTocToggle();'
768 . ' } '
769 . '</script>'
770 . "<div class='visualClear'></div>\n";
771 }
772
773 /**
774 * These two do not check for permissions: check $wgTitle->userCanEdit
775 * before calling them
776 */
777 function editSectionScriptForOther( $title, $section, $head ) {
778 $ttl = Title::newFromText( $title );
779 $url = $ttl->escapeLocalURL( 'action=edit&section='.$section );
780 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
781 }
782
783 function editSectionScript( $nt, $section, $head ) {
784 global $wgRequest;
785 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
786 return $head;
787 }
788 $url = $nt->escapeLocalURL( 'action=edit&section='.$section );
789 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
790 }
791
792 function editSectionLinkForOther( $title, $section ) {
793 global $wgRequest;
794 global $wgContLang;
795
796 $title = Title::newFromText($title);
797 $editurl = '&section='.$section;
798 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
799
800 if( $wgContLang->isRTL() ) {
801 $farside = 'left';
802 $nearside = 'right';
803 } else {
804 $farside = 'right';
805 $nearside = 'left';
806 }
807 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
808
809 }
810
811 function editSectionLink( $nt, $section ) {
812 global $wgRequest;
813 global $wgContLang;
814
815 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
816 # Section edit links would be out of sync on an old page.
817 # But, if we're diffing to the current page, they'll be
818 # correct.
819 return '';
820 }
821
822 $editurl = '&section='.$section;
823 $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
824
825 if( $wgContLang->isRTL() ) {
826 $farside = 'left';
827 $nearside = 'right';
828 } else {
829 $farside = 'right';
830 $nearside = 'left';
831 }
832 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
833
834 }
835
836 /**
837 * @access public
838 */
839 function suppressUrlExpansion() {
840 return false;
841 }
842
843 }
844
845 ?>