r8662 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r8661‎ | r8662 | r8663 >
Date:07:48, 27 April 2005
Author:timstarling
Status:old
Tags:
Comment:
* Deglobalised link placeholder handling by moving the relevant functionality from Linker::makeLinkObj() to Parser::makeLinkHolder()
* Did a similar trick for the handling of image options, in order to fix a bug reported by the parser unit test.
* Updated parser tests to match current output where appropriate.
Modified paths:
  • /trunk/phase3/includes/Linker.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)
  • /trunk/phase3/includes/ParserXML.php (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/maintenance/parserTests.php (modified) (history)
  • /trunk/phase3/maintenance/parserTests.txt (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/parserTests.php
@@ -302,6 +302,7 @@
303303 'wgCapitalLinks' => true,
304304 'wgDefaultUserOptions' => array(),
305305 'wgNoFollowLinks' => true,
 306+ 'wgThumbnailScriptPath' => false,
306307 );
307308 $this->savedGlobals = array();
308309 foreach( $settings as $var => $val ) {
Index: trunk/phase3/maintenance/parserTests.txt
@@ -1658,7 +1658,7 @@
16591659 !! input
16601660 [[Image:Foobar.jpg|thumb|This is a caption with another [[Image:icon.png|image]] inside it!]]
16611661 !! result
1662 -<div class="thumb tright"><div style="width:182px;"><b>Missing image</b><br /><i>Foobar.jpg</i> <div class="thumbcaption" >This is a caption with another <a href="https://www.mediawiki.org/wiki/Image:Icon.png" class="image" title="image"><img src="/images/9/96/Icon.png" alt="image" longdesc="/wiki/Image:Icon.png" /></a> inside it!</div></div></div>
 1662+<div class="thumb tright"><div style="width:182px;"><a href="https://www.mediawiki.org/wiki/Image:Foobar.jpg" class="internal" title="This is a caption with another Image:Icon.png inside it!"><img src="/images/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg" alt="This is a caption with another Image:Icon.png inside it!" width="180" height="20" longdesc="/wiki/Image:Foobar.jpg" /></a> <div class="thumbcaption" ><div class="magnify" style="float:right"><a href="https://www.mediawiki.org/wiki/Image:Foobar.jpg" class="internal" title="Enlarge"><img src="/skins/common/images/magnify-clip.png" width="15" height="11" alt="Enlarge" /></a></div>This is a caption with another <a href="https://www.mediawiki.org/index.php?title=Special:Upload&amp;wpDestFile=Icon.png" class="new" title="Image:Icon.png">Image:Icon.png</a> inside it!</div></div></div>
16631663
16641664 !! end
16651665
Index: trunk/phase3/includes/ParserXML.php
@@ -144,7 +144,7 @@
145145 # There's stuff missing here...
146146 if ($nt->getNamespace() == NS_IMAGE) {
147147 $options[] = $display_title;
148 - return $skin->makeImageLinkObj($nt, implode('|', $options));
 148+ return $parser->makeImage($nt, implode('|', $options));
149149 } else {
150150 # Default
151151 $title = $target;
@@ -639,4 +639,4 @@
640640 }
641641
642642 }
643 -?>
\ No newline at end of file
 643+?>
Index: trunk/phase3/includes/Linker.php
@@ -16,20 +16,14 @@
1717 * @package MediaWiki
1818 */
1919 class Linker {
20 - var $linktrail ; # linktrail regexp
21 - var $postParseLinkColour = false;
2220
23 - /** @todo document */
24 - function Linker() {
25 - global $wgContLang;
26 - $this->linktrail = $wgContLang->linkTrail();
27 - }
28 -
 21+ function Linker() {}
 22+
2923 /**
30 - * Get/set accessor for delayed link colouring
 24+ * OBSOLETE
3125 */
32 - function postParseLinkColour( $setting = NULL ) {
33 - return wfSetVar( $this->postParseLinkColour, $setting );
 26+ function postParseLinkColour( $s = NULL ) {
 27+ return NULL;
3428 }
3529
3630 /** @todo document */
@@ -137,12 +131,12 @@
138132 * Pass a title object, not a title string
139133 */
140134 function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
141 - global $wgOut, $wgUser, $wgLinkHolders, $wgInputEncoding;
 135+ global $wgOut, $wgUser, $wgInputEncoding;
142136 $fname = 'Skin::makeLinkObj';
143137 wfProfileIn( $fname );
144138
145139 # Fail gracefully
146 - if ( ! isset($nt) ) {
 140+ if ( ! is_object($nt) ) {
147141 # wfDebugDieBacktrace();
148142 wfProfileOut( $fname );
149143 return "<!-- ERROR -->{$prefix}{$text}{$trail}";
@@ -183,45 +177,10 @@
184178
185179 $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
186180
187 - if( $this->postParseLinkColour ) {
188 - # There's no existence check, but this will prevent
189 - # interwiki links from being parsed as external links.
190 - global $wgInterwikiLinkHolders;
191 - $nr = array_push($wgInterwikiLinkHolders, $t);
192 - $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
193 - } else {
194 - return $t;
195 - }
196 - } elseif ( 0 == $ns && "" == $dbkey ) {
197 - # A self-link with a fragment; skip existence check.
 181+ return $t;
 182+ } elseif ( $nt->isAlwaysKnown() ) {
 183+ # Image links, special page links and self-links with fragements are always known.
198184 $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
199 - } elseif ( ( NS_SPECIAL == $ns ) || ( NS_IMAGE == $ns ) ) {
200 - # These are always shown as existing, currently.
201 - # Special pages don't exist in the database; images may
202 - # occasionally be present when there is no description
203 - # page per se, so we always shown them.
204 - $retVal = $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
205 - } elseif ( $this->postParseLinkColour ) {
206 - wfProfileIn( $fname.'-postparse' );
207 - # Insert a placeholder, and we'll work out the existence checks
208 - # in a big lump later.
209 - $inside = '';
210 - if ( '' != $trail ) {
211 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
212 - $inside = $m[1];
213 - $trail = $m[2];
214 - }
215 - }
216 -
217 - # These get picked up by Parser::replaceLinkHolders()
218 - $nr = array_push( $wgLinkHolders['namespaces'], $nt->getNamespace() );
219 - $wgLinkHolders['dbkeys'][] = $dbkey;
220 - $wgLinkHolders['queries'][] = $query;
221 - $wgLinkHolders['texts'][] = $prefix.$text.$inside;
222 - $wgLinkHolders['titles'][] =& $nt;
223 -
224 - $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
225 - wfProfileOut( $fname.'-postparse' );
226185 } else {
227186 wfProfileIn( $fname.'-immediate' );
228187 # Work out link colour immediately
@@ -294,14 +253,8 @@
295254 $text = htmlspecialchars( $nt->getPrefixedText() );
296255 }
297256 $style = $this->getInternalLinkAttributesObj( $nt, $text );
298 -
299 - $inside = '';
300 - if ( '' != $trail ) {
301 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
302 - $inside = $m[1];
303 - $trail = $m[2];
304 - }
305 - }
 257+
 258+ list( $inside, $trail ) = Linker::splitTrail( $trail );
306259 $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
307260 wfProfileOut( $fname );
308261 return $r;
@@ -331,14 +284,8 @@
332285 $text = htmlspecialchars( $nt->getPrefixedText() );
333286 }
334287 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
335 -
336 - $inside = '';
337 - if ( '' != $trail ) {
338 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
339 - $inside = $m[1];
340 - $trail = $m[2];
341 - }
342 - }
 288+
 289+ list( $inside, $trail ) = Linker::splitTrail( $trail );
