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