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