343290 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
344291
345292 wfProfileOut( $fname );
@@ -358,13 +305,7 @@
359306 }
360307 $style = $this->getInternalLinkAttributesObj( $nt, $text, 'stub' );
361308
362 - $inside = '';
363 - if ( '' != $trail ) {
364 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
365 - $inside = $m[1];
366 - $trail = $m[2];
367 - }
368 - }
 309+ list( $inside, $trail ) = Linker::splitTrail( $trail );
369310 $s = "<a href=\"{$u}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
370311 return $s;
371312 }
@@ -375,13 +316,7 @@
376317 if ( '' == $text ) {
377318 $text = htmlspecialchars( $nt->getPrefixedText() );
378319 }
379 - $inside = '';
380 - if ( '' != $trail ) {
381 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
382 - $inside = $m[1];
383 - $trail = $m[2];
384 - }
385 - }
 320+ list( $inside, $trail ) = Linker::splitTrail( $trail );
386321 return "<strong>{$prefix}{$text}{$inside}</strong>{$trail}";
387322 }
388323
@@ -396,8 +331,13 @@
397332 return htmlspecialchars( $basename );
398333 }
399334
400 - /** @todo document */
 335+ /** Obsolete alias */
401336 function makeImage( $url, $alt = '' ) {
 337+ return $this->makeExternalImage( $url, $alt );
 338+ }
 339+
 340+ /** @todo document */
 341+ function makeExternalImage( $url, $alt = '' ) {
402342 global $wgOut;
403343 if ( '' == $alt ) {
404344 $alt = $this->fnamePart( $url );
@@ -407,81 +347,15 @@
408348 }
409349
410350 /** @todo document */
411 - function makeImageLink( $name, $url, $alt = '' ) {
412 - $nt = Title::makeTitleSafe( NS_IMAGE, $name );
413 - return $this->makeImageLinkObj( $nt, $alt );
414 - }
415 -
416 - /** @todo document */
417 - function makeImageLinkObj( $nt, $alt = '' ) {
418 - global $wgContLang, $wgUseImageResize;
419 - global $wgUser, $wgThumbLimits;
 351+ function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
 352+ $thumb = false, $manual_thumb = '' )
 353+ {
 354+ global $wgContLang, $wgUser, $wgThumbLimits;
420355
421356 $img = new Image( $nt );
422357 $url = $img->getViewURL();
423 -
424 - $align = '';
425358 $prefix = $postfix = '';
426 -
427 - # Check if the alt text is of the form "options|alt text"
428 - # Options are:
429 - # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
430 - # * left no resizing, just left align. label is used for alt= only
431 - # * right same, but right aligned
432 - # * none same, but not aligned
433 - # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
434 - # * center center the image
435 - # * framed Keep original image size, no magnify-button.
436 -
437 - $part = explode( '|', $alt);
438 -
439 - $mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
440 - $mwLeft =& MagicWord::get( MAG_IMG_LEFT );
441 - $mwRight =& MagicWord::get( MAG_IMG_RIGHT );
442 - $mwNone =& MagicWord::get( MAG_IMG_NONE );
443 - $mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
444 - $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
445 - $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
446 - $alt = '';
447 -
448 - $height = $framed = $thumb = false;
449 - $manual_thumb = "" ;
450 -
451 - foreach( $part as $key => $val ) {
452 - $val_parts = explode ( "=" , $val , 2 ) ;
453 - $left_part = array_shift ( $val_parts ) ;
454 - if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
455 - $thumb=true;
456 - } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
457 - # use manually specified thumbnail
458 - $thumb=true;
459 - $manual_thumb = array_shift ( $val_parts ) ;
460 - } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
461 - # remember to set an alignment, don't render immediately
462 - $align = 'right';
463 - } elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
464 - # remember to set an alignment, don't render immediately
465 - $align = 'left';
466 - } elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
467 - # remember to set an alignment, don't render immediately
468 - $align = 'center';
469 - } elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
470 - # remember to set an alignment, don't render immediately
471 - $align = 'none';
472 - } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
473 - # $match is the image width in pixels
474 - if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
475 - $width = intval( $m[1] );
476 - $height = intval( $m[2] );
477 - } else {
478 - $width = intval($match);
479 - }
480 - } elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
481 - $framed=true;
482 - } else {
483 - $alt = $val;
484 - }
485 - }
 359+
