Suggest running update.php on database error
[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 $attribs['title'] = wfMsgReal( 'editsectionhint', array( $tooltip ), true, $lang );
1381 }
1382 $link = $this->link( $nt, wfMsgExt( 'editsection', array( 'language' => $lang ) ),
1383 $attribs,
1384 array( 'action' => 'edit', 'section' => $section ),
1385 array( 'noclasses', 'known' )
1386 );
1387
1388 # Run the old hook. This takes up half of the function . . . hopefully
1389 # we can rid of it someday.
1390 $attribs = '';
1391 if ( $tooltip ) {
1392 $attribs = htmlspecialchars( wfMsgReal( 'editsectionhint', array( $tooltip ), true, $lang ) );
1393 $attribs = " title=\"$attribs\"";
1394 }
1395 $result = null;
1396 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result ) );
1397 if ( !is_null( $result ) ) {
1398 # For reverse compatibility, add the brackets *after* the hook is
1399 # run, and even add them to hook-provided text. (This is the main
1400 # reason that the EditSectionLink hook is deprecated in favor of
1401 # DoEditSectionLink: it can't change the brackets or the span.)
1402 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
1403 return "<span class=\"editsection\">$result</span>";
1404 }
1405
1406 # Add the brackets and the span, and *then* run the nice new hook, with
1407 # clean and non-redundant arguments.
1408 $result = wfMsgExt( 'editsection-brackets', array( 'escape', 'replaceafter', 'language' => $lang ), $link );
1409 $result = "<span class=\"editsection\">$result</span>";
1410
1411 wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result ) );
1412 return $result;
1413 }
1414
1415 /**
1416 * Create a headline for content
1417 *
1418 * @param $level Integer: the level of the headline (1-6)
1419 * @param $attribs String: any attributes for the headline, starting with
1420 * a space and ending with '>'
1421 * This *must* be at least '>' for no attribs
1422 * @param $anchor String: the anchor to give the headline (the bit after the #)
1423 * @param $text String: the text of the header
1424 * @param $link String: HTML to add for the section edit link
1425 * @param $legacyAnchor Mixed: a second, optional anchor to give for
1426 * backward compatibility (false to omit)
1427 *
1428 * @return String: HTML headline
1429 */
1430 public function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
1431 $ret = "<h$level$attribs"
1432 . $link
1433 . " <span class=\"mw-headline\" id=\"$anchor\">$text</span>"
1434 . "</h$level>";
1435 if ( $legacyAnchor !== false ) {
1436 $ret = "<div id=\"$legacyAnchor\"></div>$ret";
1437 }
1438 return $ret;
1439 }
1440
1441 /**
1442 * Split a link trail, return the "inside" portion and the remainder of the trail
1443 * as a two-element array
1444 */
1445 static function splitTrail( $trail ) {
1446 static $regex = false;
1447 if ( $regex === false ) {
1448 global $wgContLang;
1449 $regex = $wgContLang->linkTrail();
1450 }
1451 $inside = '';
1452 if ( $trail !== '' ) {
1453 $m = array();
1454 if ( preg_match( $regex, $trail, $m ) ) {
1455 $inside = $m[1];
1456 $trail = $m[2];
1457 }
1458 }
1459 return array( $inside, $trail );
1460 }
1461
1462 /**
1463 * Generate a rollback link for a given revision. Currently it's the
1464 * caller's responsibility to ensure that the revision is the top one. If
1465 * it's not, of course, the user will get an error message.
1466 *
1467 * If the calling page is called with the parameter &bot=1, all rollback
1468 * links also get that parameter. It causes the edit itself and the rollback
1469 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1470 * changes, so this allows sysops to combat a busy vandal without bothering
1471 * other users.
1472 *
1473 * @param $rev Revision object
1474 */
1475 function generateRollback( $rev ) {
1476 return '<span class="mw-rollback-link">['
1477 . $this->buildRollbackLink( $rev )
1478 . ']</span>';
1479 }
1480
1481 /**
1482 * Build a raw rollback link, useful for collections of "tool" links
1483 *
1484 * @param $rev Revision object
1485 * @return String: HTML fragment
1486 */
1487 public function buildRollbackLink( $rev ) {
1488 global $wgRequest, $wgUser;
1489 $title = $rev->getTitle();
1490 $query = array(
1491 'action' => 'rollback',
1492 'from' => $rev->getUserText()
1493 );
1494 if ( $wgRequest->getBool( 'bot' ) ) {
1495 $query['bot'] = '1';
1496 $query['hidediff'] = '1'; // bug 15999
1497 }
1498 $query['token'] = $wgUser->editToken( array( $title->getPrefixedText(),
1499 $rev->getUserText() ) );
1500 return $this->link( $title, wfMsgHtml( 'rollbacklink' ),
1501 array( 'title' => wfMsg( 'tooltip-rollback' ) ),
1502 $query, array( 'known', 'noclasses' ) );
1503 }
1504
1505 /**
1506 * Returns HTML for the "templates used on this page" list.
1507 *
1508 * @param $templates Array of templates from Article::getUsedTemplate
1509 * or similar
1510 * @param $preview Boolean: whether this is for a preview
1511 * @param $section Boolean: whether this is for a section edit
1512 * @return String: HTML output
1513 */
1514 public function formatTemplates( $templates, $preview = false, $section = false ) {
1515 wfProfileIn( __METHOD__ );
1516
1517 $outText = '';
1518 if ( count( $templates ) > 0 ) {
1519 # Do a batch existence check
1520 $batch = new LinkBatch;
1521 foreach ( $templates as $title ) {
1522 $batch->addObj( $title );
1523 }
1524 $batch->execute();
1525
1526 # Construct the HTML
1527 $outText = '<div class="mw-templatesUsedExplanation">';
1528 if ( $preview ) {
1529 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ), count( $templates ) );
1530 } elseif ( $section ) {
1531 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ), count( $templates ) );
1532 } else {
1533 $outText .= wfMsgExt( 'templatesused', array( 'parse' ), count( $templates ) );
1534 }
1535 $outText .= "</div><ul>\n";
1536
1537 usort( $templates, array( 'Title', 'compare' ) );
1538 foreach ( $templates as $titleObj ) {
1539 $r = $titleObj->getRestrictions( 'edit' );
1540 if ( in_array( 'sysop', $r ) ) {
1541 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1542 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1543 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1544 } else {
1545 $protected = '';
1546 }
1547 if ( $titleObj->quickUserCan( 'edit' ) ) {
1548 $editLink = $this->link(
1549 $titleObj,
1550 wfMsg( 'editlink' ),
1551 array(),
1552 array( 'action' => 'edit' )
1553 );
1554 } else {
1555 $editLink = $this->link(
1556 $titleObj,
1557 wfMsg( 'viewsourcelink' ),
1558 array(),
1559 array( 'action' => 'edit' )
1560 );
1561 }
1562 $outText .= '<li>' . $this->link( $titleObj ) . ' (' . $editLink . ') ' . $protected . '</li>';
1563 }
1564 $outText .= '</ul>';
1565 }
1566 wfProfileOut( __METHOD__ );
1567 return $outText;
1568 }
1569
1570 /**
1571 * Returns HTML for the "hidden categories on this page" list.
1572 *
1573 * @param $hiddencats Array of hidden categories from Article::getHiddenCategories
1574 * or similar
1575 * @return String: HTML output
1576 */
1577 public function formatHiddenCategories( $hiddencats ) {
1578 global $wgLang;
1579 wfProfileIn( __METHOD__ );
1580
1581 $outText = '';
1582 if ( count( $hiddencats ) > 0 ) {
1583 # Construct the HTML
1584 $outText = '<div class="mw-hiddenCategoriesExplanation">';
1585 $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
1586 $outText .= "</div><ul>\n";
1587
1588 foreach ( $hiddencats as $titleObj ) {
1589 $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
1590 }
1591 $outText .= '</ul>';
1592 }
1593 wfProfileOut( __METHOD__ );
1594 return $outText;
1595 }
1596
1597 /**
1598 * Format a size in bytes for output, using an appropriate
1599 * unit (B, KB, MB or GB) according to the magnitude in question
1600 *
1601 * @param $size Size to format
1602 * @return String
1603 */
1604 public function formatSize( $size ) {
1605 global $wgLang;
1606 return htmlspecialchars( $wgLang->formatSize( $size ) );
1607 }
1608
1609 /**
1610 * Given the id of an interface element, constructs the appropriate title
1611 * attribute from the system messages. (Note, this is usually the id but
1612 * isn't always, because sometimes the accesskey needs to go on a different
1613 * element than the id, for reverse-compatibility, etc.)
1614 *
1615 * @param $name String: id of the element, minus prefixes.
1616 * @param $options Mixed: null or the string 'withaccess' to add an access-
1617 * key hint
1618 * @return String: contents of the title attribute (which you must HTML-
1619 * escape), or false for no title attribute
1620 */
1621 public function titleAttrib( $name, $options = null ) {
1622 wfProfileIn( __METHOD__ );
1623
1624 if ( wfEmptyMsg( "tooltip-$name" ) ) {
1625 $tooltip = false;
1626 } else {
1627 $tooltip = wfMsg( "tooltip-$name" );
1628 # Compatibility: formerly some tooltips had [alt-.] hardcoded
1629 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1630 # Message equal to '-' means suppress it.
1631 if ( $tooltip == '-' ) {
1632 $tooltip = false;
1633 }
1634 }
1635
1636 if ( $options == 'withaccess' ) {
1637 $accesskey = $this->accesskey( $name );
1638 if ( $accesskey !== false ) {
1639 if ( $tooltip === false || $tooltip === '' ) {
1640 $tooltip = "[$accesskey]";
1641 } else {
1642 $tooltip .= " [$accesskey]";
1643 }
1644 }
1645 }
1646
1647 wfProfileOut( __METHOD__ );
1648 return $tooltip;
1649 }
1650
1651 /**
1652 * Given the id of an interface element, constructs the appropriate
1653 * accesskey attribute from the system messages. (Note, this is usually
1654 * the id but isn't always, because sometimes the accesskey needs to go on
1655 * a different element than the id, for reverse-compatibility, etc.)
1656 *
1657 * @param $name String: id of the element, minus prefixes.
1658 * @return String: contents of the accesskey attribute (which you must HTML-
1659 * escape), or false for no accesskey attribute
1660 */
1661 public function accesskey( $name ) {
1662 wfProfileIn( __METHOD__ );
1663
1664 if ( wfEmptyMsg( "accesskey-$name" ) ) {
1665 $accesskey = false;
1666 } else {
1667 $accesskey = wfMsg( "accesskey-$name" );
1668 if ( $accesskey === '' || $accesskey === '-' ) {
1669 # FIXME: Per standard MW behavior, a value of '-' means to suppress the
1670 # attribute, but this is broken for accesskey: that might be a useful
1671 # value.
1672 $accesskey = false;
1673 }
1674 }
1675
1676 wfProfileOut( __METHOD__ );
1677 return $accesskey;
1678 }
1679
1680 /**
1681 * Creates a (show/hide) link for deleting revisions/log entries
1682 *
1683 * @param $query Array: query parameters to be passed to link()
1684 * @param $restricted Boolean: set to true to use a <strong> instead of a <span>
1685 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1686 *
1687 * @return String: HTML <a> link to Special:Revisiondelete, wrapped in a
1688 * span to allow for customization of appearance with CSS
1689 */
1690 public function revDeleteLink( $query = array(), $restricted = false, $delete = true ) {
1691 $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
1692 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1693 $tag = $restricted ? 'strong' : 'span';
1694 $link = $this->link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
1695 return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
1696 }
1697
1698 /**
1699 * Creates a dead (show/hide) link for deleting revisions/log entries
1700 *
1701 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1702 *
1703 * @return string HTML text wrapped in a span to allow for customization
1704 * of appearance with CSS
1705 */
1706 public function revDeleteLinkDisabled( $delete = true ) {
1707 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1708 return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($text)" );
1709 }
1710
1711 /* Deprecated methods */
1712
1713 /**
1714 * @deprecated
1715 */
1716 function postParseLinkColour( $s = null ) {
1717 wfDeprecated( __METHOD__ );
1718 return null;
1719 }
1720
1721
1722 /**
1723 * @deprecated Use link()
1724 *
1725 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
1726 * it if you already have a title object handy. See makeLinkObj for further documentation.
1727 *
1728 * @param $title String: the text of the title
1729 * @param $text String: link text
1730 * @param $query String: optional query part
1731 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1732 * be included in the link text. Other characters will be appended after
1733 * the end of the link.
1734 */
1735 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
1736 wfProfileIn( __METHOD__ );
1737 $nt = Title::newFromText( $title );
1738 if ( $nt instanceof Title ) {
1739 $result = $this->makeLinkObj( $nt, $text, $query, $trail );
1740 } else {
1741 wfDebug( 'Invalid title passed to Linker::makeLink(): "' . $title . "\"\n" );
1742 $result = $text == "" ? $title : $text;
1743 }
1744
1745 wfProfileOut( __METHOD__ );
1746 return $result;
1747 }
1748
1749 /**
1750 * @deprecated Use link()
1751 *
1752 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
1753 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
1754 *
1755 * @param $title String: the text of the title
1756 * @param $text String: link text
1757 * @param $query String: optional query part
1758 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1759 * be included in the link text. Other characters will be appended after
1760 * the end of the link.
1761 * @param $prefix String: Optional prefix
1762 * @param $aprops String: extra attributes to the a-element
1763 */
1764 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '', $aprops = '' ) {
1765 $nt = Title::newFromText( $title );
1766 if ( $nt instanceof Title ) {
1767 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
1768 } else {
1769 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "' . $title . "\"\n" );
1770 return $text == '' ? $title : $text;
1771 }
1772 }
1773
1774 /**
1775 * @deprecated Use link()
1776 *
1777 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
1778 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
1779 *
1780 * @param $title String: The text of the title
1781 * @param $text String: Link text
1782 * @param $query String: Optional query part
1783 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1784 * be included in the link text. Other characters will be appended after
1785 * the end of the link.
1786 */
1787 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
1788 $nt = Title::newFromText( $title );
1789 if ( $nt instanceof Title ) {
1790 return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
1791 } else {
1792 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "' . $title . "\"\n" );
1793 return $text == '' ? $title : $text;
1794 }
1795 }
1796
1797 /**
1798 * @deprecated Use link()
1799 *
1800 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
1801 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
1802 *
1803 * @param $title String: the text of the title
1804 * @param $text String: link text
1805 * @param $query String: optional query part
1806 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1807 * be included in the link text. Other characters will be appended after
1808 * the end of the link.
1809 */
1810 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
1811 wfDeprecated( __METHOD__ );
1812 $nt = Title::newFromText( $title );
1813 if ( $nt instanceof Title ) {
1814 return $this->makeStubLinkObj( $nt, $text, $query, $trail );
1815 } else {
1816 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "' . $title . "\"\n" );
1817 return $text == '' ? $title : $text;
1818 }
1819 }
1820
1821 /**
1822 * @deprecated Use link()
1823 *
1824 * Make a link for a title which may or may not be in the database. If you need to
1825 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
1826 * call to this will result in a DB query.
1827 *
1828 * @param $nt Title: the title object to make the link from, e.g. from
1829 * Title::newFromText.
1830 * @param $text String: link text
1831 * @param $query String: optional query part
1832 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1833 * be included in the link text. Other characters will be appended after
1834 * the end of the link.
1835 * @param $prefix String: optional prefix. As trail, only before instead of after.
1836 */
1837 function makeLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1838 wfProfileIn( __METHOD__ );
1839
1840 $query = wfCgiToArray( $query );
1841 list( $inside, $trail ) = Linker::splitTrail( $trail );
1842 if ( $text === '' ) {
1843 $text = $this->linkText( $nt );
1844 }
1845
1846 $ret = $this->link( $nt, "$prefix$text$inside", array(), $query ) . $trail;
1847
1848 wfProfileOut( __METHOD__ );
1849 return $ret;
1850 }
1851
1852 /**
1853 * @deprecated Use link()
1854 *
1855 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
1856 * it doesn't have to do a database query. It's also valid for interwiki titles and special
1857 * pages.
1858 *
1859 * @param $title Title object of target page
1860 * @param $text String: text to replace the title
1861 * @param $query String: link target
1862 * @param $trail String: text after link
1863 * @param $prefix String: text before link text
1864 * @param $aprops String: extra attributes to the a-element
1865 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
1866 * @return the a-element
1867 */
1868 function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
1869 wfProfileIn( __METHOD__ );
1870
1871 if ( $text == '' ) {
1872 $text = $this->linkText( $title );
1873 }
1874 $attribs = Sanitizer::mergeAttributes(
1875 Sanitizer::decodeTagAttributes( $aprops ),
1876 Sanitizer::decodeTagAttributes( $style )
1877 );
1878 $query = wfCgiToArray( $query );
1879 list( $inside, $trail ) = Linker::splitTrail( $trail );
1880
1881 $ret = $this->link( $title, "$prefix$text$inside", $attribs, $query,
1882 array( 'known', 'noclasses' ) ) . $trail;
1883
1884 wfProfileOut( __METHOD__ );
1885 return $ret;
1886 }
1887
1888 /**
1889 * @deprecated Use link()
1890 *
1891 * Make a red link to the edit page of a given title.
1892 *
1893 * @param $title Title object of the target page
1894 * @param $text String: Link text
1895 * @param $query String: Optional query part
1896 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1897 * be included in the link text. Other characters will be appended after
1898 * the end of the link.
1899 * @param $prefix String: Optional prefix
1900 */
1901 function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
1902 wfProfileIn( __METHOD__ );
1903
1904 list( $inside, $trail ) = Linker::splitTrail( $trail );
1905 if ( $text === '' ) {
1906 $text = $this->linkText( $title );
1907 }
1908
1909 $ret = $this->link( $title, "$prefix$text$inside", array(),
1910 wfCgiToArray( $query ), 'broken' ) . $trail;
1911
1912 wfProfileOut( __METHOD__ );
1913 return $ret;
1914 }
1915
1916 /**
1917 * @deprecated Use link()
1918 *
1919 * Make a brown link to a short article.
1920 *
1921 * @param $nt Title object of the target page
1922 * @param $text String: link text
1923 * @param $query String: optional query part
1924 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1925 * be included in the link text. Other characters will be appended after
1926 * the end of the link.
1927 * @param $prefix String: Optional prefix
1928 */
1929 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1930 // wfDeprecated( __METHOD__ );
1931 return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
1932 }
1933
1934 /**
1935 * @deprecated Use link()
1936 *
1937 * Make a coloured link.
1938 *
1939 * @param $nt Title object of the target page
1940 * @param $colour Integer: colour of the link
1941 * @param $text String: link text
1942 * @param $query String: optional query part
1943 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1944 * be included in the link text. Other characters will be appended after
1945 * the end of the link.
1946 * @param $prefix String: Optional prefix
1947 */
1948 function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
1949 // wfDeprecated( __METHOD__ );
1950 if ( $colour != '' ) {
1951 $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
1952 } else $style = '';
1953 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
1954 }
1955
1956 /** Obsolete alias */
1957 function makeImage( $url, $alt = '' ) {
1958 wfDeprecated( __METHOD__ );
1959 return $this->makeExternalImage( $url, $alt );
1960 }
1961
1962 /**
1963 * Creates the HTML source for images
1964 * @deprecated use makeImageLink2
1965 *
1966 * @param $title Title object
1967 * @param $label String: label text
1968 * @param $alt String: alt text
1969 * @param $align String: horizontal alignment: none, left, center, right)
1970 * @param $handlerParams Array: parameters to be passed to the media handler
1971 * @param $framed Boolean: shows image in original size in a frame
1972 * @param $thumb Boolean: shows image as thumbnail in a frame
1973 * @param $manualthumb String: image name for the manual thumbnail
1974 * @param $valign String: vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
1975 * @param $time String: timestamp of the file, set as false for current
1976 * @return String
1977 */
1978 function makeImageLinkObj( $title, $label, $alt, $align = '', $handlerParams = array(), $framed = false,
1979 $thumb = false, $manualthumb = '', $valign = '', $time = false )
1980 {
1981 $frameParams = array( 'alt' => $alt, 'caption' => $label );
1982 if ( $align ) {
1983 $frameParams['align'] = $align;
1984 }
1985 if ( $framed ) {
1986 $frameParams['framed'] = true;
1987 }
1988 if ( $thumb ) {
1989 $frameParams['thumbnail'] = true;
1990 }
1991 if ( $manualthumb ) {
1992 $frameParams['manualthumb'] = $manualthumb;
1993 }
1994 if ( $valign ) {
1995 $frameParams['valign'] = $valign;
1996 }
1997 $file = wfFindFile( $title, array( 'time' => $time ) );
1998 return $this->makeImageLink2( $title, $file, $frameParams, $handlerParams, $time );
1999 }
2000
2001 /** @deprecated use Linker::makeMediaLinkObj() */
2002 function makeMediaLink( $name, $unused = '', $text = '', $time = false ) {
2003 $nt = Title::makeTitleSafe( NS_FILE, $name );
2004 return $this->makeMediaLinkObj( $nt, $text, $time );
2005 }
2006
2007 /**
2008 * Used to generate section edit links that point to "other" pages
2009 * (sections that are really part of included pages).
2010 *
2011 * @deprecated use Linker::doEditSectionLink()
2012 * @param $title Title string.
2013 * @param $section Integer: section number.
2014 */
2015 public function editSectionLinkForOther( $title, $section ) {
2016 wfDeprecated( __METHOD__ );
2017 $title = Title::newFromText( $title );
2018 return $this->doEditSectionLink( $title, $section );
2019 }
2020
2021 /**
2022 * @deprecated use Linker::doEditSectionLink()
2023 * @param $nt Title object.
2024 * @param $section Integer: section number.
2025 * @param $hint Link String: title, or default if omitted or empty
2026 */
2027 public function editSectionLink( Title $nt, $section, $hint = '' ) {
2028 wfDeprecated( __METHOD__ );
2029 if ( $hint === '' ) {
2030 # No way to pass an actual empty $hint here! The new interface al-
2031 # lows this, so we have to do this for compatibility.
2032 $hint = null;
2033 }
2034 return $this->doEditSectionLink( $nt, $section, $hint );
2035 }
2036
2037 /**
2038 * Returns the attributes for the tooltip and access key.
2039 */
2040 public function tooltipAndAccesskeyAttribs( $name ) {
2041 global $wgEnableTooltipsAndAccesskeys;
2042 if ( !$wgEnableTooltipsAndAccesskeys )
2043 return array();
2044 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2045 # no attribute" instead of "output '' as value for attribute", this
2046 # would be three lines.
2047 $attribs = array(
2048 'title' => $this->titleAttrib( $name, 'withaccess' ),
2049 'accesskey' => $this->accesskey( $name )
2050 );
2051 if ( $attribs['title'] === false ) {
2052 unset( $attribs['title'] );
2053 }
2054 if ( $attribs['accesskey'] === false ) {
2055 unset( $attribs['accesskey'] );
2056 }
2057 return $attribs;
2058 }
2059 /**
2060 * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
2061 */
2062 public function tooltipAndAccesskey( $name ) {
2063 return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) );
2064 }
2065
2066
2067 /** @deprecated Returns raw bits of HTML, use titleAttrib() */
2068 public function tooltip( $name, $options = null ) {
2069 global $wgEnableTooltipsAndAccesskeys;
2070 if ( !$wgEnableTooltipsAndAccesskeys )
2071 return '';
2072 # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
2073 # no attribute" instead of "output '' as value for attribute", this
2074 # would be two lines.
2075 $tooltip = $this->titleAttrib( $name, $options );
2076 if ( $tooltip === false ) {
2077 return '';
2078 }
2079 return Xml::expandAttributes( array(
2080 'title' => $this->titleAttrib( $name, $options )
2081 ) );
2082 }
2083 }