/
Linker.php
2441 lines (2240 loc) 路 80.2 KB
/
Linker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Methods to make links and related items.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Some internal bits split of from Skin.php. These functions are used
* for primarily page content: links, embedded images, table of contents. Links
* are also used in the skin.
*
* @ingroup Skins
*/
class Linker {
/**
* Flags for userToolLinks()
*/
const TOOL_LINKS_NOBLOCK = 1;
const TOOL_LINKS_EMAIL = 2;
/**
* Get the appropriate HTML attributes to add to the "a" element of an
* external link, as created by [wikisyntax].
*
* @param string $class the contents of the class attribute; if an empty
* string is passed, which is the default value, defaults to 'external'.
* @return string
* @deprecated since 1.18 Just pass the external class directly to something using Html::expandAttributes
*/
static function getExternalLinkAttributes( $class = 'external' ) {
wfDeprecated( __METHOD__, '1.18' );
return self::getLinkAttributesInternal( '', $class );
}
/**
* Get the appropriate HTML attributes to add to the "a" element of an interwiki link.
*
* @param string $title the title text for the link, URL-encoded (???) but
* not HTML-escaped
* @param string $unused unused
* @param string $class the contents of the class attribute; if an empty
* string is passed, which is the default value, defaults to 'external'.
* @return string
*/
static function getInterwikiLinkAttributes( $title, $unused = null, $class = 'external' ) {
global $wgContLang;
# @todo FIXME: We have a whole bunch of handling here that doesn't happen in
# getExternalLinkAttributes, why?
$title = urldecode( $title );
$title = $wgContLang->checkTitleEncoding( $title );
$title = preg_replace( '/[\\x00-\\x1f]/', ' ', $title );
return self::getLinkAttributesInternal( $title, $class );
}
/**
* Get the appropriate HTML attributes to add to the "a" element of an internal link.
*
* @param string $title the title text for the link, URL-encoded (???) but
* not HTML-escaped
* @param string $unused unused
* @param string $class the contents of the class attribute, default none
* @return string
*/
static function getInternalLinkAttributes( $title, $unused = null, $class = '' ) {
$title = urldecode( $title );
$title = str_replace( '_', ' ', $title );
return self::getLinkAttributesInternal( $title, $class );
}
/**
* Get the appropriate HTML attributes to add to the "a" element of an internal
* link, given the Title object for the page we want to link to.
*
* @param $nt Title
* @param string $unused unused
* @param string $class the contents of the class attribute, default none
* @param $title Mixed: optional (unescaped) string to use in the title
* attribute; if false, default to the name of the page we're linking to
* @return string
*/
static function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
if ( $title === false ) {
$title = $nt->getPrefixedText();
}
return self::getLinkAttributesInternal( $title, $class );
}
/**
* Common code for getLinkAttributesX functions
*
* @param $title string
* @param $class string
*
* @return string
*/
private static function getLinkAttributesInternal( $title, $class ) {
$title = htmlspecialchars( $title );
$class = htmlspecialchars( $class );
$r = '';
if ( $class != '' ) {
$r .= " class=\"$class\"";
}
if ( $title != '' ) {
$r .= " title=\"$title\"";
}
return $r;
}
/**
* Return the CSS colour of a known link
*
* @param $t Title object
* @param $threshold Integer: user defined threshold
* @return String: CSS class
*/
public static function getLinkColour( $t, $threshold ) {
$colour = '';
if ( $t->isRedirect() ) {
# Page is a redirect
$colour = 'mw-redirect';
} elseif ( $threshold > 0 && $t->isContentPage() &&
$t->exists() && $t->getLength() < $threshold
) {
# Page is a stub
$colour = 'stub';
}
return $colour;
}
/**
* This function returns an HTML link to the given target. It serves a few
* purposes:
* 1) If $target is a Title, the correct URL to link to will be figured
* out automatically.
* 2) It automatically adds the usual classes for various types of link
* targets: "new" for red links, "stub" for short articles, etc.
* 3) It escapes all attribute values safely so there's no risk of XSS.
* 4) It provides a default tooltip if the target is a Title (the page
* name of the target).
* link() replaces the old functions in the makeLink() family.
*
* @since 1.18 Method exists since 1.16 as non-static, made static in 1.18.
* You can call it using this if you want to keep compat with these:
* $linker = class_exists( 'DummyLinker' ) ? new DummyLinker() : new Linker();
* $linker->link( ... );
*
* @param $target Title Can currently only be a Title, but this may
* change to support Images, literal URLs, etc.
* @param $html string The HTML contents of the <a> element, i.e.,
* the link text. This is raw HTML and will not be escaped. If null,
* defaults to the prefixed text of the Title; or if the Title is just a
* fragment, the contents of the fragment.
* @param array $customAttribs A key => value array of extra HTML attributes,
* such as title and class. (href is ignored.) Classes will be
* merged with the default classes, while other attributes will replace
* default attributes. All passed attribute values will be HTML-escaped.
* A false attribute value means to suppress that attribute.
* @param $query array The query string to append to the URL
* you're linking to, in key => value array form. Query keys and values
* will be URL-encoded.
* @param string|array $options String or array of strings:
* 'known': Page is known to exist, so don't check if it does.
* 'broken': Page is known not to exist, so don't check if it does.
* 'noclasses': Don't add any classes automatically (includes "new",
* "stub", "mw-redirect", "extiw"). Only use the class attribute
* provided, if any, so you get a simple blue link with no funny i-
* cons.
* 'forcearticlepath': Use the article path always, even with a querystring.
* Has compatibility issues on some setups, so avoid wherever possible.
* 'http': Force a full URL with http:// as the scheme.
* 'https': Force a full URL with https:// as the scheme.
* @return string HTML <a> attribute
*/
public static function link(
$target, $html = null, $customAttribs = array(), $query = array(), $options = array()
) {
wfProfileIn( __METHOD__ );
if ( !$target instanceof Title ) {
wfProfileOut( __METHOD__ );
return "<!-- ERROR -->$html";
}
if ( is_string( $query ) ) {
// some functions withing core using this still hand over query strings
wfDeprecated( __METHOD__ . ' with parameter $query as string (should be array)', '1.20' );
$query = wfCgiToArray( $query );
}
$options = (array)$options;
$dummy = new DummyLinker; // dummy linker instance for bc on the hooks
$ret = null;
if ( !wfRunHooks( 'LinkBegin', array( $dummy, $target, &$html,
&$customAttribs, &$query, &$options, &$ret ) ) ) {
wfProfileOut( __METHOD__ );
return $ret;
}
# Normalize the Title if it's a special page
$target = self::normaliseSpecialPage( $target );
# If we don't know whether the page exists, let's find out.
wfProfileIn( __METHOD__ . '-checkPageExistence' );
if ( !in_array( 'known', $options ) and !in_array( 'broken', $options ) ) {
if ( $target->isKnown() ) {
$options[] = 'known';
} else {
$options[] = 'broken';
}
}
wfProfileOut( __METHOD__ . '-checkPageExistence' );
$oldquery = array();
if ( in_array( "forcearticlepath", $options ) && $query ) {
$oldquery = $query;
$query = array();
}
# Note: we want the href attribute first, for prettiness.
$attribs = array( 'href' => self::linkUrl( $target, $query, $options ) );
if ( in_array( 'forcearticlepath', $options ) && $oldquery ) {
$attribs['href'] = wfAppendQuery( $attribs['href'], $oldquery );
}
$attribs = array_merge(
$attribs,
self::linkAttribs( $target, $customAttribs, $options )
);
if ( is_null( $html ) ) {
$html = self::linkText( $target );
}
$ret = null;
if ( wfRunHooks( 'LinkEnd', array( $dummy, $target, $options, &$html, &$attribs, &$ret ) ) ) {
$ret = Html::rawElement( 'a', $attribs, $html );
}
wfProfileOut( __METHOD__ );
return $ret;
}
/**
* Identical to link(), except $options defaults to 'known'.
* @return string
*/
public static function linkKnown(
$target, $html = null, $customAttribs = array(),
$query = array(), $options = array( 'known', 'noclasses' ) )
{
return self::link( $target, $html, $customAttribs, $query, $options );
}
/**
* Returns the Url used to link to a Title
*
* @param $target Title
* @param array $query query parameters
* @param $options Array
* @return String
*/
private static function linkUrl( $target, $query, $options ) {
wfProfileIn( __METHOD__ );
# We don't want to include fragments for broken links, because they
# generally make no sense.
if ( in_array( 'broken', $options ) && $target->mFragment !== '' ) {
$target = clone $target;
$target->mFragment = '';
}
# If it's a broken link, add the appropriate query pieces, unless
# there's already an action specified, or unless 'edit' makes no sense
# (i.e., for a nonexistent special page).
if ( in_array( 'broken', $options ) && empty( $query['action'] )
&& !$target->isSpecialPage() ) {
$query['action'] = 'edit';
$query['redlink'] = '1';
}
if ( in_array( 'http', $options ) ) {
$proto = PROTO_HTTP;
} elseif ( in_array( 'https', $options ) ) {
$proto = PROTO_HTTPS;
} else {
$proto = PROTO_RELATIVE;
}
$ret = $target->getLinkURL( $query, false, $proto );
wfProfileOut( __METHOD__ );
return $ret;
}
/**
* Returns the array of attributes used when linking to the Title $target
*
* @param $target Title
* @param $attribs
* @param $options
*
* @return array
*/
private static function linkAttribs( $target, $attribs, $options ) {
wfProfileIn( __METHOD__ );
global $wgUser;
$defaults = array();
if ( !in_array( 'noclasses', $options ) ) {
wfProfileIn( __METHOD__ . '-getClasses' );
# Now build the classes.
$classes = array();
if ( in_array( 'broken', $options ) ) {
$classes[] = 'new';
}
if ( $target->isExternal() ) {
$classes[] = 'extiw';
}
if ( !in_array( 'broken', $options ) ) { # Avoid useless calls to LinkCache (see r50387)
$colour = self::getLinkColour( $target, $wgUser->getStubThreshold() );
if ( $colour !== '' ) {
$classes[] = $colour; # mw-redirect or stub
}
}
if ( $classes != array() ) {
$defaults['class'] = implode( ' ', $classes );
}
wfProfileOut( __METHOD__ . '-getClasses' );
}
# Get a default title attribute.
if ( $target->getPrefixedText() == '' ) {
# A link like [[#Foo]]. This used to mean an empty title
# attribute, but that's silly. Just don't output a title.
} elseif ( in_array( 'known', $options ) ) {
$defaults['title'] = $target->getPrefixedText();
} else {
$defaults['title'] = wfMessage( 'red-link-title', $target->getPrefixedText() )->text();
}
# Finally, merge the custom attribs with the default ones, and iterate
# over that, deleting all "false" attributes.
$ret = array();
$merged = Sanitizer::mergeAttributes( $defaults, $attribs );
foreach ( $merged as $key => $val ) {
# A false value suppresses the attribute, and we don't want the
# href attribute to be overridden.
if ( $key != 'href' and $val !== false ) {
$ret[$key] = $val;
}
}
wfProfileOut( __METHOD__ );
return $ret;
}
/**
* Default text of the links to the Title $target
*
* @param $target Title
*
* @return string
*/
private static function linkText( $target ) {
// We might be passed a non-Title by make*LinkObj(). Fail gracefully.
if ( !$target instanceof Title ) {
return '';
}
// If the target is just a fragment, with no title, we return the fragment
// text. Otherwise, we return the title text itself.
if ( $target->getPrefixedText() === '' && $target->getFragment() !== '' ) {
return htmlspecialchars( $target->getFragment() );
}
return htmlspecialchars( $target->getPrefixedText() );
}
/**
* Generate either a normal exists-style link or a stub link, depending
* on the given page size.
*
* @param $size Integer
* @param $nt Title object.
* @param $text String
* @param $query String
* @param $trail String
* @param $prefix String
* @return string HTML of link
* @deprecated since 1.17
*/
static function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
global $wgUser;
wfDeprecated( __METHOD__, '1.17' );
$threshold = $wgUser->getStubThreshold();
$colour = ( $size < $threshold ) ? 'stub' : '';
// @todo FIXME: Replace deprecated makeColouredLinkObj by link()
return self::makeColouredLinkObj( $nt, $colour, $text, $query, $trail, $prefix );
}
/**
* Make appropriate markup for a link to the current article. This is currently rendered
* as the bold link text. The calling sequence is the same as the other make*LinkObj static functions,
* despite $query not being used.
*
* @param $nt Title
* @param string $html [optional]
* @param string $query [optional]
* @param string $trail [optional]
* @param string $prefix [optional]
*
*
* @return string
*/
public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) {
if ( $html == '' ) {
$html = htmlspecialchars( $nt->getPrefixedText() );
}
list( $inside, $trail ) = self::splitTrail( $trail );
return "<strong class=\"selflink\">{$prefix}{$html}{$inside}</strong>{$trail}";
}
/**
* Get a message saying that an invalid title was encountered.
* This should be called after a method like Title::makeTitleSafe() returned
* a value indicating that the title object is invalid.
*
* @param $context IContextSource context to use to get the messages
* @param int $namespace Namespace number
* @param string $title Text of the title, without the namespace part
* @return string
*/
public static function getInvalidTitleDescription( IContextSource $context, $namespace, $title ) {
global $wgContLang;
// First we check whether the namespace exists or not.
if ( MWNamespace::exists( $namespace ) ) {
if ( $namespace == NS_MAIN ) {
$name = $context->msg( 'blanknamespace' )->text();
} else {
$name = $wgContLang->getFormattedNsText( $namespace );
}
return $context->msg( 'invalidtitle-knownnamespace', $namespace, $name, $title )->text();
} else {
return $context->msg( 'invalidtitle-unknownnamespace', $namespace, $title )->text();
}
}
/**
* @param $title Title
* @return Title
*/
static function normaliseSpecialPage( Title $title ) {
if ( $title->isSpecialPage() ) {
list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
if ( !$name ) {
return $title;
}
$ret = SpecialPage::getTitleFor( $name, $subpage );
$ret->mFragment = $title->getFragment();
return $ret;
} else {
return $title;
}
}
/**
* Returns the filename part of an url.
* Used as alternative text for external images.
*
* @param $url string
*
* @return string
*/
private static function fnamePart( $url ) {
$basename = strrchr( $url, '/' );
if ( false === $basename ) {
$basename = $url;
} else {
$basename = substr( $basename, 1 );
}
return $basename;
}
/**
* Return the code for images which were added via external links,
* via Parser::maybeMakeExternalImage().
*
* @param $url
* @param $alt
*
* @return string
*/
public static function makeExternalImage( $url, $alt = '' ) {
if ( $alt == '' ) {
$alt = self::fnamePart( $url );
}
$img = '';
$success = wfRunHooks( 'LinkerMakeExternalImage', array( &$url, &$alt, &$img ) );
if ( !$success ) {
wfDebug( "Hook LinkerMakeExternalImage changed the output of external image with url {$url} and alt text {$alt} to {$img}\n", true );
return $img;
}
return Html::element( 'img',
array(
'src' => $url,
'alt' => $alt ) );
}
/**
* Given parameters derived from [[Image:Foo|options...]], generate the
* HTML that that syntax inserts in the page.
*
* @param $parser Parser object
* @param $title Title object of the file (not the currently viewed page)
* @param $file File object, or false if it doesn't exist
* @param array $frameParams associative array of parameters external to the media handler.
* Boolean parameters are indicated by presence or absence, the value is arbitrary and
* will often be false.
* thumbnail If present, downscale and frame
* manualthumb Image name to use as a thumbnail, instead of automatic scaling
* framed Shows image in original size in a frame
* frameless Downscale but don't frame
* upright If present, tweak default sizes for portrait orientation
* upright_factor Fudge factor for "upright" tweak (default 0.75)
* border If present, show a border around the image
* align Horizontal alignment (left, right, center, none)
* valign Vertical alignment (baseline, sub, super, top, text-top, middle,
* bottom, text-bottom)
* alt Alternate text for image (i.e. alt attribute). Plain text.
* class HTML for image classes. Plain text.
* caption HTML for image caption.
* link-url URL to link to
* link-title Title object to link to
* link-target Value for the target attribute, only with link-url
* no-link Boolean, suppress description link
*
* @param array $handlerParams associative array of media handler parameters, to be passed
* to transform(). Typical keys are "width" and "page".
* @param string $time timestamp of the file, set as false for current
* @param string $query query params for desc url
* @param $widthOption: Used by the parser to remember the user preference thumbnailsize
* @since 1.20
* @return String: HTML for an image, with links, wrappers, etc.
*/
public static function makeImageLink( /*Parser*/ $parser, Title $title, $file, $frameParams = array(),
$handlerParams = array(), $time = false, $query = "", $widthOption = null )
{
$res = null;
$dummy = new DummyLinker;
if ( !wfRunHooks( 'ImageBeforeProduceHTML', array( &$dummy, &$title,
&$file, &$frameParams, &$handlerParams, &$time, &$res ) ) ) {
return $res;
}
if ( $file && !$file->allowInlineDisplay() ) {
wfDebug( __METHOD__ . ': ' . $title->getPrefixedDBkey() . " does not allow inline display\n" );
return self::link( $title );
}
// Shortcuts
$fp =& $frameParams;
$hp =& $handlerParams;
// Clean up parameters
$page = isset( $hp['page'] ) ? $hp['page'] : false;
if ( !isset( $fp['align'] ) ) {
$fp['align'] = '';
}
if ( !isset( $fp['alt'] ) ) {
$fp['alt'] = '';
}
if ( !isset( $fp['title'] ) ) {
$fp['title'] = '';
}
if ( !isset( $fp['class'] ) ) {
$fp['class'] = '';
}
$prefix = $postfix = '';
if ( 'center' == $fp['align'] ) {
$prefix = '<div class="center">';
$postfix = '</div>';
$fp['align'] = 'none';
}
if ( $file && !isset( $hp['width'] ) ) {
if ( isset( $hp['height'] ) && $file->isVectorized() ) {
// If its a vector image, and user only specifies height
// we don't want it to be limited by its "normal" width.
global $wgSVGMaxSize;
$hp['width'] = $wgSVGMaxSize;
} else {
$hp['width'] = $file->getWidth( $page );
}
if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
global $wgThumbLimits, $wgThumbUpright;
if ( $widthOption === null || !isset( $wgThumbLimits[$widthOption] ) ) {
$widthOption = User::getDefaultOption( 'thumbsize' );
}
// Reduce width for upright images when parameter 'upright' is used
if ( isset( $fp['upright'] ) && $fp['upright'] == 0 ) {
$fp['upright'] = $wgThumbUpright;
}
// 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
$prefWidth = isset( $fp['upright'] ) ?
round( $wgThumbLimits[$widthOption] * $fp['upright'], -1 ) :
$wgThumbLimits[$widthOption];
// Use width which is smaller: real image width or user preference width
// Unless image is scalable vector.
if ( !isset( $hp['height'] ) && ( $hp['width'] <= 0 ||
$prefWidth < $hp['width'] || $file->isVectorized() ) ) {
$hp['width'] = $prefWidth;
}
}
}
if ( isset( $fp['thumbnail'] ) || isset( $fp['manualthumb'] ) || isset( $fp['framed'] ) ) {
# Create a thumbnail. Alignment depends on the writing direction of
# the page content language (right-aligned for LTR languages,
# left-aligned for RTL languages)
#
# If a thumbnail width has not been provided, it is set
# to the default user option as specified in Language*.php
if ( $fp['align'] == '' ) {
if ( $parser instanceof Parser ) {
$fp['align'] = $parser->getTargetLanguage()->alignEnd();
} else {
# backwards compatibility, remove with makeImageLink2()
global $wgContLang;
$fp['align'] = $wgContLang->alignEnd();
}
}
return $prefix . self::makeThumbLink2( $title, $file, $fp, $hp, $time, $query ) . $postfix;
}
if ( $file && isset( $fp['frameless'] ) ) {
$srcWidth = $file->getWidth( $page );
# For "frameless" option: do not present an image bigger than the source (for bitmap-style images)
# This is the same behavior as the "thumb" option does it already.
if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
$hp['width'] = $srcWidth;
}
}
if ( $file && isset( $hp['width'] ) ) {
# Create a resized image, without the additional thumbnail features
$thumb = $file->transform( $hp );
} else {
$thumb = false;
}
if ( !$thumb ) {
$s = self::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
} else {
self::processResponsiveImages( $file, $thumb, $hp );
$params = array(
'alt' => $fp['alt'],
'title' => $fp['title'],
'valign' => isset( $fp['valign'] ) ? $fp['valign'] : false,
'img-class' => $fp['class'] );
if ( isset( $fp['border'] ) ) {
// TODO: BUG? Both values are identical
$params['img-class'] .= ( $params['img-class'] !== '' ) ? ' thumbborder' : 'thumbborder';
}
$params = self::getImageLinkMTOParams( $fp, $query, $parser ) + $params;
$s = $thumb->toHtml( $params );
}
if ( $fp['align'] != '' ) {
$s = "<div class=\"float{$fp['align']}\">{$s}</div>";
}
return str_replace( "\n", ' ', $prefix . $s . $postfix );
}
/**
* See makeImageLink()
* When this function is removed, remove if( $parser instanceof Parser ) check there too
* @deprecated since 1.20
*/
public static function makeImageLink2( Title $title, $file, $frameParams = array(),
$handlerParams = array(), $time = false, $query = "", $widthOption = null ) {
return self::makeImageLink( null, $title, $file, $frameParams,
$handlerParams, $time, $query, $widthOption );
}
/**
* Get the link parameters for MediaTransformOutput::toHtml() from given
* frame parameters supplied by the Parser.
* @param array $frameParams The frame parameters
* @param string $query An optional query string to add to description page links
* @return array
*/
private static function getImageLinkMTOParams( $frameParams, $query = '', $parser = null ) {
$mtoParams = array();
if ( isset( $frameParams['link-url'] ) && $frameParams['link-url'] !== '' ) {
$mtoParams['custom-url-link'] = $frameParams['link-url'];
if ( isset( $frameParams['link-target'] ) ) {
$mtoParams['custom-target-link'] = $frameParams['link-target'];
}
if ( $parser ) {
$extLinkAttrs = $parser->getExternalLinkAttribs( $frameParams['link-url'] );
foreach ( $extLinkAttrs as $name => $val ) {
// Currently could include 'rel' and 'target'
$mtoParams['parser-extlink-' . $name] = $val;
}
}
} elseif ( isset( $frameParams['link-title'] ) && $frameParams['link-title'] !== '' ) {
$mtoParams['custom-title-link'] = self::normaliseSpecialPage( $frameParams['link-title'] );
} elseif ( !empty( $frameParams['no-link'] ) ) {
// No link
} else {
$mtoParams['desc-link'] = true;
$mtoParams['desc-query'] = $query;
}
return $mtoParams;
}
/**
* Make HTML for a thumbnail including image, border and caption
* @param $title Title object
* @param $file File object or false if it doesn't exist
* @param $label String
* @param $alt String
* @param $align String
* @param $params Array
* @param $framed Boolean
* @param $manualthumb String
* @return mixed
*/
public static function makeThumbLinkObj( Title $title, $file, $label = '', $alt,
$align = 'right', $params = array(), $framed = false, $manualthumb = "" )
{
$frameParams = array(
'alt' => $alt,
'caption' => $label,
'align' => $align
);
if ( $framed ) {
$frameParams['framed'] = true;
}
if ( $manualthumb ) {
$frameParams['manualthumb'] = $manualthumb;
}
return self::makeThumbLink2( $title, $file, $frameParams, $params );
}
/**
* @param $title Title
* @param $file File
* @param array $frameParams
* @param array $handlerParams
* @param bool $time
* @param string $query
* @return mixed
*/
public static function makeThumbLink2( Title $title, $file, $frameParams = array(),
$handlerParams = array(), $time = false, $query = "" )
{
global $wgStylePath, $wgContLang;
$exists = $file && $file->exists();
# Shortcuts
$fp =& $frameParams;
$hp =& $handlerParams;
$page = isset( $hp['page'] ) ? $hp['page'] : false;
if ( !isset( $fp['align'] ) ) {
$fp['align'] = 'right';
}
if ( !isset( $fp['alt'] ) ) {
$fp['alt'] = '';
}
if ( !isset( $fp['title'] ) ) {
$fp['title'] = '';
}
if ( !isset( $fp['caption'] ) ) {
$fp['caption'] = '';
}
if ( empty( $hp['width'] ) ) {
// Reduce width for upright images when parameter 'upright' is used
$hp['width'] = isset( $fp['upright'] ) ? 130 : 180;
}
$thumb = false;
$noscale = false;
$manualthumb = false;
if ( !$exists ) {
$outerWidth = $hp['width'] + 2;
} else {
if ( isset( $fp['manualthumb'] ) ) {
# Use manually specified thumbnail
$manual_title = Title::makeTitleSafe( NS_FILE, $fp['manualthumb'] );
if ( $manual_title ) {
$manual_img = wfFindFile( $manual_title );
if ( $manual_img ) {
$thumb = $manual_img->getUnscaledThumb( $hp );
$manualthumb = true;
} else {
$exists = false;
}
}
} elseif ( isset( $fp['framed'] ) ) {
// Use image dimensions, don't scale
$thumb = $file->getUnscaledThumb( $hp );
$noscale = true;
} else {
# Do not present an image bigger than the source, for bitmap-style images
# This is a hack to maintain compatibility with arbitrary pre-1.10 behavior
$srcWidth = $file->getWidth( $page );
if ( $srcWidth && !$file->mustRender() && $hp['width'] > $srcWidth ) {
$hp['width'] = $srcWidth;
}
$thumb = $file->transform( $hp );
}
if ( $thumb ) {
$outerWidth = $thumb->getWidth() + 2;
} else {
$outerWidth = $hp['width'] + 2;
}
}
# ThumbnailImage::toHtml() already adds page= onto the end of DjVu URLs
# So we don't need to pass it here in $query. However, the URL for the
# zoom icon still needs it, so we make a unique query for it. See bug 14771
$url = $title->getLocalURL( $query );
if ( $page ) {
$url = wfAppendQuery( $url, array( 'page' => $page ) );
}
if ( $manualthumb &&
!isset( $fp['link-title'] ) &&
!isset( $fp['link-url'] ) &&
!isset( $fp['no-link'] ) ) {
$fp['link-url'] = $url;
}
$s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
if ( !$exists ) {
$s .= self::makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time == true );
$zoomIcon = '';
} elseif ( !$thumb ) {
$s .= wfMessage( 'thumbnail_error', '' )->escaped();
$zoomIcon = '';
} else {
if ( !$noscale && !$manualthumb ) {
self::processResponsiveImages( $file, $thumb, $hp );
}
$params = array(
'alt' => $fp['alt'],
'title' => $fp['title'],
'img-class' => ( isset( $fp['class'] ) && $fp['class'] !== '' ) ? $fp['class'] . ' thumbimage' : 'thumbimage'
);
$params = self::getImageLinkMTOParams( $fp, $query ) + $params;
$s .= $thumb->toHtml( $params );
if ( isset( $fp['framed'] ) ) {
$zoomIcon = "";
} else {
$zoomIcon = Html::rawElement( 'div', array( 'class' => 'magnify' ),
Html::rawElement( 'a', array(
'href' => $url,
'class' => 'internal',
'title' => wfMessage( 'thumbnail-more' )->text() ),
Html::element( 'img', array(
'src' => $wgStylePath . '/common/images/magnify-clip' . ( $wgContLang->isRTL() ? '-rtl' : '' ) . '.png',
'width' => 15,
'height' => 11,
'alt' => "" ) ) ) );
}
}
$s .= ' <div class="thumbcaption">' . $zoomIcon . $fp['caption'] . "</div></div></div>";
return str_replace( "\n", ' ', $s );
}
/**
* Process responsive images: add 1.5x and 2x subimages to the thumbnail, where
* applicable.
*
* @param File $file
* @param MediaOutput $thumb
* @param array $hp image parameters
*/
protected static function processResponsiveImages( $file, $thumb, $hp ) {
global $wgResponsiveImages;
if ( $wgResponsiveImages ) {
$hp15 = $hp;
$hp15['width'] = round( $hp['width'] * 1.5 );
$hp20 = $hp;
$hp20['width'] = $hp['width'] * 2;
if ( isset( $hp['height'] ) ) {
$hp15['height'] = round( $hp['height'] * 1.5 );
$hp20['height'] = $hp['height'] * 2;
}
$thumb15 = $file->transform( $hp15 );
$thumb20 = $file->transform( $hp20 );
if ( $thumb15->url !== $thumb->url ) {
$thumb->responsiveUrls['1.5'] = $thumb15->url;
}
if ( $thumb20->url !== $thumb->url ) {
$thumb->responsiveUrls['2'] = $thumb20->url;
}
}
}
/**
* Make a "broken" link to an image
*
* @param $title Title object
* @param string $label link label (plain text)
* @param string $query query string
* @param $unused1 Unused parameter kept for b/c
* @param $unused2 Unused parameter kept for b/c
* @param $time Boolean: a file of a certain timestamp was requested
* @return String
*/
public static function makeBrokenImageLinkObj( $title, $label = '', $query = '', $unused1 = '', $unused2 = '', $time = false ) {
global $wgEnableUploads, $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
if ( ! $title instanceof Title ) {
return "<!-- ERROR -->" . htmlspecialchars( $label );
}
wfProfileIn( __METHOD__ );
if ( $label == '' ) {
$label = $title->getPrefixedText();
}
$encLabel = htmlspecialchars( $label );
$currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
if ( ( $wgUploadMissingFileUrl || $wgUploadNavigationUrl || $wgEnableUploads ) && !$currentExists ) {
$redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
if ( $redir ) {
wfProfileOut( __METHOD__ );
return self::linkKnown( $title, $encLabel, array(), wfCgiToArray( $query ) );
}
$href = self::getUploadUrl( $title, $query );
wfProfileOut( __METHOD__ );
return '<a href="' . htmlspecialchars( $href ) . '" class="new" title="' .
htmlspecialchars( $title->getPrefixedText(), ENT_QUOTES ) . '">' .
$encLabel . '</a>';
}
wfProfileOut( __METHOD__ );
return self::linkKnown( $title, $encLabel, array(), wfCgiToArray( $query ) );
}
/**
* Get the URL to upload a certain file
*
* @param $destFile Title object of the file to upload
* @param string $query urlencoded query string to prepend
* @return String: urlencoded URL
*/
protected static function getUploadUrl( $destFile, $query = '' ) {
global $wgUploadMissingFileUrl, $wgUploadNavigationUrl;
$q = 'wpDestFile=' . $destFile->getPartialURL();
if ( $query != '' ) {
$q .= '&' . $query;
}
if ( $wgUploadMissingFileUrl ) {
return wfAppendQuery( $wgUploadMissingFileUrl, $q );
} elseif ( $wgUploadNavigationUrl ) {
return wfAppendQuery( $wgUploadNavigationUrl, $q );
} else {
$upload = SpecialPage::getTitleFor( 'Upload' );
return $upload->getLocalURL( $q );
}
}
/**
* Create a direct link to a given uploaded file.
*
* @param $title Title object.
* @param string $html pre-sanitized HTML
* @param string $time MW timestamp of file creation time