486360 if ( 'center' == $align )
487361 {
488362 $prefix = '<div class="center">';
@@ -503,7 +377,7 @@
504378 }
505379
506380
507 - if ( ! isset($width) ) {
 381+ if ( $width === false ) {
508382 $wopt = $wgUser->getOption( 'thumbsize' );
509383
510384 if( !isset( $wgThumbLimits[$wopt] ) ) {
@@ -513,28 +387,21 @@
514388 $width = $wgThumbLimits[$wopt];
515389 }
516390
517 - return $prefix.$this->makeThumbLinkObj( $img, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
 391+ return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
518392
519 - } elseif ( isset($width) ) {
 393+ } elseif ( $width ) {
520394
521395 # Create a resized image, without the additional thumbnail
522396 # features
523397
524 - if ( ( ! $height === false )
525 - && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
 398+ if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
526399 $width = $img->getWidth() * $height / $img->getHeight();
527400 }
528 - if ( '' == $manual_thumb ) $url = $img->createThumb( $width );
 401+ if ( '' == $manual_thumb ) {
 402+ $url = $img->createThumb( $width );
 403+ }
529404 }
530405
531 - # FIXME: This is a gross hack using a global.
532 - # Replace link color holders in the caption text so the
533 - # text portion can be placed int the alt/title attributes.
534 - global $wgParser;
535 - $wgParser->replaceLinkHolders( $alt );
536 -
537 - $alt = Sanitizer::stripAllTags( $alt );
538 -
539406 $u = $nt->escapeLocalURL();
540407 if ( $url == '' ) {
541408 $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
@@ -553,14 +420,10 @@
554421 * Make HTML for a thumbnail including image, border and caption
555422 * $img is an Image object
556423 */
557 - function makeThumbLinkObj( $img, $label = '', $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
 424+ function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
558425 global $wgStylePath, $wgContLang;
559 - # $image = Title::makeTitleSafe( NS_IMAGE, $name );
560426 $url = $img->getViewURL();
561427
562 - #$label = htmlspecialchars( $label );
563 - $alt = Sanitizer::stripAllTags( $label );
564 -
565428 $width = $height = 0;
566429 if ( $img->exists() )
567430 {
@@ -661,14 +524,7 @@
662525 $text = htmlspecialchars( $nt->getPrefixedText() );
663526 }
664527 $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
665 -
666 - $inside = '';
667 - if ( '' != $trail ) {
668 - if ( preg_match( $this->linktrail, $trail, $m ) ) {
669 - $inside = $m[1];
670 - $trail = $m[2];
671 - }
672 - }
 528+ list( $inside, $trail ) = Linker::splitTrail( $trail );
673529 $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
674530
675531 wfProfileOut( $fname );
@@ -920,5 +776,28 @@
921777 }
922778 return "<div class=\"editsection\" style=\"float:$farside;margin-$nearside:5px;\">[".$url."]</div>";
923779 }
 780+
 781+ /**
 782+ * Split a link trail, return the "inside" portion and the remainder of the trail
 783+ * as a two-element array
 784+ *
 785+ * @static
 786+ */
 787+ function splitTrail( $trail ) {
 788+ static $regex = false;
 789+ if ( $regex === false ) {
 790+ global $wgContLang;
 791+ $regex = $wgContLang->linkTrail();
 792+ }
 793+ $inside = '';
 794+ if ( '' != $trail ) {
 795+ if ( preg_match( $regex, $trail, $m ) ) {
 796+ $inside = $m[1];
 797+ $trail = $m[2];
 798+ }
 799+ }
 800+ return array( $inside, $trail );
 801+ }
 802+
924803 }
925804 ?>
Index: trunk/phase3/includes/Parser.php
@@ -101,6 +101,7 @@
102102 # Cleared with clearState():
103103 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
104104 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
 105+ var $mInterwikiLinkHolders, $mLinkHolders;
105106
106107 # Temporary:
107108 var $mOptions, $mTitle, $mOutputType,
@@ -117,6 +118,7 @@
118119 * @access public
119120 */
120121 function Parser() {
 122+ global $wgContLang;
121123 $this->mTemplates = array();
122124 $this->mTemplatePath = array();
123125 $this->mTagHooks = array();
@@ -138,6 +140,14 @@
139141 $this->mStripState = array();
140142 $this->mArgStack = array();
141143 $this->mInPre = false;
 144+ $this->mInterwikiLinkHolders = array();
 145+ $this->mLinkHolders = array(
 146+ 'namespaces' => array(),
 147+ 'dbkeys' => array(),
 148+ 'queries' => array(),
 149+ 'texts' => array(),
 150+ 'titles' => array()
 151+ );
142152 }
143153
144154 /**
@@ -166,7 +176,7 @@
167177 $this->mOutputType = OT_HTML;
168178
169179 $this->mStripState = NULL;
170 - global $fnord; $fnord = 1;
 180+
171181 //$text = $this->strip( $text, $this->mStripState );
172182 // VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
173183 $x =& $this->mStripState;
@@ -965,8 +975,6 @@
966976 wfProfileIn( $fname );
967977
968978 $sk =& $this->mOptions->getSkin();
969 - global $wgContLang;
970 - $linktrail = $wgContLang->linkTrail();
971979
972980 $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
973981
@@ -989,7 +997,7 @@
990998
991999 # If the link text is an image URL, replace it with an <img> tag
9921000 # This happened by accident in the original parser, but some people used it extensively
993 - $img = $this->maybeMakeImageLink( $text );
 1001+ $img = $this->maybeMakeExternalImage( $text );
9941002 if ( $img !== false ) {
9951003 $text = $img;
9961004 }
@@ -1013,17 +1021,14 @@
10141022 } else {
10151023 # Have link text, e.g. [http://domain.tld/some.link text]s
10161024 # Check for trail
1017 - if ( preg_match( $linktrail, $trail, $m2 ) ) {
1018 - $dtrail = $m2[1];
1019 - $trail = $m2[2];
1020 - }
 1025+ list( $dtrail, $trail ) = Linker::splitTrail( $trail );
10211026 }
10221027
10231028 $text = $wgContLang->markNoConversion($text);
10241029
10251030 # Replace &amp; from obsolete syntax with &.
10261031 # All HTML entities will be escaped by makeExternalLink()
1027 - # or maybeMakeImageLink()
 1032+ # or maybeMakeExternalImage()
10281033 $url = str_replace( '&amp;', '&', $url );
10291034
10301035 # Process the trail (i.e. everything after this link up until start of the next link),
@@ -1089,11 +1094,11 @@
10901095
10911096 # Replace &amp; from obsolete syntax with &.
10921097 # All HTML entities will be escaped by makeExternalLink()
1093 - # or maybeMakeImageLink()
 1098+ # or maybeMakeExternalImage()
10941099 $url = str_replace( '&amp;', '&', $url );
10951100
10961101 # Is this an external image?
1097 - $text = $this->maybeMakeImageLink( $url );
 1102+ $text = $this->maybeMakeExternalImage( $url );
10981103 if ( $text === false ) {
10991104 # Not an image, make a link
11001105 $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free' );
@@ -1111,13 +1116,13 @@
11121117 * make an image if it's allowed
11131118 * @access private
11141119 */
1115 - function maybeMakeImageLink( $url ) {
 1120+ function maybeMakeExternalImage( $url ) {
11161121 $sk =& $this->mOptions->getSkin();
11171122 $text = false;
11181123 if ( $this->mOptions->getAllowExternalImages() ) {
11191124 if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
11201125 # Image found
1121 - $text = $sk->makeImage( htmlspecialchars( $url ) );
 1126+ $text = $sk->makeExternalImage( htmlspecialchars( $url ) );
11221127 }
11231128 }
11241129 return $text;
@@ -1140,10 +1145,6 @@
11411146 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
11421147
11431148 $sk =& $this->mOptions->getSkin();
1144 - global $wgUseOldExistenceCheck;
1145 - # "Post-parse link colour check" works only on wiki text since it's now
1146 - # in Parser. Enable it, then disable it when we're done.
1147 - $saveParseColour = $sk->postParseLinkColour( !$wgUseOldExistenceCheck );
11481149
11491150 #split the entire text string on occurences of [[
11501151 $a = explode( '[[', ' ' . $s );
@@ -1327,7 +1328,7 @@
13281329 $text = $this->replaceInternalLinks($text);
13291330
13301331 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
1331 - $s .= $prefix . str_replace('http://', 'http-noparse://', $sk->makeImageLinkObj( $nt, $text ) ) . $trail;
 1332+ $s .= $prefix . str_replace('http://', 'http-noparse://', $this->makeImage( $nt, $text ) ) . $trail;
13321333 $wgLinkCache->addImageLinkObj( $nt );
13331334
13341335 wfProfileOut( "$fname-image" );
@@ -1343,10 +1344,7 @@
13441345 $s = rtrim($s . "\n"); # bug 87
13451346
13461347 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1347 - $pPLC=$sk->postParseLinkColour();
1348 - $sk->postParseLinkColour( false );
13491348 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1350 - $sk->postParseLinkColour( $pPLC );
13511349 $wgLinkCache->resume();
13521350
13531351 if ( $wasblank ) {
@@ -1388,14 +1386,54 @@
13891387 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
13901388 continue;
13911389 }
1392 - $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
 1390+ if ( $nt->isAlwaysKnown() ) {
 1391+ $s .= $sk->makeKnownLinkObj( $nt, $text, '', $trail, $prefix );
 1392+ } else {
 1393+ /**
 1394+ * Add a link placeholder
 1395+ * Later, this will be replaced by a real link, after the existence or
 1396+ * non-existence of all the links is known
 1397+ */
 1398+ $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
 1399+ }
13931400 }
1394 - $sk->postParseLinkColour( $saveParseColour );
13951401 wfProfileOut( $fname );
13961402 return $s;
13971403 }
13981404
13991405 /**
 1406+ * Make a link placeholder. The text returned can be later resolved to a real link with
 1407+ * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
 1408+ * parsing of interwiki links, and secondly to allow all extistence checks and
 1409+ * article length checks (for stub links) to be bundled into a single query.
 1410+ *
 1411+ */
 1412+ function makeLinkHolder( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
 1413+ if ( ! is_object($nt) ) {
 1414+ # Fail gracefully
 1415+ $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
 1416+ } else {
 1417+ # Separate the link trail from the rest of the link
 1418+ list( $inside, $trail ) = Linker::splitTrail( $trail );
 1419+
 1420+ if ( $nt->isExternal() ) {
 1421+ $iwRecord = array( $nt->getPrefixedDBkey(), $prefix.$text.$inside );
 1422+ $nr = array_push($this->mInterwikiLinkHolders, $iwRecord);
 1423+ $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
 1424+ } else {
 1425+ $nr = array_push( $this->mLinkHolders['namespaces'], $nt->getNamespace() );
 1426+ $this->mLinkHolders['dbkeys'][] = $nt->getDBkey();
 1427+ $this->mLinkHolders['queries'][] = $query;
 1428+ $this->mLinkHolders['texts'][] = $prefix.$text.$inside;
 1429+ $this->mLinkHolders['titles'][] =& $nt;
 1430+
 1431+ $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
 1432+ }
 1433+ }
 1434+ return $retVal;
 1435+ }
 1436+
 1437+ /**
14001438 * Return true if subpage links should be expanded on this page.
14011439 * @return bool
14021440 */
@@ -2409,10 +2447,10 @@
24102448 # turns into
24112449 # link text with suffix
24122450 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2413 - "\$wgLinkHolders['texts'][\$1]",
 2451+ "\$this->mLinkHolders['texts'][\$1]",
