Don't require commandLine.inc when not using the command line; instead, move wfWaitFo...
[lhc/web/wiklou.git] / includes / Linker.php
1 <?php
2 /**
3 * Split off some of the internal bits from Skin.php.
4 * These functions are used for primarily page content:
5 * links, embedded images, table of contents. Links are
6 * also used in the skin.
7 * For the moment, Skin is a descendent class of Linker.
8 * In the future, it should probably be further split
9 * so that ever other bit of the wiki doesn't have to
10 * go loading up Skin to get at it.
11 *
12 * @addtogroup Skins
13 */
14 class Linker {
15
16 /**
17 * Flags for userToolLinks()
18 */
19 const TOOL_LINKS_NOBLOCK = 1;
20
21 function __construct() {}
22
23 /**
24 * @deprecated
25 */
26 function postParseLinkColour( $s = NULL ) {
27 return NULL;
28 }
29
30 /** @todo document */
31 function getExternalLinkAttributes( $link, $text, $class='' ) {
32 $link = htmlspecialchars( $link );
33
34 $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
35
36 $r .= " title=\"{$link}\"";
37 return $r;
38 }
39
40 function getInterwikiLinkAttributes( $link, $text, $class='' ) {
41 global $wgContLang;
42
43 $link = urldecode( $link );
44 $link = $wgContLang->checkTitleEncoding( $link );
45 $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
46 $link = htmlspecialchars( $link );
47
48 $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
49
50 $r .= " title=\"{$link}\"";
51 return $r;
52 }
53
54 /** @todo document */
55 function getInternalLinkAttributes( $link, $text, $class='' ) {
56 $link = urldecode( $link );
57 $link = str_replace( '_', ' ', $link );
58 $link = htmlspecialchars( $link );
59 $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
60 $r .= " title=\"{$link}\"";
61 return $r;
62 }
63
64 /**
65 * @param $nt Title object.
66 * @param $text String: FIXME
67 * @param $class String: CSS class of the link, default ''.
68 */
69 function getInternalLinkAttributesObj( &$nt, $text, $class = '', $titleAttr = false ) {
70 $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
71 if ( $titleAttr === false ) {
72 $r .= ' title="' . $nt->getEscapedText() . '"';
73 } else {
74 $r .= ' title="' . htmlspecialchars( $titleAttr ) . '"';
75 }
76 return $r;
77 }
78
79 /**
80 * Return the CSS colour of a known link
81 *
82 * @param mixed $s
83 * @param integer $threshold user defined threshold
84 * @return string CSS class
85 */
86 function getLinkColour( $s, $threshold ) {
87 if( $s === false ) {
88 return '';
89 }
90
91 $colour = '';
92 if ( !empty( $s->page_is_redirect ) ) {
93 # Page is a redirect
94 $colour = 'mw-redirect';
95 } elseif ( $threshold > 0 && $s->page_len < $threshold && Namespace::isContent( $s->page_namespace ) ) {
96 # Page is a stub
97 $colour = 'stub';
98 }
99 return $colour;
100 }
101
102 /**
103 * This function is a shortcut to makeLinkObj(Title::newFromText($title),...). Do not call
104 * it if you already have a title object handy. See makeLinkObj for further documentation.
105 *
106 * @param $title String: the text of the title
107 * @param $text String: link text
108 * @param $query String: optional query part
109 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
110 * be included in the link text. Other characters will be appended after
111 * the end of the link.
112 */
113 function makeLink( $title, $text = '', $query = '', $trail = '' ) {
114 wfProfileIn( __METHOD__ );
115 $nt = Title::newFromText( $title );
116 if ( $nt instanceof Title ) {
117 $result = $this->makeLinkObj( $nt, $text, $query, $trail );
118 } else {
119 wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
120 $result = $text == "" ? $title : $text;
121 }
122
123 wfProfileOut( __METHOD__ );
124 return $result;
125 }
126
127 /**
128 * This function is a shortcut to makeKnownLinkObj(Title::newFromText($title),...). Do not call
129 * it if you already have a title object handy. See makeKnownLinkObj for further documentation.
130 *
131 * @param $title String: the text of the title
132 * @param $text String: link text
133 * @param $query String: optional query part
134 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
135 * be included in the link text. Other characters will be appended after
136 * the end of the link.
137 */
138 function makeKnownLink( $title, $text = '', $query = '', $trail = '', $prefix = '',$aprops = '') {
139 $nt = Title::newFromText( $title );
140 if ( $nt instanceof Title ) {
141 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix , $aprops );
142 } else {
143 wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
144 return $text == '' ? $title : $text;
145 }
146 }
147
148 /**
149 * This function is a shortcut to makeBrokenLinkObj(Title::newFromText($title),...). Do not call
150 * it if you already have a title object handy. See makeBrokenLinkObj for further documentation.
151 *
152 * @param string $title The text of the title
153 * @param string $text Link text
154 * @param string $query Optional query part
155 * @param string $trail Optional trail. Alphabetic characters at the start of this string will
156 * be included in the link text. Other characters will be appended after
157 * the end of the link.
158 */
159 function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
160 $nt = Title::newFromText( $title );
161 if ( $nt instanceof Title ) {
162 return $this->makeBrokenLinkObj( $nt, $text, $query, $trail );
163 } else {
164 wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
165 return $text == '' ? $title : $text;
166 }
167 }
168
169 /**
170 * @deprecated use makeColouredLinkObj
171 *
172 * This function is a shortcut to makeStubLinkObj(Title::newFromText($title),...). Do not call
173 * it if you already have a title object handy. See makeStubLinkObj for further documentation.
174 *
175 * @param $title String: the text of the title
176 * @param $text String: link text
177 * @param $query String: optional query part
178 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
179 * be included in the link text. Other characters will be appended after
180 * the end of the link.
181 */
182 function makeStubLink( $title, $text = '', $query = '', $trail = '' ) {
183 $nt = Title::newFromText( $title );
184 if ( $nt instanceof Title ) {
185 return $this->makeStubLinkObj( $nt, $text, $query, $trail );
186 } else {
187 wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
188 return $text == '' ? $title : $text;
189 }
190 }
191
192 /**
193 * Make a link for a title which may or may not be in the database. If you need to
194 * call this lots of times, pre-fill the link cache with a LinkBatch, otherwise each
195 * call to this will result in a DB query.
196 *
197 * @param $nt Title: the title object to make the link from, e.g. from
198 * Title::newFromText.
199 * @param $text String: link text
200 * @param $query String: optional query part
201 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
202 * be included in the link text. Other characters will be appended after
203 * the end of the link.
204 * @param $prefix String: optional prefix. As trail, only before instead of after.
205 */
206 function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
207 global $wgUser;
208 wfProfileIn( __METHOD__ );
209
210 if ( !$nt instanceof Title ) {
211 # Fail gracefully
212 wfProfileOut( __METHOD__ );
213 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
214 }
215
216 if ( $nt->isExternal() ) {
217 $u = $nt->getFullURL();
218 $link = $nt->getPrefixedURL();
219 if ( '' == $text ) { $text = $nt->getPrefixedText(); }
220 $style = $this->getInterwikiLinkAttributes( $link, $text, 'extiw' );
221
222 $inside = '';
223 if ( '' != $trail ) {
224 $m = array();
225 if ( preg_match( '/^([a-z]+)(.*)$$/sD', $trail, $m ) ) {
226 $inside = $m[1];
227 $trail = $m[2];
228 }
229 }
230 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
231
232 wfProfileOut( __METHOD__ );
233 return $t;
234 } elseif ( $nt->isAlwaysKnown() ) {
235 # Image links, special page links and self-links with fragements are always known.
236 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
237 } else {
238 wfProfileIn( __METHOD__.'-immediate' );
239
240 # Handles links to special pages which do not exist in the database:
241 if( $nt->getNamespace() == NS_SPECIAL ) {
242 if( SpecialPage::exists( $nt->getDBkey() ) ) {
243 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
244 } else {
245 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
246 }
247 wfProfileOut( __METHOD__.'-immediate' );
248 wfProfileOut( __METHOD__ );
249 return $retVal;
250 }
251
252 # Work out link colour immediately
253 $aid = $nt->getArticleID() ;
254 if ( 0 == $aid ) {
255 $retVal = $this->makeBrokenLinkObj( $nt, $text, $query, $trail, $prefix );
256 } else {
257 $colour = '';
258 if ( $nt->isContentPage() ) {
259 # FIXME: This is stupid, we should combine this query with
260 # the Title::getArticleID() query above.
261 $threshold = $wgUser->getOption('stubthreshold');
262 $dbr = wfGetDB( DB_SLAVE );
263 $s = $dbr->selectRow(
264 array( 'page' ),
265 array( 'page_len', 'page_is_redirect', 'page_namespace' ),
266 array( 'page_id' => $aid ), __METHOD__ ) ;
267 $colour = $this->getLinkColour( $s, $threshold );
268 }
269 $retVal = $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
270 }
271 wfProfileOut( __METHOD__.'-immediate' );
272 }
273 wfProfileOut( __METHOD__ );
274 return $retVal;
275 }
276
277 /**
278 * Make a link for a title which definitely exists. This is faster than makeLinkObj because
279 * it doesn't have to do a database query. It's also valid for interwiki titles and special
280 * pages.
281 *
282 * @param $nt Title object of target page
283 * @param $text String: text to replace the title
284 * @param $query String: link target
285 * @param $trail String: text after link
286 * @param $prefix String: text before link text
287 * @param $aprops String: extra attributes to the a-element
288 * @param $style String: style to apply - if empty, use getInternalLinkAttributesObj instead
289 * @return the a-element
290 */
291 function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
292 wfProfileIn( __METHOD__ );
293
294 if ( !$nt instanceof Title ) {
295 # Fail gracefully
296 wfProfileOut( __METHOD__ );
297 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
298 }
299
300 $u = $nt->escapeLocalURL( $query );
301 if ( $nt->getFragment() != '' ) {
302 if( $nt->getPrefixedDbkey() == '' ) {
303 $u = '';
304 if ( '' == $text ) {
305 $text = htmlspecialchars( $nt->getFragment() );
306 }
307 }
308 $u .= $nt->getFragmentForURL();
309 }
310 if ( $text == '' ) {
311 $text = htmlspecialchars( $nt->getPrefixedText() );
312 }
313 if ( $style == '' ) {
314 $style = $this->getInternalLinkAttributesObj( $nt, $text );
315 }
316
317 if ( $aprops !== '' ) $aprops = ' ' . $aprops;
318
319 list( $inside, $trail ) = Linker::splitTrail( $trail );
320 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
321 wfProfileOut( __METHOD__ );
322 return $r;
323 }
324
325 /**
326 * Make a red link to the edit page of a given title.
327 *
328 * @param $nt Title object of the target page
329 * @param $text String: Link text
330 * @param $query String: Optional query part
331 * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
332 * be included in the link text. Other characters will be appended after
333 * the end of the link.
334 */
335 function makeBrokenLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
336 wfProfileIn( __METHOD__ );
337
338 if ( !$nt instanceof Title ) {
339 # Fail gracefully
340 wfProfileOut( __METHOD__ );
341 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
342 }
343
344 if( $nt->getNamespace() == NS_SPECIAL ) {
345 $q = $query;
346 } else if ( '' == $query ) {
347 $q = 'action=edit&redlink=1';
348 } else {
349 $q = 'action=edit&redlink=1&'.$query;
350 }
351 $u = $nt->escapeLocalURL( $q );
352
353 $titleText = $nt->getPrefixedText();
354 if ( '' == $text ) {
355 $text = htmlspecialchars( $titleText );
356 }
357 $titleAttr = wfMsg( 'red-link-title', $titleText );
358 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'new', $titleAttr );
359 list( $inside, $trail ) = Linker::splitTrail( $trail );
360
361 wfRunHooks( 'BrokenLink', array( &$this, $nt, $query, &$u, &$style, &$prefix, &$text, &$inside, &$trail ) );
362 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
363
364 wfProfileOut( __METHOD__ );
365 return $s;
366 }
367
368 /**
369 * @deprecated use makeColouredLinkObj
370 *
371 * Make a brown link to a short article.
372 *
373 * @param $nt Title object of the target page
374 * @param $text String: link text
375 * @param $query String: optional query part
376 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
377 * be included in the link text. Other characters will be appended after
378 * the end of the link.
379 */
380 function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
381 return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
382 }
383
384 /**
385 * Make a coloured link.
386 *
387 * @param $nt Title object of the target page
388 * @param $colour Integer: colour of the link
389 * @param $text String: link text
390 * @param $query String: optional query part
391 * @param $trail String: optional trail. Alphabetic characters at the start of this string will
392 * be included in the link text. Other characters will be appended after
393 * the end of the link.
394 */
395 function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
396
397 if($colour != ''){
398 $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
399 } else $style = '';
400 return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix, '', $style );
401 }
402
403 /**
404 * Generate either a normal exists-style link or a stub link, depending
405 * on the given page size.
406 *
407 * @param $size Integer
408 * @param $nt Title object.
409 * @param $text String
410 * @param $query String
411 * @param $trail String
412 * @param $prefix String
413 * @return string HTML of link
414 */
415 function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
416 global $wgUser;
417 $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
418 $colour = ( $size < $threshold ) ? 'stub' : '';
419 return $this->makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
420 }
421
422 /**
423 * Make appropriate markup for a link to the current article. This is currently rendered
424 * as the bold link text. The calling sequence is the same as the other make*LinkObj functions,
425 * despite $query not being used.
426 */
427 function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
428 if ( '' == $text ) {
429 $text = htmlspecialchars( $nt->getPrefixedText() );
430 }
431 list( $inside, $trail ) = Linker::splitTrail( $trail );
432 return "<strong class=\"selflink\">{$prefix}{$text}{$inside}</strong>{$trail}";
433 }
434
435 /** @todo document */
436 function fnamePart( $url ) {
437 $basename = strrchr( $url, '/' );
438 if ( false === $basename ) {
439 $basename = $url;
440 } else {
441 $basename = substr( $basename, 1 );
442 }
443 return htmlspecialchars( $basename );
444 }
445
446 /** Obsolete alias */
447 function makeImage( $url, $alt = '' ) {
448 return $this->makeExternalImage( $url, $alt );
449 }
450
451 /** @todo document */
452 function makeExternalImage( $url, $alt = '' ) {
453 if ( '' == $alt ) {
454 $alt = $this->fnamePart( $url );
455 }
456 $s = '<img src="'.$url.'" alt="'.$alt.'" />';
457 return $s;
458 }
459
460 /**
461 * Creates the HTML source for images
462 * @deprecated use makeImageLink2
463 *
464 * @param object $title
465 * @param string $label label text
466 * @param string $alt alt text
467 * @param string $align horizontal alignment: none, left, center, right)
468 * @param array $handlerParams Parameters to be passed to the media handler
469 * @param boolean $framed shows image in original size in a frame
470 * @param boolean $thumb shows image as thumbnail in a frame
471 * @param string $manualthumb image name for the manual thumbnail
472 * @param string $valign vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
473 * @param string $time, timestamp of the file, set as false for current
474 * @return string
475 */
476 function makeImageLinkObj( $title, $label, $alt, $align = '', $handlerParams = array(), $framed = false,
477 $thumb = false, $manualthumb = '', $valign = '', $time = false )
478 {
479 $frameParams = array( 'alt' => $alt, 'caption' => $label );
480 if ( $align ) {
481 $frameParams['align'] = $align;
482 }
483 if ( $framed ) {
484 $frameParams['framed'] = true;
485 }
486 if ( $thumb ) {
487 $frameParams['thumbnail'] = true;
488 }
489 if ( $manualthumb ) {
490 $frameParams['manualthumb'] = $manualthumb;
491 }
492 if ( $valign ) {
493 $frameParams['valign'] = $valign;
494 }
495 $file = wfFindFile( $title, $time );
496 return $this->makeImageLink2( $title, $file, $frameParams, $handlerParams, $time );
497 }
498
499 /**
500 * Given parameters derived from [[Image:Foo|options...]], generate the
501 * HTML that that syntax inserts in the page.
502 *
503 * @param Title $title Title object
504 * @param File $file File object, or false if it doesn't exist
505 *
506 * @param array $frameParams Associative array of parameters external to the media handler.
507 * Boolean parameters are indicated by presence or absence, the value is arbitrary and
508 * will often be false.
509 * thumbnail If present, downscale and frame
510 * manualthumb Image name to use as a thumbnail, instead of automatic scaling
511 * framed Shows image in original size in a frame
512 * frameless Downscale but don't frame
513 * upright If present, tweak default sizes for portrait orientation
514 * upright_factor Fudge factor for "upright" tweak (default 0.75)
515 * border If present, show a border around the image
516 * align Horizontal alignment (left, right, center, none)
517 * valign Vertical alignment (baseline, sub, super, top, text-top, middle,
518 * bottom, text-bottom)
519 * alt Alternate text for image (i.e. alt attribute). Plain text.
520 * caption HTML for image caption.
521 *
522 * @param array $handlerParams Associative array of media handler parameters, to be passed
523 * to transform(). Typical keys are "width" and "page".
524 * @param string $time, timestamp of the file, set as false for current
525 * @return string HTML for an image, with links, wrappers, etc.
526 */
527 function makeImageLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false ) {
528 $res = null;
529 if( !wfRunHooks( 'ImageBeforeProduceHTML', array( &$this, &$title,
530 &$file, &$frameParams, &$handlerParams, &$time, &$res ) ) ) {
531 return $res;
532 }
533
534 global $wgContLang, $wgUser, $wgThumbLimits, $wgThumbUpright;
535 if ( $file && !$file->allowInlineDisplay() ) {
536 wfDebug( __METHOD__.': '.$title->getPrefixedDBkey()." does not allow inline display\n" );
537 return $this->makeKnownLinkObj( $title );
538 }
539
540 // Shortcuts
541 $fp =& $frameParams;
542 $hp =& $handlerParams;
543
544 // Clean up parameters
545 $page = isset( $hp['page'] ) ? $hp['page'] : false;
546 if ( !isset( $fp['align'] ) ) $fp['align'] = '';
547 if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
548
549 $prefix = $postfix = '';
550
551 if ( 'center' == $fp['align'] )
552 {
553 $prefix = '<div class="center">';
554 $postfix = '</div>';
555 $fp['align'] = 'none';
556 }
557 if ( $file && !isset( $hp['width'] ) ) {
558 $hp['width'] = $file->getWidth( $page );
559
560 if( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
561 $wopt = $wgUser->getOption( 'thumbsize' );
562
563 if( !isset( $wgThumbLimits[$wopt] ) ) {
564 $wopt = User::getDefaultOption( 'thumbsize' );
565 }
566
567 // Reduce width for upright images when parameter 'upright' is used
568 if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
569 $fp['upright'] = $wgThumbUpright;
570 }
571 // Use width which is smaller: real image width or user preference width
572 // 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
573 $prefWidth = isset( $fp['upright'] ) ?
574 round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) :
575 $wgThumbLimits[$wopt];
576 if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
577 $hp['width'] = $prefWidth;
578 }
579 }
580 }
581
582 if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
583
584 # Create a thumbnail. Alignment depends on language
585 # writing direction, # right aligned for left-to-right-
586 # languages ("Western languages"), left-aligned
587 # for right-to-left-languages ("Semitic languages")
588 #
589 # If thumbnail width has not been provided, it is set
590 # to the default user option as specified in Language*.php
591 if ( $fp['align'] == '' ) {
592 $fp['align'] = $wgContLang->isRTL() ? 'left' : 'right';
593 }
594 return $prefix.$this->makeThumbLink2( $title, $file, $fp, $hp, $time ).$postfix;
595 }
596
597 if ( $file && isset( $fp['frameless'] ) ) {
598 $srcWidth = $file->getWidth( $page );
599 # For "frameless" option: do not present an image bigger than the source (for bitmap-style images)
600 # This is the same behaviour as the "thumb" option does it already.
601 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
602 $hp['width'] = $srcWidth;
603 }
604 }
605
606 if ( $file && $hp['width'] ) {
607 # Create a resized image, without the additional thumbnail features
608 $thumb = $file->transform( $hp );
609 } else {
610 $thumb = false;
611 }
612
613 if ( !$thumb ) {
614 $s = $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
615 } else {
616 $s = $thumb->toHtml( array(
617 'desc-link' => true,
618 'alt' => $fp['alt'],
619 'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false ,
620 'img-class' => isset( $fp['border'] ) ? 'thumbborder' : false ) );
621 }
622 if ( '' != $fp['align'] ) {
623 $s = "<div class=\"float{$fp['align']}\"><span>{$s}</span></div>";
624 }
625 return str_replace("\n", ' ',$prefix.$s.$postfix);
626 }
627
628 /**
629 * Make HTML for a thumbnail including image, border and caption
630 * @param Title $title
631 * @param File $file File object or false if it doesn't exist
632 */
633 function makeThumbLinkObj( Title $title, $file, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manualthumb = "" ) {
634 $frameParams = array(
635 'alt' => $alt,
636 'caption' => $label,
637 'align' => $align
638 );
639 if ( $framed ) $frameParams['framed'] = true;
640 if ( $manualthumb ) $frameParams['manualthumb'] = $manualthumb;
641 return $this->makeThumbLink2( $title, $file, $frameParams, $params );
642 }
643
644 function makeThumbLink2( Title $title, $file, $frameParams = array(), $handlerParams = array(), $time = false ) {
645 global $wgStylePath, $wgContLang;
646 $exists = $file && $file->exists();
647
648 # Shortcuts
649 $fp =& $frameParams;
650 $hp =& $handlerParams;
651
652 $page = isset( $hp['page'] ) ? $hp['page'] : false;
653 if ( !isset( $fp['align'] ) ) $fp['align'] = 'right';
654 if ( !isset( $fp['alt'] ) ) $fp['alt'] = '';
655 if ( !isset( $fp['caption'] ) ) $fp['caption'] = '';
656
657 if ( empty( $hp['width'] ) ) {
658 // Reduce width for upright images when parameter 'upright' is used
659 $hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
660 }
661 $thumb = false;
662
663 if ( !$exists ) {
664 $outerWidth = $hp['width'] + 2;
665 } else {
666 if ( isset( $fp['manualthumb'] ) ) {
667 # Use manually specified thumbnail
668 $manual_title = Title::makeTitleSafe( NS_IMAGE, $fp['manualthumb'] );
669 if( $manual_title ) {
670 $manual_img = wfFindFile( $manual_title );
671 if ( $manual_img ) {
672 $thumb = $manual_img->getUnscaledThumb();
673 } else {
674 $exists = false;
675 }
676 }
677 } elseif ( isset( $fp['framed'] ) ) {
678 // Use image dimensions, don't scale
679 $thumb = $file->getUnscaledThumb( $page );
680 } else {
681 # Do not present an image bigger than the source, for bitmap-style images
682 # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
683 $srcWidth = $file->getWidth( $page );
684 if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
685 $hp['width'] = $srcWidth;
686 }
687 $thumb = $file->transform( $hp );
688 }
689
690 if ( $thumb ) {
691 $outerWidth = $thumb->getWidth() + 2;
692 } else {
693 $outerWidth = $hp['width'] + 2;
694 }
695 }
696
697 $query = $page ? 'page=' . urlencode( $page ) : '';
698 $url = $title->getLocalURL( $query );
699
700 $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
701
702 $s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
703 if( !$exists ) {
704 $s .= $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
705 $zoomicon = '';
706 } elseif ( !$thumb ) {
707 $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
708 $zoomicon = '';
709 } else {
710 $s .= $thumb->toHtml( array(
711 'alt' => $fp['alt'],
712 'img-class' => 'thumbimage',
713 'desc-link' => true ) );
714 if ( isset( $fp['framed'] ) ) {
715 $zoomicon="";
716 } else {
717 $zoomicon = '<div class="magnify">'.
718 '<a href="'.$url.'" class="internal" title="'.$more.'">'.
719 '<img src="'.$wgStylePath.'/common/images/magnify-clip.png" ' .
720 'width="15" height="11" alt="" /></a></div>';
721 }
722 }
723 $s .= ' <div class="thumbcaption">'.$zoomicon.$fp['caption']."</div></div></div>";
724 return str_replace("\n", ' ', $s);
725 }
726
727 /**
728 * Make a "broken" link to an image
729 *
730 * @param Title $title Image title
731 * @param string $text Link label
732 * @param string $query Query string
733 * @param string $trail Link trail
734 * @param string $prefix Link prefix
735 * @param bool $time, a file of a certain timestamp was requested
736 * @return string
737 */
738 public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '', $time = false ) {
739 global $wgEnableUploads;
740 if( $title instanceof Title ) {
741 wfProfileIn( __METHOD__ );
742 $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
743 if( $wgEnableUploads && !$currentExists ) {
744 $upload = SpecialPage::getTitleFor( 'Upload' );
745 if( $text == '' )
746 $text = htmlspecialchars( $title->getPrefixedText() );
747 $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
748 if( $redir ) {
749 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
750 }
751 $q = 'wpDestFile=' . $title->getPartialUrl();
752 if( $query != '' )
753 $q .= '&' . $query;
754 list( $inside, $trail ) = self::splitTrail( $trail );
755 $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
756 wfProfileOut( __METHOD__ );
757 return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
758 . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
759 } else {
760 wfProfileOut( __METHOD__ );
761 return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
762 }
763 } else {
764 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
765 }
766 }
767
768 /** @deprecated use Linker::makeMediaLinkObj() */
769 function makeMediaLink( $name, $unused = '', $text = '' ) {
770 $nt = Title::makeTitleSafe( NS_IMAGE, $name );
771 return $this->makeMediaLinkObj( $nt, $text );
772 }
773
774 /**
775 * Create a direct link to a given uploaded file.
776 *
777 * @param $title Title object.
778 * @param $text String: pre-sanitized HTML
779 * @return string HTML
780 *
781 * @public
782 * @todo Handle invalid or missing images better.
783 */
784 function makeMediaLinkObj( $title, $text = '' ) {
785 if( is_null( $title ) ) {
786 ### HOTFIX. Instead of breaking, return empty string.
787 return $text;
788 } else {
789 $img = wfFindFile( $title );
790 if( $img ) {
791 $url = $img->getURL();
792 $class = 'internal';
793 } else {
794 $upload = SpecialPage::getTitleFor( 'Upload' );
795 $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDBkey() ) );
796 $class = 'new';
797 }
798 $alt = htmlspecialchars( $title->getText() );
799 if( $text == '' ) {
800 $text = $alt;
801 }
802 $u = htmlspecialchars( $url );
803 return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$text}</a>";
804 }
805 }
806
807 /** @todo document */
808 function specialLink( $name, $key = '' ) {
809 global $wgContLang;
810
811 if ( '' == $key ) { $key = strtolower( $name ); }
812 $pn = $wgContLang->ucfirst( $name );
813 return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
814 wfMsg( $key ) );
815 }
816
817 /** @todo document */
818 function makeExternalLink( $url, $text, $escape = true, $linktype = '', $ns = null ) {
819 $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype );
820 global $wgNoFollowLinks, $wgNoFollowNsExceptions;
821 if( $wgNoFollowLinks && !(isset($ns) && in_array($ns, $wgNoFollowNsExceptions)) ) {
822 $style .= ' rel="nofollow"';
823 }
824 $url = htmlspecialchars( $url );
825 if( $escape ) {
826 $text = htmlspecialchars( $text );
827 }
828 return '<a href="'.$url.'"'.$style.'>'.$text.'</a>';
829 }
830
831 /**
832 * Make user link (or user contributions for unregistered users)
833 * @param $userId Integer: user id in database.
834 * @param $userText String: user name in database
835 * @return string HTML fragment
836 * @private
837 */
838 function userLink( $userId, $userText ) {
839 $encName = htmlspecialchars( $userText );
840 if( $userId == 0 ) {
841 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
842 return $this->makeKnownLinkObj( $contribsPage,
843 $encName);
844 } else {
845 $userPage = Title::makeTitle( NS_USER, $userText );
846 return $this->makeLinkObj( $userPage, $encName );
847 }
848 }
849
850 /**
851 * Generate standard user tool links (talk, contributions, block link, etc.)
852 *
853 * @param int $userId User identifier
854 * @param string $userText User name or IP address
855 * @param bool $redContribsWhenNoEdits Should the contributions link be red if the user has no edits?
856 * @param int $flags Customisation flags (e.g. self::TOOL_LINKS_NOBLOCK)
857 * @return string
858 */
859 public function userToolLinks( $userId, $userText, $redContribsWhenNoEdits = false, $flags = 0 ) {
860 global $wgUser, $wgDisableAnonTalk, $wgSysopUserBans;
861 $talkable = !( $wgDisableAnonTalk && 0 == $userId );
862 $blockable = ( $wgSysopUserBans || 0 == $userId ) && !$flags & self::TOOL_LINKS_NOBLOCK;
863
864 $items = array();
865 if( $talkable ) {
866 $items[] = $this->userTalkLink( $userId, $userText );
867 }
868 if( $userId ) {
869 // check if the user has an edit
870 if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
871 $style = " class='new'";
872 } else {
873 $style = '';
874 }
875 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $userText );
876
877 $items[] = $this->makeKnownLinkObj( $contribsPage, wfMsgHtml( 'contribslink' ), '', '', '', '', $style );
878 }
879 if( $blockable && $wgUser->isAllowed( 'block' ) ) {
880 $items[] = $this->blockLink( $userId, $userText );
881 }
882
883 if( $items ) {
884 return ' (' . implode( ' | ', $items ) . ')';
885 } else {
886 return '';
887 }
888 }
889
890 /**
891 * Alias for userToolLinks( $userId, $userText, true );
892 */
893 public function userToolLinksRedContribs( $userId, $userText ) {
894 return $this->userToolLinks( $userId, $userText, true );
895 }
896
897
898 /**
899 * @param $userId Integer: user id in database.
900 * @param $userText String: user name in database.
901 * @return string HTML fragment with user talk link
902 * @private
903 */
904 function userTalkLink( $userId, $userText ) {
905 $userTalkPage = Title::makeTitle( NS_USER_TALK, $userText );
906 $userTalkLink = $this->makeLinkObj( $userTalkPage, wfMsgHtml( 'talkpagelinktext' ) );
907 return $userTalkLink;
908 }
909
910 /**
911 * @param $userId Integer: userid
912 * @param $userText String: user name in database.
913 * @return string HTML fragment with block link
914 * @private
915 */
916 function blockLink( $userId, $userText ) {
917 $blockPage = SpecialPage::getTitleFor( 'Blockip', $userText );
918 $blockLink = $this->makeKnownLinkObj( $blockPage,
919 wfMsgHtml( 'blocklink' ) );
920 return $blockLink;
921 }
922
923 /**
924 * Generate a user link if the current user is allowed to view it
925 * @param $rev Revision object.
926 * @param $isPublic, bool, show only if all users can see it
927 * @return string HTML
928 */
929 function revUserLink( $rev, $isPublic = false ) {
930 if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
931 $link = wfMsgHtml( 'rev-deleted-user' );
932 } else if( $rev->userCan( Revision::DELETED_USER ) ) {
933 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() );
934 } else {
935 $link = wfMsgHtml( 'rev-deleted-user' );
936 }
937 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
938 return '<span class="history-deleted">' . $link . '</span>';
939 }
940 return $link;
941 }
942
943 /**
944 * Generate a user tool link cluster if the current user is allowed to view it
945 * @param $rev Revision object.
946 * @param $isPublic, bool, show only if all users can see it
947 * @return string HTML
948 */
949 function revUserTools( $rev, $isPublic = false ) {
950 if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
951 $link = wfMsgHtml( 'rev-deleted-user' );
952 } else if( $rev->userCan( Revision::DELETED_USER ) ) {
953 $link = $this->userLink( $rev->getRawUser(), $rev->getRawUserText() ) .
954 ' ' . $this->userToolLinks( $rev->getRawUser(), $rev->getRawUserText() );
955 } else {
956 $link = wfMsgHtml( 'rev-deleted-user' );
957 }
958 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
959 return ' <span class="history-deleted">' . $link . '</span>';
960 }
961 return $link;
962 }
963
964 /**
965 * This function is called by all recent changes variants, by the page history,
966 * and by the user contributions list. It is responsible for formatting edit
967 * comments. It escapes any HTML in the comment, but adds some CSS to format
968 * auto-generated comments (from section editing) and formats [[wikilinks]].
969 *
970 * @author Erik Moeller <moeller@scireview.de>
971 *
972 * Note: there's not always a title to pass to this function.
973 * Since you can't set a default parameter for a reference, I've turned it
974 * temporarily to a value pass. Should be adjusted further. --brion
975 *
976 * @param string $comment
977 * @param mixed $title Title object (to generate link to the section in autocomment) or null
978 * @param bool $local Whether section links should refer to local page
979 */
980 function formatComment($comment, $title = NULL, $local = false) {
981 wfProfileIn( __METHOD__ );
982
983 # Sanitize text a bit:
984 $comment = str_replace( "\n", " ", $comment );
985 $comment = htmlspecialchars( $comment );
986
987 # Render autocomments and make links:
988 $comment = $this->formatAutoComments( $comment, $title, $local );
989 $comment = $this->formatLinksInComment( $comment );
990
991 wfProfileOut( __METHOD__ );
992 return $comment;
993 }
994
995 /**
996 * The pattern for autogen comments is / * foo * /, which makes for
997 * some nasty regex.
998 * We look for all comments, match any text before and after the comment,
999 * add a separator where needed and format the comment itself with CSS
1000 * Called by Linker::formatComment.
1001 *
1002 * @param string $comment Comment text
1003 * @param object $title An optional title object used to links to sections
1004 * @return string $comment formatted comment
1005 *
1006 * @todo Document the $local parameter.
1007 */
1008 private function formatAutocomments( $comment, $title = NULL, $local = false ) {
1009 $match = array();
1010 while (preg_match('!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment,$match)) {
1011 $pre=$match[1];
1012 $auto=$match[2];
1013 $post=$match[3];
1014 $link='';
1015 if( $title ) {
1016 $section = $auto;
1017
1018 # Generate a valid anchor name from the section title.
1019 # Hackish, but should generally work - we strip wiki
1020 # syntax, including the magic [[: that is used to
1021 # "link rather than show" in case of images and
1022 # interlanguage links.
1023 $section = str_replace( '[[:', '', $section );
1024 $section = str_replace( '[[', '', $section );
1025 $section = str_replace( ']]', '', $section );
1026 if ( $local ) {
1027 $sectionTitle = Title::newFromText( '#' . $section);
1028 } else {
1029 $sectionTitle = wfClone( $title );
1030 $sectionTitle->mFragment = $section;
1031 }
1032 $link = $this->makeKnownLinkObj( $sectionTitle, wfMsgForContent( 'sectionlink' ) );
1033 }
1034 $auto = $link . $auto;
1035 if( $pre ) {
1036 # written summary $presep autocomment (summary /* section */)
1037 $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
1038 }
1039 if( $post ) {
1040 # autocomment $postsep written summary (/* section */ summary)
1041 $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
1042 }
1043 $auto = '<span class="autocomment">' . $auto . '</span>';
1044 $comment = $pre . $auto . $post;
1045 }
1046
1047 return $comment;
1048 }
1049
1050 /**
1051 * Formats wiki links and media links in text; all other wiki formatting
1052 * is ignored
1053 *
1054 * @fixme doesn't handle sub-links as in image thumb texts like the main parser
1055 * @param string $comment Text to format links in
1056 * @return string
1057 */
1058 public function formatLinksInComment( $comment ) {
1059 return preg_replace_callback(
1060 '/\[\[:?(.*?)(\|(.*?))*\]\]([^[]*)/',
1061 array( $this, 'formatLinksInCommentCallback' ),
1062 $comment );
1063 }
1064
1065 protected function formatLinksInCommentCallback( $match ) {
1066 global $wgContLang;
1067
1068 $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
1069 $medians .= preg_quote( $wgContLang->getNsText( NS_MEDIA ), '/' ) . '):';
1070
1071 $comment = $match[0];
1072
1073 # Handle link renaming [[foo|text]] will show link as "text"
1074 if( "" != $match[3] ) {
1075 $text = $match[3];
1076 } else {
1077 $text = $match[1];
1078 }
1079 $submatch = array();
1080 if( preg_match( '/^' . $medians . '(.*)$/i', $match[1], $submatch ) ) {
1081 # Media link; trail not supported.
1082 $linkRegexp = '/\[\[(.*?)\]\]/';
1083 $thelink = $this->makeMediaLink( $submatch[1], "", $text );
1084 } else {
1085 # Other kind of link
1086 if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
1087 $trail = $submatch[1];
1088 } else {
1089 $trail = "";
1090 }
1091 $linkRegexp = '/\[\[(.*?)\]\]' . preg_quote( $trail, '/' ) . '/';
1092 if (isset($match[1][0]) && $match[1][0] == ':')
1093 $match[1] = substr($match[1], 1);
1094 $thelink = $this->makeLink( $match[1], $text, "", $trail );
1095 }
1096 $comment = preg_replace( $linkRegexp, StringUtils::escapeRegexReplacement( $thelink ), $comment, 1 );
1097
1098 return $comment;
1099 }
1100
1101 /**
1102 * Wrap a comment in standard punctuation and formatting if
1103 * it's non-empty, otherwise return empty string.
1104 *
1105 * @param string $comment
1106 * @param mixed $title Title object (to generate link to section in autocomment) or null
1107 * @param bool $local Whether section links should refer to local page
1108 *
1109 * @return string
1110 */
1111 function commentBlock( $comment, $title = NULL, $local = false ) {
1112 // '*' used to be the comment inserted by the software way back
1113 // in antiquity in case none was provided, here for backwards
1114 // compatability, acc. to brion -ævar
1115 if( $comment == '' || $comment == '*' ) {
1116 return '';
1117 } else {
1118 $formatted = $this->formatComment( $comment, $title, $local );
1119 return " <span class=\"comment\">($formatted)</span>";
1120 }
1121 }
1122
1123 /**
1124 * Wrap and format the given revision's comment block, if the current
1125 * user is allowed to view it.
1126 *
1127 * @param Revision $rev
1128 * @param bool $local Whether section links should refer to local page
1129 * @param $isPublic, show only if all users can see it
1130 * @return string HTML
1131 */
1132 function revComment( Revision $rev, $local = false, $isPublic = false ) {
1133 if( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic ) {
1134 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1135 } else if( $rev->userCan( Revision::DELETED_COMMENT ) ) {
1136 $block = $this->commentBlock( $rev->getRawComment(), $rev->getTitle(), $local );
1137 } else {
1138 $block = " <span class=\"comment\">" . wfMsgHtml( 'rev-deleted-comment' ) . "</span>";
1139 }
1140 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
1141 return " <span class=\"history-deleted\">$block</span>";
1142 }
1143 return $block;
1144 }
1145
1146 /** @todo document */
1147 function tocIndent() {
1148 return "\n<ul>";
1149 }
1150
1151 /** @todo document */
1152 function tocUnindent($level) {
1153 return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
1154 }
1155
1156 /**
1157 * parameter level defines if we are on an indentation level
1158 */
1159 function tocLine( $anchor, $tocline, $tocnumber, $level ) {
1160 return "\n<li class=\"toclevel-$level\"><a href=\"#" .
1161 $anchor . '"><span class="tocnumber">' .
1162 $tocnumber . '</span> <span class="toctext">' .
1163 $tocline . '</span></a>';
1164 }
1165
1166 /** @todo document */
1167 function tocLineEnd() {
1168 return "</li>\n";
1169 }
1170
1171 /** @todo document */
1172 function tocList($toc) {
1173 global $wgJsMimeType;
1174 $title = wfMsgHtml('toc') ;
1175 return
1176 '<table id="toc" class="toc" summary="' . $title .'"><tr><td>'
1177 . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
1178 . $toc
1179 # no trailing newline, script should not be wrapped in a
1180 # paragraph
1181 . "</ul>\n</td></tr></table>"
1182 . '<script type="' . $wgJsMimeType . '">'
1183 . ' if (window.showTocToggle) {'
1184 . ' var tocShowText = "' . wfEscapeJsString( wfMsg('showtoc') ) . '";'
1185 . ' var tocHideText = "' . wfEscapeJsString( wfMsg('hidetoc') ) . '";'
1186 . ' showTocToggle();'
1187 . ' } '
1188 . "</script>\n";
1189 }
1190
1191 /**
1192 * Used to generate section edit links that point to "other" pages
1193 * (sections that are really part of included pages).
1194 *
1195 * @param $title Title string.
1196 * @param $section Integer: section number.
1197 */
1198 public function editSectionLinkForOther( $title, $section ) {
1199 $title = Title::newFromText( $title );
1200 return $this->doEditSectionLink( $title, $section, '', 'EditSectionLinkForOther' );
1201 }
1202
1203 /**
1204 * @param $nt Title object.
1205 * @param $section Integer: section number.
1206 * @param $hint Link String: title, or default if omitted or empty
1207 */
1208 public function editSectionLink( Title $nt, $section, $hint='' ) {
1209 if( $hint != '' ) {
1210 $hint = wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) );
1211 $hint = " title=\"$hint\"";
1212 }
1213 return $this->doEditSectionLink( $nt, $section, $hint, 'EditSectionLink' );
1214 }
1215
1216 /**
1217 * Implement editSectionLink and editSectionLinkForOther.
1218 *
1219 * @param $nt Title object
1220 * @param $section Integer, section number
1221 * @param $hint String, for HTML title attribute
1222 * @param $hook String, name of hook to run
1223 * @return String, HTML to use for edit link
1224 */
1225 protected function doEditSectionLink( Title $nt, $section, $hint, $hook ) {
1226 global $wgContLang;
1227 $editurl = '&section='.$section;
1228 $url = $this->makeKnownLinkObj(
1229 $nt,
1230 wfMsg('editsection'),
1231 'action=edit'.$editurl,
1232 '', '', '', $hint
1233 );
1234 $result = null;
1235
1236 // The two hooks have slightly different interfaces . . .
1237 if( $hook == 'EditSectionLink' ) {
1238 wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $hint, $url, &$result ) );
1239 } elseif( $hook == 'EditSectionLinkForOther' ) {
1240 wfRunHooks( 'EditSectionLinkForOther', array( &$this, $nt, $section, $url, &$result ) );
1241 }
1242
1243 // For reverse compatibility, add the brackets *after* the hook is run,
1244 // and even add them to hook-provided text.
1245 if( is_null( $result ) ) {
1246 $result = wfMsg( 'editsection-brackets', $url );
1247 } else {
1248 $result = wfMsg( 'editsection-brackets', $result );
1249 }
1250 return "<span class=\"editsection\">$result</span>";
1251 }
1252
1253 /**
1254 * Create a headline for content
1255 *
1256 * @param int $level The level of the headline (1-6)
1257 * @param string $attribs Any attributes for the headline, starting with a space and ending with '>'
1258 * This *must* be at least '>' for no attribs
1259 * @param string $anchor The anchor to give the headline (the bit after the #)
1260 * @param string $text The text of the header
1261 * @param string $link HTML to add for the section edit link
1262 *
1263 * @return string HTML headline
1264 */
1265 public function makeHeadline( $level, $attribs, $anchor, $text, $link ) {
1266 return "<a name=\"$anchor\"></a><h$level$attribs$link <span class=\"mw-headline\">$text</span></h$level>";
1267 }
1268
1269 /**
1270 * Split a link trail, return the "inside" portion and the remainder of the trail
1271 * as a two-element array
1272 *
1273 * @static
1274 */
1275 static function splitTrail( $trail ) {
1276 static $regex = false;
1277 if ( $regex === false ) {
1278 global $wgContLang;
1279 $regex = $wgContLang->linkTrail();
1280 }
1281 $inside = '';
1282 if ( '' != $trail ) {
1283 $m = array();
1284 if ( preg_match( $regex, $trail, $m ) ) {
1285 $inside = $m[1];
1286 $trail = $m[2];
1287 }
1288 }
1289 return array( $inside, $trail );
1290 }
1291
1292 /**
1293 * Generate a rollback link for a given revision. Currently it's the
1294 * caller's responsibility to ensure that the revision is the top one. If
1295 * it's not, of course, the user will get an error message.
1296 *
1297 * If the calling page is called with the parameter &bot=1, all rollback
1298 * links also get that parameter. It causes the edit itself and the rollback
1299 * to be marked as "bot" edits. Bot edits are hidden by default from recent
1300 * changes, so this allows sysops to combat a busy vandal without bothering
1301 * other users.
1302 *
1303 * @param Revision $rev
1304 */
1305 function generateRollback( $rev ) {
1306 return '<span class="mw-rollback-link">['
1307 . $this->buildRollbackLink( $rev )
1308 . ']</span>';
1309 }
1310
1311 /**
1312 * Build a raw rollback link, useful for collections of "tool" links
1313 *
1314 * @param Revision $rev
1315 * @return string
1316 */
1317 public function buildRollbackLink( $rev ) {
1318 global $wgRequest, $wgUser;
1319 $title = $rev->getTitle();
1320 $extra = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
1321 $extra .= '&token=' . urlencode( $wgUser->editToken( array( $title->getPrefixedText(),
1322 $rev->getUserText() ) ) );
1323 return $this->makeKnownLinkObj(
1324 $title,
1325 wfMsgHtml( 'rollbacklink' ),
1326 'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extra
1327 );
1328 }
1329
1330 /**
1331 * Returns HTML for the "templates used on this page" list.
1332 *
1333 * @param array $templates Array of templates from Article::getUsedTemplate
1334 * or similar
1335 * @param bool $preview Whether this is for a preview
1336 * @param bool $section Whether this is for a section edit
1337 * @return string HTML output
1338 */
1339 public function formatTemplates( $templates, $preview = false, $section = false) {
1340 global $wgUser;
1341 wfProfileIn( __METHOD__ );
1342
1343 $sk = $wgUser->getSkin();
1344
1345 $outText = '';
1346 if ( count( $templates ) > 0 ) {
1347 # Do a batch existence check
1348 $batch = new LinkBatch;
1349 foreach( $templates as $title ) {
1350 $batch->addObj( $title );
1351 }
1352 $batch->execute();
1353
1354 # Construct the HTML
1355 $outText = '<div class="mw-templatesUsedExplanation">';
1356 if ( $preview ) {
1357 $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
1358 } elseif ( $section ) {
1359 $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
1360 } else {
1361 $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
1362 }
1363 $outText .= '</div><ul>';
1364
1365 foreach ( $templates as $titleObj ) {
1366 $r = $titleObj->getRestrictions( 'edit' );
1367 if ( in_array( 'sysop', $r ) ) {
1368 $protected = wfMsgExt( 'template-protected', array( 'parseinline' ) );
1369 } elseif ( in_array( 'autoconfirmed', $r ) ) {
1370 $protected = wfMsgExt( 'template-semiprotected', array( 'parseinline' ) );
1371 } else {
1372 $protected = '';
1373 }
1374 $outText .= '<li>' . $sk->makeLinkObj( $titleObj ) . ' ' . $protected . '</li>';
1375 }
1376 $outText .= '</ul>';
1377 }
1378 wfProfileOut( __METHOD__ );
1379 return $outText;
1380 }
1381
1382 /**
1383 * Returns HTML for the "hidden categories on this page" list.
1384 *
1385 * @param array $hiddencats Array of hidden categories from Article::getHiddenCategories
1386 * or similar
1387 * @return string HTML output
1388 */
1389 public function formatHiddenCategories( $hiddencats) {
1390 global $wgUser, $wgLang;
1391 wfProfileIn( __METHOD__ );
1392
1393 $sk = $wgUser->getSkin();
1394
1395 $outText = '';
1396 if ( count( $hiddencats ) > 0 ) {
1397 # Construct the HTML
1398 $outText = '<div class="mw-hiddenCategoriesExplanation">';
1399 $outText .= wfMsgExt( 'hiddencategories', array( 'parse' ), $wgLang->formatnum( count( $hiddencats ) ) );
1400 $outText .= '</div><ul>';
1401
1402 foreach ( $hiddencats as $titleObj ) {
1403 $outText .= '<li>' . $sk->makeKnownLinkObj( $titleObj ) . '</li>'; # If it's hidden, it must exist - no need to check with a LinkBatch
1404 }
1405 $outText .= '</ul>';
1406 }
1407 wfProfileOut( __METHOD__ );
1408 return $outText;
1409 }
1410
1411 /**
1412 * Format a size in bytes for output, using an appropriate
1413 * unit (B, KB, MB or GB) according to the magnitude in question
1414 *
1415 * @param $size Size to format
1416 * @return string
1417 */
1418 public function formatSize( $size ) {
1419 global $wgLang;
1420 return htmlspecialchars( $wgLang->formatSize( $size ) );
1421 }
1422
1423 /**
1424 * Given the id of an interface element, constructs the appropriate title
1425 * and accesskey attributes from the system messages. (Note, this is usu-
1426 * ally the id but isn't always, because sometimes the accesskey needs to
1427 * go on a different element than the id, for reverse-compatibility, etc.)
1428 *
1429 * @param string $name Id of the element, minus prefixes.
1430 * @return string title and accesskey attributes, ready to drop in an
1431 * element (e.g., ' title="This does something [x]" accesskey="x"').
1432 */
1433 public function tooltipAndAccesskey($name) {
1434 $fname="Linker::tooltipAndAccesskey";
1435 wfProfileIn($fname);
1436 $out = '';
1437
1438 $tooltip = wfMsg('tooltip-'.$name);
1439 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1440 // Compatibility: formerly some tooltips had [alt-.] hardcoded
1441 $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1442 $out .= ' title="'.htmlspecialchars($tooltip);
1443 }
1444 $accesskey = wfMsg('accesskey-'.$name);
1445 if ($accesskey && $accesskey != '-' && !wfEmptyMsg('accesskey-'.$name, $accesskey)) {
1446 if ($out) $out .= " [$accesskey]\" accesskey=\"$accesskey\"";
1447 else $out .= " title=\"[$accesskey]\" accesskey=\"$accesskey\"";
1448 } elseif ($out) {
1449 $out .= '"';
1450 }
1451 wfProfileOut($fname);
1452 return $out;
1453 }
1454
1455 /**
1456 * Given the id of an interface element, constructs the appropriate title
1457 * attribute from the system messages. (Note, this is usually the id but
1458 * isn't always, because sometimes the accesskey needs to go on a different
1459 * element than the id, for reverse-compatibility, etc.)
1460 *
1461 * @param string $name Id of the element, minus prefixes.
1462 * @return string title attribute, ready to drop in an element
1463 * (e.g., ' title="This does something"').
1464 */
1465 public function tooltip($name) {
1466 $out = '';
1467
1468 $tooltip = wfMsg('tooltip-'.$name);
1469 if (!wfEmptyMsg('tooltip-'.$name, $tooltip) && $tooltip != '-') {
1470 $out = ' title="'.htmlspecialchars($tooltip).'"';
1471 }
1472
1473 return $out;
1474 }
1475 }