Revert r29671, it was based on a misunderstanding of the purpose of the LoggedOut...
[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 $q = 'wpDestFile=' . $title->getPartialUrl();
727 if( $query != '' )
728 $q .= '&' . $query;
729 list( $inside, $trail ) = self::splitTrail( $trail );
730 $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
731 wfProfileOut( __METHOD__ );
732 return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
733 . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
734 } else {
735 wfProfileOut( __METHOD__ );
736 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
737 }
738 } else {
739 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
740 }
741 }
742
743 /** @deprecated use Linker::makeMediaLinkObj() */
744 function makeMediaLink( $name, $unused = '', $text = '' ) {
745 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
746 return $this->makeMediaLinkObj( $nt, $text );
747 }
748
749 /**
750 * Create a direct link to a given uploaded file.
751 *
752 * @param $title Title object.
753 * @param $text String: pre-sanitized HTML
754 * @return string HTML
755 *
756 * @public
757 * @todo Handle invalid or missing images better.
758 */
759 function makeMediaLinkObj( $title, $text = '' ) {
760 if( is_null( $title ) ) {
761 ### HOTFIX. Instead of breaking, return empty string.
762 return $text;
763 } else {
764 $img = wfFindFile( $title );
765 if( $img ) {
766 $url = $img->getURL();
767 $class = 'internal';
768 } else {
769 $upload = SpecialPage::getTitleFor( 'Upload' );
770 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDbKey() ) );
771 $class = 'new';
772 }
773 $alt = htmlspecialchars( $title->getText() );
774 if( $text == '' ) {
775 $text = $alt;
776 }
777 $u = htmlspecialchars( $url );
778 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
779 }
780 }
781
782 /** @todo document */
783 function specialLink( $name, $key = '' ) {
784 global $wgContLang;
785
786 if ( '' == $key ) { $key = strtolower( $name ); }
787 $pn = $wgContLang->ucfirst( $name );
788 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
789 wfMsg( $key ) );
790 }
791
792 /** @todo document */
793 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
794 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
795 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
796 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
797 $style .= ' rel="nofollow"';
798 }
799 $url = htmlspecialchars( $url );
800 if( $escape ) {
801 $text = htmlspecialchars( $text );
802 }
803 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
804 }
805
806 /**
807 * Make user link (or user contributions for unregistered users)
808 * @param $userId Integer: user id in database.
809 * @param $userText String: user name in database
810 * @return string HTML fragment
811 * @private
812 */
813 function userLink( $userId, $userText ) {
814 $encName = htmlspecialchars( $userText );
815 if( $userId == 0 ) {
816 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
817 return $this->makeKnownLinkObj( $contribsPage,
818 $encName);
819 } else {
820 $userPage = Title::makeTitle( NS_USER, $userText );
821 return $this->makeLinkObj( $userPage, $encName );
822 }
823 }
824
825 /**
826 * Generate standard user tool links (talk, contributions, block link, etc.)
827 *
828 * @param int $userId User identifier
829 * @param string $userText User name or IP address
830 * @param bool $redContribsWhenNoEdits Should the contributions link be red if the user has no edits?
831 * @param int $flags Customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
832 * @return string
833 */
834 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0 ) {
835 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
836 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
837 $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
838
839 $items = array();
840 if( $talkable ) {
841 $items[] = $this->userTalkLink( $userId, $userText );
842 }
843 if( $userId ) {
844 // check if the user has an edit
845 if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
846 $style = " class='new'";
847 } else {
848 $style = '';
849 }
850 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
851
852 $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
853 }
854 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
855 $items[] = $this->blockLink( $userId, $userText );
856 }
857
858 if( $items ) {
859 return ' (' . implode( ' | ', $items ) . ')';
860 } else {
861 return '';
862 }
863 }
864
865 /**
866 * Alias for userToolLinks( $userId, $userText, true );
867 */
868 public function userToolLinksRedContribs( $userId, $userText ) {
869 return $this->userToolLinks( $userId, $userText, true );
870 }
871
872
873 /**
874 * @param $userId Integer: user id in database.
875 * @param $userText String: user name in database.
876 * @return string HTML fragment with user talk link
877 * @private
878 */
879 function userTalkLink( $userId, $userText ) {
880 $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
881 $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
882 return $userTalkLink;
883 }
884
885 /**
886 * @param $userId Integer: userid
887 * @param $userText String: user name in database.
888 * @return string HTML fragment with block link
889 * @private
890 */
891 function blockLink( $userId, $userText ) {
892 $blockPage = SpecialPage::getTitleFor( 'Blockip', $userText );
893 $blockLink = $this->makeKnownLinkObj( $blockPage,
894 wfMsgHtml( 'blocklink' ) );
895 return $blockLink;
896 }
897
898 /**
899 * Generate a user link if the current user is allowed to view it
900 * @param $rev Revision object.
901 * @return string HTML
902 */
903 function revUserLink( $rev ) {
904 if( $rev->userCan( Revision::DELETED_USER ) ) {
905 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
906 } else {
907 $link = wfMsgHtml( 'rev-deleted-user' );
908 }
909 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
910 return '<span class="history-deleted">' . $link . '</span>';
911 }
912 return $link;
913 }
914
915 /**
916 * Generate a user tool link cluster if the current user is allowed to view it
917 * @param $rev Revision object.
918 * @return string HTML
919 */
920 function revUserTools( $rev ) {
921 if( $rev->userCan( Revision::DELETED_USER ) ) {
922 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
923 ' ' .
924 $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
925 } else {
926 $link = wfMsgHtml( 'rev-deleted-user' );
927 }
928 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
929 return '<span class="history-deleted">' . $link . '</span>';
930 }
931 return $link;
932 }
933
934 /**
935 * This function is called by all recent changes variants, by the page history,
936 * and by the user contributions list. It is responsible for formatting edit
937 * comments. It escapes any HTML in the comment, but adds some CSS to format
938 * auto-generated comments (from section editing) and formats [[wikilinks]].
939 *
940 * @author Erik Moeller <moeller@scireview.de>
941 *
942 * Note: there's not always a title to pass to this function.
943 * Since you can't set a default parameter for a reference, I've turned it
944 * temporarily to a value pass. Should be adjusted further. --brion
945 *
946 * @param string $comment
947 * @param mixed $title Title object (to generate link to the section in autocomment) or null
948 * @param bool $local Whether section links should refer to local page
949 */
950 function formatComment($comment, $title = NULL, $local = false) {
951 wfProfileIn( __METHOD__ );
952
953 # Sanitize text a bit:
954 $comment = str_replace( "\n", " ", $comment );
955 $comment = htmlspecialchars( $comment );
956
957 # Render autocomments and make links:
958 $comment = $this->formatAutoComments( $comment, $title, $local );
959 $comment = $this->formatLinksInComment( $comment );
960
961 wfProfileOut( __METHOD__ );
962 return $comment;
963 }
964
965 /**
966 * The pattern for autogen comments is / * foo * /, which makes for
967 * some nasty regex.
968 * We look for all comments, match any text before and after the comment,
969 * add a separator where needed and format the comment itself with CSS
970 * Called by Linker::formatComment.
971 *
972 * @param $comment Comment text
973 * @param $title An optional title object used to links to sections
974 *
975 * @todo Document the $local parameter.
976 */
977 private function formatAutocomments( $comment, $title = NULL, $local = false ) {
978 $match = array();
979 while (preg_match('!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment,$match)) {
980 $pre=$match[1];
981 $auto=$match[2];
982 $post=$match[3];
983 $link='';
984 if( $title ) {
985 $section = $auto;
986
987 # Generate a valid anchor name from the section title.
988 # Hackish, but should generally work - we strip wiki
989 # syntax, including the magic [[: that is used to
990 # "link rather than show" in case of images and
991 # interlanguage links.
992 $section = str_replace( '[[:', '', $section );
993 $section = str_replace( '[[', '', $section );
994 $section = str_replace( ']]', '', $section );
995 if ( $local ) {
996 $sectionTitle = Title::newFromText( '#' . $section);
997 } else {
998 $sectionTitle = wfClone( $title );
999 $sectionTitle->mFragment = $section;
1000 }
1001 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsg( 'sectionlink' ) );
1002 }
1003 $sep='-';
1004 $auto=$link.$auto;
1005 if($pre) { $auto = $sep.' '.$auto; }
1006 if($post) { $auto .= ' '.$sep; }
1007 $auto='<span class="autocomment">'.$auto.'</span>';
1008 $comment=$pre.$auto.$post;
1009 }
1010
1011 return $comment;
1012 }
1013
1014 /**
1015 * Formats wiki links and media links in text; all other wiki formatting
1016 * is ignored
1017 *
1018 * @fixme doesn't handle sub-links as in image thumb texts like the main parser
1019 * @param string $comment Text to format links in
1020 * @return string
1021 */
1022 public function formatLinksInComment( $comment ) {
1023 return preg_replace_callback(
1024 '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
1025 array( $this, 'formatLinksInCommentCallback' ),
1026 $comment );
1027 }
1028
1029 protected function formatLinksInCommentCallback( $match ) {
1030 global $wgContLang;
1031
1032 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
1033 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
1034
1035 $comment = $match[0];
1036
1037 # Handle link renaming [[foo|text]] will show link as "text"
1038 if( "" != $match[3] ) {
1039 $text = $match[3];
1040 } else {
1041 $text = $match[1];
1042 }
1043 $submatch = array();
1044 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1045 # Media link; trail not supported.
1046 $linkRegexp = '/\[\[(.*?)\]\]/';
1047 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
1048 } else {
1049 # Other kind of link
1050 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
1051 $trail = $submatch[1];
1052 } else {
1053 $trail = "";
1054 }
1055 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1056 if (isset($match[1][0]) && $match[1][0] == ':')
1057 $match[1] = substr($match[1], 1);
1058 $thelink = $this->makeLink( $match[1], $text, "", $trail );
1059 }
1060 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
1061
1062 return $comment;
1063 }
1064
1065 /**
1066 * Wrap a comment in standard punctuation and formatting if
1067 * it's non-empty, otherwise return empty string.
1068 *
1069 * @param string $comment
1070 * @param mixed $title Title object (to generate link to section in autocomment) or null
1071 * @param bool $local Whether section links should refer to local page
1072 *
1073 * @return string
1074 */
1075 function commentBlock( $comment, $title = NULL, $local = false ) {
1076 // '*' used to be the comment inserted by the software way back
1077 // in antiquity in case none was provided, here for backwards
1078 // compatability, acc. to brion -ævar
1079 if( $comment == '' || $comment == '*' ) {
1080 return '';
1081 } else {
1082 $formatted = $this->formatComment( $comment, $title, $local );
1083 return " <span class=\"comment\">($formatted)</span>";
1084 }
1085 }
1086
1087 /**
1088 * Wrap and format the given revision's comment block, if the current
1089 * user is allowed to view it.
1090 *
1091 * @param Revision $rev
1092 * @param bool $local Whether section links should refer to local page
1093 * @return string HTML
1094 */
1095 function revComment( Revision $rev, $local = false ) {
1096 if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
1097 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
1098 } else {
1099 $block = " <span class=\"comment\">" .
1100 wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1101 }
1102 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
1103 return " <span class=\"history-deleted\">$block</span>";
1104 }
1105 return $block;
1106 }
1107
1108 /** @todo document */
1109 function tocIndent() {
1110 return "\n<ul>";
1111 }
1112
1113 /** @todo document */
1114 function tocUnindent($level) {
1115 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
1116 }
1117
1118 /**
1119 * parameter level defines if we are on an indentation level
1120 */
1121 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1122 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1123 $anchor . '"><span class="tocnumber">' .
1124 $tocnumber . '</span> <span class="toctext">' .
1125 $tocline . '</span></a>';
1126 }
1127
1128 /** @todo document */
1129 function tocLineEnd() {
1130 return "</li>\n";
1131 }
1132
1133 /** @todo document */
1134 function tocList($toc) {
1135 global $wgJsMimeType;
1136 $title = wfMsgHtml('toc') ;
1137 return
1138 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1139 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1140 . $toc
1141 # no trailing newline, script should not be wrapped in a
1142 # paragraph
1143 . "</ul>\n</td></tr></table>"
1144 . '<script type="' . $wgJsMimeType . '">'
1145 . ' if (window.showTocToggle) {'
1146 . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
1147 . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
1148 . ' showTocToggle();'
1149 . ' } '
1150 . "</script>\n";
1151 }
1152
1153 /**
1154 * Used to generate section edit links that point to "other" pages
1155 * (sections that are really part of included pages).
1156 *
1157 * @param $title Title string.
1158 * @param $section Integer: section number.
1159 */
1160 public function editSectionLinkForOther( $title, $section ) {
1161 $title = Title::newFromText( $title );
1162 return $this->doEditSectionLink( $title, $section, '', 'EditSectionLinkForOther' );
1163 }
1164
1165 /**
1166 * @param $nt Title object.
1167 * @param $section Integer: section number.
1168 * @param $hint Link String: title, or default if omitted or empty
1169 */
1170 public function editSectionLink( Title $nt, $section, $hint='' ) {
1171 if( $hint != '' ) {
1172 $hint = wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) );
1173 $hint = " title=\"$hint\"";
1174 }
1175 return $this->doEditSectionLink( $nt, $section, $hint, 'EditSectionLink' );
1176 }
1177
1178 /**
1179 * Implement editSectionLink and editSectionLinkForOther.
1180 *
1181 * @param $nt Title object
1182 * @param $section Integer, section number
1183 * @param $hint String, for HTML title attribute
1184 * @param $hook String, name of hook to run
1185 * @return String, HTML to use for edit link
1186 */
1187 protected function doEditSectionLink( Title $nt, $section, $hint, $hook ) {
1188 global $wgContLang;
1189 $editurl = '&section='.$section;
1190 $url = $this->makeKnownLinkObj(
1191 $nt,
1192 wfMsg('editsection'),
1193 'action=edit'.$editurl,
1194 '', '', '', $hint
1195 );
1196 $result = null;
1197
1198 // The two hooks have slightly different interfaces . . .
1199 if( $hook == 'EditSectionLink' ) {
1200 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $hint, $url, &$result ) );
1201 } elseif( $hook == 'EditSectionLinkForOther' ) {
1202 wfRunHooks( 'EditSectionLinkForOther', array( &$this, $nt, $section, $url, &$result ) );
1203 }
1204
1205 // For reverse compatibility, add the brackets *after* the hook is run,
1206 // and even add them to hook-provided text.
1207 if( is_null( $result ) ) {
1208 $result = wfMsg( 'editsection-brackets', $url );
1209 } else {
1210 $result = wfMsg( 'editsection-brackets', $result );
1211 }
1212 return "<span class=\"editsection\">$result</span>";
1213 }
1214
1215 /**
1216 * Create a headline for content
1217 *
1218 * @param int $level The level of the headline (1-6)
1219 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1220 * This *must* be at least '>' for no attribs
1221 * @param string $anchor The anchor to give the headline (the bit after the #)
1222 * @param string $text The text of the header
1223 * @param string $link HTML to add for the section edit link
1224 *
1225 * @return string HTML headline
1226 */
1227 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1228 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1229 }
1230
1231 /**
1232 * Split a link trail, return the "inside" portion and the remainder of the trail
1233 * as a two-element array
1234 *
1235 * @static
1236 */
1237 static function splitTrail( $trail ) {
1238 static $regex = false;
1239 if ( $regex === false ) {
1240 global $wgContLang;
1241 $regex = $wgContLang->linkTrail();
1242 }
1243 $inside = '';
1244 if ( '' != $trail ) {
1245 $m = array();
1246 if ( preg_match( $regex, $trail, $m ) ) {
1247 $inside = $m[1];
1248 $trail = $m[2];
1249 }
1250 }
1251 return array( $inside, $trail );
1252 }
1253
1254 /**
1255 * Generate a rollback link for a given revision. Currently it's the
1256 * caller's responsibility to ensure that the revision is the top one. If
1257 * it's not, of course, the user will get an error message.
1258 *
1259 * If the calling page is called with the parameter &bot=1, all rollback
1260 * links also get that parameter. It causes the edit itself and the rollback
1261 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1262 * changes, so this allows sysops to combat a busy vandal without bothering
1263 * other users.
1264 *
1265 * @param Revision $rev
1266 */
1267 function generateRollback( $rev ) {
1268 return '<span class="mw-rollback-link">['
1269 . $this->buildRollbackLink( $rev )
1270 . ']</span>';
1271 }
1272
1273 /**
1274 * Build a raw rollback link, useful for collections of "tool" links
1275 *
1276 * @param Revision $rev
1277 * @return string
1278 */
1279 public function buildRollbackLink( $rev ) {
1280 global $wgRequest, $wgUser;
1281 $title = $rev->getTitle();
1282 $extra = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
1283 $extra .= '&token=' . urlencode( $wgUser->editToken( array( $title->getPrefixedText(),
1284 $rev->getUserText() ) ) );
1285 return $this->makeKnownLinkObj(
1286 $title,
1287 wfMsgHtml( 'rollbacklink' ),
1288 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extra
1289 );
1290 }
1291
1292 /**
1293 * Returns HTML for the "templates used on this page" list.
1294 *
1295 * @param array $templates Array of templates from Article::getUsedTemplate
1296 * or similar
1297 * @param bool $preview Whether this is for a preview
1298 * @param bool $section Whether this is for a section edit
1299 * @return string HTML output
1300 */
1301 public function formatTemplates( $templates, $preview = false, $section = false) {
1302 global $wgUser;
1303 wfProfileIn( __METHOD__ );
1304
1305 $sk = $wgUser->getSkin();
1306
1307 $outText = '';
1308 if ( count( $templates ) > 0 ) {
1309 # Do a batch existence check
1310 $batch = new LinkBatch;
1311 foreach( $templates as $title ) {
1312 $batch->addObj( $title );
1313 }
1314 $batch->execute();
1315
1316 # Construct the HTML
1317 $outText = '<div class="mw-templatesUsedExplanation">';
1318 if ( $preview ) {
1319 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1320 } elseif ( $section ) {
1321 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1322 } else {
1323 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1324 }
1325 $outText .= '</div><ul>';
1326
1327 foreach ( $templates as $titleObj ) {
1328 $r = $titleObj->getRestrictions( 'edit' );
1329 if ( in_array( 'sysop', $r ) ) {
1330 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1331 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1332 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1333 } else {
1334 $protected = '';
1335 }
1336 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1337 }
1338 $outText .= '</ul>';
1339 }
1340 wfProfileOut( __METHOD__ );
1341 return $outText;
1342 }
1343
1344 /**
1345 * Format a size in bytes for output, using an appropriate
1346 * unit (B, KB, MB or GB) according to the magnitude in question
1347 *
1348 * @param $size Size to format
1349 * @return string
1350 */
1351 public function formatSize( $size ) {
1352 global $wgLang;
1353 return htmlspecialchars( $wgLang->formatSize( $size ) );
1354 }
1355
1356 /**
1357 * Given the id of an interface element, constructs the appropriate title
1358 * and accesskey attributes from the system messages. (Note, this is usu-
1359 * ally the id but isn't always, because sometimes the accesskey needs to
1360 * go on a different element than the id, for reverse-compatibility, etc.)
1361 *
1362 * @param string $name Id of the element, minus prefixes.
1363 * @return string title and accesskey attributes, ready to drop in an
1364 * element (e.g., ' title="This does something [x]" accesskey="x"').
1365 */
1366 public function tooltipAndAccesskey($name) {
1367 $fname="Linker::tooltipAndAccesskey";
1368 wfProfileIn($fname);
1369 $out = '';
1370
1371 $tooltip = wfMsg('tooltip-'.$name);
1372 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1373 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1374 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1375 $out .= ' title="'.htmlspecialchars($tooltip);
1376 }
1377 $accesskey = wfMsg('accesskey-'.$name);
1378 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1379 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1380 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1381 } elseif ($out) {
1382 $out .= '"';
1383 }
1384 wfProfileOut($fname);
1385 return $out;
1386 }
1387
1388 /**
1389 * Given the id of an interface element, constructs the appropriate title
1390 * attribute from the system messages. (Note, this is usually the id but
1391 * isn't always, because sometimes the accesskey needs to go on a different
1392 * element than the id, for reverse-compatibility, etc.)
1393 *
1394 * @param string $name Id of the element, minus prefixes.
1395 * @return string title attribute, ready to drop in an element
1396 * (e.g., ' title="This does something"').
1397 */
1398 public function tooltip($name) {
1399 $out = '';
1400
1401 $tooltip = wfMsg('tooltip-'.$name);
1402 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1403 $out = ' title="'.htmlspecialchars($tooltip).'"';
1404 }
1405
1406 return $out;
1407 }
1408 }