24142452 $canonized_headline );
24152453 $canonized_headline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
2416 - "\$wgInterwikiLinkHolders[\$1]",
 2454+ "\$this->mInterwikiLinkHolders[\$1][1]",
24172455 $canonized_headline );
24182456
24192457 # strip out HTML
@@ -2791,36 +2829,30 @@
27922830 * $options is a bit field, RLH_FOR_UPDATE to select for update
27932831 */
27942832 function replaceLinkHolders( &$text, $options = 0 ) {
2795 - global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
2796 - global $wgInterwikiLinkHolders;
2797 - global $outputReplace;
2798 -
2799 - if ( $wgUseOldExistenceCheck ) {
2800 - return array();
2801 - }
 2833+ global $wgUser, $wgLinkCache;
 2834+ global $wgOutputReplace;
28022835
28032836 $fname = 'Parser::replaceLinkHolders';
28042837 wfProfileIn( $fname );
28052838
28062839 $pdbks = array();
28072840 $colours = array();
 2841+ $sk = $this->mOptions->getSkin();
28082842
2809 - #if ( !empty( $tmpLinks[0] ) ) { #TODO
2810 - if ( !empty( $wgLinkHolders['namespaces'] ) ) {
 2843+ if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
28112844 wfProfileIn( $fname.'-check' );
28122845 $dbr =& wfGetDB( DB_SLAVE );
28132846 $page = $dbr->tableName( 'page' );
2814 - $sk = $wgUser->getSkin();
28152847 $threshold = $wgUser->getOption('stubthreshold');
28162848
28172849 # Sort by namespace
2818 - asort( $wgLinkHolders['namespaces'] );
 2850+ asort( $this->mLinkHolders['namespaces'] );
28192851
28202852 # Generate query
28212853 $query = false;
2822 - foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
 2854+ foreach ( $this->mLinkHolders['namespaces'] as $key => $val ) {
28232855 # Make title object
2824 - $title = $wgLinkHolders['titles'][$key];
 2856+ $title = $this->mLinkHolders['titles'][$key];
28252857
28262858 # Skip invalid entries.
28272859 # Result will be ugly, but prevents crash.
@@ -2850,7 +2882,7 @@
28512883 $query .= ', ';
28522884 }
28532885
2854 - $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
 2886+ $query .= $dbr->addQuotes( $this->mLinkHolders['dbkeys'][$key] );
28552887 }
28562888 }
28572889 if ( $query ) {
@@ -2886,25 +2918,25 @@
28872919
28882920 # Construct search and replace arrays
28892921 wfProfileIn( $fname.'-construct' );
2890 - $outputReplace = array();
2891 - foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
 2922+ $wgOutputReplace = array();
 2923+ foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
28922924 $pdbk = $pdbks[$key];
2893 - $searchkey = '<!--LINK '.$key.'-->';
2894 - $title = $wgLinkHolders['titles'][$key];
 2925+ $searchkey = "<!--LINK $key-->";
 2926+ $title = $this->mLinkHolders['titles'][$key];
28952927 if ( empty( $colours[$pdbk] ) ) {
28962928 $wgLinkCache->addBadLink( $pdbk );
28972929 $colours[$pdbk] = 0;
2898 - $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
2899 - $wgLinkHolders['texts'][$key],
2900 - $wgLinkHolders['queries'][$key] );
 2930+ $wgOutputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
 2931+ $this->mLinkHolders['texts'][$key],
 2932+ $this->mLinkHolders['queries'][$key] );
