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