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