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