(Bug #27634) TOC title appears in wrong language
[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 wfProfileOut( __METHOD__ );
726 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
727 }
728 }
729
730 /**
731 * Get the URL to upload a certain file
732 *
733 * @param $destFile Title object of the file to upload
734 * @param $query String: urlencoded query string to prepend
735 * @return String: urlencoded URL
736 */
737 protected function getUploadUrl( $destFile, $query = '' ) {
738 global $wgUploadMissingFileUrl;
739 $q = 'wpDestFile=' . $destFile->getPartialUrl();
740 if ( $query != '' )
741 $q .= '&' . $query;
742
743 if ( $wgUploadMissingFileUrl ) {
744 return wfAppendQuery( $wgUploadMissingFileUrl, $q );
745 } else {
746 $upload = SpecialPage::getTitleFor( 'Upload' );
747 return $upload->getLocalUrl( $q );
748 }
749 }
750
751 /**
752 * Create a direct link to a given uploaded file.
753 *
754 * @param $title Title object.
755 * @param $text String: pre-sanitized HTML
756 * @param $time string: time image was created
757 * @return String: HTML
758 *
759 * @todo Handle invalid or missing images better.
760 */
761 public function makeMediaLinkObj( $title, $text = '', $time = false ) {
762 if ( is_null( $title ) ) {
763 # # # HOTFIX. Instead of breaking, return empty string.
764 return $text;
765 } else {
766 $img = wfFindFile( $title, array( 'time' => $time ) );
767 if ( $img ) {
768 $url = $img->getURL();
769 $class = 'internal';
770 } else {
771 $url = $this->getUploadUrl( $title );
772 $class = 'new';
773 }
774 $alt = htmlspecialchars( $title->getText(), ENT_QUOTES );
775 if ( $text == '' ) {
776 $text = $alt;
777 }
778 $u = htmlspecialchars( $url );
779 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
780 }
781 }
782
783 /**
784 * Make a link to a special page given its name and, optionally,
785 * a message key from the link text.
786 * Usage example: $skin->specialLink( 'recentchanges' )
787 */
788 function specialLink( $name, $key = '' ) {
789 if ( $key == '' ) { $key = strtolower( $name ); }
790
791 return $this->linkKnown( SpecialPage::getTitleFor( $name ) , wfMsg( $key ) );
792 }
793
794 /**
795 * Make an external link
796 * @param $url String: URL to link to
797 * @param $text String: text of link
798 * @param $escape Boolean: do we escape the link text?
799 * @param $linktype String: type of external link. Gets added to the classes
800 * @param $attribs Array of extra attributes to <a>
801 */
802 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
803 $class = "external";
804 if ( isset($linktype) && $linktype ) {
805 $class .= " $linktype";
806 }
807 if ( isset($attribs['class']) && $attribs['class'] ) {
808 $class .= " {$attribs['class']}";
809 }
810 $attribs['class'] = $class;
811
812 if ( $escape ) {
813 $text = htmlspecialchars( $text );
814 }
815 $link = '';
816 $success = wfRunHooks( 'LinkerMakeExternalLink', array( &$url, &$text, &$link, &$attribs, $linktype ) );
817 if ( !$success ) {
818 wfDebug( "Hook LinkerMakeExternalLink changed the output of link with url {$url} and text {$text} to {$link}\n", true );
819 return $link;
820 }
821 $attribs['href'] = $url;
822 return Html::rawElement( 'a', $attribs, $text );
823 }
824
825 /**
826 * Make user link (or user contributions for unregistered users)
827 * @param $userId Integer: user id in database.
828 * @param $userText String: user name in database
829 * @return String: HTML fragment
830 * @private
831 */
832 function userLink( $userId, $userText ) {
833 if ( $userId == 0 ) {
834 $page = SpecialPage::getTitleFor( 'Contributions', $userText );
835 } else {
836 $page = Title::makeTitle( NS_USER, $userText );
837 }
838 return $this->link( $page, htmlspecialchars( $userText ), array( 'class' => 'mw-userlink' ) );
839 }
840
841 /**
842 * Generate standard user tool links (talk, contributions, block link, etc.)
843 *
844 * @param $userId Integer: user identifier
845 * @param $userText String: user name or IP address
846 * @param $redContribsWhenNoEdits Boolean: should the contributions link be
847 * red if the user has no edits?
848 * @param $flags Integer: customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
849 * @param $edits Integer: user edit count (optional, for performance)
850 * @return String: HTML fragment
851 */
852 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0, $edits = null ) {
853 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans, $wgLang;
854 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
855 $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
856
857 $items = array();
858 if ( $talkable ) {
859 $items[] = $this->userTalkLink( $userId, $userText );
860 }
861 if ( $userId ) {
862 // check if the user has an edit
863 $attribs = array();
864 if ( $redContribsWhenNoEdits ) {
865 $count = !is_null( $edits ) ? $edits : User::edits( $userId );
866 if ( $count == 0 ) {
867 $attribs['class'] = 'new';
868 }
869 }
870 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
871
872 $items[] = $this->link( $contribsPage, wfMsgHtml( 'contribslink' ), $attribs );
873 }
874 if ( $blockable && $wgUser->isAllowed( 'block' ) ) {
875 $items[] = $this->blockLink( $userId, $userText );
876 }
877
878 if ( $items ) {
879 return ' <span class="mw-usertoollinks">(' . $wgLang->pipeList( $items ) . ')</span>';
880 } else {
881 return '';
882 }
883 }
884
885 /**
886 * Alias for userToolLinks( $userId, $userText, true );
887 * @param $userId Integer: user identifier
888 * @param $userText String: user name or IP address
889 * @param $edits Integer: user edit count (optional, for performance)
890 */
891 public function userToolLinksRedContribs( $userId, $userText, $edits = null ) {
892 return $this->userToolLinks( $userId, $userText, true, 0, $edits );
893 }
894
895
896 /**
897 * @param $userId Integer: user id in database.
898 * @param $userText String: user name in database.
899 * @return String: HTML fragment with user talk link
900 * @private
901 */
902 function userTalkLink( $userId, $userText ) {
903 $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
904 $userTalkLink = $this->link( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
905 return $userTalkLink;
906 }
907
908 /**
909 * @param $userId Integer: userid
910 * @param $userText String: user name in database.
911 * @return String: HTML fragment with block link
912 * @private
913 */
914 function blockLink( $userId, $userText ) {
915 $blockPage = SpecialPage::getTitleFor( 'Blockip', $userText );
916 $blockLink = $this->link( $blockPage, wfMsgHtml( 'blocklink' ) );
917 return $blockLink;
918 }
919
920 /**
921 * Generate a user link if the current user is allowed to view it
922 * @param $rev Revision object.
923 * @param $isPublic Boolean: show only if all users can see it
924 * @return String: HTML fragment
925 */
926 function revUserLink( $rev, $isPublic = false ) {
927 if ( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
928 $link = wfMsgHtml( 'rev-deleted-user' );
929 } else if ( $rev->userCan( Revision::DELETED_USER ) ) {
930 $link = $this->userLink( $rev->getUser( Revision::FOR_THIS_USER ),
931 $rev->getUserText( Revision::FOR_THIS_USER ) );
932 } else {
933 $link = wfMsgHtml( 'rev-deleted-user' );
934 }
935 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
936 return '<span class="history-deleted">' . $link . '</span>';
937 }
938 return $link;
939 }
940
941 /**
942 * Generate a user tool link cluster if the current user is allowed to view it
943 * @param $rev Revision object.
944 * @param $isPublic Boolean: show only if all users can see it
945 * @return string HTML
946 */
947 function revUserTools( $rev, $isPublic = false ) {
948 if ( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
949 $link = wfMsgHtml( 'rev-deleted-user' );
950 } else if ( $rev->userCan( Revision::DELETED_USER ) ) {
951 $userId = $rev->getUser( Revision::FOR_THIS_USER );
952 $userText = $rev->getUserText( Revision::FOR_THIS_USER );
953 $link = $this->userLink( $userId, $userText ) .
954 ' ' . $this->userToolLinks( $userId, $userText );
955 } else {
956 $link = wfMsgHtml( 'rev-deleted-user' );
957 }
958 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
959 return ' <span class="history-deleted">' . $link . '</span>';
960 }
961 return $link;
962 }
963
964 /**
965 * This function is called by all recent changes variants, by the page history,
966 * and by the user contributions list. It is responsible for formatting edit
967 * comments. It escapes any HTML in the comment, but adds some CSS to format
968 * auto-generated comments (from section editing) and formats [[wikilinks]].
969 *
970 * @author Erik Moeller <moeller@scireview.de>
971 *
972 * Note: there's not always a title to pass to this function.
973 * Since you can't set a default parameter for a reference, I've turned it
974 * temporarily to a value pass. Should be adjusted further. --brion
975 *
976 * @param $comment String
977 * @param $title Mixed: Title object (to generate link to the section in autocomment) or null
978 * @param $local Boolean: whether section links should refer to local page
979 */
980 function formatComment( $comment, $title = null, $local = false ) {
981 wfProfileIn( __METHOD__ );
982
983 # Sanitize text a bit:
984 $comment = str_replace( "\n", " ", $comment );
985 # Allow HTML entities (for bug 13815)
986 $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
987
988 # Render autocomments and make links:
989 $comment = $this->formatAutocomments( $comment, $title, $local );
990 $comment = $this->formatLinksInComment( $comment, $title, $local );
991
992 wfProfileOut( __METHOD__ );
993 return $comment;
994 }
995
996 /**
997 * The pattern for autogen comments is / * foo * /, which makes for
998 * some nasty regex.
999 * We look for all comments, match any text before and after the comment,
1000 * add a separator where needed and format the comment itself with CSS
1001 * Called by Linker::formatComment.
1002 *
1003 * @param $comment String: comment text
1004 * @param $title An optional title object used to links to sections
1005 * @param $local Boolean: whether section links should refer to local page
1006 * @return String: formatted comment
1007 */
1008 private function formatAutocomments( $comment, $title = null, $local = false ) {
1009 // Bah!
1010 $this->autocommentTitle = $title;
1011 $this->autocommentLocal = $local;
1012 $comment = preg_replace_callback(
1013 '!(.*)/\*\s*(.*?)\s*\*/(.*)!',
1014 array( $this, 'formatAutocommentsCallback' ),
1015 $comment );
1016 unset( $this->autocommentTitle );
1017 unset( $this->autocommentLocal );
1018 return $comment;
1019 }
1020
1021 private function formatAutocommentsCallback( $match ) {
1022 $title = $this->autocommentTitle;
1023 $local = $this->autocommentLocal;
1024
1025 $pre = $match[1];
1026 $auto = $match[2];
1027 $post = $match[3];
1028 $link = '';
1029 if ( $title ) {
1030 $section = $auto;
1031
1032 # Remove links that a user may have manually put in the autosummary
1033 # This could be improved by copying as much of Parser::stripSectionName as desired.
1034 $section = str_replace( '[[:', '', $section );
1035 $section = str_replace( '[[', '', $section );
1036 $section = str_replace( ']]', '', $section );
1037
1038 $section = Sanitizer::normalizeSectionNameWhitespace( $section ); # bug 22784
1039 if ( $local ) {
1040 $sectionTitle = Title::newFromText( '#' . $section );
1041 } else {
1042 $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
1043 $title->getDBkey(), $section );
1044 }
1045 if ( $sectionTitle ) {
1046 $link = $this->link( $sectionTitle,
1047 htmlspecialchars( wfMsgForContent( 'sectionlink' ) ), array(), array(),
1048 'noclasses' );
1049 } else {
1050 $link = '';
1051 }
1052 }
1053 $auto = "$link$auto";
1054 if ( $pre ) {
1055 # written summary $presep autocomment (summary /* section */)
1056 $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
1057 }
1058 if ( $post ) {
1059 # autocomment $postsep written summary (/* section */ summary)
1060 $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
1061 }
1062 $auto = '<span class="autocomment">' . $auto . '</span>';
1063 $comment = $pre . $auto . $post;
1064 return $comment;
1065 }
1066
1067 /**
1068 * Formats wiki links and media links in text; all other wiki formatting
1069 * is ignored
1070 *
1071 * @todo Fixme: doesn't handle sub-links as in image thumb texts like the main parser
1072 * @param $comment String: text to format links in
1073 * @param $title An optional title object used to links to sections
1074 * @param $local Boolean: whether section links should refer to local page
1075 * @return String
1076 */
1077 public function formatLinksInComment( $comment, $title = null, $local = false ) {
1078 $this->commentContextTitle = $title;
1079 $this->commentLocal = $local;
1080 $html = preg_replace_callback(
1081 '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
1082 array( $this, 'formatLinksInCommentCallback' ),
1083 $comment );
1084 unset( $this->commentContextTitle );
1085 unset( $this->commentLocal );
1086 return $html;
1087 }
1088
1089 protected function formatLinksInCommentCallback( $match ) {
1090 global $wgContLang;
1091
1092 $medians = '(?:' . preg_quote( MWNamespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
1093 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
1094
1095 $comment = $match[0];
1096
1097 # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
1098 if ( strpos( $match[1], '%' ) !== false ) {
1099 $match[1] = str_replace( array( '<', '>' ), array( '&lt;', '&gt;' ), rawurldecode( $match[1] ) );
1100 }
1101
1102 # Handle link renaming [[foo|text]] will show link as "text"
1103 if ( $match[3] != "" ) {
1104 $text = $match[3];
1105 } else {
1106 $text = $match[1];
1107 }
1108 $submatch = array();
1109 $thelink = null;
1110 if ( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1111 # Media link; trail not supported.
1112 $linkRegexp = '/\[\[(.*?)\]\]/';
1113 $title = Title::makeTitleSafe( NS_FILE, $submatch[1] );
1114 $thelink = $this->makeMediaLinkObj( $title, $text );
1115 } else {
1116 # Other kind of link
1117 if ( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
1118 $trail = $submatch[1];
1119 } else {
1120 $trail = "";
1121 }
1122 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1123 if ( isset( $match[1][0] ) && $match[1][0] == ':' )
1124 $match[1] = substr( $match[1], 1 );
1125 list( $inside, $trail ) = Linker::splitTrail( $trail );
1126
1127 $linkText = $text;
1128 $linkTarget = Linker::normalizeSubpageLink( $this->commentContextTitle,
1129 $match[1], $linkText );
1130
1131 $target = Title::newFromText( $linkTarget );
1132 if ( $target ) {
1133 if ( $target->getText() == '' && $target->getInterwiki() === ''
1134 && !$this->commentLocal && $this->commentContextTitle )
1135 {
1136 $newTarget = clone ( $this->commentContextTitle );
1137 $newTarget->setFragment( '#' . $target->getFragment() );
1138 $target = $newTarget;
1139 }
1140 $thelink = $this->link(
1141 $target,
1142 $linkText . $inside
1143 ) . $trail;
1144 }
1145 }
1146 if ( $thelink ) {
1147 // If the link is still valid, go ahead and replace it in!
1148 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
1149 }
1150
1151 return $comment;
1152 }
1153
1154 static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
1155 # Valid link forms:
1156 # Foobar -- normal
1157 # :Foobar -- override special treatment of prefix (images, language links)
1158 # /Foobar -- convert to CurrentPage/Foobar
1159 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1160 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1161 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1162
1163 wfProfileIn( __METHOD__ );
1164 $ret = $target; # default return value is no change
1165
1166 # Some namespaces don't allow subpages,
1167 # so only perform processing if subpages are allowed
1168 if ( $contextTitle && MWNamespace::hasSubpages( $contextTitle->getNamespace() ) ) {
1169 $hash = strpos( $target, '#' );
1170 if ( $hash !== false ) {
1171 $suffix = substr( $target, $hash );
1172 $target = substr( $target, 0, $hash );
1173 } else {
1174 $suffix = '';
1175 }
1176 # bug 7425
1177 $target = trim( $target );
1178 # Look at the first character
1179 if ( $target != '' && $target { 0 } === '/' ) {
1180 # / at end means we don't want the slash to be shown
1181 $m = array();
1182 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1183 if ( $trailingSlashes ) {
1184 $noslash = $target = substr( $target, 1, -strlen( $m[0][0] ) );
1185 } else {
1186 $noslash = substr( $target, 1 );
1187 }
1188
1189 $ret = $contextTitle->getPrefixedText() . '/' . trim( $noslash ) . $suffix;
1190 if ( $text === '' ) {
1191 $text = $target . $suffix;
1192 } # this might be changed for ugliness reasons
1193 } else {
1194 # check for .. subpage backlinks
1195 $dotdotcount = 0;
1196 $nodotdot = $target;
1197 while ( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1198 ++$dotdotcount;
1199 $nodotdot = substr( $nodotdot, 3 );
1200 }
1201 if ( $dotdotcount > 0 ) {
1202 $exploded = explode( '/', $contextTitle->GetPrefixedText() );
1203 if ( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1204 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1205 # / at the end means don't show full path
1206 if ( substr( $nodotdot, -1, 1 ) === '/' ) {
1207 $nodotdot = substr( $nodotdot, 0, -1 );
1208 if ( $text === '' ) {
1209 $text = $nodotdot . $suffix;
1210 }
1211 }
1212 $nodotdot = trim( $nodotdot );
1213 if ( $nodotdot != '' ) {
1214 $ret .= '/' . $nodotdot;
1215 }
1216 $ret .= $suffix;
1217 }
1218 }
1219 }
1220 }
1221
1222 wfProfileOut( __METHOD__ );
1223 return $ret;
1224 }
1225
1226 /**
1227 * Wrap a comment in standard punctuation and formatting if
1228 * it's non-empty, otherwise return empty string.
1229 *
1230 * @param $comment String
1231 * @param $title Mixed: Title object (to generate link to section in autocomment) or null
1232 * @param $local Boolean: whether section links should refer to local page
1233 *
1234 * @return string
1235 */
1236 function commentBlock( $comment, $title = null, $local = false ) {
1237 // '*' used to be the comment inserted by the software way back
1238 // in antiquity in case none was provided, here for backwards
1239 // compatability, acc. to brion -ævar
1240 if ( $comment == '' || $comment == '*' ) {
1241 return '';
1242 } else {
1243 $formatted = $this->formatComment( $comment, $title, $local );
1244 return " <span class=\"comment\">($formatted)</span>";
1245 }
1246 }
1247
1248 /**
1249 * Wrap and format the given revision's comment block, if the current
1250 * user is allowed to view it.
1251 *
1252 * @param $rev Revision object
1253 * @param $local Boolean: whether section links should refer to local page
1254 * @param $isPublic Boolean: show only if all users can see it
1255 * @return String: HTML fragment
1256 */
1257 function revComment( Revision $rev, $local = false, $isPublic = false ) {
1258 if ( $rev->getRawComment() == "" ) return "";
1259 if ( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic ) {
1260 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1261 } else if ( $rev->userCan( Revision::DELETED_COMMENT ) ) {
1262 $block = $this->commentBlock( $rev->getComment( Revision::FOR_THIS_USER ),
1263 $rev->getTitle(), $local );
1264 } else {
1265 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1266 }
1267 if ( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
1268 return " <span class=\"history-deleted\">$block</span>";
1269 }
1270 return $block;
1271 }
1272
1273 public function formatRevisionSize( $size ) {
1274 if ( $size == 0 ) {
1275 $stxt = wfMsgExt( 'historyempty', 'parsemag' );
1276 } else {
1277 global $wgLang;
1278 $stxt = wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $size ) );
1279 $stxt = "($stxt)";
1280 }
1281 $stxt = htmlspecialchars( $stxt );
1282 return "<span class=\"history-size\">$stxt</span>";
1283 }
1284
1285 /**
1286 * Add another level to the Table of Contents
1287 */
1288 function tocIndent() {
1289 return "\n<ul>";
1290 }
1291
1292 /**
1293 * Finish one or more sublevels on the Table of Contents
1294 */
1295 function tocUnindent( $level ) {
1296 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level > 0 ? $level : 0 );
1297 }
1298
1299 /**
1300 * parameter level defines if we are on an indentation level
1301 */
1302 function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
1303 $classes = "toclevel-$level";
1304 if ( $sectionIndex !== false )
1305 $classes .= " tocsection-$sectionIndex";
1306 return "\n<li class=\"$classes\"><a href=\"#" .
1307 $anchor . '"><span class="tocnumber">' .
1308 $tocnumber . '</span> <span class="toctext">' .
1309 $tocline . '</span></a>';
1310 }
1311
1312 /**
1313 * End a Table Of Contents line.
1314 * tocUnindent() will be used instead if we're ending a line below
1315 * the new level.
1316 */
1317 function tocLineEnd() {
1318 return "</li>\n";
1319 }
1320
1321 /**
1322 * Wraps the TOC in a table and provides the hide/collapse javascript.
1323 *
1324 * @param $toc String: html of the Table Of Contents
1325 * @param $lang mixed: Language code for the toc title
1326 * @return String: full html of the TOC
1327 */
1328 function tocList( $toc, $lang = false ) {
1329 $title = wfMsgExt( 'toc', array( 'language' => $lang, 'escape' ) );
1330 return
1331 '<table id="toc" class="toc"><tr><td>'
1332 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1333 . $toc
1334 . "</ul>\n</td></tr></table>\n";
1335 }
1336
1337 /**
1338 * Generate a table of contents from a section tree
1339 * Currently unused.
1340 *
1341 * @param $tree Return value of ParserOutput::getSections()
1342 * @return String: HTML fragment
1343 */
1344 public function generateTOC( $tree ) {
1345 $toc = '';
1346 $lastLevel = 0;
1347 foreach ( $tree as $section ) {
1348 if ( $section['toclevel'] > $lastLevel )
1349 $toc .= $this->tocIndent();
1350 else if ( $section['toclevel'] < $lastLevel )
1351 $toc .= $this->tocUnindent(
1352 $lastLevel - $section['toclevel'] );
1353 else
1354 $toc .= $this->tocLineEnd();
1355
1356 $toc .= $this->tocLine( $section['anchor'],
1357 $section['line'], $section['number'],
1358 $section['toclevel'], $section['index'] );
1359 $lastLevel = $section['toclevel'];
1360 }
1361 $toc .= $this->tocLineEnd();
1362 return $this->tocList( $toc );
1363 }
1364
1365 /**
1366 * Create a section edit link. This supersedes editSectionLink() and
1367 * editSectionLinkForOther().
1368 *
1369 * @param $nt Title The title being linked to (may not be the same as
1370 * $wgTitle, if the section is included from a template)
1371 * @param $section string The designation of the section being pointed to,
1372 * to be included in the link, like "&section=$section"
1373 * @param $tooltip string The tooltip to use for the link: will be escaped
1374 * and wrapped in the 'editsectionhint' message
1375 * @param $lang string Language code
1376 * @return string HTML to use for edit link
1377 */
1378 public function doEditSectionLink( Title $nt, $section, $tooltip = null, $lang = false ) {
1379 // HTML generated here should probably have userlangattributes
1380 // added to it for LTR text on RTL pages
1381 $attribs = array();
1382 if ( !is_null( $tooltip ) ) {
1383 # Bug 25462: undo double-escaping.
1384 $tooltip = Sanitizer::decodeCharReferences( $tooltip );
1385 $attribs['title'] = wfMsgReal( 'editsectionhint', array( $tooltip ), true, $lang );
1386 }
1387 $link = $this->link( $nt, wfMsgExt( 'editsection', array( 'language' => $lang ) ),
1388 $attribs,
1389 array( 'action' => 'edit', 'section' => $section ),
1390 array( 'noclasses', 'known' )
1391 );
1392
1393 # Run the old hook. This takes up half of the function . . . hopefully
1394 # we can rid of it someday.
1395 $attribs = '';
1396 if ( $tooltip ) {
1397 $attribs = htmlspecialchars( wfMsgReal( 'editsectionhint', array( $tooltip ), true, $lang ) );
1398 $attribs = " title=\"$attribs\"";
1399 }
1400 $result = null;
1401 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result, $lang ) );
1402 if ( !is_null( $result ) ) {
1403 # For reverse compatibility, add the brackets *after* the hook is
1404 # run, and even add them to hook-provided text. (This is the main
1405 # reason that the EditSectionLink hook is deprecated in favor of
1406 # DoEditSectionLink: it can't change the brackets or the span.)
1407 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $result );
1408 return "<span class=\"editsection\">$result</span>";
1409 }
1410
1411 # Add the brackets and the span, and *then* run the nice new hook, with
1412 # clean and non-redundant arguments.
1413 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
1414 $result = "<span class=\"editsection\">$result</span>";
1415
1416 wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result, $lang ) );
1417 return $result;
1418 }
1419
1420 /**
1421 * Create a headline for content
1422 *
1423 * @param $level Integer: the level of the headline (1-6)
1424 * @param $attribs String: any attributes for the headline, starting with
1425 * a space and ending with '>'
1426 * This *must* be at least '>' for no attribs
1427 * @param $anchor String: the anchor to give the headline (the bit after the #)
1428 * @param $text String: the text of the header
1429 * @param $link String: HTML to add for the section edit link
1430 * @param $legacyAnchor Mixed: a second, optional anchor to give for
1431 * backward compatibility (false to omit)
1432 *
1433 * @return String: HTML headline
1434 */
1435 public function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
1436 $ret = "<h$level$attribs"
1437 . $link
1438 . " <span class=\"mw-headline\" id=\"$anchor\">$text</span>"
1439 . "</h$level>";
1440 if ( $legacyAnchor !== false ) {
1441 $ret = "<div id=\"$legacyAnchor\"></div>$ret";
1442 }
1443 return $ret;
1444 }
1445
1446 /**
1447 * Split a link trail, return the "inside" portion and the remainder of the trail
1448 * as a two-element array
1449 */
1450 static function splitTrail( $trail ) {
1451 static $regex = false;
1452 if ( $regex === false ) {
1453 global $wgContLang;
1454 $regex = $wgContLang->linkTrail();
1455 }
1456 $inside = '';
1457 if ( $trail !== '' ) {
1458 $m = array();
1459 if ( preg_match( $regex, $trail, $m ) ) {
1460 $inside = $m[1];
1461 $trail = $m[2];
1462 }
1463 }
1464 return array( $inside, $trail );
1465 }
1466
1467 /**
1468 * Generate a rollback link for a given revision. Currently it's the
1469 * caller's responsibility to ensure that the revision is the top one. If
1470 * it's not, of course, the user will get an error message.
1471 *
1472 * If the calling page is called with the parameter &bot=1, all rollback
1473 * links also get that parameter. It causes the edit itself and the rollback
1474 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1475 * changes, so this allows sysops to combat a busy vandal without bothering
1476 * other users.
1477 *
1478 * @param $rev Revision object
1479 */
1480 function generateRollback( $rev ) {
1481 return '<span class="mw-rollback-link">['
1482 . $this->buildRollbackLink( $rev )
1483 . ']</span>';
1484 }
1485
1486 /**
1487 * Build a raw rollback link, useful for collections of "tool" links
1488 *
1489 * @param $rev Revision object
1490 * @return String: HTML fragment
1491 */
1492 public function buildRollbackLink( $rev ) {
1493 global $wgRequest, $wgUser;
1494 $title = $rev->getTitle();
1495 $query = array(
1496 'action' => 'rollback',
1497 'from' => $rev->getUserText(),
1498 'token' => $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ),
1499 );
1500 if ( $wgRequest->getBool( 'bot' ) ) {
1501 $query['bot'] = '1';
1502 $query['hidediff'] = '1'; // bug 15999
1503 }
1504 return $this->link(
1505 $title,
1506 wfMsgHtml( 'rollbacklink' ),
1507 array( 'title' => wfMsg( 'tooltip-rollback' ) ),
1508 $query,
1509 array( 'known', 'noclasses' )
1510 );
1511 }
1512
1513 /**
1514 * Returns HTML for the "templates used on this page" list.
1515 *
1516 * @param $templates Array of templates from Article::getUsedTemplate
1517 * or similar
1518 * @param $preview Boolean: whether this is for a preview
1519 * @param $section Boolean: whether this is for a section edit
1520 * @return String: HTML output
1521 */
1522 public function formatTemplates( $templates, $preview = false, $section = false ) {
1523 wfProfileIn( __METHOD__ );
1524
1525 $outText = '';
1526 if ( count( $templates ) > 0 ) {
1527 # Do a batch existence check
1528 $batch = new LinkBatch;
1529 foreach ( $templates as $title ) {
1530 $batch->addObj( $title );
1531 }
1532 $batch->execute();
1533
1534 # Construct the HTML
1535 $outText = '<div class="mw-templatesUsedExplanation">';
1536 if ( $preview ) {
1537 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ), count( $templates ) );
1538 } elseif ( $section ) {
1539 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ), count( $templates ) );
1540 } else {
1541 $outText .= wfMsgExt( 'templatesused', array( 'parse' ), count( $templates ) );
1542 }
1543 $outText .= "</div><ul>\n";
1544
1545 usort( $templates, array( 'Title', 'compare' ) );
1546 foreach ( $templates as $titleObj ) {
1547 $r = $titleObj->getRestrictions( 'edit' );
1548 if ( in_array( 'sysop', $r ) ) {
1549 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1550 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1551 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1552 } else {
1553 $protected = '';
1554 }
1555 if ( $titleObj->quickUserCan( 'edit' ) ) {
1556 $editLink = $this->link(
1557 $titleObj,
1558 wfMsg( 'editlink' ),
1559 array(),
1560 array( 'action' => 'edit' )
1561 );
1562 } else {
1563 $editLink = $this->link(
1564 $titleObj,
1565 wfMsg( 'viewsourcelink' ),
1566 array(),
1567 array( 'action' => 'edit' )
1568 );
1569 }
1570 $outText .= '<li>' . $this->link( $titleObj ) . ' (' . $editLink . ') ' . $protected . '</li>';
1571 }
1572 $outText .= '</ul>';
1573 }
1574 wfProfileOut( __METHOD__ );
1575 return $outText;
1576 }
1577
1578 /**
1579 * Returns HTML for the "hidden categories on this page" list.
1580 *
1581 * @param $hiddencats Array of hidden categories from Article::getHiddenCategories
1582 * or similar
1583 * @return String: HTML output
1584 */
1585 public function formatHiddenCategories( $hiddencats ) {
1586 global $wgLang;
1587 wfProfileIn( __METHOD__ );
1588
1589 $outText = '';
1590 if ( count( $hiddencats ) > 0 ) {
1591 # Construct the HTML
1592 $outText = '<div class="mw-hiddenCategoriesExplanation">';
1593 $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
1594 $outText .= "</div><ul>\n";
1595
1596 foreach ( $hiddencats as $titleObj ) {
1597 $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
1598 }
1599 $outText .= '</ul>';
1600 }
1601 wfProfileOut( __METHOD__ );
1602 return $outText;
1603 }
1604
1605 /**
1606 * Format a size in bytes for output, using an appropriate
1607 * unit (B, KB, MB or GB) according to the magnitude in question
1608 *
1609 * @param $size Size to format
1610 * @return String
1611 */
1612 public function formatSize( $size ) {
1613 global $wgLang;
1614 return htmlspecialchars( $wgLang->formatSize( $size ) );
1615 }
1616
1617 /**
1618 * Given the id of an interface element, constructs the appropriate title
1619 * attribute from the system messages. (Note, this is usually the id but
1620 * isn't always, because sometimes the accesskey needs to go on a different
1621 * element than the id, for reverse-compatibility, etc.)
1622 *
1623 * @param $name String: id of the element, minus prefixes.
1624 * @param $options Mixed: null or the string 'withaccess' to add an access-
1625 * key hint
1626 * @return String: contents of the title attribute (which you must HTML-
1627 * escape), or false for no title attribute
1628 */
1629 public function titleAttrib( $name, $options = null ) {
1630 wfProfileIn( __METHOD__ );
1631
1632 $message = wfMessage( "tooltip-$name" );
1633
1634 if ( !$message->exists() ) {
1635 $tooltip = false;
1636 } else {
1637 $tooltip = $message->text();
1638 # Compatibility: formerly some tooltips had [alt-.] hardcoded
1639 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1640 # Message equal to '-' means suppress it.
1641 if ( $tooltip == '-' ) {
1642 $tooltip = false;
1643 }
1644 }
1645
1646 if ( $options == 'withaccess' ) {
1647 $accesskey = $this->accesskey( $name );
1648 if ( $accesskey !== false ) {
1649 if ( $tooltip === false || $tooltip === '' ) {
1650 $tooltip = "[$accesskey]";
1651 } else {
1652 $tooltip .= " [$accesskey]";
1653 }
1654 }
1655 }
1656
1657 wfProfileOut( __METHOD__ );
1658 return $tooltip;
1659 }
1660
1661 /**
1662 * Given the id of an interface element, constructs the appropriate
1663 * accesskey attribute from the system messages. (Note, this is usually
1664 * the id but isn't always, because sometimes the accesskey needs to go on
1665 * a different element than the id, for reverse-compatibility, etc.)
1666 *
1667 * @param $name String: id of the element, minus prefixes.
1668 * @return String: contents of the accesskey attribute (which you must HTML-
1669 * escape), or false for no accesskey attribute
1670 */
1671 public function accesskey( $name ) {
1672 if ( isset( $this->accesskeycache[$name] ) ) {
1673 return $this->accesskeycache[$name];
1674 }
1675 wfProfileIn( __METHOD__ );
1676
1677 $message = wfMessage( "accesskey-$name" );
1678
1679 if ( !$message->exists() ) {
1680 $accesskey = false;
1681 } else {
1682 $accesskey = $message->plain();
1683 if ( $accesskey === '' || $accesskey === '-' ) {
1684 # FIXME: Per standard MW behavior, a value of '-' means to suppress the
1685 # attribute, but this is broken for accesskey: that might be a useful
1686 # value.
1687 $accesskey = false;
1688 }
1689 }
1690
1691 wfProfileOut( __METHOD__ );
1692 return $this->accesskeycache[$name] = $accesskey;
1693 }
1694
1695 /**
1696 * Creates a (show/hide) link for deleting revisions/log entries
1697 *
1698 * @param $query Array: query parameters to be passed to link()
1699 * @param $restricted Boolean: set to true to use a <strong> instead of a <span>
1700 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1701 *
1702 * @return String: HTML <a> link to Special:Revisiondelete, wrapped in a
1703 * span to allow for customization of appearance with CSS
1704 */
1705 public function revDeleteLink( $query = array(), $restricted = false, $delete = true ) {
1706 $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
1707 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1708 $tag = $restricted ? 'strong' : 'span';
1709 $link = $this->link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
1710 return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
1711 }
1712
1713 /**
1714 * Creates a dead (show/hide) link for deleting revisions/log entries
1715 *
1716 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1717 *
1718 * @return string HTML text wrapped in a span to allow for customization
1719 * of appearance with CSS
1720 */
1721 public function revDeleteLinkDisabled( $delete = true ) {
1722 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1723 return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($text)" );
1724 }
1725
1726 /* Deprecated methods */
1727
1728 /**
1729 * @deprecated @since 1.16 Use link()
1730 *
1731 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
1732 * it if you already have a title object handy. See makeLinkObj for further documentation.
1733 *
1734 * @param $title String: the text of the title
1735 * @param $text String: link text
1736 * @param $query String: optional query part
1737 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1738 * be included in the link text. Other characters will be appended after
1739 * the end of the link.
1740 */
1741 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
1742 wfProfileIn( __METHOD__ );
1743 $nt = Title::newFromText( $title );
1744 if ( $nt instanceof Title ) {
1745 $result = $this->makeLinkObj( $nt, $text, $query, $trail );
1746 } else {
1747 wfDebug( 'Invalid title passed to Linker::makeLink(): "' . $title . "\"\n" );
1748 $result = $text == "" ? $title : $text;
1749 }
1750
1751 wfProfileOut( __METHOD__ );
1752 return $result;
1753 }
1754
1755 /**
1756 * @deprecated @since 1.16 Use link()
1757 *
1758 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
1759 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
1760 *
1761 * @param $title String: the text of the title
1762 * @param $text String: link text
1763 * @param $query String: optional query part
1764 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1765 * be included in the link text. Other characters will be appended after
1766 * the end of the link.
1767 * @param $prefix String: Optional prefix
1768 * @param $aprops String: extra attributes to the a-element
1769 */
1770 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '', $aprops = '' ) {
1771 $nt = Title::newFromText( $title );
1772 if ( $nt instanceof Title ) {
1773 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
1774 } else {
1775 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "' . $title . "\"\n" );
1776 return $text == '' ? $title : $text;
1777 }
1778 }
1779
1780 /**
1781 * @deprecated @since 1.16 Use link()
1782 *
1783 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
1784 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
1785 *
1786 * @param $title String: The text of the title
1787 * @param $text String: Link text
1788 * @param $query String: Optional query part
1789 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1790 * be included in the link text. Other characters will be appended after
1791 * the end of the link.
1792 */
1793 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
1794 $nt = Title::newFromText( $title );
1795 if ( $nt instanceof Title ) {
1796 return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
1797 } else {
1798 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "' . $title . "\"\n" );
1799 return $text == '' ? $title : $text;
1800 }
1801 }
1802
1803 /**
1804 * @deprecated @since 1.16 Use link()
1805 *
1806 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
1807 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
1808 *
1809 * @param $title String: the text of the title
1810 * @param $text String: link text
1811 * @param $query String: optional query part
1812 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1813 * be included in the link text. Other characters will be appended after
1814 * the end of the link.
1815 */
1816 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
1817 wfDeprecated( __METHOD__ );
1818 $nt = Title::newFromText( $title );
1819 if ( $nt instanceof Title ) {
1820 return $this->makeStubLinkObj( $nt, $text, $query, $trail );
1821 } else {
1822 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "' . $title . "\"\n" );
1823 return $text == '' ? $title : $text;
1824 }
1825 }
1826
1827 /**
1828 * @deprecated @since 1.16 Use link()
1829 *
1830 * Make a link for a title which may or may not be in the database. If you need to
1831 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
1832 * call to this will result in a DB query.
1833 *
1834 * @param $nt Title: the title object to make the link from, e.g. from
1835 * Title::newFromText.
1836 * @param $text String: link text
1837 * @param $query String: optional query part
1838 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1839 * be included in the link text. Other characters will be appended after
1840 * the end of the link.
1841 * @param $prefix String: optional prefix. As trail, only before instead of after.
1842 */
1843 function makeLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1844 wfProfileIn( __METHOD__ );
1845
1846 $query = wfCgiToArray( $query );
1847 list( $inside, $trail ) = Linker::splitTrail( $trail );
1848 if ( $text === '' ) {
1849 $text = $this->linkText( $nt );
1850 }
1851
1852 $ret = $this->link( $nt, "$prefix$text$inside", array(), $query ) . $trail;
1853
1854 wfProfileOut( __METHOD__ );
1855 return $ret;
1856 }
1857
1858 /**
1859 * @deprecated @since 1.16 Use link()
1860 *
1861 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
1862 * it doesn't have to do a database query. It's also valid for interwiki titles and special
1863 * pages.
1864 *
1865 * @param $title Title object of target page
1866 * @param $text String: text to replace the title
1867 * @param $query String: link target
1868 * @param $trail String: text after link
1869 * @param $prefix String: text before link text
1870 * @param $aprops String: extra attributes to the a-element
1871 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
1872 * @return the a-element
1873 */
1874 function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
1875 wfProfileIn( __METHOD__ );
1876
1877 if ( $text == '' ) {
1878 $text = $this->linkText( $title );
1879 }
1880 $attribs = Sanitizer::mergeAttributes(
1881 Sanitizer::decodeTagAttributes( $aprops ),
1882 Sanitizer::decodeTagAttributes( $style )
1883 );
1884 $query = wfCgiToArray( $query );
1885 list( $inside, $trail ) = Linker::splitTrail( $trail );
1886
1887 $ret = $this->link( $title, "$prefix$text$inside", $attribs, $query,
1888 array( 'known', 'noclasses' ) ) . $trail;
1889
1890 wfProfileOut( __METHOD__ );
1891 return $ret;
1892 }
1893
1894 /**
1895 * @deprecated @since 1.16 Use link()
1896 *
1897 * Make a red link to the edit page of a given title.
1898 *
1899 * @param $title Title object of the target page
1900 * @param $text String: Link text
1901 * @param $query String: Optional query part
1902 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1903 * be included in the link text. Other characters will be appended after
1904 * the end of the link.
1905 * @param $prefix String: Optional prefix
1906 */
1907 function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
1908 wfProfileIn( __METHOD__ );
1909
1910 list( $inside, $trail ) = Linker::splitTrail( $trail );
1911 if ( $text === '' ) {
1912 $text = $this->linkText( $title );
1913 }
1914
1915 $ret = $this->link( $title, "$prefix$text$inside", array(),
1916 wfCgiToArray( $query ), 'broken' ) . $trail;
1917
1918 wfProfileOut( __METHOD__ );
1919 return $ret;
1920 }
1921
1922 /**
1923 * @deprecated @since 1.16 Use link()
1924 *
1925 * Make a brown link to a short article.
1926 *
1927 * @param $nt Title object of the target page
1928 * @param $text String: link text
1929 * @param $query String: optional query part
1930 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1931 * be included in the link text. Other characters will be appended after
1932 * the end of the link.
1933 * @param $prefix String: Optional prefix
1934 */
1935 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1936 return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
1937 }
1938
1939 /**
1940 * @deprecated @since 1.16 Use link()
1941 *
1942 * Make a coloured link.
1943 *
1944 * @param $nt Title object of the target page
1945 * @param $colour Integer: colour of the link
1946 * @param $text String: link text
1947 * @param $query String: optional query part
1948 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1949 * be included in the link text. Other characters will be appended after
1950 * the end of the link.
1951 * @param $prefix String: Optional prefix
1952 */
1953 function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
1954 if ( $colour != '' ) {
1955 $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
1956 } else $style = '';
1957 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
1958 }
1959
1960 /** Obsolete alias */
1961 function makeImage( $url, $alt = '' ) {
1962 wfDeprecated( __METHOD__ );
1963 return $this->makeExternalImage( $url, $alt );
1964 }
1965
1966 /**
1967 * Creates the HTML source for images
1968 * @deprecated @since 1.16 use makeImageLink2
1969 *
1970 * @param $title Title object
1971 * @param $label String: label text
1972 * @param $alt String: alt text
1973 * @param $align String: horizontal alignment: none, left, center, right)
1974 * @param $handlerParams Array: parameters to be passed to the media handler
1975 * @param $framed Boolean: shows image in original size in a frame
1976 * @param $thumb Boolean: shows image as thumbnail in a frame
1977 * @param $manualthumb String: image name for the manual thumbnail
1978 * @param $valign String: vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
1979 * @param $time String: timestamp of the file, set as false for current
1980 * @return String
1981 */
1982 function makeImageLinkObj( $title, $label, $alt, $align = '', $handlerParams = array(), $framed = false,
1983 $thumb = false, $manualthumb = '', $valign = '', $time = false )
1984 {
1985 $frameParams = array( 'alt' => $alt, 'caption' => $label );
1986 if ( $align ) {
1987 $frameParams['align'] = $align;
1988 }
1989 if ( $framed ) {
1990 $frameParams['framed'] = true;
1991 }
1992 if ( $thumb ) {
1993 $frameParams['thumbnail'] = true;
1994 }
1995 if ( $manualthumb ) {
1996 $frameParams['manualthumb'] = $manualthumb;
1997 }
1998 if ( $valign ) {
1999 $frameParams['valign'] = $valign;
2000 }
2001 $file = wfFindFile( $title, array( 'time' => $time ) );
2002 return $this->makeImageLink2( $title, $file, $frameParams, $handlerParams, $time );
2003 }
2004
2005 /** @deprecated use Linker::makeMediaLinkObj() */
2006 function makeMediaLink( $name, $unused = '', $text = '', $time = false ) {
2007 $nt = Title::makeTitleSafe( NS_FILE, $name );
2008 return $this->makeMediaLinkObj( $nt, $text, $time );
2009 }
2010
2011 /**
2012 * Returns the attributes for the tooltip and access key.
2013 */
2014 public function tooltipAndAccesskeyAttribs( $name ) {
2015 global $wgEnableTooltipsAndAccesskeys;
2016 if ( !$wgEnableTooltipsAndAccesskeys )
2017 return array();
2018 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2019 # no attribute" instead of "output '' as value for attribute", this
2020 # would be three lines.
2021 $attribs = array(
2022 'title' => $this->titleAttrib( $name, 'withaccess' ),
2023 'accesskey' => $this->accesskey( $name )
2024 );
2025 if ( $attribs['title'] === false ) {
2026 unset( $attribs['title'] );
2027 }
2028 if ( $attribs['accesskey'] === false ) {
2029 unset( $attribs['accesskey'] );
2030 }
2031 return $attribs;
2032 }
2033
2034 /**
2035 * @deprecated @since 1.14
2036 * Returns raw bits of HTML, use titleAttrib() and accesskey()
2037 */
2038 public function tooltipAndAccesskey( $name ) {
2039 return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) );
2040 }
2041
2042 /**
2043 * @deprecated @since 1.14
2044 * Returns raw bits of HTML, use titleAttrib()
2045 */
2046 public function tooltip( $name, $options = null ) {
2047 global $wgEnableTooltipsAndAccesskeys;
2048 if ( !$wgEnableTooltipsAndAccesskeys )
2049 return '';
2050 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2051 # no attribute" instead of "output '' as value for attribute", this
2052 # would be two lines.
2053 $tooltip = $this->titleAttrib( $name, $options );
2054 if ( $tooltip === false ) {
2055 return '';
2056 }
2057 return Xml::expandAttributes( array(
2058 'title' => $this->titleAttrib( $name, $options )
2059 ) );
2060 }
2061 }