* (bug 10828) typo in French
[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 * For the moment, Skin is a descendent class of Linker.
8 * In the future, it should probably be further split
9 * so that ever other bit of the wiki doesn't have to
10 * go loading up Skin to get at it.
11 *
12 * @addtogroup Skins
13 */
14 class Linker {
15
16 /**
17 * Flags for userToolLinks()
18 */
19 const TOOL_LINKS_NOBLOCK = 1;
20
21 function __construct() {}
22
23 /**
24 * @deprecated
25 */
26 function postParseLinkColour( $s = NULL ) {
27 return NULL;
28 }
29
30 /** @todo document */
31 function getExternalLinkAttributes( $link, $text, $class='' ) {
32 $link = htmlspecialchars( $link );
33
34 $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
35
36 $r .= " title=\"{$link}\"";
37 return $r;
38 }
39
40 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
41 global $wgContLang;
42
43 $link = urldecode( $link );
44 $link = $wgContLang->checkTitleEncoding( $link );
45 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
46 $link = htmlspecialchars( $link );
47
48 $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
49
50 $r .= " title=\"{$link}\"";
51 return $r;
52 }
53
54 /** @todo document */
55 function getInternalLinkAttributes( $link, $text, $broken = false ) {
56 $link = urldecode( $link );
57 $link = str_replace( '_', ' ', $link );
58 $link = htmlspecialchars( $link );
59
60 if( $broken == 'stub' ) {
61 $r = ' class="stub"';
62 } else if ( $broken == 'yes' ) {
63 $r = ' class="new"';
64 } else {
65 $r = '';
66 }
67
68 $r .= " title=\"{$link}\"";
69 return $r;
70 }
71
72 /**
73 * @param $nt Title object.
74 * @param $text String: FIXME
75 * @param $broken Boolean: FIXME, default 'false'.
76 */
77 function getInternalLinkAttributesObj( &$nt, $text, $broken = false ) {
78 if( $broken == 'stub' ) {
79 $r = ' class="stub"';
80 } else if ( $broken == 'yes' ) {
81 $r = ' class="new"';
82 } else {
83 $r = '';
84 }
85
86 $r .= ' title="' . $nt->getEscapedText() . '"';
87 return $r;
88 }
89
90 /**
91 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
92 * it if you already have a title object handy. See makeLinkObj for further documentation.
93 *
94 * @param $title String: the text of the title
95 * @param $text String: link text
96 * @param $query String: optional query part
97 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
98 * be included in the link text. Other characters will be appended after
99 * the end of the link.
100 */
101 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
102 wfProfileIn( 'Linker::makeLink' );
103 $nt = Title::newFromText( $title );
104 if ($nt) {
105 $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
106 } else {
107 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
108 $result = $text == "" ? $title : $text;
109 }
110
111 wfProfileOut( 'Linker::makeLink' );
112 return $result;
113 }
114
115 /**
116 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
117 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
118 *
119 * @param $title String: the text of the title
120 * @param $text String: link text
121 * @param $query String: optional query part
122 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
123 * be included in the link text. Other characters will be appended after
124 * the end of the link.
125 */
126 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
127 $nt = Title::newFromText( $title );
128 if ($nt) {
129 return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
130 } else {
131 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
132 return $text == '' ? $title : $text;
133 }
134 }
135
136 /**
137 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
138 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
139 *
140 * @param string $title The text of the title
141 * @param string $text Link text
142 * @param string $query Optional query part
143 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
144 * be included in the link text. Other characters will be appended after
145 * the end of the link.
146 */
147 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
148 $nt = Title::newFromText( $title );
149 if ($nt) {
150 return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
151 } else {
152 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
153 return $text == '' ? $title : $text;
154 }
155 }
156
157 /**
158 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
159 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
160 *
161 * @param $title String: the text of the title
162 * @param $text String: link text
163 * @param $query String: optional query part
164 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
165 * be included in the link text. Other characters will be appended after
166 * the end of the link.
167 */
168 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
169 $nt = Title::newFromText( $title );
170 if ($nt) {
171 return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
172 } else {
173 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
174 return $text == '' ? $title : $text;
175 }
176 }
177
178 /**
179 * Make a link for a title which may or may not be in the database. If you need to
180 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
181 * call to this will result in a DB query.
182 *
183 * @param $nt Title: the title object to make the link from, e.g. from
184 * Title::newFromText.
185 * @param $text String: link text
186 * @param $query String: optional query part
187 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
188 * be included in the link text. Other characters will be appended after
189 * the end of the link.
190 * @param $prefix String: optional prefix. As trail, only before instead of after.
191 */
192 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
193 global $wgUser;
194 $fname = 'Linker::makeLinkObj';
195 wfProfileIn( $fname );
196
197 # Fail gracefully
198 if ( ! is_object($nt) ) {
199 # throw new MWException();
200 wfProfileOut( $fname );
201 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
202 }
203
204 if ( $nt->isExternal() ) {
205 $u = $nt->getFullURL();
206 $link = $nt->getPrefixedURL();
207 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
208 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
209
210 $inside = '';
211 if ( '' != $trail ) {
212 $m = array();
213 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
214 $inside = $m[1];
215 $trail = $m[2];
216 }
217 }
218 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
219
220 wfProfileOut( $fname );
221 return $t;
222 } elseif ( $nt->isAlwaysKnown() ) {
223 # Image links, special page links and self-links with fragements are always known.
224 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
225 } else {
226 wfProfileIn( $fname.'-immediate' );
227
228 # Handles links to special pages wich do not exist in the database:
229 if( $nt->getNamespace() == NS_SPECIAL ) {
230 if( SpecialPage::exists( $nt->getDbKey() ) ) {
231 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
232 } else {
233 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
234 }
235 wfProfileOut( $fname.'-immediate' );
236 wfProfileOut( $fname );
237 return $retVal;
238 }
239
240 # Work out link colour immediately
241 $aid = $nt->getArticleID() ;
242 if ( 0 == $aid ) {
243 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
244 } else {
245 $stub = false;
246 if ( $nt->isContentPage() ) {
247 $threshold = $wgUser->getOption('stubthreshold');
248 if ( $threshold > 0 ) {
249 $dbr = wfGetDB( DB_SLAVE );
250 $s = $dbr->selectRow(
251 array( 'page' ),
252 array( 'page_len',
253 'page_is_redirect' ),
254 array( 'page_id' => $aid ), $fname ) ;
255 $stub = ( $s !== false && !$s->page_is_redirect &&
256 $s->page_len < $threshold );
257 }
258 }
259 if ( $stub ) {
260 $retVal = $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
261 } else {
262 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
263 }
264 }
265 wfProfileOut( $fname.'-immediate' );
266 }
267 wfProfileOut( $fname );
268 return $retVal;
269 }
270
271 /**
272 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
273 * it doesn't have to do a database query. It's also valid for interwiki titles and special
274 * pages.
275 *
276 * @param $nt Title object of target page
277 * @param $text String: text to replace the title
278 * @param $query String: link target
279 * @param $trail String: text after link
280 * @param $prefix String: text before link text
281 * @param $aprops String: extra attributes to the a-element
282 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
283 * @return the a-element
284 */
285 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
286
287 $fname = 'Linker::makeKnownLinkObj';
288 wfProfileIn( $fname );
289
290 if ( !is_object( $nt ) ) {
291 wfProfileOut( $fname );
292 return $text;
293 }
294
295 $u = $nt->escapeLocalURL( $query );
296 if ( $nt->getFragment() != '' ) {
297 if( $nt->getPrefixedDbkey() == '' ) {
298 $u = '';
299 if ( '' == $text ) {
300 $text = htmlspecialchars( $nt->getFragment() );
301 }
302 }
303 $u .= $nt->getFragmentForURL();
304 }
305 if ( $text == '' ) {
306 $text = htmlspecialchars( $nt->getPrefixedText() );
307 }
308 if ( $style == '' ) {
309 $style = $this->getInternalLinkAttributesObj( $nt, $text );
310 }
311
312 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
313
314 list( $inside, $trail ) = Linker::splitTrail( $trail );
315 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
316 wfProfileOut( $fname );
317 return $r;
318 }
319
320 /**
321 * Make a red link to the edit page of a given title.
322 *
323 * @param $title String: The text of the title
324 * @param $text String: Link text
325 * @param $query String: Optional query part
326 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
327 * be included in the link text. Other characters will be appended after
328 * the end of the link.
329 */
330 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
331 # Fail gracefully
332 if ( ! isset($nt) ) {
333 # throw new MWException();
334 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
335 }
336
337 $fname = 'Linker::makeBrokenLinkObj';
338 wfProfileIn( $fname );
339
340 if( $nt->getNamespace() == NS_SPECIAL ) {
341 $q = $query;
342 } else if ( '' == $query ) {
343 $q = 'action=edit';
344 } else {
345 $q = 'action=edit&'.$query;
346 }
347 $u = $nt->escapeLocalURL( $q );
348
349 if ( '' == $text ) {
350 $text = htmlspecialchars( $nt->getPrefixedText() );
351 }
352 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
353
354 list( $inside, $trail ) = Linker::splitTrail( $trail );
355 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
356
357 wfProfileOut( $fname );
358 return $s;
359 }
360
361 /**
362 * Make a brown link to a short article.
363 *
364 * @param $title String: the text of the title
365 * @param $text String: link text
366 * @param $query String: optional query part
367 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
368 * be included in the link text. Other characters will be appended after
369 * the end of the link.
370 */
371 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
372 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
373 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
374 }
375
376 /**
377 * Generate either a normal exists-style link or a stub link, depending
378 * on the given page size.
379 *
380 * @param $size Integer
381 * @param $nt Title object.
382 * @param $text String
383 * @param $query String
384 * @param $trail String
385 * @param $prefix String
386 * @return string HTML of link
387 */
388 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
389 global $wgUser;
390 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
391 if( $size < $threshold ) {
392 return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
393 } else {
394 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
395 }
396 }
397
398 /**
399 * Make appropriate markup for a link to the current article. This is currently rendered
400 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
401 * despite $query not being used.
402 */
403 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
404 if ( '' == $text ) {
405 $text = htmlspecialchars( $nt->getPrefixedText() );
406 }
407 list( $inside, $trail ) = Linker::splitTrail( $trail );
408 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
409 }
410
411 /** @todo document */
412 function fnamePart( $url ) {
413 $basename = strrchr( $url, '/' );
414 if ( false === $basename ) {
415 $basename = $url;
416 } else {
417 $basename = substr( $basename, 1 );
418 }
419 return htmlspecialchars( $basename );
420 }
421
422 /** Obsolete alias */
423 function makeImage( $url, $alt = '' ) {
424 return $this->makeExternalImage( $url, $alt );
425 }
426
427 /** @todo document */
428 function makeExternalImage( $url, $alt = '' ) {
429 if ( '' == $alt ) {
430 $alt = $this->fnamePart( $url );
431 }
432 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
433 return $s;
434 }
435
436 /** Creates the HTML source for images
437 * @param object $nt
438 * @param string $label label text
439 * @param string $alt alt text
440 * @param string $align horizontal alignment: none, left, center, right)
441 * @param array $params some format keywords: width, height, page, upright, upright_factor, frameless, border
442 * @param boolean $framed shows image in original size in a frame
443 * @param boolean $thumb shows image as thumbnail in a frame
444 * @param string $manual_thumb image name for the manual thumbnail
445 * @param string $valign vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
446 * @return string
447 */
448 function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
449 $thumb = false, $manual_thumb = '', $valign = '', $time = false )
450 {
451 global $wgContLang, $wgUser, $wgThumbLimits, $wgThumbUpright;
452
453 $img = wfFindFile( $nt, $time );
454
455 if ( $img && !$img->allowInlineDisplay() ) {
456 wfDebug( __METHOD__.': '.$nt->getPrefixedDBkey()." does not allow inline display\n" );
457 return $this->makeKnownLinkObj( $nt );
458 }
459
460 $error = $prefix = $postfix = '';
461 $page = isset( $params['page'] ) ? $params['page'] : false;
462
463 if ( 'center' == $align )
464 {
465 $prefix = '<div class="center">';
466 $postfix = '</div>';
467 $align = 'none';
468 }
469 if ( $img && !isset( $params['width'] ) ) {
470 $params['width'] = $img->getWidth( $page );
471 if( $thumb || $framed || isset( $params['frameless'] ) ) {
472 $wopt = $wgUser->getOption( 'thumbsize' );
473
474 if( !isset( $wgThumbLimits[$wopt] ) ) {
475 $wopt = User::getDefaultOption( 'thumbsize' );
476 }
477
478 // Reduce width for upright images when parameter 'upright' is used
479 if ( !isset( $params['upright_factor'] ) || $params['upright_factor'] == 0 ) {
480 $params['upright_factor'] = $wgThumbUpright;
481 }
482 // Use width which is smaller: real image width or user preference width
483 // For caching health: If width scaled down due to upright parameter, round to full __0 pixel to avoid the creation of a lot of odd thumbs
484 $params['width'] = min( $params['width'], isset( $params['upright'] ) ? round( $wgThumbLimits[$wopt] * $params['upright_factor'], -1 ) : $wgThumbLimits[$wopt] );
485 }
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 # to the default user option as specified in Language*.php
497 if ( $align == '' ) {
498 $align = $wgContLang->isRTL() ? 'left' : 'right';
499 }
500 return $prefix.$this->makeThumbLinkObj( $nt, $img, $label, $alt, $align, $params, $framed, $manual_thumb ).$postfix;
501 }
502
503 if ( $img && $params['width'] ) {
504 # Create a resized image, without the additional thumbnail features
505 $thumb = $img->transform( $params );
506 } else {
507 $thumb = false;
508 }
509
510 if ( $page ) {
511 $query = 'page=' . urlencode( $page );
512 } else {
513 $query = '';
514 }
515 $u = $nt->getLocalURL( $query );
516 $imgAttribs = array(
517 'alt' => $alt,
518 'longdesc' => $u
519 );
520
521 if ( $valign ) {
522 $imgAttribs['style'] = "vertical-align: $valign";
523 }
524 if ( isset( $params['border'] ) ) {
525 $imgAttribs['class'] = "thumbborder";
526 }
527 $linkAttribs = array(
528 'href' => $u,
529 'class' => 'image',
530 'title' => $alt
531 );
532
533 if ( !$thumb ) {
534 $s = $this->makeBrokenImageLinkObj( $nt );
535 } else {
536 $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
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 * @param Title $nt
547 * @param Image $img Image object or false if it doesn't exist
548 */
549 function makeThumbLinkObj( Title $nt, $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "" ) {
550 global $wgStylePath, $wgContLang;
551 $exists = $img && $img->exists();
552
553 $page = isset( $params['page'] ) ? $params['page'] : false;
554
555 if ( empty( $params['width'] ) ) {
556 // Reduce width for upright images when parameter 'upright' is used
557 $params['width'] = isset( $params['upright'] ) ? 130 : 180;
558 }
559 $thumb = false;
560
561 if ( !$exists ) {
562 $outerWidth = $params['width'] + 2;
563 } else {
564 if ( $manual_thumb != '' ) {
565 # Use manually specified thumbnail
566 $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb );
567 if( $manual_title ) {
568 $manual_img = wfFindFile( $manual_title );
569 if ( $manual_img ) {
570 $thumb = $manual_img->getUnscaledThumb();
571 } else {
572 $exists = false;
573 }
574 }
575 } elseif ( $framed ) {
576 // Use image dimensions, don't scale
577 $thumb = $img->getUnscaledThumb( $page );
578 } else {
579 # Do not present an image bigger than the source, for bitmap-style images
580 # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
581 $srcWidth = $img->getWidth( $page );
582 if ( $srcWidth && !$img->mustRender() && $params['width'] > $srcWidth ) {
583 $params['width'] = $srcWidth;
584 }
585 $thumb = $img->transform( $params );
586 }
587
588 if ( $thumb ) {
589 $outerWidth = $thumb->getWidth() + 2;
590 } else {
591 $outerWidth = $params['width'] + 2;
592 }
593 }
594
595 $query = $page ? 'page=' . urlencode( $page ) : '';
596 $u = $nt->getLocalURL( $query );
597
598 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
599 $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
600 $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
601
602 $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
603 if( !$exists ) {
604 $s .= $this->makeBrokenImageLinkObj( $nt );
605 $zoomicon = '';
606 } elseif ( !$thumb ) {
607 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
608 $zoomicon = '';
609 } else {
610 $imgAttribs = array(
611 'alt' => $alt,
612 'longdesc' => $u,
613 'class' => 'thumbimage'
614 );
615 $linkAttribs = array(
616 'href' => $u,
617 'class' => 'internal',
618 'title' => $alt
619 );
620
621 $s .= $thumb->toHtml( $imgAttribs, $linkAttribs );
622 if ( $framed ) {
623 $zoomicon="";
624 } else {
625 $zoomicon = '<div class="magnify" style="float:'.$magnifyalign.'">'.
626 '<a href="'.$u.'" class="internal" title="'.$more.'">'.
627 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
628 'width="15" height="11" alt="" /></a></div>';
629 }
630 }
631 $s .= ' <div class="thumbcaption"'.$textalign.'>'.$zoomicon.$label."</div></div></div>";
632 return str_replace("\n", ' ', $s);
633 }
634
635 /**
636 * Make a "broken" link to an image
637 *
638 * @param Title $title Image title
639 * @param string $text Link label
640 * @param string $query Query string
641 * @param string $trail Link trail
642 * @param string $prefix Link prefix
643 * @return string
644 */
645 public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
646 global $wgEnableUploads;
647 if( $title instanceof Title ) {
648 wfProfileIn( __METHOD__ );
649 if( $wgEnableUploads ) {
650 $upload = SpecialPage::getTitleFor( 'Upload' );
651 if( $text == '' )
652 $text = htmlspecialchars( $title->getPrefixedText() );
653 $q = 'wpDestFile=' . $title->getPartialUrl();
654 if( $query != '' )
655 $q .= '&' . $query;
656 list( $inside, $trail ) = self::splitTrail( $trail );
657 $style = $this->getInternalLinkAttributesObj( $title, $text, 'yes' );
658 wfProfileOut( __METHOD__ );
659 return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
660 . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
661 } else {
662 wfProfileOut( __METHOD__ );
663 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
664 }
665 } else {
666 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
667 }
668 }
669
670 /** @deprecated use Linker::makeMediaLinkObj() */
671 function makeMediaLink( $name, $unused = '', $text = '' ) {
672 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
673 return $this->makeMediaLinkObj( $nt, $text );
674 }
675
676 /**
677 * Create a direct link to a given uploaded file.
678 *
679 * @param $title Title object.
680 * @param $text String: pre-sanitized HTML
681 * @return string HTML
682 *
683 * @public
684 * @todo Handle invalid or missing images better.
685 */
686 function makeMediaLinkObj( $title, $text = '' ) {
687 if( is_null( $title ) ) {
688 ### HOTFIX. Instead of breaking, return empty string.
689 return $text;
690 } else {
691 $img = wfFindFile( $title );
692 if( $img ) {
693 $url = $img->getURL();
694 $class = 'internal';
695 } else {
696 $upload = SpecialPage::getTitleFor( 'Upload' );
697 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDbKey() ) );
698 $class = 'new';
699 }
700 $alt = htmlspecialchars( $title->getText() );
701 if( $text == '' ) {
702 $text = $alt;
703 }
704 $u = htmlspecialchars( $url );
705 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
706 }
707 }
708
709 /** @todo document */
710 function specialLink( $name, $key = '' ) {
711 global $wgContLang;
712
713 if ( '' == $key ) { $key = strtolower( $name ); }
714 $pn = $wgContLang->ucfirst( $name );
715 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
716 wfMsg( $key ) );
717 }
718
719 /** @todo document */
720 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
721 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
722 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
723 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
724 $style .= ' rel="nofollow"';
725 }
726 $url = htmlspecialchars( $url );
727 if( $escape ) {
728 $text = htmlspecialchars( $text );
729 }
730 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
731 }
732
733 /**
734 * Make user link (or user contributions for unregistered users)
735 * @param $userId Integer: user id in database.
736 * @param $userText String: user name in database
737 * @return string HTML fragment
738 * @private
739 */
740 function userLink( $userId, $userText ) {
741 $encName = htmlspecialchars( $userText );
742 if( $userId == 0 ) {
743 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
744 return $this->makeKnownLinkObj( $contribsPage,
745 $encName);
746 } else {
747 $userPage = Title::makeTitle( NS_USER, $userText );
748 return $this->makeLinkObj( $userPage, $encName );
749 }
750 }
751
752 /**
753 * Generate standard user tool links (talk, contributions, block link, etc.)
754 *
755 * @param int $userId User identifier
756 * @param string $userText User name or IP address
757 * @param bool $redContribsWhenNoEdits Should the contributions link be red if the user has no edits?
758 * @param int $flags Customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
759 * @return string
760 */
761 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0 ) {
762 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
763 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
764 $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
765
766 $items = array();
767 if( $talkable ) {
768 $items[] = $this->userTalkLink( $userId, $userText );
769 }
770 if( $userId ) {
771 // check if the user has an edit
772 if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
773 $style = " class='new'";
774 } else {
775 $style = '';
776 }
777 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
778
779 $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
780 }
781 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
782 $items[] = $this->blockLink( $userId, $userText );
783 }
784
785 if( $items ) {
786 return ' (' . implode( ' | ', $items ) . ')';
787 } else {
788 return '';
789 }
790 }
791
792 /**
793 * Alias for userToolLinks( $userId, $userText, true );
794 */
795 public function userToolLinksRedContribs( $userId, $userText ) {
796 return $this->userToolLinks( $userId, $userText, true );
797 }
798
799
800 /**
801 * @param $userId Integer: user id in database.
802 * @param $userText String: user name in database.
803 * @return string HTML fragment with user talk link
804 * @private
805 */
806 function userTalkLink( $userId, $userText ) {
807 $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
808 $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
809 return $userTalkLink;
810 }
811
812 /**
813 * @param $userId Integer: userid
814 * @param $userText String: user name in database.
815 * @return string HTML fragment with block link
816 * @private
817 */
818 function blockLink( $userId, $userText ) {
819 $blockPage = SpecialPage::getTitleFor( 'Blockip', $userText );
820 $blockLink = $this->makeKnownLinkObj( $blockPage,
821 wfMsgHtml( 'blocklink' ) );
822 return $blockLink;
823 }
824
825 /**
826 * Generate a user link if the current user is allowed to view it
827 * @param $rev Revision object.
828 * @return string HTML
829 */
830 function revUserLink( $rev ) {
831 if( $rev->userCan( Revision::DELETED_USER ) ) {
832 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
833 } else {
834 $link = wfMsgHtml( 'rev-deleted-user' );
835 }
836 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
837 return '<span class="history-deleted">' . $link . '</span>';
838 }
839 return $link;
840 }
841
842 /**
843 * Generate a user tool link cluster if the current user is allowed to view it
844 * @param $rev Revision object.
845 * @return string HTML
846 */
847 function revUserTools( $rev ) {
848 if( $rev->userCan( Revision::DELETED_USER ) ) {
849 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
850 ' ' .
851 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
852 } else {
853 $link = wfMsgHtml( 'rev-deleted-user' );
854 }
855 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
856 return '<span class="history-deleted">' . $link . '</span>';
857 }
858 return $link;
859 }
860
861 /**
862 * This function is called by all recent changes variants, by the page history,
863 * and by the user contributions list. It is responsible for formatting edit
864 * comments. It escapes any HTML in the comment, but adds some CSS to format
865 * auto-generated comments (from section editing) and formats [[wikilinks]].
866 *
867 * @author Erik Moeller <moeller@scireview.de>
868 *
869 * Note: there's not always a title to pass to this function.
870 * Since you can't set a default parameter for a reference, I've turned it
871 * temporarily to a value pass. Should be adjusted further. --brion
872 *
873 * @param string $comment
874 * @param mixed $title Title object (to generate link to the section in autocomment) or null
875 * @param bool $local Whether section links should refer to local page
876 */
877 function formatComment($comment, $title = NULL, $local = false) {
878 wfProfileIn( __METHOD__ );
879
880 # Sanitize text a bit:
881 $comment = str_replace( "\n", " ", $comment );
882 $comment = htmlspecialchars( $comment );
883
884 # Render autocomments and make links:
885 $comment = $this->formatAutoComments( $comment, $title, $local );
886 $comment = $this->formatLinksInComment( $comment );
887
888 wfProfileOut( __METHOD__ );
889 return $comment;
890 }
891
892 /**
893 * The pattern for autogen comments is / * foo * /, which makes for
894 * some nasty regex.
895 * We look for all comments, match any text before and after the comment,
896 * add a separator where needed and format the comment itself with CSS
897 * Called by Linker::formatComment.
898 *
899 * @param $comment Comment text
900 * @param $title An optional title object used to links to sections
901 *
902 * @todo Document the $local parameter.
903 */
904 private function formatAutocomments( $comment, $title = NULL, $local = false ) {
905 $match = array();
906 while (preg_match('!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment,$match)) {
907 $pre=$match[1];
908 $auto=$match[2];
909 $post=$match[3];
910 $link='';
911 if( $title ) {
912 $section = $auto;
913
914 # Generate a valid anchor name from the section title.
915 # Hackish, but should generally work - we strip wiki
916 # syntax, including the magic [[: that is used to
917 # "link rather than show" in case of images and
918 # interlanguage links.
919 $section = str_replace( '[[:', '', $section );
920 $section = str_replace( '[[', '', $section );
921 $section = str_replace( ']]', '', $section );
922 if ( $local ) {
923 $sectionTitle = Title::newFromText( '#' . $section);
924 } else {
925 $sectionTitle = wfClone( $title );
926 $sectionTitle->mFragment = $section;
927 }
928 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
929 }
930 $sep='-';
931 $auto=$link.$auto;
932 if($pre) { $auto = $sep.' '.$auto; }
933 if($post) { $auto .= ' '.$sep; }
934 $auto='<span class="autocomment">'.$auto.'</span>';
935 $comment=$pre.$auto.$post;
936 }
937
938 return $comment;
939 }
940
941 /**
942 * Formats wiki links and media links in text; all other wiki formatting
943 * is ignored
944 *
945 * @param string $comment Text to format links in
946 * @return string
947 */
948 public function formatLinksInComment( $comment ) {
949 global $wgContLang;
950
951 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
952 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
953
954 while(preg_match('/\[\[:?(.*?)(\|(.*?))*\]\](.*)$/',$comment,$match)) {
955 # Handle link renaming [[foo|text]] will show link as "text"
956 if( "" != $match[3] ) {
957 $text = $match[3];
958 } else {
959 $text = $match[1];
960 }
961 $submatch = array();
962 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
963 # Media link; trail not supported.
964 $linkRegexp = '/\[\[(.*?)\]\]/';
965 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
966 } else {
967 # Other kind of link
968 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
969 $trail = $submatch[1];
970 } else {
971 $trail = "";
972 }
973 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
974 if (isset($match[1][0]) && $match[1][0] == ':')
975 $match[1] = substr($match[1], 1);
976 $thelink = $this->makeLink( $match[1], $text, "", $trail );
977 }
978 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
979 }
980
981 return $comment;
982 }
983
984 /**
985 * Wrap a comment in standard punctuation and formatting if
986 * it's non-empty, otherwise return empty string.
987 *
988 * @param string $comment
989 * @param mixed $title Title object (to generate link to section in autocomment) or null
990 * @param bool $local Whether section links should refer to local page
991 *
992 * @return string
993 */
994 function commentBlock( $comment, $title = NULL, $local = false ) {
995 // '*' used to be the comment inserted by the software way back
996 // in antiquity in case none was provided, here for backwards
997 // compatability, acc. to brion -ævar
998 if( $comment == '' || $comment == '*' ) {
999 return '';
1000 } else {
1001 $formatted = $this->formatComment( $comment, $title, $local );
1002 return " <span class=\"comment\">($formatted)</span>";
1003 }
1004 }
1005
1006 /**
1007 * Wrap and format the given revision's comment block, if the current
1008 * user is allowed to view it.
1009 *
1010 * @param Revision $rev
1011 * @param bool $local Whether section links should refer to local page
1012 * @return string HTML
1013 */
1014 function revComment( Revision $rev, $local = false ) {
1015 if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
1016 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
1017 } else {
1018 $block = " <span class=\"comment\">" .
1019 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1020 }
1021 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
1022 return " <span class=\"history-deleted\">$block</span>";
1023 }
1024 return $block;
1025 }
1026
1027 /** @todo document */
1028 function tocIndent() {
1029 return "\n<ul>";
1030 }
1031
1032 /** @todo document */
1033 function tocUnindent($level) {
1034 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
1035 }
1036
1037 /**
1038 * parameter level defines if we are on an indentation level
1039 */
1040 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1041 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1042 $anchor . '"><span class="tocnumber">' .
1043 $tocnumber . '</span> <span class="toctext">' .
1044 $tocline . '</span></a>';
1045 }
1046
1047 /** @todo document */
1048 function tocLineEnd() {
1049 return "</li>\n";
1050 }
1051
1052 /** @todo document */
1053 function tocList($toc) {
1054 global $wgJsMimeType;
1055 $title = wfMsgHtml('toc') ;
1056 return
1057 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1058 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1059 . $toc
1060 # no trailing newline, script should not be wrapped in a
1061 # paragraph
1062 . "</ul>\n</td></tr></table>"
1063 . '<script type="' . $wgJsMimeType . '">'
1064 . ' if (window.showTocToggle) {'
1065 . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
1066 . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
1067 . ' showTocToggle();'
1068 . ' } '
1069 . "</script>\n";
1070 }
1071
1072 /**
1073 * Used to generate section edit links that point to "other" pages
1074 * (sections that are really part of included pages).
1075 *
1076 * @param $title Title string.
1077 * @param $section Integer: section number.
1078 */
1079 public function editSectionLinkForOther( $title, $section ) {
1080 $title = Title::newFromText( $title );
1081 return $this->doEditSectionLink( $title, $section, '', 'EditSectionLinkForOther' );
1082 }
1083
1084 /**
1085 * @param $nt Title object.
1086 * @param $section Integer: section number.
1087 * @param $hint Link String: title, or default if omitted or empty
1088 */
1089 public function editSectionLink( Title $nt, $section, $hint='' ) {
1090 if( $hint != '' ) {
1091 $hint = wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) );
1092 $hint = " title=\"$hint\"";
1093 }
1094 return $this->doEditSectionLink( $nt, $section, $hint, 'EditSectionLink' );
1095 }
1096
1097 /**
1098 * Implement editSectionLink and editSectionLinkForOther.
1099 *
1100 * @param $nt Title object
1101 * @param $section Integer, section number
1102 * @param $hint String, for HTML title attribute
1103 * @param $hook String, name of hook to run
1104 * @return String, HTML to use for edit link
1105 */
1106 protected function doEditSectionLink( Title $nt, $section, $hint, $hook ) {
1107 global $wgContLang;
1108 $editurl = '&section='.$section;
1109 $url = $this->makeKnownLinkObj(
1110 $nt,
1111 wfMsg('editsection'),
1112 'action=edit'.$editurl,
1113 '', '', '', $hint
1114 );
1115 $result = null;
1116
1117 // The two hooks have slightly different interfaces . . .
1118 if( $hook == 'EditSectionLink' ) {
1119 wfRunHooks( $hook, array( &$this, $nt, $section, $hint, $url, &$result ) );
1120 } elseif( $hook == 'EditSectionLinkForOther' ) {
1121 wfRunHooks( $hook, array( &$this, $nt, $section, $url, &$result ) );
1122 }
1123
1124 // For reverse compatibility, add the brackets *after* the hook is run,
1125 // and even add them to hook-provided text.
1126 if( is_null( $result ) ) {
1127 $result = wfMsg( 'editsection-brackets', $url );
1128 } else {
1129 $result = wfMsg( 'editsection-brackets', $result );
1130 }
1131 return "<span class=\"editsection\">$result</span>";
1132 }
1133
1134 /**
1135 * Create a headline for content
1136 *
1137 * @param int $level The level of the headline (1-6)
1138 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1139 * This *must* be at least '>' for no attribs
1140 * @param string $anchor The anchor to give the headline (the bit after the #)
1141 * @param string $text The text of the header
1142 * @param string $link HTML to add for the section edit link
1143 *
1144 * @return string HTML headline
1145 */
1146 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1147 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1148 }
1149
1150 /**
1151 * Split a link trail, return the "inside" portion and the remainder of the trail
1152 * as a two-element array
1153 *
1154 * @static
1155 */
1156 static function splitTrail( $trail ) {
1157 static $regex = false;
1158 if ( $regex === false ) {
1159 global $wgContLang;
1160 $regex = $wgContLang->linkTrail();
1161 }
1162 $inside = '';
1163 if ( '' != $trail ) {
1164 $m = array();
1165 if ( preg_match( $regex, $trail, $m ) ) {
1166 $inside = $m[1];
1167 $trail = $m[2];
1168 }
1169 }
1170 return array( $inside, $trail );
1171 }
1172
1173 /**
1174 * Generate a rollback link for a given revision. Currently it's the
1175 * caller's responsibility to ensure that the revision is the top one. If
1176 * it's not, of course, the user will get an error message.
1177 *
1178 * If the calling page is called with the parameter &bot=1, all rollback
1179 * links also get that parameter. It causes the edit itself and the rollback
1180 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1181 * changes, so this allows sysops to combat a busy vandal without bothering
1182 * other users.
1183 *
1184 * @param Revision $rev
1185 */
1186 function generateRollback( $rev ) {
1187 return '<span class="mw-rollback-link">['
1188 . $this->buildRollbackLink( $rev )
1189 . ']</span>';
1190 }
1191
1192 /**
1193 * Build a raw rollback link, useful for collections of "tool" links
1194 *
1195 * @param Revision $rev
1196 * @return string
1197 */
1198 public function buildRollbackLink( $rev ) {
1199 global $wgRequest, $wgUser;
1200 $title = $rev->getTitle();
1201 $extra = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
1202 $extra .= '&token=' . urlencode( $wgUser->editToken( array( $title->getPrefixedText(),
1203 $rev->getUserText() ) ) );
1204 return $this->makeKnownLinkObj(
1205 $title,
1206 wfMsgHtml( 'rollbacklink' ),
1207 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extra
1208 );
1209 }
1210
1211 /**
1212 * Returns HTML for the "templates used on this page" list.
1213 *
1214 * @param array $templates Array of templates from Article::getUsedTemplate
1215 * or similar
1216 * @param bool $preview Whether this is for a preview
1217 * @param bool $section Whether this is for a section edit
1218 * @return string HTML output
1219 */
1220 public function formatTemplates( $templates, $preview = false, $section = false) {
1221 global $wgUser;
1222 wfProfileIn( __METHOD__ );
1223
1224 $sk = $wgUser->getSkin();
1225
1226 $outText = '';
1227 if ( count( $templates ) > 0 ) {
1228 # Do a batch existence check
1229 $batch = new LinkBatch;
1230 foreach( $templates as $title ) {
1231 $batch->addObj( $title );
1232 }
1233 $batch->execute();
1234
1235 # Construct the HTML
1236 $outText = '<div class="mw-templatesUsedExplanation">';
1237 if ( $preview ) {
1238 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1239 } elseif ( $section ) {
1240 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1241 } else {
1242 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1243 }
1244 $outText .= '</div><ul>';
1245
1246 foreach ( $templates as $titleObj ) {
1247 $r = $titleObj->getRestrictions( 'edit' );
1248 if ( in_array( 'sysop', $r ) ) {
1249 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1250 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1251 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1252 } else {
1253 $protected = '';
1254 }
1255 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1256 }
1257 $outText .= '</ul>';
1258 }
1259 wfProfileOut( __METHOD__ );
1260 return $outText;
1261 }
1262
1263 /**
1264 * Format a size in bytes for output, using an appropriate
1265 * unit (B, KB, MB or GB) according to the magnitude in question
1266 *
1267 * @param $size Size to format
1268 * @return string
1269 */
1270 public function formatSize( $size ) {
1271 global $wgLang;
1272 // For small sizes no decimal places necessary
1273 $round = 0;
1274 if( $size > 1024 ) {
1275 $size = $size / 1024;
1276 if( $size > 1024 ) {
1277 $size = $size / 1024;
1278 // For MB and bigger two decimal places are smarter
1279 $round = 2;
1280 if( $size > 1024 ) {
1281 $size = $size / 1024;
1282 $msg = 'size-gigabytes';
1283 } else {
1284 $msg = 'size-megabytes';
1285 }
1286 } else {
1287 $msg = 'size-kilobytes';
1288 }
1289 } else {
1290 $msg = 'size-bytes';
1291 }
1292 $size = round( $size, $round );
1293 return wfMsgHtml( $msg, $wgLang->formatNum( $size ) );
1294 }
1295
1296 /**
1297 * Given the id of an interface element, constructs the appropriate title
1298 * and accesskey attributes from the system messages. (Note, this is usu-
1299 * ally the id but isn't always, because sometimes the accesskey needs to
1300 * go on a different element than the id, for reverse-compatibility, etc.)
1301 *
1302 * @param string $name Id of the element, minus prefixes.
1303 * @return string title and accesskey attributes, ready to drop in an
1304 * element (e.g., ' title="This does something [x]" accesskey="x"').
1305 */
1306 public function tooltipAndAccesskey($name) {
1307 $out = '';
1308
1309 $tooltip = wfMsg('tooltip-'.$name);
1310 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1311 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1312 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1313 $out .= ' title="'.htmlspecialchars($tooltip);
1314 }
1315 $accesskey = wfMsg('accesskey-'.$name);
1316 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1317 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1318 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1319 } elseif ($out) {
1320 $out .= '"';
1321 }
1322 return $out;
1323 }
1324
1325 /**
1326 * Given the id of an interface element, constructs the appropriate title
1327 * attribute from the system messages. (Note, this is usually the id but
1328 * isn't always, because sometimes the accesskey needs to go on a different
1329 * element than the id, for reverse-compatibility, etc.)
1330 *
1331 * @param string $name Id of the element, minus prefixes.
1332 * @return string title attribute, ready to drop in an element
1333 * (e.g., ' title="This does something"').
1334 */
1335 public function tooltip($name) {
1336 $out = '';
1337
1338 $tooltip = wfMsg('tooltip-'.$name);
1339 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1340 $out = ' title="'.htmlspecialchars($tooltip).'"';
1341 }
1342
1343 return $out;
1344 }
1345 }
1346
1347
1348