revert r76127, r76129, and r83535 b/c I'm backing out the fix for bug #542. Fixing...
[lhc/web/wiklou.git] / includes / Linker.php
1 <?php
2 /**
3 * Some internal bits split of from Skin.php. These functions are used
4 * for primarily page content: links, embedded images, table of contents. Links
5 * are also used in the skin.
6 *
7 * @ingroup Skins
8 */
9 class Linker {
10
11 /**
12 * Flags for userToolLinks()
13 */
14 const TOOL_LINKS_NOBLOCK = 1;
15
16 /**
17 * Get the appropriate HTML attributes to add to the "a" element of an ex-
18 * ternal link, as created by [wikisyntax].
19 *
20 * @param $class String: the contents of the class attribute; if an empty
21 * string is passed, which is the default value, defaults to 'external'.
22 * @deprecated Just pass the external class directly to something using Html::expandAttributes
23 */
24 static function getExternalLinkAttributes( $class = 'external' ) {
25 wfDeprecated( __METHOD__ );
26 return self::getLinkAttributesInternal( '', $class );
27 }
28
29 /**
30 * Get the appropriate HTML attributes to add to the "a" element of an in-
31 * terwiki link.
32 *
33 * @param $title String: the title text for the link, URL-encoded (???) but
34 * not HTML-escaped
35 * @param $unused String: unused
36 * @param $class String: the contents of the class attribute; if an empty
37 * string is passed, which is the default value, defaults to 'external'.
38 */
39 static function getInterwikiLinkAttributes( $title, $unused = null, $class = 'external' ) {
40 global $wgContLang;
41
42 # FIXME: We have a whole bunch of handling here that doesn't happen in
43 # getExternalLinkAttributes, why?
44 $title = urldecode( $title );
45 $title = $wgContLang->checkTitleEncoding( $title );
46 $title = preg_replace( '/[\\x00-\\x1f]/', ' ', $title );
47
48 return self::getLinkAttributesInternal( $title, $class );
49 }
50
51 /**
52 * Get the appropriate HTML attributes to add to the "a" element of an in-
53 * ternal link.
54 *
55 * @param $title String: the title text for the link, URL-encoded (???) but
56 * not HTML-escaped
57 * @param $unused String: unused
58 * @param $class String: the contents of the class attribute, default none
59 */
60 static function getInternalLinkAttributes( $title, $unused = null, $class = '' ) {
61 $title = urldecode( $title );
62 $title = str_replace( '_', ' ', $title );
63 return self::getLinkAttributesInternal( $title, $class );
64 }
65
66 /**
67 * Get the appropriate HTML attributes to add to the "a" element of an in-
68 * ternal link, given the Title object for the page we want to link to.
69 *
70 * @param $nt Title
71 * @param $unused String: unused
72 * @param $class String: the contents of the class attribute, default none
73 * @param $title Mixed: optional (unescaped) string to use in the title
74 * attribute; if false, default to the name of the page we're linking to
75 */
76 static function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
77 if ( $title === false ) {
78 $title = $nt->getPrefixedText();
79 }
80 return self::getLinkAttributesInternal( $title, $class );
81 }
82
83 /**
84 * Common code for getLinkAttributesX functions
85 */
86 private static function getLinkAttributesInternal( $title, $class ) {
87 $title = htmlspecialchars( $title );
88 $class = htmlspecialchars( $class );
89 $r = '';
90 if ( $class != '' ) {
91 $r .= " class=\"$class\"";
92 }
93 if ( $title != '' ) {
94 $r .= " title=\"$title\"";
95 }
96 return $r;
97 }
98
99 /**
100 * Return the CSS colour of a known link
101 *
102 * @param $t Title object
103 * @param $threshold Integer: user defined threshold
104 * @return String: CSS class
105 */
106 static function getLinkColour( $t, $threshold ) {
107 $colour = '';
108 if ( $t->isRedirect() ) {
109 # Page is a redirect
110 $colour = 'mw-redirect';
111 } elseif ( $threshold > 0 &&
112 $t->exists() && $t->getLength() < $threshold &&
113 $t->isContentPage() ) {
114 # Page is a stub
115 $colour = 'stub';
116 }
117 return $colour;
118 }
119
120 /**
121 * This function returns an HTML link to the given target. It serves a few
122 * purposes:
123 * 1) If $target is a Title, the correct URL to link to will be figured
124 * out automatically.
125 * 2) It automatically adds the usual classes for various types of link
126 * targets: "new" for red links, "stub" for short articles, etc.
127 * 3) It escapes all attribute values safely so there's no risk of XSS.
128 * 4) It provides a default tooltip if the target is a Title (the page
129 * name of the target).
130 * link() replaces the old functions in the makeLink() family.
131 *
132 * @param $target Title Can currently only be a Title, but this may
133 * change to support Images, literal URLs, etc.
134 * @param $text string The HTML contents of the <a> element, i.e.,
135 * the link text. This is raw HTML and will not be escaped. If null,
136 * defaults to the prefixed text of the Title; or if the Title is just a
137 * fragment, the contents of the fragment.
138 * @param $customAttribs array A key => value array of extra HTML attri-
139 * butes, such as title and class. (href is ignored.) Classes will be
140 * merged with the default classes, while other attributes will replace
141 * default attributes. All passed attribute values will be HTML-escaped.
142 * A false attribute value means to suppress that attribute.
143 * @param $query array The query string to append to the URL
144 * you're linking to, in key => value array form. Query keys and values
145 * will be URL-encoded.
146 * @param $options mixed String or array of strings:
147 * 'known': Page is known to exist, so don't check if it does.
148 * 'broken': Page is known not to exist, so don't check if it does.
149 * 'noclasses': Don't add any classes automatically (includes "new",
150 * "stub", "mw-redirect", "extiw"). Only use the class attribute
151 * provided, if any, so you get a simple blue link with no funny i-
152 * cons.
153 * 'forcearticlepath': Use the article path always, even with a querystring.
154 * Has compatibility issues on some setups, so avoid wherever possible.
155 * @return string HTML <a> attribute
156 */
157 public static function link(
158 $target, $text = null, $customAttribs = array(), $query = array(), $options = array()
159 ) {
160 wfProfileIn( __METHOD__ );
161 if ( !$target instanceof Title ) {
162 wfProfileOut( __METHOD__ );
163 return "<!-- ERROR -->$text";
164 }
165 $options = (array)$options;
166
167 $dummy = new DummyLinker; // dummy linker instance for bc on the hooks
168
169 $ret = null;
170 if ( !wfRunHooks( 'LinkBegin', array( $dummy, $target, &$text,
171 &$customAttribs, &$query, &$options, &$ret ) ) ) {
172 wfProfileOut( __METHOD__ );
173 return $ret;
174 }
175
176 # Normalize the Title if it's a special page
177 $target = self::normaliseSpecialPage( $target );
178
179 # If we don't know whether the page exists, let's find out.
180 wfProfileIn( __METHOD__ . '-checkPageExistence' );
181 if ( !in_array( 'known', $options ) and !in_array( 'broken', $options ) ) {
182 if ( $target->isKnown() ) {
183 $options[] = 'known';
184 } else {
185 $options[] = 'broken';
186 }
187 }
188 wfProfileOut( __METHOD__ . '-checkPageExistence' );
189
190 $oldquery = array();
191 if ( in_array( "forcearticlepath", $options ) && $query ) {
192 $oldquery = $query;
193 $query = array();
194 }
195
196 # Note: we want the href attribute first, for prettiness.
197 $attribs = array( 'href' => self::linkUrl( $target, $query, $options ) );
198 if ( in_array( 'forcearticlepath', $options ) && $oldquery ) {
199 $attribs['href'] = wfAppendQuery( $attribs['href'], wfArrayToCgi( $oldquery ) );
200 }
201
202 $attribs = array_merge(
203 $attribs,
204 self::linkAttribs( $target, $customAttribs, $options )
205 );
206 if ( is_null( $text ) ) {
207 $text = self::linkText( $target );
208 }
209
210 $ret = null;
211 if ( wfRunHooks( 'LinkEnd', array( $dummy, $target, $options, &$text, &$attribs, &$ret ) ) ) {
212 $ret = Html::rawElement( 'a', $attribs, $text );
213 }
214
215 wfProfileOut( __METHOD__ );
216 return $ret;
217 }
218
219 /**
220 * Identical to link(), except $options defaults to 'known'.
221 */
222 public static function linkKnown(
223 $target, $text = null, $customAttribs = array(),
224 $query = array(), $options = array( 'known', 'noclasses' ) )
225 {
226 return self::link( $target, $text, $customAttribs, $query, $options );
227 }
228
229 /**
230 * Returns the Url used to link to a Title
231 *
232 * @param $target Title
233 */
234 private static function linkUrl( $target, $query, $options ) {
235 wfProfileIn( __METHOD__ );
236 # We don't want to include fragments for broken links, because they
237 # generally make no sense.
238 if ( in_array( 'broken', $options ) && $target->mFragment !== '' ) {
239 $target = clone $target;
240 $target->mFragment = '';
241 }
242
243 # If it's a broken link, add the appropriate query pieces, unless
244 # there's already an action specified, or unless 'edit' makes no sense
245 # (i.e., for a nonexistent special page).
246 if ( in_array( 'broken', $options ) && empty( $query['action'] )
247 && $target->getNamespace() != NS_SPECIAL ) {
248 $query['action'] = 'edit';
249 $query['redlink'] = '1';
250 }
251 $ret = $target->getLinkUrl( $query );
252 wfProfileOut( __METHOD__ );
253 return $ret;
254 }
255
256 /**
257 * Returns the array of attributes used when linking to the Title $target
258 *
259 * @param $target Title
260 */
261 private static function linkAttribs( $target, $attribs, $options ) {
262 wfProfileIn( __METHOD__ );
263 global $wgUser;
264 $defaults = array();
265
266 if ( !in_array( 'noclasses', $options ) ) {
267 wfProfileIn( __METHOD__ . '-getClasses' );
268 # Now build the classes.
269 $classes = array();
270
271 if ( in_array( 'broken', $options ) ) {
272 $classes[] = 'new';
273 }
274
275 if ( $target->isExternal() ) {
276 $classes[] = 'extiw';
277 }
278
279 if ( !in_array( 'broken', $options ) ) { # Avoid useless calls to LinkCache (see r50387)
280 $colour = self::getLinkColour( $target, $wgUser->getStubThreshold() );
281 if ( $colour !== '' ) {
282 $classes[] = $colour; # mw-redirect or stub
283 }
284 }
285 if ( $classes != array() ) {
286 $defaults['class'] = implode( ' ', $classes );
287 }
288 wfProfileOut( __METHOD__ . '-getClasses' );
289 }
290
291 # Get a default title attribute.
292 if ( $target->getPrefixedText() == '' ) {
293 # A link like [[#Foo]]. This used to mean an empty title
294 # attribute, but that's silly. Just don't output a title.
295 } elseif ( in_array( 'known', $options ) ) {
296 $defaults['title'] = $target->getPrefixedText();
297 } else {
298 $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
299 }
300
301 # Finally, merge the custom attribs with the default ones, and iterate
302 # over that, deleting all "false" attributes.
303 $ret = array();
304 $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
305 foreach ( $merged as $key => $val ) {
306 # A false value suppresses the attribute, and we don't want the
307 # href attribute to be overridden.
308 if ( $key != 'href' and $val !== false ) {
309 $ret[$key] = $val;
310 }
311 }
312 wfProfileOut( __METHOD__ );
313 return $ret;
314 }
315
316 /**
317 * Default text of the links to the Title $target
318 *
319 * @param $target Title
320 */
321 private static function linkText( $target ) {
322 # We might be passed a non-Title by make*LinkObj(). Fail gracefully.
323 if ( !$target instanceof Title ) {
324 return '';
325 }
326
327 # If the target is just a fragment, with no title, we return the frag-
328 # ment text. Otherwise, we return the title text itself.
329 if ( $target->getPrefixedText() === '' && $target->getFragment() !== '' ) {
330 return htmlspecialchars( $target->getFragment() );
331 }
332 return htmlspecialchars( $target->getPrefixedText() );
333 }
334
335 /**
336 * Generate either a normal exists-style link or a stub link, depending
337 * on the given page size.
338 *
339 * @param $size Integer
340 * @param $nt Title object.
341 * @param $text String
342 * @param $query String
343 * @param $trail String
344 * @param $prefix String
345 * @return string HTML of link
346 * @deprecated since 1.17
347 */
348 static function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
349 global $wgUser;
350 wfDeprecated( __METHOD__ );
351
352 $threshold = $wgUser->getStubThreshold();
353 $colour = ( $size < $threshold ) ? 'stub' : '';
354 // FIXME: replace deprecated makeColouredLinkObj by link()
355 return self::makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
356 }
357
358 /**
359 * Make appropriate markup for a link to the current article. This is currently rendered
360 * as the bold link text. The calling sequence is the same as the other make*LinkObj static functions,
361 * despite $query not being used.
362 *
363 * @param $nt Title
364 */
365 static function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
366 if ( $text == '' ) {
367 $text = htmlspecialchars( $nt->getPrefixedText() );
368 }
369 list( $inside, $trail ) = self::splitTrail( $trail );
370 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
371 }
372
373 static function normaliseSpecialPage( Title $title ) {
374 if ( $title->getNamespace() == NS_SPECIAL ) {
375 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
376 if ( !$name ) {
377 return $title;
378 }
379 $ret = SpecialPage::getTitleFor( $name, $subpage );
380 $ret->mFragment = $title->getFragment();
381 return $ret;
382 } else {
383 return $title;
384 }
385 }
386
387 /**
388 * Returns the filename part of an url.
389 * Used as alternative text for external images.
390 */
391 static function fnamePart( $url ) {
392 $basename = strrchr( $url, '/' );
393 if ( false === $basename ) {
394 $basename = $url;
395 } else {
396 $basename = substr( $basename, 1 );
397 }
398 return $basename;
399 }
400
401 /**
402 * Return the code for images which were added via external links,
403 * via Parser::maybeMakeExternalImage().
404 */
405 static function makeExternalImage( $url, $alt = '' ) {
406 if ( $alt == '' ) {
407 $alt = self::fnamePart( $url );
408 }
409 $img = '';
410 $success = wfRunHooks( 'LinkerMakeExternalImage', array( &$url, &$alt, &$img ) );
411 if ( !$success ) {
412 wfDebug( "Hook LinkerMakeExternalImage changed the output of external image with url {$url} and alt text {$alt} to {$img}\n", true );
413 return $img;
414 }
415 return Html::element( 'img',
416 array(
417 'src' => $url,
418 'alt' => $alt ) );
419 }
420
421 /**
422 * Given parameters derived from [[Image:Foo|options...]], generate the
423 * HTML that that syntax inserts in the page.
424 *
425 * @param $title Title object
426 * @param $file File object, or false if it doesn't exist
427 * @param $frameParams Array: associative array of parameters external to the media handler.
428 * Boolean parameters are indicated by presence or absence, the value is arbitrary and
429 * will often be false.
430 * thumbnail If present, downscale and frame
431 * manualthumb Image name to use as a thumbnail, instead of automatic scaling
432 * framed Shows image in original size in a frame
433 * frameless Downscale but don't frame
434 * upright If present, tweak default sizes for portrait orientation
435 * upright_factor Fudge factor for "upright" tweak (default 0.75)
436 * border If present, show a border around the image
437 * align Horizontal alignment (left, right, center, none)
438 * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
439 * bottom, text-bottom)
440 * alt Alternate text for image (i.e. alt attribute). Plain text.
441 * caption HTML for image caption.
442 * link-url URL to link to
443 * link-title Title object to link to
444 * link-target Value for the target attribue, only with link-url
445 * no-link Boolean, suppress description link
446 *
447 * @param $handlerParams Array: associative array of media handler parameters, to be passed
448 * to transform(). Typical keys are "width" and "page".
449 * @param $time String: timestamp of the file, set as false for current
450 * @param $query String: query params for desc url
451 * @param $widthOption: Used by the parser to remember the user preference thumbnailsize
452 * @return String: HTML for an image, with links, wrappers, etc.
453 */
454 static function makeImageLink2( Title $title, $file, $frameParams = array(),
455 $handlerParams = array(), $time = false, $query = "", $widthOption = null )
456 {
457 $res = null;
458 $dummy = new DummyLinker;
459 if ( !wfRunHooks( 'ImageBeforeProduceHTML', array( &$dummy, &$title,
460 &$file, &$frameParams, &$handlerParams, &$time, &$res ) ) ) {
461 return $res;
462 }
463
464 if ( $file && !$file->allowInlineDisplay() ) {
465 wfDebug( __METHOD__ . ': ' . $title->getPrefixedDBkey() . " does not allow inline display\n" );
466 return self::link( $title );
467 }
468
469 // Shortcuts
470 $fp =& $frameParams;
471 $hp =& $handlerParams;
472
473 // Clean up parameters
474 $page = isset( $hp['page'] ) ? $hp['page'] : false;
475 if ( !isset( $fp['align'] ) ) {
476 $fp['align'] = '';
477 }
478 if ( !isset( $fp['alt'] ) ) {
479 $fp['alt'] = '';
480 }
481 if ( !isset( $fp['title'] ) ) {
482 $fp['title'] = '';
483 }
484
485 $prefix = $postfix = '';
486
487 if ( 'center' == $fp['align'] ) {
488 $prefix = '<div class="center">';
489 $postfix = '</div>';
490 $fp['align'] = 'none';
491 }
492 if ( $file && !isset( $hp['width'] ) ) {
493 $hp['width'] = $file->getWidth( $page );
494
495 if ( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
496 global $wgThumbLimits, $wgThumbUpright;
497 if ( !isset( $widthOption ) || !isset( $wgThumbLimits[$widthOption] ) ) {
498 $widthOption = User::getDefaultOption( 'thumbsize' );
499 }
500
501 // Reduce width for upright images when parameter 'upright' is used
502 if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
503 $fp['upright'] = $wgThumbUpright;
504 }
505 // 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
506 $prefWidth = isset( $fp['upright'] ) ?
507 round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
508 $wgThumbLimits[$widthOption];
509
510 // Use width which is smaller: real image width or user preference width
511 // Unless image is scalable vector.
512 if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
513 $prefWidth < $hp['width'] || $file->isVectorized() ) ) {
514 $hp['width'] = $prefWidth;
515 }
516 }
517 }
518
519 if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
520 global $wgContLang;
521 # Create a thumbnail. Alignment depends on language
522 # writing direction, # right aligned for left-to-right-
523 # languages ("Western languages"), left-aligned
524 # for right-to-left-languages ("Semitic languages")
525 #
526 # If thumbnail width has not been provided, it is set
527 # to the default user option as specified in Language*.php
528 if ( $fp['align'] == '' ) {
529 $fp['align'] = $wgContLang->alignEnd();
530 }
531 return $prefix . self::makeThumbLink2( $title, $file, $fp, $hp, $time, $query ) . $postfix;
532 }
533
534 if ( $file && isset( $fp['frameless'] ) ) {
535 $srcWidth = $file->getWidth( $page );
536 # For "frameless" option: do not present an image bigger than the source (for bitmap-style images)
537 # This is the same behaviour as the "thumb" option does it already.
538 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
539 $hp['width'] = $srcWidth;
540 }
541 }
542
543 if ( $file && isset( $hp['width'] ) ) {
544 # Create a resized image, without the additional thumbnail features
545 $thumb = $file->transform( $hp );
546 } else {
547 $thumb = false;
548 }
549
550 if ( !$thumb ) {
551 $s = self::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
552 } else {
553 $params = array(
554 'alt' => $fp['alt'],
555 'title' => $fp['title'],
556 'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
557 'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false );
558 $params = self::getImageLinkMTOParams( $fp, $query ) + $params;
559
560 $s = $thumb->toHtml( $params );
561 }
562 if ( $fp['align'] != '' ) {
563 $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
564 }
565 return str_replace( "\n", ' ', $prefix . $s . $postfix );
566 }
567
568 /**
569 * Get the link parameters for MediaTransformOutput::toHtml() from given
570 * frame parameters supplied by the Parser.
571 * @param $frameParams The frame parameters
572 * @param $query An optional query string to add to description page links
573 */
574 static function getImageLinkMTOParams( $frameParams, $query = '' ) {
575 $mtoParams = array();
576 if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
577 $mtoParams['custom-url-link'] = $frameParams['link-url'];
578 if ( isset( $frameParams['link-target'] ) ) {
579 $mtoParams['custom-target-link'] = $frameParams['link-target'];
580 }
581 } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
582 $mtoParams['custom-title-link'] = self::normaliseSpecialPage( $frameParams['link-title'] );
583 } elseif ( !empty( $frameParams['no-link'] ) ) {
584 // No link
585 } else {
586 $mtoParams['desc-link'] = true;
587 $mtoParams['desc-query'] = $query;
588 }
589 return $mtoParams;
590 }
591
592 /**
593 * Make HTML for a thumbnail including image, border and caption
594 * @param $title Title object
595 * @param $file File object or false if it doesn't exist
596 * @param $label String
597 * @param $alt String
598 * @param $align String
599 * @param $params Array
600 * @param $framed Boolean
601 * @param $manualthumb String
602 */
603 static function makeThumbLinkObj( Title $title, $file, $label = '', $alt,
604 $align = 'right', $params = array(), $framed = false , $manualthumb = "" )
605 {
606 $frameParams = array(
607 'alt' => $alt,
608 'caption' => $label,
609 'align' => $align
610 );
611 if ( $framed ) {
612 $frameParams['framed'] = true;
613 }
614 if ( $manualthumb ) {
615 $frameParams['manualthumb'] = $manualthumb;
616 }
617 return self::makeThumbLink2( $title, $file, $frameParams, $params );
618 }
619
620 /**
621 * @param $title Title
622 * @param $file File
623 * @param array $frameParams
624 * @param array $handlerParams
625 * @param bool $time
626 * @param string $query
627 * @return mixed
628 */
629 static function makeThumbLink2( Title $title, $file, $frameParams = array(),
630 $handlerParams = array(), $time = false, $query = "" )
631 {
632 global $wgStylePath;
633 $exists = $file && $file->exists();
634
635 # Shortcuts
636 $fp =& $frameParams;
637 $hp =& $handlerParams;
638
639 $page = isset( $hp['page'] ) ? $hp['page'] : false;
640 if ( !isset( $fp['align'] ) ) $fp['align'] = 'right';
641 if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
642 if ( !isset( $fp['title'] ) ) $fp['title'] = '';
643 if ( !isset( $fp['caption'] ) ) $fp['caption'] = '';
644
645 if ( empty( $hp['width'] ) ) {
646 // Reduce width for upright images when parameter 'upright' is used
647 $hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
648 }
649 $thumb = false;
650
651 if ( !$exists ) {
652 $outerWidth = $hp['width'] + 2;
653 } else {
654 if ( isset( $fp['manualthumb'] ) ) {
655 # Use manually specified thumbnail
656 $manual_title = Title::makeTitleSafe( NS_FILE, $fp['manualthumb'] );
657 if ( $manual_title ) {
658 $manual_img = wfFindFile( $manual_title );
659 if ( $manual_img ) {
660 $thumb = $manual_img->getUnscaledThumb( $hp );
661 } else {
662 $exists = false;
663 }
664 }
665 } elseif ( isset( $fp['framed'] ) ) {
666 // Use image dimensions, don't scale
667 $thumb = $file->getUnscaledThumb( $hp );
668 } else {
669 # Do not present an image bigger than the source, for bitmap-style images
670 # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
671 $srcWidth = $file->getWidth( $page );
672 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
673 $hp['width'] = $srcWidth;
674 }
675 $thumb = $file->transform( $hp );
676 }
677
678 if ( $thumb ) {
679 $outerWidth = $thumb->getWidth() + 2;
680 } else {
681 $outerWidth = $hp['width'] + 2;
682 }
683 }
684
685 # ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
686 # So we don't need to pass it here in $query. However, the URL for the
687 # zoom icon still needs it, so we make a unique query for it. See bug 14771
688 $url = $title->getLocalURL( $query );
689 if ( $page ) {
690 $url = wfAppendQuery( $url, 'page=' . urlencode( $page ) );
691 }
692
693 $s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
694 if ( !$exists ) {
695 $s .= self::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
696 $zoomIcon = '';
697 } elseif ( !$thumb ) {
698 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
699 $zoomIcon = '';
700 } else {
701 $params = array(
702 'alt' => $fp['alt'],
703 'title' => $fp['title'],
704 'img-class' => 'thumbimage' );
705 $params = self::getImageLinkMTOParams( $fp, $query ) + $params;
706 $s .= $thumb->toHtml( $params );
707 if ( isset( $fp['framed'] ) ) {
708 $zoomIcon = "";
709 } else {
710 $zoomIcon = '<div class="magnify">' .
711 '<a href="' . htmlspecialchars( $url ) . '" class="internal" ' .
712 'title="' . htmlspecialchars( wfMsg( 'thumbnail-more' ) ) . '">' .
713 '<img src="' . htmlspecialchars( $wgStylePath ) . '/common/images/magnify-clip.png" ' .
714 'width="15" height="11" alt="" /></a></div>';
715 }
716 }
717 $s .= ' <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
718 return str_replace( "\n", ' ', $s );
719 }
720
721 /**
722 * Make a "broken" link to an image
723 *
724 * @param $title Title object
725 * @param $text String: link label in unescaped text form
726 * @param $query String: query string
727 * @param $trail String: link trail (HTML fragment)
728 * @param $prefix String: link prefix (HTML fragment)
729 * @param $time Boolean: a file of a certain timestamp was requested
730 * @return String
731 */
732 public static function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '', $time = false ) {
733 global $wgEnableUploads, $wgUploadMissingFileUrl;
734 if ( $title instanceof Title ) {
735 wfProfileIn( __METHOD__ );
736 $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
737
738 list( $inside, $trail ) = self::splitTrail( $trail );
739 if ( $text == '' )
740 $text = htmlspecialchars( $title->getPrefixedText() );
741
742 if ( ( $wgUploadMissingFileUrl || $wgEnableUploads ) && !$currentExists ) {
743 $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
744
745 if ( $redir ) {
746 wfProfileOut( __METHOD__ );
747 return self::linkKnown( $title, "$prefix$text$inside", array(), $query ) . $trail;
748 }
749
750 $href = self::getUploadUrl( $title, $query );
751
752 wfProfileOut( __METHOD__ );
753 return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
754 htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES ) . '">' .
755 "$prefix$text$inside</a>$trail";
756 } else {
757 wfProfileOut( __METHOD__ );
758 return self::linkKnown( $title, "$prefix$text$inside", array(), $query ) . $trail;
759 }
760 } else {
761 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
762 }
763 }
764
765 /**
766 * Get the URL to upload a certain file
767 *
768 * @param $destFile Title object of the file to upload
769 * @param $query String: urlencoded query string to prepend
770 * @return String: urlencoded URL
771 */
772 protected static function getUploadUrl( $destFile, $query = '' ) {
773 global $wgUploadMissingFileUrl;
774 $q = 'wpDestFile=' . $destFile->getPartialUrl();
775 if ( $query != '' )
776 $q .= '&' . $query;
777
778 if ( $wgUploadMissingFileUrl ) {
779 return wfAppendQuery( $wgUploadMissingFileUrl, $q );
780 } else {
781 $upload = SpecialPage::getTitleFor( 'Upload' );
782 return $upload->getLocalUrl( $q );
783 }
784 }
785
786 /**
787 * Create a direct link to a given uploaded file.
788 *
789 * @param $title Title object.
790 * @param $text String: pre-sanitized HTML
791 * @param $time string: MW timestamp of file creation time
792 * @return String: HTML
793 */
794 public static function makeMediaLinkObj( $title, $text = '', $time = false ) {
795 $img = wfFindFile( $title, array( 'time' => $time ) );
796 return self::makeMediaLinkFile( $title, $img, $text );
797 }
798
799 /**
800 * Create a direct link to a given uploaded file.
801 * This will make a broken link if $file is false.
802 *
803 * @param $title Title object.
804 * @param $file mixed File object or false
805 * @param $text String: pre-sanitized HTML
806 * @return String: HTML
807 *
808 * @todo Handle invalid or missing images better.
809 */
810 public static function makeMediaLinkFile( Title $title, $file, $text = '' ) {
811 if ( $file && $file->exists() ) {
812 $url = $file->getURL();
813 $class = 'internal';
814 } else {
815 $url = self::getUploadUrl( $title );
816 $class = 'new';
817 }
818 $alt = htmlspecialchars( $title->getText(), ENT_QUOTES );
819 if ( $text == '' ) {
820 $text = $alt;
821 }
822 $u = htmlspecialchars( $url );
823 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
824 }
825
826 /**
827 * Make a link to a special page given its name and, optionally,
828 * a message key from the link text.
829 * Usage example: $skin->specialLink( 'recentchanges' )
830 */
831 static function specialLink( $name, $key = '' ) {
832 if ( $key == '' ) { $key = strtolower( $name ); }
833
834 return self::linkKnown( SpecialPage::getTitleFor( $name ) , wfMsg( $key ) );
835 }
836
837 /**
838 * Make an external link
839 * @param $url String: URL to link to
840 * @param $text String: text of link
841 * @param $escape Boolean: do we escape the link text?
842 * @param $linktype String: type of external link. Gets added to the classes
843 * @param $attribs Array of extra attributes to <a>
844 */
845 static function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
846 $class = "external";
847 if ( isset($linktype) && $linktype ) {
848 $class .= " $linktype";
849 }
850 if ( isset($attribs['class']) && $attribs['class'] ) {
851 $class .= " {$attribs['class']}";
852 }
853 $attribs['class'] = $class;
854
855 if ( $escape ) {
856 $text = htmlspecialchars( $text );
857 }
858 $link = '';
859 $success = wfRunHooks( 'LinkerMakeExternalLink',
860 array( &$url, &$text, &$link, &$attribs, $linktype ) );
861 if ( !$success ) {
862 wfDebug( "Hook LinkerMakeExternalLink changed the output of link with url {$url} and text {$text} to {$link}\n", true );
863 return $link;
864 }
865 $attribs['href'] = $url;
866 return Html::rawElement( 'a', $attribs, $text );
867 }
868
869 /**
870 * Make user link (or user contributions for unregistered users)
871 * @param $userId Integer: user id in database.
872 * @param $userText String: user name in database
873 * @return String: HTML fragment
874 * @private
875 */
876 static function userLink( $userId, $userText ) {
877 if ( $userId == 0 ) {
878 $page = SpecialPage::getTitleFor( 'Contributions', $userText );
879 } else {
880 $page = Title::makeTitle( NS_USER, $userText );
881 }
882 return self::link( $page, htmlspecialchars( $userText ), array( 'class' => 'mw-userlink' ) );
883 }
884
885 /**
886 * Generate standard user tool links (talk, contributions, block link, etc.)
887 *
888 * @param $userId Integer: user identifier
889 * @param $userText String: user name or IP address
890 * @param $redContribsWhenNoEdits Boolean: should the contributions link be
891 * red if the user has no edits?
892 * @param $flags Integer: customisation flags (e.g. Linker::TOOL_LINKS_NOBLOCK)
893 * @param $edits Integer: user edit count (optional, for performance)
894 * @return String: HTML fragment
895 */
896 public static function userToolLinks(
897 $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits = null
898 ) {
899 global $wgUser, $wgDisableAnonTalk, $wgLang;
900 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
901 $blockable = !$flags & self::TOOL_LINKS_NOBLOCK;
902
903 $items = array();
904 if ( $talkable ) {
905 $items[] = self::userTalkLink( $userId, $userText );
906 }
907 if ( $userId ) {
908 // check if the user has an edit
909 $attribs = array();
910 if ( $redContribsWhenNoEdits ) {
911 $count = !is_null( $edits ) ? $edits : User::edits( $userId );
912 if ( $count == 0 ) {
913 $attribs['class'] = 'new';
914 }
915 }
916 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
917
918 $items[] = self::link( $contribsPage, wfMsgHtml( 'contribslink' ), $attribs );
919 }
920 if ( $blockable && $wgUser->isAllowed( 'block' ) ) {
921 $items[] = self::blockLink( $userId, $userText );
922 }
923
924 if ( $items ) {
925 return ' <span class="mw-usertoollinks">(' . $wgLang->pipeList( $items ) . ')</span>';
926 } else {
927 return '';
928 }
929 }
930
931 /**
932 * Alias for userToolLinks( $userId, $userText, true );
933 * @param $userId Integer: user identifier
934 * @param $userText String: user name or IP address
935 * @param $edits Integer: user edit count (optional, for performance)
936 */
937 public static function userToolLinksRedContribs( $userId, $userText, $edits = null ) {
938 return self::userToolLinks( $userId, $userText, true, 0, $edits );
939 }
940
941
942 /**
943 * @param $userId Integer: user id in database.
944 * @param $userText String: user name in database.
945 * @return String: HTML fragment with user talk link
946 * @private
947 */
948 static function userTalkLink( $userId, $userText ) {
949 $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
950 $userTalkLink = self::link( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
951 return $userTalkLink;
952 }
953
954 /**
955 * @param $userId Integer: userid
956 * @param $userText String: user name in database.
957 * @return String: HTML fragment with block link
958 * @private
959 */
960 static function blockLink( $userId, $userText ) {
961 $blockPage = SpecialPage::getTitleFor( 'Block', $userText );
962 $blockLink = self::link( $blockPage, wfMsgHtml( 'blocklink' ) );
963 return $blockLink;
964 }
965
966 /**
967 * Generate a user link if the current user is allowed to view it
968 * @param $rev Revision object.
969 * @param $isPublic Boolean: show only if all users can see it
970 * @return String: HTML fragment
971 */
972 static function revUserLink( $rev, $isPublic = false ) {
973 if ( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
974 $link = wfMsgHtml( 'rev-deleted-user' );
975 } else if ( $rev->userCan( Revision::DELETED_USER ) ) {
976 $link = self::userLink( $rev->getUser( Revision::FOR_THIS_USER ),
977 $rev->getUserText( Revision::FOR_THIS_USER ) );
978 } else {
979 $link = wfMsgHtml( 'rev-deleted-user' );
980 }
981 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
982 return '<span class="history-deleted">' . $link . '</span>';
983 }
984 return $link;
985 }
986
987 /**
988 * Generate a user tool link cluster if the current user is allowed to view it
989 * @param $rev Revision object.
990 * @param $isPublic Boolean: show only if all users can see it
991 * @return string HTML
992 */
993 static function revUserTools( $rev, $isPublic = false ) {
994 if ( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
995 $link = wfMsgHtml( 'rev-deleted-user' );
996 } else if ( $rev->userCan( Revision::DELETED_USER ) ) {
997 $userId = $rev->getUser( Revision::FOR_THIS_USER );
998 $userText = $rev->getUserText( Revision::FOR_THIS_USER );
999 $link = self::userLink( $userId, $userText ) .
1000 ' ' . self::userToolLinks( $userId, $userText );
1001 } else {
1002 $link = wfMsgHtml( 'rev-deleted-user' );
1003 }
1004 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
1005 return ' <span class="history-deleted">' . $link . '</span>';
1006 }
1007 return $link;
1008 }
1009
1010 /**
1011 * This function is called by all recent changes variants, by the page history,
1012 * and by the user contributions list. It is responsible for formatting edit
1013 * comments. It escapes any HTML in the comment, but adds some CSS to format
1014 * auto-generated comments (from section editing) and formats [[wikilinks]].
1015 *
1016 * @author Erik Moeller <moeller@scireview.de>
1017 *
1018 * Note: there's not always a title to pass to this function.
1019 * Since you can't set a default parameter for a reference, I've turned it
1020 * temporarily to a value pass. Should be adjusted further. --brion
1021 *
1022 * @param $comment String
1023 * @param $title Mixed: Title object (to generate link to the section in autocomment) or null
1024 * @param $local Boolean: whether section links should refer to local page
1025 */
1026 static function formatComment( $comment, $title = null, $local = false ) {
1027 wfProfileIn( __METHOD__ );
1028
1029 # Sanitize text a bit:
1030 $comment = str_replace( "\n", " ", $comment );
1031 # Allow HTML entities (for bug 13815)
1032 $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
1033
1034 # Render autocomments and make links:
1035 $comment = self::formatAutocomments( $comment, $title, $local );
1036 $comment = self::formatLinksInComment( $comment, $title, $local );
1037
1038 wfProfileOut( __METHOD__ );
1039 return $comment;
1040 }
1041
1042 static $autocommentTitle;
1043 static $autocommentLocal;
1044
1045 /**
1046 * The pattern for autogen comments is / * foo * /, which makes for
1047 * some nasty regex.
1048 * We look for all comments, match any text before and after the comment,
1049 * add a separator where needed and format the comment itself with CSS
1050 * Called by Linker::formatComment.
1051 *
1052 * @param $comment String: comment text
1053 * @param $title An optional title object used to links to sections
1054 * @param $local Boolean: whether section links should refer to local page
1055 * @return String: formatted comment
1056 */
1057 private static function formatAutocomments( $comment, $title = null, $local = false ) {
1058 // Bah!
1059 self::$autocommentTitle = $title;
1060 self::$autocommentLocal = $local;
1061 $comment = preg_replace_callback(
1062 '!(.*)/\*\s*(.*?)\s*\*/(.*)!',
1063 array( 'Linker', 'formatAutocommentsCallback' ),
1064 $comment );
1065 self::$autocommentTitle = null;
1066 self::$autocommentLocal = null;
1067 return $comment;
1068 }
1069
1070 private static function formatAutocommentsCallback( $match ) {
1071 $title = self::$autocommentTitle;
1072 $local = self::$autocommentLocal;
1073
1074 $pre = $match[1];
1075 $auto = $match[2];
1076 $post = $match[3];
1077 $link = '';
1078 if ( $title ) {
1079 $section = $auto;
1080
1081 # Remove links that a user may have manually put in the autosummary
1082 # This could be improved by copying as much of Parser::stripSectionName as desired.
1083 $section = str_replace( '[[:', '', $section );
1084 $section = str_replace( '[[', '', $section );
1085 $section = str_replace( ']]', '', $section );
1086
1087 $section = Sanitizer::normalizeSectionNameWhitespace( $section ); # bug 22784
1088 if ( $local ) {
1089 $sectionTitle = Title::newFromText( '#' . $section );
1090 } else {
1091 $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
1092 $title->getDBkey(), $section );
1093 }
1094 if ( $sectionTitle ) {
1095 $link = self::link( $sectionTitle,
1096 htmlspecialchars( wfMsgForContent( 'sectionlink' ) ), array(), array(),
1097 'noclasses' );
1098 } else {
1099 $link = '';
1100 }
1101 }
1102 $auto = "$link$auto";
1103 if ( $pre ) {
1104 # written summary $presep autocomment (summary /* section */)
1105 $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
1106 }
1107 if ( $post ) {
1108 # autocomment $postsep written summary (/* section */ summary)
1109 $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
1110 }
1111 $auto = '<span class="autocomment">' . $auto . '</span>';
1112 $comment = $pre . $auto . $post;
1113 return $comment;
1114 }
1115
1116 static $commentContextTitle;
1117 static $commentLocal;
1118
1119 /**
1120 * Formats wiki links and media links in text; all other wiki formatting
1121 * is ignored
1122 *
1123 * @todo Fixme: doesn't handle sub-links as in image thumb texts like the main parser
1124 * @param $comment String: text to format links in
1125 * @param $title An optional title object used to links to sections
1126 * @param $local Boolean: whether section links should refer to local page
1127 * @return String
1128 */
1129 public static function formatLinksInComment( $comment, $title = null, $local = false ) {
1130 self::$commentContextTitle = $title;
1131 self::$commentLocal = $local;
1132 $html = preg_replace_callback(
1133 '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
1134 array( 'Linker', 'formatLinksInCommentCallback' ),
1135 $comment );
1136 self::$commentContextTitle = null;
1137 self::$commentLocal = null;
1138 return $html;
1139 }
1140
1141 protected static function formatLinksInCommentCallback( $match ) {
1142 global $wgContLang;
1143
1144 $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
1145 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
1146
1147 $comment = $match[0];
1148
1149 # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
1150 if ( strpos( $match[1], '%' ) !== false ) {
1151 $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), rawurldecode( $match[1] ) );
1152 }
1153
1154 # Handle link renaming [[foo|text]] will show link as "text"
1155 if ( $match[3] != "" ) {
1156 $text = $match[3];
1157 } else {
1158 $text = $match[1];
1159 }
1160 $submatch = array();
1161 $thelink = null;
1162 if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1163 # Media link; trail not supported.
1164 $linkRegexp = '/\[\[(.*?)\]\]/';
1165 $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
1166 $thelink = self::makeMediaLinkObj( $title, $text );
1167 } else {
1168 # Other kind of link
1169 if ( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
1170 $trail = $submatch[1];
1171 } else {
1172 $trail = "";
1173 }
1174 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1175 if ( isset( $match[1][0] ) && $match[1][0] == ':' )
1176 $match[1] = substr( $match[1], 1 );
1177 list( $inside, $trail ) = self::splitTrail( $trail );
1178
1179 $linkText = $text;
1180 $linkTarget = self::normalizeSubpageLink( self::$commentContextTitle,
1181 $match[1], $linkText );
1182
1183 $target = Title::newFromText( $linkTarget );
1184 if ( $target ) {
1185 if ( $target->getText() == '' && $target->getInterwiki() === ''
1186 && !self::$commentLocal && self::$commentContextTitle )
1187 {
1188 $newTarget = clone ( self::$commentContextTitle );
1189 $newTarget->setFragment( '#' . $target->getFragment() );
1190 $target = $newTarget;
1191 }
1192 $thelink = self::link(
1193 $target,
1194 $linkText . $inside
1195 ) . $trail;
1196 }
1197 }
1198 if ( $thelink ) {
1199 // If the link is still valid, go ahead and replace it in!
1200 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
1201 }
1202
1203 return $comment;
1204 }
1205
1206 /**
1207 * @param $contextTitle Title
1208 * @param $target
1209 * @param $text
1210 * @return string
1211 */
1212 static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
1213 # Valid link forms:
1214 # Foobar -- normal
1215 # :Foobar -- override special treatment of prefix (images, language links)
1216 # /Foobar -- convert to CurrentPage/Foobar
1217 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1218 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1219 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1220
1221 wfProfileIn( __METHOD__ );
1222 $ret = $target; # default return value is no change
1223
1224 # Some namespaces don't allow subpages,
1225 # so only perform processing if subpages are allowed
1226 if ( $contextTitle && MWNamespace::hasSubpages( $contextTitle->getNamespace() ) ) {
1227 $hash = strpos( $target, '#' );
1228 if ( $hash !== false ) {
1229 $suffix = substr( $target, $hash );
1230 $target = substr( $target, 0, $hash );
1231 } else {
1232 $suffix = '';
1233 }
1234 # bug 7425
1235 $target = trim( $target );
1236 # Look at the first character
1237 if ( $target != '' && $target { 0 } === '/' ) {
1238 # / at end means we don't want the slash to be shown
1239 $m = array();
1240 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1241 if ( $trailingSlashes ) {
1242 $noslash = $target = substr( $target, 1, -strlen( $m[0][0] ) );
1243 } else {
1244 $noslash = substr( $target, 1 );
1245 }
1246
1247 $ret = $contextTitle->getPrefixedText() . '/' . trim( $noslash ) . $suffix;
1248 if ( $text === '' ) {
1249 $text = $target . $suffix;
1250 } # this might be changed for ugliness reasons
1251 } else {
1252 # check for .. subpage backlinks
1253 $dotdotcount = 0;
1254 $nodotdot = $target;
1255 while ( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1256 ++$dotdotcount;
1257 $nodotdot = substr( $nodotdot, 3 );
1258 }
1259 if ( $dotdotcount > 0 ) {
1260 $exploded = explode( '/', $contextTitle->GetPrefixedText() );
1261 if ( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1262 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1263 # / at the end means don't show full path
1264 if ( substr( $nodotdot, -1, 1 ) === '/' ) {
1265 $nodotdot = substr( $nodotdot, 0, -1 );
1266 if ( $text === '' ) {
1267 $text = $nodotdot . $suffix;
1268 }
1269 }
1270 $nodotdot = trim( $nodotdot );
1271 if ( $nodotdot != '' ) {
1272 $ret .= '/' . $nodotdot;
1273 }
1274 $ret .= $suffix;
1275 }
1276 }
1277 }
1278 }
1279
1280 wfProfileOut( __METHOD__ );
1281 return $ret;
1282 }
1283
1284 /**
1285 * Wrap a comment in standard punctuation and formatting if
1286 * it's non-empty, otherwise return empty string.
1287 *
1288 * @param $comment String
1289 * @param $title Mixed: Title object (to generate link to section in autocomment) or null
1290 * @param $local Boolean: whether section links should refer to local page
1291 * @param $embraced Boolean: whether the formatted comment should be embraced with ()
1292 * @return string
1293 */
1294 static function commentBlock( $comment, $title = null, $local = false, $embraced = true ) {
1295 // '*' used to be the comment inserted by the software way back
1296 // in antiquity in case none was provided, here for backwards
1297 // compatability, acc. to brion -ævar
1298 if ( $comment == '' || $comment == '*' ) {
1299 return '';
1300 } else {
1301 $formatted = self::formatComment( $comment, $title, $local );
1302 if ( $embraced ) {
1303 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
1304 }
1305 return Html::rawElement( 'span', array( 'class' => 'comment' ), $formatted );
1306 }
1307 }
1308
1309 /**
1310 * Wrap and format the given revision's comment block, if the current
1311 * user is allowed to view it.
1312 *
1313 * @param $rev Revision object
1314 * @param $local Boolean: whether section links should refer to local page
1315 * @param $isPublic Boolean: show only if all users can see it
1316 * @return String: HTML fragment
1317 */
1318 static function revComment( Revision $rev, $local = false, $isPublic = false ) {
1319 if ( $rev->getRawComment() == "" ) {
1320 return "";
1321 }
1322 if ( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic ) {
1323 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1324 } else if ( $rev->userCan( Revision::DELETED_COMMENT ) ) {
1325 $block = self::commentBlock( $rev->getComment( Revision::FOR_THIS_USER ),
1326 $rev->getTitle(), $local );
1327 } else {
1328 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1329 }
1330 if ( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
1331 return " <span class=\"history-deleted\">$block</span>";
1332 }
1333 return $block;
1334 }
1335
1336 public static function formatRevisionSize( $size ) {
1337 if ( $size == 0 ) {
1338 $stxt = wfMsgExt( 'historyempty', 'parsemag' );
1339 } else {
1340 global $wgLang;
1341 $stxt = wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $size ) );
1342 $stxt = "($stxt)";
1343 }
1344 $stxt = htmlspecialchars( $stxt );
1345 return "<span class=\"history-size\">$stxt</span>";
1346 }
1347
1348 /**
1349 * Add another level to the Table of Contents
1350 */
1351 static function tocIndent() {
1352 return "\n<ul>";
1353 }
1354
1355 /**
1356 * Finish one or more sublevels on the Table of Contents
1357 */
1358 static function tocUnindent( $level ) {
1359 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level > 0 ? $level : 0 );
1360 }
1361
1362 /**
1363 * parameter level defines if we are on an indentation level
1364 */
1365 static function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
1366 $classes = "toclevel-$level";
1367 if ( $sectionIndex !== false )
1368 $classes .= " tocsection-$sectionIndex";
1369 return "\n<li class=\"$classes\"><a href=\"#" .
1370 $anchor . '"><span class="tocnumber">' .
1371 $tocnumber . '</span> <span class="toctext">' .
1372 $tocline . '</span></a>';
1373 }
1374
1375 /**
1376 * End a Table Of Contents line.
1377 * tocUnindent() will be used instead if we're ending a line below
1378 * the new level.
1379 */
1380 static function tocLineEnd() {
1381 return "</li>\n";
1382 }
1383
1384 /**
1385 * Wraps the TOC in a table and provides the hide/collapse javascript.
1386 *
1387 * @param $toc String: html of the Table Of Contents
1388 * @param $lang mixed: Language code for the toc title
1389 * @return String: full html of the TOC
1390 */
1391 static function tocList( $toc, $lang = false ) {
1392 $title = wfMsgExt( 'toc', array( 'language' => $lang, 'escape' ) );
1393 return
1394 '<table id="toc" class="toc"><tr><td>'
1395 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1396 . $toc
1397 . "</ul>\n</td></tr></table>\n";
1398 }
1399
1400 /**
1401 * Generate a table of contents from a section tree
1402 * Currently unused.
1403 *
1404 * @param $tree Return value of ParserOutput::getSections()
1405 * @return String: HTML fragment
1406 */
1407 public static function generateTOC( $tree ) {
1408 $toc = '';
1409 $lastLevel = 0;
1410 foreach ( $tree as $section ) {
1411 if ( $section['toclevel'] > $lastLevel )
1412 $toc .= self::tocIndent();
1413 else if ( $section['toclevel'] < $lastLevel )
1414 $toc .= self::tocUnindent(
1415 $lastLevel - $section['toclevel'] );
1416 else
1417 $toc .= self::tocLineEnd();
1418
1419 $toc .= self::tocLine( $section['anchor'],
1420 $section['line'], $section['number'],
1421 $section['toclevel'], $section['index'] );
1422 $lastLevel = $section['toclevel'];
1423 }
1424 $toc .= self::tocLineEnd();
1425 return self::tocList( $toc );
1426 }
1427
1428 /**
1429 * Create a headline for content
1430 *
1431 * @param $level Integer: the level of the headline (1-6)
1432 * @param $attribs String: any attributes for the headline, starting with
1433 * a space and ending with '>'
1434 * This *must* be at least '>' for no attribs
1435 * @param $anchor String: the anchor to give the headline (the bit after the #)
1436 * @param $text String: the text of the header
1437 * @param $link String: HTML to add for the section edit link
1438 * @param $legacyAnchor Mixed: a second, optional anchor to give for
1439 * backward compatibility (false to omit)
1440 *
1441 * @return String: HTML headline
1442 */
1443 public static function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
1444 $ret = "<h$level$attribs"
1445 . $link
1446 . " <span class=\"mw-headline\" id=\"$anchor\">$text</span>"
1447 . "</h$level>";
1448 if ( $legacyAnchor !== false ) {
1449 $ret = "<div id=\"$legacyAnchor\"></div>$ret";
1450 }
1451 return $ret;
1452 }
1453
1454 /**
1455 * Split a link trail, return the "inside" portion and the remainder of the trail
1456 * as a two-element array
1457 */
1458 static function splitTrail( $trail ) {
1459 global $wgContLang;
1460 $regex = $wgContLang->linkTrail();
1461 $inside = '';
1462 if ( $trail !== '' ) {
1463 $m = array();
1464 if ( preg_match( $regex, $trail, $m ) ) {
1465 $inside = $m[1];
1466 $trail = $m[2];
1467 }
1468 }
1469 return array( $inside, $trail );
1470 }
1471
1472 /**
1473 * Generate a rollback link for a given revision. Currently it's the
1474 * caller's responsibility to ensure that the revision is the top one. If
1475 * it's not, of course, the user will get an error message.
1476 *
1477 * If the calling page is called with the parameter &bot=1, all rollback
1478 * links also get that parameter. It causes the edit itself and the rollback
1479 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1480 * changes, so this allows sysops to combat a busy vandal without bothering
1481 * other users.
1482 *
1483 * @param $rev Revision object
1484 */
1485 static function generateRollback( $rev ) {
1486 return '<span class="mw-rollback-link">['
1487 . self::buildRollbackLink( $rev )
1488 . ']</span>';
1489 }
1490
1491 /**
1492 * Build a raw rollback link, useful for collections of "tool" links
1493 *
1494 * @param $rev Revision object
1495 * @return String: HTML fragment
1496 */
1497 public static function buildRollbackLink( $rev ) {
1498 global $wgRequest, $wgUser;
1499 $title = $rev->getTitle();
1500 $query = array(
1501 'action' => 'rollback',
1502 'from' => $rev->getUserText(),
1503 'token' => $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ),
1504 );
1505 if ( $wgRequest->getBool( 'bot' ) ) {
1506 $query['bot'] = '1';
1507 $query['hidediff'] = '1'; // bug 15999
1508 }
1509 return self::link(
1510 $title,
1511 wfMsgHtml( 'rollbacklink' ),
1512 array( 'title' => wfMsg( 'tooltip-rollback' ) ),
1513 $query,
1514 array( 'known', 'noclasses' )
1515 );
1516 }
1517
1518 /**
1519 * Returns HTML for the "templates used on this page" list.
1520 *
1521 * @param $templates Array of templates from Article::getUsedTemplate
1522 * or similar
1523 * @param $preview Boolean: whether this is for a preview
1524 * @param $section Boolean: whether this is for a section edit
1525 * @return String: HTML output
1526 */
1527 public static function formatTemplates( $templates, $preview = false, $section = false ) {
1528 wfProfileIn( __METHOD__ );
1529
1530 $outText = '';
1531 if ( count( $templates ) > 0 ) {
1532 # Do a batch existence check
1533 $batch = new LinkBatch;
1534 foreach ( $templates as $title ) {
1535 $batch->addObj( $title );
1536 }
1537 $batch->execute();
1538
1539 # Construct the HTML
1540 $outText = '<div class="mw-templatesUsedExplanation">';
1541 if ( $preview ) {
1542 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ), count( $templates ) );
1543 } elseif ( $section ) {
1544 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ), count( $templates ) );
1545 } else {
1546 $outText .= wfMsgExt( 'templatesused', array( 'parse' ), count( $templates ) );
1547 }
1548 $outText .= "</div><ul>\n";
1549
1550 usort( $templates, array( 'Title', 'compare' ) );
1551 foreach ( $templates as $titleObj ) {
1552 $r = $titleObj->getRestrictions( 'edit' );
1553 if ( in_array( 'sysop', $r ) ) {
1554 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1555 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1556 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1557 } else {
1558 $protected = '';
1559 }
1560 if ( $titleObj->quickUserCan( 'edit' ) ) {
1561 $editLink = self::link(
1562 $titleObj,
1563 wfMsg( 'editlink' ),
1564 array(),
1565 array( 'action' => 'edit' )
1566 );
1567 } else {
1568 $editLink = self::link(
1569 $titleObj,
1570 wfMsg( 'viewsourcelink' ),
1571 array(),
1572 array( 'action' => 'edit' )
1573 );
1574 }
1575 $outText .= '<li>' . self::link( $titleObj ) . ' (' . $editLink . ') ' . $protected . '</li>';
1576 }
1577 $outText .= '</ul>';
1578 }
1579 wfProfileOut( __METHOD__ );
1580 return $outText;
1581 }
1582
1583 /**
1584 * Returns HTML for the "hidden categories on this page" list.
1585 *
1586 * @param $hiddencats Array of hidden categories from Article::getHiddenCategories
1587 * or similar
1588 * @return String: HTML output
1589 */
1590 public static function formatHiddenCategories( $hiddencats ) {
1591 global $wgLang;
1592 wfProfileIn( __METHOD__ );
1593
1594 $outText = '';
1595 if ( count( $hiddencats ) > 0 ) {
1596 # Construct the HTML
1597 $outText = '<div class="mw-hiddenCategoriesExplanation">';
1598 $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
1599 $outText .= "</div><ul>\n";
1600
1601 foreach ( $hiddencats as $titleObj ) {
1602 $outText .= '<li>' . self::link( $titleObj, null, array(), array(), 'known' ) . "</li>\n"; # If it's hidden, it must exist - no need to check with a LinkBatch
1603 }
1604 $outText .= '</ul>';
1605 }
1606 wfProfileOut( __METHOD__ );
1607 return $outText;
1608 }
1609
1610 /**
1611 * Format a size in bytes for output, using an appropriate
1612 * unit (B, KB, MB or GB) according to the magnitude in question
1613 *
1614 * @param $size Size to format
1615 * @return String
1616 */
1617 public static function formatSize( $size ) {
1618 global $wgLang;
1619 return htmlspecialchars( $wgLang->formatSize( $size ) );
1620 }
1621
1622 /**
1623 * Given the id of an interface element, constructs the appropriate title
1624 * attribute from the system messages. (Note, this is usually the id but
1625 * isn't always, because sometimes the accesskey needs to go on a different
1626 * element than the id, for reverse-compatibility, etc.)
1627 *
1628 * @param $name String: id of the element, minus prefixes.
1629 * @param $options Mixed: null or the string 'withaccess' to add an access-
1630 * key hint
1631 * @return String: contents of the title attribute (which you must HTML-
1632 * escape), or false for no title attribute
1633 */
1634 public static function titleAttrib( $name, $options = null ) {
1635 wfProfileIn( __METHOD__ );
1636
1637 $message = wfMessage( "tooltip-$name" );
1638
1639 if ( !$message->exists() ) {
1640 $tooltip = false;
1641 } else {
1642 $tooltip = $message->text();
1643 # Compatibility: formerly some tooltips had [alt-.] hardcoded
1644 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1645 # Message equal to '-' means suppress it.
1646 if ( $tooltip == '-' ) {
1647 $tooltip = false;
1648 }
1649 }
1650
1651 if ( $options == 'withaccess' ) {
1652 $accesskey = self::accesskey( $name );
1653 if ( $accesskey !== false ) {
1654 if ( $tooltip === false || $tooltip === '' ) {
1655 $tooltip = "[$accesskey]";
1656 } else {
1657 $tooltip .= " [$accesskey]";
1658 }
1659 }
1660 }
1661
1662 wfProfileOut( __METHOD__ );
1663 return $tooltip;
1664 }
1665
1666 static $accesskeycache;
1667
1668 /**
1669 * Given the id of an interface element, constructs the appropriate
1670 * accesskey attribute from the system messages. (Note, this is usually
1671 * the id but isn't always, because sometimes the accesskey needs to go on
1672 * a different element than the id, for reverse-compatibility, etc.)
1673 *
1674 * @param $name String: id of the element, minus prefixes.
1675 * @return String: contents of the accesskey attribute (which you must HTML-
1676 * escape), or false for no accesskey attribute
1677 */
1678 public static function accesskey( $name ) {
1679 if ( isset( self::$accesskeycache[$name] ) ) {
1680 return self::$accesskeycache[$name];
1681 }
1682 wfProfileIn( __METHOD__ );
1683
1684 $message = wfMessage( "accesskey-$name" );
1685
1686 if ( !$message->exists() ) {
1687 $accesskey = false;
1688 } else {
1689 $accesskey = $message->plain();
1690 if ( $accesskey === '' || $accesskey === '-' ) {
1691 # FIXME: Per standard MW behavior, a value of '-' means to suppress the
1692 # attribute, but this is broken for accesskey: that might be a useful
1693 # value.
1694 $accesskey = false;
1695 }
1696 }
1697
1698 wfProfileOut( __METHOD__ );
1699 return self::$accesskeycache[$name] = $accesskey;
1700 }
1701
1702 /**
1703 * Creates a (show/hide) link for deleting revisions/log entries
1704 *
1705 * @param $query Array: query parameters to be passed to link()
1706 * @param $restricted Boolean: set to true to use a <strong> instead of a <span>
1707 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1708 *
1709 * @return String: HTML <a> link to Special:Revisiondelete, wrapped in a
1710 * span to allow for customization of appearance with CSS
1711 */
1712 public static function revDeleteLink( $query = array(), $restricted = false, $delete = true ) {
1713 $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
1714 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1715 $tag = $restricted ? 'strong' : 'span';
1716 $link = self::link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
1717 return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
1718 }
1719
1720 /**
1721 * Creates a dead (show/hide) link for deleting revisions/log entries
1722 *
1723 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1724 *
1725 * @return string HTML text wrapped in a span to allow for customization
1726 * of appearance with CSS
1727 */
1728 public static function revDeleteLinkDisabled( $delete = true ) {
1729 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1730 return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($text)" );
1731 }
1732
1733 /* Deprecated methods */
1734
1735 /**
1736 * @deprecated since 1.16 Use link()
1737 *
1738 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
1739 * it if you already have a title object handy. See makeLinkObj for further documentation.
1740 *
1741 * @param $title String: the text of the title
1742 * @param $text String: link text
1743 * @param $query String: optional query part
1744 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1745 * be included in the link text. Other characters will be appended after
1746 * the end of the link.
1747 */
1748 static function makeLink( $title, $text = '', $query = '', $trail = '' ) {
1749 wfProfileIn( __METHOD__ );
1750 $nt = Title::newFromText( $title );
1751 if ( $nt instanceof Title ) {
1752 $result = self::makeLinkObj( $nt, $text, $query, $trail );
1753 } else {
1754 wfDebug( 'Invalid title passed to self::makeLink(): "' . $title . "\"\n" );
1755 $result = $text == "" ? $title : $text;
1756 }
1757
1758 wfProfileOut( __METHOD__ );
1759 return $result;
1760 }
1761
1762 /**
1763 * @deprecated since 1.16 Use link()
1764 *
1765 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
1766 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
1767 *
1768 * @param $title String: the text of the title
1769 * @param $text String: link text
1770 * @param $query String: optional query part
1771 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1772 * be included in the link text. Other characters will be appended after
1773 * the end of the link.
1774 * @param $prefix String: Optional prefix
1775 * @param $aprops String: extra attributes to the a-element
1776 */
1777 static function makeKnownLink(
1778 $title, $text = '', $query = '', $trail = '', $prefix = '', $aprops = ''
1779 ) {
1780 $nt = Title::newFromText( $title );
1781 if ( $nt instanceof Title ) {
1782 return self::makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
1783 } else {
1784 wfDebug( 'Invalid title passed to self::makeKnownLink(): "' . $title . "\"\n" );
1785 return $text == '' ? $title : $text;
1786 }
1787 }
1788
1789 /**
1790 * @deprecated since 1.16 Use link()
1791 *
1792 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
1793 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
1794 *
1795 * @param $title String: The text of the title
1796 * @param $text String: Link text
1797 * @param $query String: Optional query part
1798 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1799 * be included in the link text. Other characters will be appended after
1800 * the end of the link.
1801 */
1802 static function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
1803 $nt = Title::newFromText( $title );
1804 if ( $nt instanceof Title ) {
1805 return self::makeBrokenLinkObj( $nt, $text, $query, $trail );
1806 } else {
1807 wfDebug( 'Invalid title passed to self::makeBrokenLink(): "' . $title . "\"\n" );
1808 return $text == '' ? $title : $text;
1809 }
1810 }
1811
1812 /**
1813 * @deprecated since 1.16 Use link()
1814 *
1815 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
1816 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
1817 *
1818 * @param $title String: the text of the title
1819 * @param $text String: link text
1820 * @param $query String: optional query part
1821 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1822 * be included in the link text. Other characters will be appended after
1823 * the end of the link.
1824 */
1825 static function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
1826 wfDeprecated( __METHOD__ );
1827 $nt = Title::newFromText( $title );
1828 if ( $nt instanceof Title ) {
1829 return self::makeStubLinkObj( $nt, $text, $query, $trail );
1830 } else {
1831 wfDebug( 'Invalid title passed to self::makeStubLink(): "' . $title . "\"\n" );
1832 return $text == '' ? $title : $text;
1833 }
1834 }
1835
1836 /**
1837 * @deprecated since 1.16 Use link()
1838 *
1839 * Make a link for a title which may or may not be in the database. If you need to
1840 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
1841 * call to this will result in a DB query.
1842 *
1843 * @param $nt Title: the title object to make the link from, e.g. from
1844 * Title::newFromText.
1845 * @param $text String: link text
1846 * @param $query String: optional query part
1847 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1848 * be included in the link text. Other characters will be appended after
1849 * the end of the link.
1850 * @param $prefix String: optional prefix. As trail, only before instead of after.
1851 */
1852 static function makeLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1853 wfProfileIn( __METHOD__ );
1854 $query = wfCgiToArray( $query );
1855 list( $inside, $trail ) = self::splitTrail( $trail );
1856 if ( $text === '' ) {
1857 $text = self::linkText( $nt );
1858 }
1859
1860 $ret = self::link( $nt, "$prefix$text$inside", array(), $query ) . $trail;
1861
1862 wfProfileOut( __METHOD__ );
1863 return $ret;
1864 }
1865
1866 /**
1867 * @deprecated since 1.16 Use link()
1868 *
1869 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
1870 * it doesn't have to do a database query. It's also valid for interwiki titles and special
1871 * pages.
1872 *
1873 * @param $title Title object of target page
1874 * @param $text String: text to replace the title
1875 * @param $query String: link target
1876 * @param $trail String: text after link
1877 * @param $prefix String: text before link text
1878 * @param $aprops String: extra attributes to the a-element
1879 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
1880 * @return the a-element
1881 */
1882 static function makeKnownLinkObj(
1883 $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = ''
1884 ) {
1885 wfProfileIn( __METHOD__ );
1886
1887 if ( $text == '' ) {
1888 $text = self::linkText( $title );
1889 }
1890 $attribs = Sanitizer::mergeAttributes(
1891 Sanitizer::decodeTagAttributes( $aprops ),
1892 Sanitizer::decodeTagAttributes( $style )
1893 );
1894 $query = wfCgiToArray( $query );
1895 list( $inside, $trail ) = self::splitTrail( $trail );
1896
1897 $ret = self::link( $title, "$prefix$text$inside", $attribs, $query,
1898 array( 'known', 'noclasses' ) ) . $trail;
1899
1900 wfProfileOut( __METHOD__ );
1901 return $ret;
1902 }
1903
1904 /**
1905 * @deprecated since 1.16 Use link()
1906 *
1907 * Make a red link to the edit page of a given title.
1908 *
1909 * @param $title Title object of the target page
1910 * @param $text String: Link text
1911 * @param $query String: Optional query part
1912 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1913 * be included in the link text. Other characters will be appended after
1914 * the end of the link.
1915 * @param $prefix String: Optional prefix
1916 */
1917 static function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
1918 wfProfileIn( __METHOD__ );
1919
1920 list( $inside, $trail ) = self::splitTrail( $trail );
1921 if ( $text === '' ) {
1922 $text = self::linkText( $title );
1923 }
1924
1925 $ret = self::link( $title, "$prefix$text$inside", array(),
1926 wfCgiToArray( $query ), 'broken' ) . $trail;
1927
1928 wfProfileOut( __METHOD__ );
1929 return $ret;
1930 }
1931
1932 /**
1933 * @deprecated since 1.16 Use link()
1934 *
1935 * Make a brown link to a short article.
1936 *
1937 * @param $nt Title object of the target page
1938 * @param $text String: link text
1939 * @param $query String: optional query part
1940 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1941 * be included in the link text. Other characters will be appended after
1942 * the end of the link.
1943 * @param $prefix String: Optional prefix
1944 */
1945 static function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1946 return self::makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
1947 }
1948
1949 /**
1950 * @deprecated since 1.16 Use link()
1951 *
1952 * Make a coloured link.
1953 *
1954 * @param $nt Title object of the target page
1955 * @param $colour Integer: colour of the link
1956 * @param $text String: link text
1957 * @param $query String: optional query part
1958 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1959 * be included in the link text. Other characters will be appended after
1960 * the end of the link.
1961 * @param $prefix String: Optional prefix
1962 */
1963 static function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
1964 if ( $colour != '' ) {
1965 $style = self::getInternalLinkAttributesObj( $nt, $text, $colour );
1966 } else {
1967 $style = '';
1968 }
1969 return self::makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
1970 }
1971
1972 /** Obsolete alias */
1973 static function makeImage( $url, $alt = '' ) {
1974 wfDeprecated( __METHOD__ );
1975 return self::makeExternalImage( $url, $alt );
1976 }
1977
1978 /**
1979 * Creates the HTML source for images
1980 * @deprecated since 1.16 use makeImageLink2
1981 *
1982 * @param $title Title object
1983 * @param $label String: label text
1984 * @param $alt String: alt text
1985 * @param $align String: horizontal alignment: none, left, center, right)
1986 * @param $handlerParams Array: parameters to be passed to the media handler
1987 * @param $framed Boolean: shows image in original size in a frame
1988 * @param $thumb Boolean: shows image as thumbnail in a frame
1989 * @param $manualthumb String: image name for the manual thumbnail
1990 * @param $valign String: vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
1991 * @param $time String: timestamp of the file, set as false for current
1992 * @return String
1993 */
1994 static function makeImageLinkObj( $title, $label, $alt, $align = '', $handlerParams = array(),
1995 $framed = false, $thumb = false, $manualthumb = '', $valign = '', $time = false )
1996 {
1997 $frameParams = array( 'alt' => $alt, 'caption' => $label );
1998 if ( $align ) {
1999 $frameParams['align'] = $align;
2000 }
2001 if ( $framed ) {
2002 $frameParams['framed'] = true;
2003 }
2004 if ( $thumb ) {
2005 $frameParams['thumbnail'] = true;
2006 }
2007 if ( $manualthumb ) {
2008 $frameParams['manualthumb'] = $manualthumb;
2009 }
2010 if ( $valign ) {
2011 $frameParams['valign'] = $valign;
2012 }
2013 $file = wfFindFile( $title, array( 'time' => $time ) );
2014 return self::makeImageLink2( $title, $file, $frameParams, $handlerParams, $time );
2015 }
2016
2017 /** @deprecated use Linker::makeMediaLinkObj() */
2018 static function makeMediaLink( $name, $unused = '', $text = '', $time = false ) {
2019 $nt = Title::makeTitleSafe( NS_FILE, $name );
2020 return self::makeMediaLinkObj( $nt, $text, $time );
2021 }
2022
2023 /**
2024 * Returns the attributes for the tooltip and access key.
2025 */
2026 public static function tooltipAndAccesskeyAttribs( $name ) {
2027 global $wgEnableTooltipsAndAccesskeys;
2028 if ( !$wgEnableTooltipsAndAccesskeys )
2029 return array();
2030 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2031 # no attribute" instead of "output '' as value for attribute", this
2032 # would be three lines.
2033 $attribs = array(
2034 'title' => self::titleAttrib( $name, 'withaccess' ),
2035 'accesskey' => self::accesskey( $name )
2036 );
2037 if ( $attribs['title'] === false ) {
2038 unset( $attribs['title'] );
2039 }
2040 if ( $attribs['accesskey'] === false ) {
2041 unset( $attribs['accesskey'] );
2042 }
2043 return $attribs;
2044 }
2045
2046 /**
2047 * @deprecated since 1.14
2048 * Returns raw bits of HTML, use titleAttrib() and accesskey()
2049 */
2050 public static function tooltipAndAccesskey( $name ) {
2051 return Xml::expandAttributes( self::tooltipAndAccesskeyAttribs( $name ) );
2052 }
2053
2054 /**
2055 * @deprecated since 1.14
2056 * Returns raw bits of HTML, use titleAttrib()
2057 */
2058 public static function tooltip( $name, $options = null ) {
2059 global $wgEnableTooltipsAndAccesskeys;
2060 if ( !$wgEnableTooltipsAndAccesskeys )
2061 return '';
2062 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2063 # no attribute" instead of "output '' as value for attribute", this
2064 # would be two lines.
2065 $tooltip = self::titleAttrib( $name, $options );
2066 if ( $tooltip === false ) {
2067 return '';
2068 }
2069 return Xml::expandAttributes( array(
2070 'title' => self::titleAttrib( $name, $options )
2071 ) );
2072 }
2073 }
2074
2075 class DummyLinker {
2076
2077 /**
2078 * Use PHP's magic __call handler to transform instance calls to a dummy instance
2079 * into static calls to the new Linker for backwards compatibility.
2080 *
2081 * @param $fname String Name of called method
2082 * @param $args Array Arguments to the method
2083 */
2084 function __call( $fname, $args ) {
2085 return call_user_func_array( array( 'Linker', $fname ), $args );
2086 }
2087
2088 }
2089