USE INDEX -> DB::useIndexClause()
[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 = preg_replace( '/<[^>]*>/', '', $label);
555 $alt = preg_replace('/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/', '&amp;', $alt);
556 $alt = str_replace( array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $alt );
557
558 $width = $height = 0;
559 if ( $img->exists() )
560 {
561 $width = $img->getWidth();
562 $height = $img->getHeight();
563 }
564 if ( 0 == $width || 0 == $height )
565 {
566 $width = $height = 200;
567 }
568 if ( $boxwidth == 0 )
569 {
570 $boxwidth = 200;
571 }
572 if ( $framed )
573 {
574 // Use image dimensions, don't scale
575 $boxwidth = $width;
576 $oboxwidth = $boxwidth + 2;
577 $boxheight = $height;
578 $thumbUrl = $url;
579 } else {
580 $h = intval( $height/($width/$boxwidth) );
581 $oboxwidth = $boxwidth + 2;
582 if ( ( ! $boxheight === false ) && ( $h > $boxheight ) )
583 {
584 $boxwidth *= $boxheight/$h;
585 } else {
586 $boxheight = $h;
587 }
588 if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
589 }
590
591 if ( $manual_thumb != '' ) # Use manually specified thumbnail
592 {
593 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
594 $manual_img = Image::newFromTitle( $manual_title );
595 $thumbUrl = $manual_img->getViewURL();
596 if ( $manual_img->exists() )
597 {
598 $width = $manual_img->getWidth();
599 $height = $manual_img->getHeight();
600 $boxwidth = $width ;
601 $boxheight = $height ;
602 $oboxwidth = $boxwidth + 2 ;
603 }
604 }
605
606 $u = $img->getEscapeLocalURL();
607
608 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
609 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
610 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
611
612 $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
613 if ( $thumbUrl == '' ) {
614 $s .= wfMsg( 'missingimage', $img->getName() );
615 $zoomicon = '';
616 } else {
617 $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
618 '<img src="'.$thumbUrl.'" alt="'.$alt.'" ' .
619 'width="'.$boxwidth.'" height="'.$boxheight.'" ' .
620 'longdesc="'.$u.'" /></a>';
621 if ( $framed ) {
622 $zoomicon="";
623 } else {
624 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
625 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
626 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
627 'width="15" height="11" alt="'.$more.'" /></a></div>';
628 }
629 }
630 $s .= ' <div class="thumbcaption" '.$textalign.'>'.$zoomicon.$label."</div></div></div>";
631 return str_replace("\n", ' ', $s);
632 }
633
634 /** @todo document */
635 function makeMediaLink( $name, $url, $alt = '' ) {
636 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
637 return $this->makeMediaLinkObj( $nt, $alt );
638 }
639
640 /** @todo document */
641 function makeMediaLinkObj( $nt, $alt = '', $nourl=false ) {
642 if ( ! isset( $nt ) )
643 {
644 ### HOTFIX. Instead of breaking, return empty string.
645 $s = $alt;
646 } else {
647 $name = $nt->getDBKey();
648 $img = Image::newFromTitle( $nt );
649 $url = $img->getURL();
650 # $nourl can be set by the parser
651 # this is a hack to mask absolute URLs, so the parser doesn't
652 # linkify them (it is currently not context-aware)
653 # 2004-10-25
654 if ($nourl) { $url=str_replace("http://","http-noparse://",$url) ; }
655 if ( empty( $alt ) ) {
656 $alt = preg_replace( '/\.(.+?)^/', '', $name );
657 }
658 $u = htmlspecialchars( $url );
659 $s = "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$alt}</a>";
660 }
661 return $s;
662 }
663
664 /** @todo document */
665 function specialLink( $name, $key = '' ) {
666 global $wgContLang;
667
668 if ( '' == $key ) { $key = strtolower( $name ); }
669 $pn = $wgContLang->ucfirst( $name );
670 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
671 wfMsg( $key ) );
672 }
673
674 /** @todo document */
675 function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) {
676 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
677 global $wgNoFollowLinks;
678 if( $wgNoFollowLinks ) {
679 $style .= ' rel="nofollow"';
680 }
681 $url = htmlspecialchars( $url );
682 if( $escape ) {
683 $text = htmlspecialchars( $text );
684 }
685 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
686 }
687
688 /**
689 * This function is called by all recent changes variants, by the page history,
690 * and by the user contributions list. It is responsible for formatting edit
691 * comments. It escapes any HTML in the comment, but adds some CSS to format
692 * auto-generated comments (from section editing) and formats [[wikilinks]].
693 *
694 * The &$title parameter must be a title OBJECT. It is used to generate a
695 * direct link to the section in the autocomment.
696 * @author Erik Moeller <moeller@scireview.de>
697 *
698 * Note: there's not always a title to pass to this function.
699 * Since you can't set a default parameter for a reference, I've turned it
700 * temporarily to a value pass. Should be adjusted further. --brion
701 */
702 function formatComment($comment, $title = NULL) {
703 $fname = 'Skin::formatComment';
704 wfProfileIn( $fname );
705
706 global $wgContLang;
707 $comment = htmlspecialchars( $comment );
708
709 # The pattern for autogen comments is / * foo * /, which makes for
710 # some nasty regex.
711 # We look for all comments, match any text before and after the comment,
712 # add a separator where needed and format the comment itself with CSS
713 while (preg_match('/(.*)\/\*\s*(.*?)\s*\*\/(.*)/', $comment,$match)) {
714 $pre=$match[1];
715 $auto=$match[2];
716 $post=$match[3];
717 $link='';
718 if($title) {
719 $section=$auto;
720
721 # This is hackish but should work in most cases.
722 $section=str_replace('[[','',$section);
723 $section=str_replace(']]','',$section);
724 $title->mFragment=$section;
725 $link=$this->makeKnownLinkObj($title,wfMsg('sectionlink'));
726 }
727 $sep='-';
728 $auto=$link.$auto;
729 if($pre) { $auto = $sep.' '.$auto; }
730 if($post) { $auto .= ' '.$sep; }
731 $auto='<span class="autocomment">'.$auto.'</span>';
732 $comment=$pre.$auto.$post;
733 }
734
735 # format regular and media links - all other wiki formatting
736 # is ignored
737 $medians = $wgContLang->getNsText(Namespace::getMedia()).':';
738 while(preg_match('/\[\[(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
739 # Handle link renaming [[foo|text]] will show link as "text"
740 if( "" != $match[3] ) {
741 $text = $match[3];
742 } else {
743 $text = $match[1];
744 }
745 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
746 # Media link; trail not supported.
747 $linkRegexp = '/\[\[(.*?)\]\]/';
748 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
749 } else {
750 # Other kind of link
751 if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
752 $trail = $submatch[1];
753 } else {
754 $trail = "";
755 }
756 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
757 if ($match[1][0] == ':')
758 $match[1] = substr($match[1], 1);
759 $thelink = $this->makeLink( $match[1], $text, "", $trail );
760 }
761 $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
762 }
763 wfProfileOut( $fname );
764 return $comment;
765 }
766
767 /** @todo document */
768 function tocIndent() {
769 return "\n<ul>";
770 }
771
772 /** @todo document */
773 function tocUnindent($level) {
774 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
775 }
776
777 /**
778 * parameter level defines if we are on an indentation level
779 */
780 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
781 return "\n<li class='toclevel-$level'><a href=\"#" . $anchor . '"><span class="tocnumber">' . $tocnumber . '</span> <span class="toctext">' . $tocline . '</span></a>';
782 }
783
784 /** @todo document */
785 function tocLineEnd() {
786 return "</li>\n";
787 }
788
789 /** @todo document */
790 function tocList($toc) {
791 return "<div id='toc'>\n"
792 . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
793 . $toc
794 . "</ul>\n</div>\n"
795 . '<script type="text/javascript">'
796 . ' if (window.showTocToggle) {'
797 . ' var tocShowText = "' . addslashes( wfMsg('showtoc') ) . '";'
798 . ' var tocHideText = "' . addslashes( wfMsg('hidetoc') ) . '"; '
799 . ' showTocToggle();'
800 . ' } '
801 . '</script>'
802 . "<div class='visualClear'></div>\n";
803 }
804
805 /**
806 * These two do not check for permissions: check $wgTitle->userCanEdit
807 * before calling them
808 */
809 function editSectionScriptForOther( $title, $section, $head ) {
810 $ttl = Title::newFromText( $title );
811 $url = $ttl->escapeLocalURL( 'action=edit&section='.$section );
812 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
813 }
814
815 /** @todo document */
816 function editSectionScript( $nt, $section, $head ) {
817 global $wgRequest;
818 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
819 return $head;
820 }
821 $url = $nt->escapeLocalURL( 'action=edit&section='.$section );
822 return '<span oncontextmenu=\'document.location="'.$url.'";return false;\'>'.$head.'</span>';
823 }
824
825 /** @todo document */
826 function editSectionLinkForOther( $title, $section ) {
827 global $wgRequest;
828 global $wgContLang;
829
830 $title = Title::newFromText($title);
831 $editurl = '&section='.$section;
832 $url = $this->makeKnownLink($title->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);
833
834 if( $wgContLang->isRTL() ) {
835 $farside = 'left';
836 $nearside = 'right';
837 } else {
838 $farside = 'right';
839 $nearside = 'left';
840 }
841 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
842
843 }
844
845 /** @todo document */
846 function editSectionLink( $nt, $section ) {
847 global $wgRequest;
848 global $wgContLang;
849
850 if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
851 # Section edit links would be out of sync on an old page.
852 # But, if we're diffing to the current page, they'll be
853 # correct.
854 return '';
855 }
856
857 $editurl = '&section='.$section;
858 $url = $this->makeKnownLink($nt->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 ?>