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