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