29012933 } elseif ( $colours[$pdbk] == 1 ) {
2902 - $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
2903 - $wgLinkHolders['texts'][$key],
2904 - $wgLinkHolders['queries'][$key] );
 2934+ $wgOutputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
 2935+ $this->mLinkHolders['texts'][$key],
 2936+ $this->mLinkHolders['queries'][$key] );
29052937 } elseif ( $colours[$pdbk] == 2 ) {
2906 - $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
2907 - $wgLinkHolders['texts'][$key],
2908 - $wgLinkHolders['queries'][$key] );
 2938+ $wgOutputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
 2939+ $this->mLinkHolders['texts'][$key],
 2940+ $this->mLinkHolders['queries'][$key] );
29092941 }
29102942 }
29112943 wfProfileOut( $fname.'-construct' );
@@ -2914,32 +2946,30 @@
29152947
29162948 $text = preg_replace_callback(
29172949 '/(<!--LINK .*?-->)/',
2918 - "outputReplaceMatches",
 2950+ "wfOutputReplaceMatches",
29192951 $text);
 2952+
29202953 wfProfileOut( $fname.'-replace' );
29212954 }
29222955
2923 - if ( !empty( $wgInterwikiLinkHolders ) ) {
 2956+ # Now process interwiki link holders
 2957+ # This is quite a bit simpler than internal links
 2958+ if ( !empty( $this->mInterwikiLinkHolders ) ) {
29242959 wfProfileIn( $fname.'-interwiki' );
2925 - $outputReplace = $wgInterwikiLinkHolders;
 2960+ # Make interwiki link HTML
 2961+ $wgOutputReplace = array();
 2962+ foreach( $this->mInterwikiLinkHolders as $i => $lh ) {
 2963+ $s = $sk->makeLink( $lh[0], $lh[1] );
 2964+ $wgOutputReplace[] = $s;
 2965+ }
 2966+
29262967 $text = preg_replace_callback(
29272968 '/<!--IWLINK (.*?)-->/',
2928 - "outputReplaceMatches",
 2969+ "wfOutputReplaceMatches",
29292970 $text );
29302971 wfProfileOut( $fname.'-interwiki' );
29312972 }
29322973
2933 - # Clear link holders, no need to fetch these ones again in a subsequent invocation
2934 - $wgLinkHolders = array(
2935 - 'namespaces' => array(),
2936 - 'dbkeys' => array(),
2937 - 'queries' => array(),
2938 - 'texts' => array(),
2939 - 'titles' => array()
2940 - );
2941 - $wgInterwikiLinkHolders = array();
2942 -
2943 -
29442974 wfProfileOut( $fname );
29452975 return $colours;
29462976 }
@@ -2952,6 +2982,8 @@
29532983 * given as text will return the HTML of a gallery with two images,
29542984 * labeled 'The number "1"' and
29552985 * 'A tree'.
 2986+ *
 2987+ * @static
