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