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