29562988 */
29572989 function renderImageGallery( $text ) {
29582990 # Setup the parser
@@ -2991,6 +3023,84 @@
29923024 }
29933025 return $ig->toHTML();
29943026 }
 3027+
 3028+ /**
 3029+ * Parse image options text and use it to make an image
 3030+ */
 3031+ function makeImage( &$nt, $options ) {
 3032+ global $wgContLang, $wgUseImageResize;
 3033+ global $wgUser, $wgThumbLimits;
 3034+
 3035+ $align = '';
 3036+
 3037+ # Check if the options text is of the form "options|alt text"
 3038+ # Options are:
 3039+ # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
 3040+ # * left no resizing, just left align. label is used for alt= only
 3041+ # * right same, but right aligned
 3042+ # * none same, but not aligned
 3043+ # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
 3044+ # * center center the image
 3045+ # * framed Keep original image size, no magnify-button.
 3046+
 3047+ $part = explode( '|', $options);
 3048+
 3049+ $mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
 3050+ $mwLeft =& MagicWord::get( MAG_IMG_LEFT );
 3051+ $mwRight =& MagicWord::get( MAG_IMG_RIGHT );
 3052+ $mwNone =& MagicWord::get( MAG_IMG_NONE );
 3053+ $mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
 3054+ $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
 3055+ $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
 3056+ $caption = '';
 3057+
 3058+ $width = $height = $framed = $thumb = false;
 3059+ $manual_thumb = "" ;
 3060+
 3061+ foreach( $part as $key => $val ) {
 3062+ $val_parts = explode ( "=" , $val , 2 ) ;
 3063+ $left_part = array_shift ( $val_parts ) ;
 3064+ if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
 3065+ $thumb=true;
 3066+ } elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
 3067+ # use manually specified thumbnail
 3068+ $thumb=true;
 3069+ $manual_thumb = array_shift ( $val_parts ) ;
 3070+ } elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
 3071+ # remember to set an alignment, don't render immediately
 3072+ $align = 'right';
 3073+ } elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
 3074+ # remember to set an alignment, don't render immediately
 3075+ $align = 'left';
 3076+ } elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
 3077+ # remember to set an alignment, don't render immediately
 3078+ $align = 'center';
 3079+ } elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
 3080+ # remember to set an alignment, don't render immediately
 3081+ $align = 'none';
 3082+ } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
 3083+ # $match is the image width in pixels
 3084+ if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
 3085+ $width = intval( $m[1] );
 3086+ $height = intval( $m[2] );
 3087+ } else {
 3088+ $width = intval($match);
 3089+ }
 3090+ } elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
 3091+ $framed=true;
 3092+ } else {
 3093+ $caption = $val;
 3094+ }
 3095+ }
 3096+ # Strip bad stuff out of the alt text
 3097+ $alt = $caption;
 3098+ $this->replaceLinkHolders( $alt );
 3099+ $alt = Sanitizer::stripAllTags( $alt );
 3100+
 3101+ # Linker does the rest
 3102+ $sk =& $this->mOptions->getSkin();
 3103+ return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $width, $height, $framed, $thumb, $manual_thumb );
 3104+ }
