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