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