29953105 }
29963106
29973107 /**
@@ -3131,9 +3241,9 @@
31323242 * Callback function used by Parser::replaceLinkHolders()
31333243 * to substitute link placeholders.
31343244 */
3135 -function &outputReplaceMatches( $matches ) {
3136 - global $outputReplace;
3137 - return $outputReplace[$matches[1]];
 3245+function &wfOutputReplaceMatches( $matches ) {
 3246+ global $wgOutputReplace;
 3247+ return $wgOutputReplace[$matches[1]];
31383248 }
31393249
31403250 /**
Index: trunk/phase3/includes/Title.php
@@ -1989,5 +1989,18 @@
19901990 return $this->getArticleId() != 0;
19911991 }
19921992
 1993+ /**
 1994+ * Should a link should be displayed as a known link, just based on its title?
 1995+ *
 1996+ * Currently, a self-link with a fragment, special pages and image pages are in
 1997+ * this category. Special pages never exist in the database. Some images do not
 1998+ * have description pages in the database, but the description page contains
 1999+ * useful history information that the user may want to link to.
 2000+ *
 2001+ */
 2002+ function isAlwaysKnown() {
 2003+ return ( 0 == $this->mNamespace && "" == $this->mDbkeyform )
 2004+ || NS_SPECIAL == $this->mNamespace || NS_IMAGE == $this->mNamespace;
 2005+ }
19932006 }
19942007 ?>
Index: trunk/phase3/includes/Skin.php
@@ -34,17 +34,6 @@
3535
3636 require_once( 'RecentChange.php' );
3737
38 -global $wgLinkHolders;
39 -$wgLinkHolders = array(
40 - 'namespaces' => array(),
41 - 'dbkeys' => array(),
42 - 'queries' => array(),
43 - 'texts' => array(),
44 - 'titles' => array()
45 -);
46 -global $wgInterwikiLinkHolders;
47 -$wgInterwikiLinkHolders = array();
48 -
4938 /**
5039 * @todo document
5140 * @package MediaWiki

Follow-up revisions

RevisionCommit summaryAuthorDate
r79742More ancient deprecated functions:...happy-melon18:53, 6 January 2011

Status & tagging log