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