008b9b866848945ca9f228aaedd08ffcf8c7a9c4
[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 MWNamespace::isContent( $t->getNamespace() ) ) {
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 # Note that redirects never count as stubs here.
270 if ( !in_array( 'broken', $options ) && $target->isRedirect() ) {
271 $classes[] = 'mw-redirect';
272 } elseif( $target->isContentPage() ) {
273 # Check for stub.
274 $threshold = $wgUser->getOption( 'stubthreshold' );
275 if( $threshold > 0 and $target->exists() and $target->getLength() < $threshold ) {
276 $classes[] = 'stub';
277 }
278 }
279 if( $classes != array() ) {
280 $defaults['class'] = implode( ' ', $classes );
281 }
282 wfProfileOut( __METHOD__ . '-getClasses' );
283 }
284
285 # Get a default title attribute.
286 if( $target->getPrefixedText() == '' ) {
287 # A link like [[#Foo]]. This used to mean an empty title
288 # attribute, but that's silly. Just don't output a title.
289 } elseif( in_array( 'known', $options ) ) {
290 $defaults['title'] = $target->getPrefixedText();
291 } else {
292 $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
293 }
294
295 # Finally, merge the custom attribs with the default ones, and iterate
296 # over that, deleting all "false" attributes.
297 $ret = array();
298 $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
299 foreach( $merged as $key => $val ) {
300 # A false value suppresses the attribute, and we don't want the
301 # href attribute to be overridden.
302 if( $key != 'href' and $val !== false ) {
303 $ret[$key] = $val;
304 }
305 }
306 wfProfileOut( __METHOD__ );
307 return $ret;
308 }
309
310 /**
311 * Default text of the links to the Title $target
312 */
313 private function linkText( $target ) {
314 # We might be passed a non-Title by make*LinkObj(). Fail gracefully.
315 if( !$target instanceof Title ) {
316 return '';
317 }
318
319 # If the target is just a fragment, with no title, we return the frag-
320 # ment text. Otherwise, we return the title text itself.
321 if( $target->getPrefixedText() === '' and $target->getFragment() !== '' ) {
322 return htmlspecialchars( $target->getFragment() );
323 }
324 return htmlspecialchars( $target->getPrefixedText() );
325 }
326
327 /**
328 * Generate either a normal exists-style link or a stub link, depending
329 * on the given page size.
330 *
331 * @param $size Integer
332 * @param $nt Title object.
333 * @param $text String
334 * @param $query String
335 * @param $trail String
336 * @param $prefix String
337 * @return string HTML of link
338 */
339 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
340 global $wgUser;
341 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
342 $colour = ( $size < $threshold ) ? 'stub' : '';
343 // FIXME: replace deprecated makeColouredLinkObj by link()
344 return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
345 }
346
347 /**
348 * Make appropriate markup for a link to the current article. This is currently rendered
349 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
350 * despite $query not being used.
351 */
352 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
353 if ( $text == '' ) {
354 $text = htmlspecialchars( $nt->getPrefixedText() );
355 }
356 list( $inside, $trail ) = Linker::splitTrail( $trail );
357 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
358 }
359
360 function normaliseSpecialPage( Title $title ) {
361 if ( $title->getNamespace() == NS_SPECIAL ) {
362 list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
363 if ( !$name ) return $title;
364 $ret = SpecialPage::getTitleFor( $name, $subpage );
365 $ret->mFragment = $title->getFragment();
366 return $ret;
367 } else {
368 return $title;
369 }
370 }
371
372 /**
373 * Returns the filename part of an url.
374 * Used as alternative text for external images.
375 */
376 function fnamePart( $url ) {
377 $basename = strrchr( $url, '/' );
378 if ( false === $basename ) {
379 $basename = $url;
380 } else {
381 $basename = substr( $basename, 1 );
382 }
383 return $basename;
384 }
385
386 /**
387 * Return the code for images which were added via external links,
388 * via Parser::maybeMakeExternalImage().
389 */
390 function makeExternalImage( $url, $alt = '' ) {
391 if ( $alt == '' ) {
392 $alt = $this->fnamePart( $url );
393 }
394 $img = '';
395 $success = wfRunHooks('LinkerMakeExternalImage', array( &$url, &$alt, &$img ) );
396 if(!$success) {
397 wfDebug("Hook LinkerMakeExternalImage changed the output of external image with url {$url} and alt text {$alt} to {$img}\n", true);
398 return $img;
399 }
400 return Html::element( 'img',
401 array(
402 'src' => $url,
403 'alt' => $alt ) );
404 }
405
406 /**
407 * Given parameters derived from [[Image:Foo|options...]], generate the
408 * HTML that that syntax inserts in the page.
409 *
410 * @param $title Title object
411 * @param $file File object, or false if it doesn't exist
412 * @param $frameParams Array: associative array of parameters external to the media handler.
413 * Boolean parameters are indicated by presence or absence, the value is arbitrary and
414 * will often be false.
415 * thumbnail If present, downscale and frame
416 * manualthumb Image name to use as a thumbnail, instead of automatic scaling
417 * framed Shows image in original size in a frame
418 * frameless Downscale but don't frame
419 * upright If present, tweak default sizes for portrait orientation
420 * upright_factor Fudge factor for "upright" tweak (default 0.75)
421 * border If present, show a border around the image
422 * align Horizontal alignment (left, right, center, none)
423 * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
424 * bottom, text-bottom)
425 * alt Alternate text for image (i.e. alt attribute). Plain text.
426 * caption HTML for image caption.
427 * link-url URL to link to
428 * link-title Title object to link to
429 * no-link Boolean, suppress description link
430 *
431 * @param $handlerParams Array: associative array of media handler parameters, to be passed
432 * to transform(). Typical keys are "width" and "page".
433 * @param $time String: timestamp of the file, set as false for current
434 * @param $query String: query params for desc url
435 * @return String: HTML for an image, with links, wrappers, etc.
436 */
437 function makeImageLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false, $query = "" ) {
438 $res = null;
439 if( !wfRunHooks( 'ImageBeforeProduceHTML', array( &$this, &$title,
440 &$file, &$frameParams, &$handlerParams, &$time, &$res ) ) ) {
441 return $res;
442 }
443
444 global $wgContLang, $wgUser, $wgThumbLimits, $wgThumbUpright;
445 if ( $file && !$file->allowInlineDisplay() ) {
446 wfDebug( __METHOD__.': '.$title->getPrefixedDBkey()." does not allow inline display\n" );
447 return $this->link( $title );
448 }
449
450 // Shortcuts
451 $fp =& $frameParams;
452 $hp =& $handlerParams;
453
454 // Clean up parameters
455 $page = isset( $hp['page'] ) ? $hp['page'] : false;
456 if ( !isset( $fp['align'] ) ) $fp['align'] = '';
457 if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
458 if ( !isset( $fp['title'] ) ) $fp['title'] = '';
459
460 $prefix = $postfix = '';
461
462 if ( 'center' == $fp['align'] ) {
463 $prefix = '<div class="center">';
464 $postfix = '</div>';
465 $fp['align'] = 'none';
466 }
467 if ( $file && !isset( $hp['width'] ) ) {
468 $hp['width'] = $file->getWidth( $page );
469
470 if( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
471 $wopt = $wgUser->getOption( 'thumbsize' );
472
473 if( !isset( $wgThumbLimits[$wopt] ) ) {
474 $wopt = User::getDefaultOption( 'thumbsize' );
475 }
476
477 // Reduce width for upright images when parameter 'upright' is used
478 if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
479 $fp['upright'] = $wgThumbUpright;
480 }
481 // Use width which is smaller: real image width or user preference width
482 // 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
483 $prefWidth = isset( $fp['upright'] ) ?
484 round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) :
485 $wgThumbLimits[$wopt];
486 if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
487 if( !isset( $hp['height'] ) ) {
488 $hp['width'] = $prefWidth;
489 }
490 }
491 }
492 }
493
494 if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
495 # Create a thumbnail. Alignment depends on language
496 # writing direction, # right aligned for left-to-right-
497 # languages ("Western languages"), left-aligned
498 # for right-to-left-languages ("Semitic languages")
499 #
500 # If thumbnail width has not been provided, it is set
501 # to the default user option as specified in Language*.php
502 if ( $fp['align'] == '' ) {
503 $fp['align'] = $wgContLang->alignEnd();
504 }
505 return $prefix.$this->makeThumbLink2( $title, $file, $fp, $hp, $time, $query ).$postfix;
506 }
507
508 if ( $file && isset( $fp['frameless'] ) ) {
509 $srcWidth = $file->getWidth( $page );
510 # For "frameless" option: do not present an image bigger than the source (for bitmap-style images)
511 # This is the same behaviour as the "thumb" option does it already.
512 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
513 $hp['width'] = $srcWidth;
514 }
515 }
516
517 if ( $file && $hp['width'] ) {
518 # Create a resized image, without the additional thumbnail features
519 $thumb = $file->transform( $hp );
520 } else {
521 $thumb = false;
522 }
523
524 if ( !$thumb ) {
525 $s = $this->makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time==true );
526 } else {
527 $params = array(
528 'alt' => $fp['alt'],
529 'title' => $fp['title'],
530 'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
531 'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false );
532 $params = $this->getImageLinkMTOParams( $fp, $query ) + $params;
533
534 $s = $thumb->toHtml( $params );
535 }
536 if ( $fp['align'] != '' ) {
537 $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
538 }
539 return str_replace("\n", ' ',$prefix.$s.$postfix);
540 }
541
542 /**
543 * Get the link parameters for MediaTransformOutput::toHtml() from given
544 * frame parameters supplied by the Parser.
545 * @param $frameParams The frame parameters
546 * @param $query An optional query string to add to description page links
547 */
548 function getImageLinkMTOParams( $frameParams, $query = '' ) {
549 $mtoParams = array();
550 if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
551 $mtoParams['custom-url-link'] = $frameParams['link-url'];
552 } elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
553 $mtoParams['custom-title-link'] = $frameParams['link-title'];
554 } elseif ( !empty( $frameParams['no-link'] ) ) {
555 // No link
556 } else {
557 $mtoParams['desc-link'] = true;
558 $mtoParams['desc-query'] = $query;
559 }
560 return $mtoParams;
561 }
562
563 /**
564 * Make HTML for a thumbnail including image, border and caption
565 * @param $title Title object
566 * @param $file File object or false if it doesn't exist
567 * @param $label String
568 * @param $alt String
569 * @param $align String
570 * @param $params Array
571 * @param $framed Boolean
572 * @param $manualthumb String
573 */
574 function makeThumbLinkObj( Title $title, $file, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manualthumb = "" ) {
575 $frameParams = array(
576 'alt' => $alt,
577 'caption' => $label,
578 'align' => $align
579 );
580 if ( $framed ) $frameParams['framed'] = true;
581 if ( $manualthumb ) $frameParams['manualthumb'] = $manualthumb;
582 return $this->makeThumbLink2( $title, $file, $frameParams, $params );
583 }
584
585 function makeThumbLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false, $query = "" ) {
586 global $wgStylePath;
587 $exists = $file && $file->exists();
588
589 # Shortcuts
590 $fp =& $frameParams;
591 $hp =& $handlerParams;
592
593 $page = isset( $hp['page'] ) ? $hp['page'] : false;
594 if ( !isset( $fp['align'] ) ) $fp['align'] = 'right';
595 if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
596 if ( !isset( $fp['title'] ) ) $fp['title'] = '';
597 if ( !isset( $fp['caption'] ) ) $fp['caption'] = '';
598
599 if ( empty( $hp['width'] ) ) {
600 // Reduce width for upright images when parameter 'upright' is used
601 $hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
602 }
603 $thumb = false;
604
605 if ( !$exists ) {
606 $outerWidth = $hp['width'] + 2;
607 } else {
608 if ( isset( $fp['manualthumb'] ) ) {
609 # Use manually specified thumbnail
610 $manual_title = Title::makeTitleSafe( NS_FILE, $fp['manualthumb'] );
611 if( $manual_title ) {
612 $manual_img = wfFindFile( $manual_title );
613 if ( $manual_img ) {
614 $thumb = $manual_img->getUnscaledThumb( $hp );
615 } else {
616 $exists = false;
617 }
618 }
619 } elseif ( isset( $fp['framed'] ) ) {
620 // Use image dimensions, don't scale
621 $thumb = $file->getUnscaledThumb( $hp );
622 } else {
623 # Do not present an image bigger than the source, for bitmap-style images
624 # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
625 $srcWidth = $file->getWidth( $page );
626 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
627 $hp['width'] = $srcWidth;
628 }
629 $thumb = $file->transform( $hp );
630 }
631
632 if ( $thumb ) {
633 $outerWidth = $thumb->getWidth() + 2;
634 } else {
635 $outerWidth = $hp['width'] + 2;
636 }
637 }
638
639 # ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
640 # So we don't need to pass it here in $query. However, the URL for the
641 # zoom icon still needs it, so we make a unique query for it. See bug 14771
642 $url = $title->getLocalURL( $query );
643 if( $page ) {
644 $url = wfAppendQuery( $url, 'page=' . urlencode( $page ) );
645 }
646
647 $s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
648 if( !$exists ) {
649 $s .= $this->makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time==true );
650 $zoomIcon = '';
651 } elseif ( !$thumb ) {
652 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
653 $zoomIcon = '';
654 } else {
655 $params = array(
656 'alt' => $fp['alt'],
657 'title' => $fp['title'],
658 'img-class' => 'thumbimage' );
659 $params = $this->getImageLinkMTOParams( $fp, $query ) + $params;
660 $s .= $thumb->toHtml( $params );
661 if ( isset( $fp['framed'] ) ) {
662 $zoomIcon = "";
663 } else {
664 $zoomIcon = '<div class="magnify">'.
665 '<a href="' . htmlspecialchars( $url ) . '" class="internal" ' .
666 'title="' . htmlspecialchars( wfMsg( 'thumbnail-more' ) ) . '">'.
667 '<img src="' . htmlspecialchars( $wgStylePath ) . '/common/images/magnify-clip.png" ' .
668 'width="15" height="11" alt="" /></a></div>';
669 }
670 }
671 $s .= ' <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
672 return str_replace("\n", ' ', $s);
673 }
674
675 /**
676 * Make a "broken" link to an image
677 *
678 * @param $title Title object
679 * @param $text String: link label
680 * @param $query String: query string
681 * @param $trail String: link trail
682 * @param $prefix String: link prefix
683 * @param $time Boolean: a file of a certain timestamp was requested
684 * @return String
685 */
686 public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '', $time = false ) {
687 global $wgEnableUploads, $wgUploadNavigationUrl;
688 if( $title instanceof Title ) {
689 wfProfileIn( __METHOD__ );
690 $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
691 if( ( $wgUploadNavigationUrl || $wgEnableUploads ) && !$currentExists ) {
692 if( $text == '' )
693 $text = htmlspecialchars( $title->getPrefixedText() );
694
695 $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
696 if( $redir ) {
697 wfProfileOut( __METHOD__ );
698 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
699 }
700
701 $href = $this->getUploadUrl( $title, $query );
702
703
704 list( $inside, $trail ) = self::splitTrail( $trail );
705
706 wfProfileOut( __METHOD__ );
707 return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
708 htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES ) . '">' .
709 htmlspecialchars( $prefix . $text . $inside, ENT_NOQUOTES ) . '</a>' . $trail;
710 } else {
711 wfProfileOut( __METHOD__ );
712 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
713 }
714 } else {
715 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
716 }
717 }
718
719 /**
720 * Get the URL to upload a certain file
721 *
722 * @param $destFile Title object of the file to upload
723 * @param $query String: urlencoded query string to prepend
724 * @return String: urlencoded URL
725 */
726 protected function getUploadUrl( $destFile, $query = '' ) {
727 global $wgUploadNavigationUrl;
728 $q = 'wpDestFile=' . $destFile->getPartialUrl();
729 if( $query != '' )
730 $q .= '&' . $query;
731
732 if( $wgUploadNavigationUrl ) {
733 return wfAppendQuery( $wgUploadNavigationUrl, $q );
734 } else {
735 $upload = SpecialPage::getTitleFor( 'Upload' );
736 return $upload->getLocalUrl( $q );
737 }
738 }
739
740 /**
741 * Create a direct link to a given uploaded file.
742 *
743 * @param $title Title object.
744 * @param $text String: pre-sanitized HTML
745 * @param $time string: time image was created
746 * @return String: HTML
747 *
748 * @todo Handle invalid or missing images better.
749 */
750 public function makeMediaLinkObj( $title, $text = '', $time = false ) {
751 if( is_null( $title ) ) {
752 ### HOTFIX. Instead of breaking, return empty string.
753 return $text;
754 } else {
755 $img = wfFindFile( $title, array( 'time' => $time ) );
756 if( $img ) {
757 $url = $img->getURL();
758 $class = 'internal';
759 } else {
760 $url = $this->getUploadUrl( $title );
761 $class = 'new';
762 }
763 $alt = htmlspecialchars( $title->getText(), ENT_QUOTES );
764 if( $text == '' ) {
765 $text = $alt;
766 }
767 $u = htmlspecialchars( $url );
768 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
769 }
770 }
771
772 /**
773 * Make a link to a special page given its name and, optionally,
774 * a message key from the link text.
775 * Usage example: $skin->specialLink( 'recentchanges' )
776 */
777 function specialLink( $name, $key = '' ) {
778 global $wgContLang;
779
780 if ( $key == '' ) { $key = strtolower( $name ); }
781 $pn = $wgContLang->ucfirst( $name );
782 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
783 wfMsg( $key ) );
784 }
785
786 /**
787 * Make an external link
788 * @param $url String: URL to link to
789 * @param $text String: text of link
790 * @param $escape Boolean: do we escape the link text?
791 * @param $linktype String: type of external link. Gets added to the classes
792 * @param $attribs Array of extra attributes to <a>
793 *
794 * @todo FIXME: This is a really crappy implementation. $linktype and
795 * 'external' are mashed into the class attrib for the link (which is made
796 * into a string). Then, if we've got additional params in $attribs, we
797 * add to it. People using this might want to change the classes (or other
798 * default link attributes), but passing $attribsText is just messy. Would
799 * make a lot more sense to make put the classes into $attribs, let the
800 * hook play with them, *then* expand it all at once.
801 */
802 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
803 if ( isset( $attribs[ 'class' ] ) ) $class = $attribs[ 'class' ]; # yet another hack :(
804 else $class = 'external ' . $linktype;
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 # no trailing newline, script should not be wrapped in a
1332 # paragraph
1333 . "</ul>\n</td></tr></table>"
1334 . Html::inlineScript(
1335 'if (window.showTocToggle) {'
1336 . ' var tocShowText = "' . Xml::escapeJsString( wfMsg('showtoc') ) . '";'
1337 . ' var tocHideText = "' . Xml::escapeJsString( wfMsg('hidetoc') ) . '";'
1338 . ' showTocToggle();'
1339 . ' } ' )
1340 . "\n";
1341 }
1342
1343 /**
1344 * Generate a table of contents from a section tree
1345 * Currently unused.
1346 *
1347 * @param $tree Return value of ParserOutput::getSections()
1348 * @return String: HTML fragment
1349 */
1350 public function generateTOC( $tree ) {
1351 $toc = '';
1352 $lastLevel = 0;
1353 foreach ( $tree as $section ) {
1354 if ( $section['toclevel'] > $lastLevel )
1355 $toc .= $this->tocIndent();
1356 else if ( $section['toclevel'] < $lastLevel )
1357 $toc .= $this->tocUnindent(
1358 $lastLevel - $section['toclevel'] );
1359 else
1360 $toc .= $this->tocLineEnd();
1361
1362 $toc .= $this->tocLine( $section['anchor'],
1363 $section['line'], $section['number'],
1364 $section['toclevel'], $section['index'] );
1365 $lastLevel = $section['toclevel'];
1366 }
1367 $toc .= $this->tocLineEnd();
1368 return $this->tocList( $toc );
1369 }
1370
1371 /**
1372 * Create a section edit link. This supersedes editSectionLink() and
1373 * editSectionLinkForOther().
1374 *
1375 * @param $nt Title The title being linked to (may not be the same as
1376 * $wgTitle, if the section is included from a template)
1377 * @param $section string The designation of the section being pointed to,
1378 * to be included in the link, like "&section=$section"
1379 * @param $tooltip string The tooltip to use for the link: will be escaped
1380 * and wrapped in the 'editsectionhint' message
1381 * @return string HTML to use for edit link
1382 */
1383 public function doEditSectionLink( Title $nt, $section, $tooltip = null ) {
1384 // HTML generated here should probably have userlangattributes
1385 // added to it for LTR text on RTL pages
1386 $attribs = array();
1387 if( !is_null( $tooltip ) ) {
1388 $attribs['title'] = wfMsg( 'editsectionhint', $tooltip );
1389 }
1390 $link = $this->link( $nt, wfMsg('editsection'),
1391 $attribs,
1392 array( 'action' => 'edit', 'section' => $section ),
1393 array( 'noclasses', 'known' )
1394 );
1395
1396 # Run the old hook. This takes up half of the function . . . hopefully
1397 # we can rid of it someday.
1398 $attribs = '';
1399 if( $tooltip ) {
1400 $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
1401 $attribs = " title=\"$attribs\"";
1402 }
1403 $result = null;
1404 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $link, &$result ) );
1405 if( !is_null( $result ) ) {
1406 # For reverse compatibility, add the brackets *after* the hook is
1407 # run, and even add them to hook-provided text. (This is the main
1408 # reason that the EditSectionLink hook is deprecated in favor of
1409 # DoEditSectionLink: it can't change the brackets or the span.)
1410 $result = wfMsgHtml( 'editsection-brackets', $result );
1411 return "<span class=\"editsection\">$result</span>";
1412 }
1413
1414 # Add the brackets and the span, and *then* run the nice new hook, with
1415 # clean and non-redundant arguments.
1416 $result = wfMsgHtml( 'editsection-brackets', $link );
1417 $result = "<span class=\"editsection\">$result</span>";
1418
1419 wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result ) );
1420 return $result;
1421 }
1422
1423 /**
1424 * Create a headline for content
1425 *
1426 * @param $level Integer: the level of the headline (1-6)
1427 * @param $attribs String: any attributes for the headline, starting with
1428 * a space and ending with '>'
1429 * This *must* be at least '>' for no attribs
1430 * @param $anchor String: the anchor to give the headline (the bit after the #)
1431 * @param $text String: the text of the header
1432 * @param $link String: HTML to add for the section edit link
1433 * @param $legacyAnchor Mixed: a second, optional anchor to give for
1434 * backward compatibility (false to omit)
1435 *
1436 * @return String: HTML headline
1437 */
1438 public function makeHeadline( $level, $attribs, $anchor, $text, $link, $legacyAnchor = false ) {
1439 $ret = "<h$level$attribs"
1440 . $link
1441 . " <span class=\"mw-headline\" id=\"$anchor\">$text</span>"
1442 . "</h$level>";
1443 if ( $legacyAnchor !== false ) {
1444 $ret = "<a id=\"$legacyAnchor\"></a>$ret";
1445 }
1446 return $ret;
1447 }
1448
1449 /**
1450 * Split a link trail, return the "inside" portion and the remainder of the trail
1451 * as a two-element array
1452 */
1453 static function splitTrail( $trail ) {
1454 static $regex = false;
1455 if ( $regex === false ) {
1456 global $wgContLang;
1457 $regex = $wgContLang->linkTrail();
1458 }
1459 $inside = '';
1460 if ( $trail !== '' ) {
1461 $m = array();
1462 if ( preg_match( $regex, $trail, $m ) ) {
1463 $inside = $m[1];
1464 $trail = $m[2];
1465 }
1466 }
1467 return array( $inside, $trail );
1468 }
1469
1470 /**
1471 * Generate a rollback link for a given revision. Currently it's the
1472 * caller's responsibility to ensure that the revision is the top one. If
1473 * it's not, of course, the user will get an error message.
1474 *
1475 * If the calling page is called with the parameter &bot=1, all rollback
1476 * links also get that parameter. It causes the edit itself and the rollback
1477 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1478 * changes, so this allows sysops to combat a busy vandal without bothering
1479 * other users.
1480 *
1481 * @param $rev Revision object
1482 */
1483 function generateRollback( $rev ) {
1484 return '<span class="mw-rollback-link">['
1485 . $this->buildRollbackLink( $rev )
1486 . ']</span>';
1487 }
1488
1489 /**
1490 * Build a raw rollback link, useful for collections of "tool" links
1491 *
1492 * @param $rev Revision object
1493 * @return String: HTML fragment
1494 */
1495 public function buildRollbackLink( $rev ) {
1496 global $wgRequest, $wgUser;
1497 $title = $rev->getTitle();
1498 $query = array(
1499 'action' => 'rollback',
1500 'from' => $rev->getUserText()
1501 );
1502 if( $wgRequest->getBool( 'bot' ) ) {
1503 $query['bot'] = '1';
1504 $query['hidediff'] = '1'; // bug 15999
1505 }
1506 $query['token'] = $wgUser->editToken( array( $title->getPrefixedText(),
1507 $rev->getUserText() ) );
1508 return $this->link( $title, wfMsgHtml( 'rollbacklink' ),
1509 array( 'title' => wfMsg( 'tooltip-rollback' ) ),
1510 $query, array( 'known', 'noclasses' ) );
1511 }
1512
1513 /**
1514 * Returns HTML for the "templates used on this page" list.
1515 *
1516 * @param $templates Array of templates from Article::getUsedTemplate
1517 * or similar
1518 * @param $preview Boolean: whether this is for a preview
1519 * @param $section Boolean: whether this is for a section edit
1520 * @return String: HTML output
1521 */
1522 public function formatTemplates( $templates, $preview = false, $section = false ) {
1523 wfProfileIn( __METHOD__ );
1524
1525 $outText = '';
1526 if ( count( $templates ) > 0 ) {
1527 # Do a batch existence check
1528 $batch = new LinkBatch;
1529 foreach( $templates as $title ) {
1530 $batch->addObj( $title );
1531 }
1532 $batch->execute();
1533
1534 # Construct the HTML
1535 $outText = '<div class="mw-templatesUsedExplanation">';
1536 if ( $preview ) {
1537 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ), count( $templates ) );
1538 } elseif ( $section ) {
1539 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ), count( $templates ) );
1540 } else {
1541 $outText .= wfMsgExt( 'templatesused', array( 'parse' ), count( $templates ) );
1542 }
1543 $outText .= "</div><ul>\n";
1544
1545 usort( $templates, array( 'Title', 'compare' ) );
1546 foreach ( $templates as $titleObj ) {
1547 $r = $titleObj->getRestrictions( 'edit' );
1548 if ( in_array( 'sysop', $r ) ) {
1549 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1550 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1551 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1552 } else {
1553 $protected = '';
1554 }
1555 if( $titleObj->quickUserCan( 'edit' ) ) {
1556 $editLink = $this->link(
1557 $titleObj,
1558 wfMsg( 'editlink' ),
1559 array(),
1560 array( 'action' => 'edit' )
1561 );
1562 } else {
1563 $editLink = $this->link(
1564 $titleObj,
1565 wfMsg( 'viewsourcelink' ),
1566 array(),
1567 array( 'action' => 'edit' )
1568 );
1569 }
1570 $outText .= '<li>' . $this->link( $titleObj ) . ' (' . $editLink . ') ' . $protected . '</li>';
1571 }
1572 $outText .= '</ul>';
1573 }
1574 wfProfileOut( __METHOD__ );
1575 return $outText;
1576 }
1577
1578 /**
1579 * Returns HTML for the "hidden categories on this page" list.
1580 *
1581 * @param $hiddencats Array of hidden categories from Article::getHiddenCategories
1582 * or similar
1583 * @return String: HTML output
1584 */
1585 public function formatHiddenCategories( $hiddencats ) {
1586 global $wgLang;
1587 wfProfileIn( __METHOD__ );
1588
1589 $outText = '';
1590 if ( count( $hiddencats ) > 0 ) {
1591 # Construct the HTML
1592 $outText = '<div class="mw-hiddenCategoriesExplanation">';
1593 $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
1594 $outText .= "</div><ul>\n";
1595
1596 foreach ( $hiddencats as $titleObj ) {
1597 $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
1598 }
1599 $outText .= '</ul>';
1600 }
1601 wfProfileOut( __METHOD__ );
1602 return $outText;
1603 }
1604
1605 /**
1606 * Format a size in bytes for output, using an appropriate
1607 * unit (B, KB, MB or GB) according to the magnitude in question
1608 *
1609 * @param $size Size to format
1610 * @return String
1611 */
1612 public function formatSize( $size ) {
1613 global $wgLang;
1614 return htmlspecialchars( $wgLang->formatSize( $size ) );
1615 }
1616
1617 /**
1618 * Given the id of an interface element, constructs the appropriate title
1619 * attribute from the system messages. (Note, this is usually the id but
1620 * isn't always, because sometimes the accesskey needs to go on a different
1621 * element than the id, for reverse-compatibility, etc.)
1622 *
1623 * @param $name String: id of the element, minus prefixes.
1624 * @param $options Mixed: null or the string 'withaccess' to add an access-
1625 * key hint
1626 * @return String: contents of the title attribute (which you must HTML-
1627 * escape), or false for no title attribute
1628 */
1629 public function titleAttrib( $name, $options = null ) {
1630 wfProfileIn( __METHOD__ );
1631
1632 $tooltip = wfMsg( "tooltip-$name" );
1633 # Compatibility: formerly some tooltips had [alt-.] hardcoded
1634 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1635
1636 # Message equal to '-' means suppress it.
1637 if ( wfEmptyMsg( "tooltip-$name", $tooltip ) || $tooltip == '-' ) {
1638 $tooltip = false;
1639 }
1640
1641 if ( $options == 'withaccess' ) {
1642 $accesskey = $this->accesskey( $name );
1643 if( $accesskey !== false ) {
1644 if ( $tooltip === false || $tooltip === '' ) {
1645 $tooltip = "[$accesskey]";
1646 } else {
1647 $tooltip .= " [$accesskey]";
1648 }
1649 }
1650 }
1651
1652 wfProfileOut( __METHOD__ );
1653 return $tooltip;
1654 }
1655
1656 /**
1657 * Given the id of an interface element, constructs the appropriate
1658 * accesskey attribute from the system messages. (Note, this is usually
1659 * the id but isn't always, because sometimes the accesskey needs to go on
1660 * a different element than the id, for reverse-compatibility, etc.)
1661 *
1662 * @param $name String: id of the element, minus prefixes.
1663 * @return String: contents of the accesskey attribute (which you must HTML-
1664 * escape), or false for no accesskey attribute
1665 */
1666 public function accesskey( $name ) {
1667 wfProfileIn( __METHOD__ );
1668
1669 $accesskey = wfMsg( "accesskey-$name" );
1670
1671 # FIXME: Per standard MW behavior, a value of '-' means to suppress the
1672 # attribute, but this is broken for accesskey: that might be a useful
1673 # value.
1674 if( $accesskey != '' && $accesskey != '-' && !wfEmptyMsg( "accesskey-$name", $accesskey ) ) {
1675 wfProfileOut( __METHOD__ );
1676 return $accesskey;
1677 }
1678
1679 wfProfileOut( __METHOD__ );
1680 return false;
1681 }
1682
1683 /**
1684 * Creates a (show/hide) link for deleting revisions/log entries
1685 *
1686 * @param $query Array: query parameters to be passed to link()
1687 * @param $restricted Boolean: set to true to use a <strong> instead of a <span>
1688 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1689 *
1690 * @return String: HTML <a> link to Special:Revisiondelete, wrapped in a
1691 * span to allow for customization of appearance with CSS
1692 */
1693 public function revDeleteLink( $query = array(), $restricted = false, $delete = true ) {
1694 $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
1695 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1696 $tag = $restricted ? 'strong' : 'span';
1697 $link = $this->link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
1698 return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
1699 }
1700
1701 /**
1702 * Creates a dead (show/hide) link for deleting revisions/log entries
1703 *
1704 * @param $delete Boolean: set to true to use (show/hide) rather than (show)
1705 *
1706 * @return string HTML text wrapped in a span to allow for customization
1707 * of appearance with CSS
1708 */
1709 public function revDeleteLinkDisabled( $delete = true ) {
1710 $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
1711 return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($text)" );
1712 }
1713
1714 /* Deprecated methods */
1715
1716 /**
1717 * @deprecated
1718 */
1719 function postParseLinkColour( $s = null ) {
1720 wfDeprecated( __METHOD__ );
1721 return null;
1722 }
1723
1724
1725 /**
1726 * @deprecated Use link()
1727 *
1728 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
1729 * it if you already have a title object handy. See makeLinkObj for further documentation.
1730 *
1731 * @param $title String: the text of the title
1732 * @param $text String: link text
1733 * @param $query String: optional query part
1734 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1735 * be included in the link text. Other characters will be appended after
1736 * the end of the link.
1737 */
1738 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
1739 wfProfileIn( __METHOD__ );
1740 $nt = Title::newFromText( $title );
1741 if ( $nt instanceof Title ) {
1742 $result = $this->makeLinkObj( $nt, $text, $query, $trail );
1743 } else {
1744 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
1745 $result = $text == "" ? $title : $text;
1746 }
1747
1748 wfProfileOut( __METHOD__ );
1749 return $result;
1750 }
1751
1752 /**
1753 * @deprecated Use link()
1754 *
1755 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
1756 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
1757 *
1758 * @param $title String: the text of the title
1759 * @param $text String: link text
1760 * @param $query String: optional query part
1761 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1762 * be included in the link text. Other characters will be appended after
1763 * the end of the link.
1764 * @param $prefix String: Optional prefix
1765 * @param $aprops String: extra attributes to the a-element
1766 */
1767 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
1768 $nt = Title::newFromText( $title );
1769 if ( $nt instanceof Title ) {
1770 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
1771 } else {
1772 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
1773 return $text == '' ? $title : $text;
1774 }
1775 }
1776
1777 /**
1778 * @deprecated Use link()
1779 *
1780 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
1781 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
1782 *
1783 * @param $title String: The text of the title
1784 * @param $text String: Link text
1785 * @param $query String: Optional query part
1786 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1787 * be included in the link text. Other characters will be appended after
1788 * the end of the link.
1789 */
1790 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
1791 $nt = Title::newFromText( $title );
1792 if ( $nt instanceof Title ) {
1793 return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
1794 } else {
1795 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
1796 return $text == '' ? $title : $text;
1797 }
1798 }
1799
1800 /**
1801 * @deprecated Use link()
1802 *
1803 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
1804 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
1805 *
1806 * @param $title String: the text of the title
1807 * @param $text String: link text
1808 * @param $query String: optional query part
1809 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1810 * be included in the link text. Other characters will be appended after
1811 * the end of the link.
1812 */
1813 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
1814 wfDeprecated( __METHOD__ );
1815 $nt = Title::newFromText( $title );
1816 if ( $nt instanceof Title ) {
1817 return $this->makeStubLinkObj( $nt, $text, $query, $trail );
1818 } else {
1819 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
1820 return $text == '' ? $title : $text;
1821 }
1822 }
1823
1824 /**
1825 * @deprecated Use link()
1826 *
1827 * Make a link for a title which may or may not be in the database. If you need to
1828 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
1829 * call to this will result in a DB query.
1830 *
1831 * @param $nt Title: the title object to make the link from, e.g. from
1832 * Title::newFromText.
1833 * @param $text String: link text
1834 * @param $query String: optional query part
1835 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1836 * be included in the link text. Other characters will be appended after
1837 * the end of the link.
1838 * @param $prefix String: optional prefix. As trail, only before instead of after.
1839 */
1840 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
1841 wfProfileIn( __METHOD__ );
1842
1843 $query = wfCgiToArray( $query );
1844 list( $inside, $trail ) = Linker::splitTrail( $trail );
1845 if( $text === '' ) {
1846 $text = $this->linkText( $nt );
1847 }
1848
1849 $ret = $this->link( $nt, "$prefix$text$inside", array(), $query ) . $trail;
1850
1851 wfProfileOut( __METHOD__ );
1852 return $ret;
1853 }
1854
1855 /**
1856 * @deprecated Use link()
1857 *
1858 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
1859 * it doesn't have to do a database query. It's also valid for interwiki titles and special
1860 * pages.
1861 *
1862 * @param $title Title object of target page
1863 * @param $text String: text to replace the title
1864 * @param $query String: link target
1865 * @param $trail String: text after link
1866 * @param $prefix String: text before link text
1867 * @param $aprops String: extra attributes to the a-element
1868 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
1869 * @return the a-element
1870 */
1871 function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
1872 wfProfileIn( __METHOD__ );
1873
1874 if ( $text == '' ) {
1875 $text = $this->linkText( $title );
1876 }
1877 $attribs = Sanitizer::mergeAttributes(
1878 Sanitizer::decodeTagAttributes( $aprops ),
1879 Sanitizer::decodeTagAttributes( $style )
1880 );
1881 $query = wfCgiToArray( $query );
1882 list( $inside, $trail ) = Linker::splitTrail( $trail );
1883
1884 $ret = $this->link( $title, "$prefix$text$inside", $attribs, $query,
1885 array( 'known', 'noclasses' ) ) . $trail;
1886
1887 wfProfileOut( __METHOD__ );
1888 return $ret;
1889 }
1890
1891 /**
1892 * @deprecated Use link()
1893 *
1894 * Make a red link to the edit page of a given title.
1895 *
1896 * @param $title Title object of the target page
1897 * @param $text String: Link text
1898 * @param $query String: Optional query part
1899 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
1900 * be included in the link text. Other characters will be appended after
1901 * the end of the link.
1902 * @param $prefix String: Optional prefix
1903 */
1904 function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
1905 wfProfileIn( __METHOD__ );
1906
1907 list( $inside, $trail ) = Linker::splitTrail( $trail );
1908 if( $text === '' ) {
1909 $text = $this->linkText( $title );
1910 }
1911 $nt = $this->normaliseSpecialPage( $title );
1912
1913 $ret = $this->link( $title, "$prefix$text$inside", array(),
1914 wfCgiToArray( $query ), 'broken' ) . $trail;
1915
1916 wfProfileOut( __METHOD__ );
1917 return $ret;
1918 }
1919
1920 /**
1921 * @deprecated Use link()
1922 *
1923 * Make a brown link to a short article.
1924 *
1925 * @param $nt Title object of the target page
1926 * @param $text String: link text
1927 * @param $query String: optional query part
1928 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1929 * be included in the link text. Other characters will be appended after
1930 * the end of the link.
1931 * @param $prefix String: Optional prefix
1932 */
1933 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1934 wfDeprecated( __METHOD__ );
1935 return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
1936 }
1937
1938 /**
1939 * @deprecated Use link()
1940 *
1941 * Make a coloured link.
1942 *
1943 * @param $nt Title object of the target page
1944 * @param $colour Integer: colour of the link
1945 * @param $text String: link text
1946 * @param $query String: optional query part
1947 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
1948 * be included in the link text. Other characters will be appended after
1949 * the end of the link.
1950 * @param $prefix String: Optional prefix
1951 */
1952 function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
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 }