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