}
$text = $this->doAllQuotes( $text );
$text = $this->replaceInternalLinks ( $text );
- # Another call to replace links and images inside captions of images
- $text = $this->replaceInternalLinks ( $text );
$text = $this->replaceExternalLinks( $text );
$text = $this->doMagicLinks( $text );
$text = $this->doTableStuff( $text );
$redirect = MagicWord::get ( MAG_REDIRECT ) ;
+ #split the entire text string on occurences of [[
$a = explode( '[[', ' ' . $s );
+ #get the first element (all text up to first [[), and remove the space we added
$s = array_shift( $a );
$s = substr( $s, 1 );
# Match a link having the form [[namespace:link|alternate]]trail
static $e1 = FALSE;
if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
+ # Match cases where there is no "]]", which might still be images
+ static $e1_img = FALSE;
+ if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
# Match the end of a line for a word that's not followed by whitespace,
# e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
$useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
- # Special and Media are pseudo-namespaces; no pages actually exist in them
$nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
wfProfileOut( $fname.'-setup' );
- # start procedeeding each line
- foreach ( $a as $line ) {
+ # Loop for each link
+ for ($k = 0; isset( $a[$k] ); $k++) {
+ $line = $a[$k];
wfProfileIn( $fname.'-prefixhandling' );
if ( $useLinkPrefixExtension ) {
if ( preg_match( $e2, $s, $m ) ) {
}
wfProfileOut( $fname.'-prefixhandling' );
+ $might_be_img = false;
+
if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
$text = $m[2];
# fix up urlencoded title texts
if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
$trail = $m[3];
+ } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
+ $might_be_img = true;
+ $text = $m[2];
+ if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
+ $trail = "";
} else { # Invalid form; output directly
$s .= $prefix . '[[' . $line ;
continue;
$link = substr($link, 1);
}
- $wasblank = ( '' == $text );
- if( $wasblank ) $text = $link;
-
$nt = Title::newFromText( $link );
if( !$nt ) {
$s .= $prefix . '[[' . $line;
$ns = $nt->getNamespace();
$iw = $nt->getInterWiki();
+ if ($might_be_img) { # if this is actually an invalid link
+ if ($ns == NS_IMAGE && $noforce) { #but might be an image
+ $found = false;
+ while (isset ($a[$k+1]) ) {
+ #look at the next 'line' to see if we can close it there
+ $next_line = array_shift(array_splice( $a, $k + 1, 1) );
+ if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
+ # the first ]] closes the inner link, the second the image
+ $found = true;
+ $text .= '[[' . $m[1];
+ $trail = $m[2];
+ break;
+ } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
+ #if there's exactly one ]] that's fine, we'll keep looking
+ $text .= '[[' . $m[0];
+ } else {
+ #if $next_line is invalid too, we need look no further
+ $text .= '[[' . $next_line;
+ break;
+ }
+ }
+ if ( !$found ) {
+ # we couldn't find the end of this imageLink, so output it raw
+ $s .= $prefix . '[[' . $link . '|' . $text;
+ # note: no $trail, because without an end, there *is* no trail
+ continue;
+ }
+ } else { #it's not an image, so output it raw
+ $s .= $prefix . '[[' . $link . '|' . $text;
+ # note: no $trail, because without an end, there *is* no trail
+ continue;
+ }
+ }
+
+ $wasblank = ( '' == $text );
+ if( $wasblank ) $text = $link;
+
+
# Link not escaped by : , create the various objects
if( $noforce ) {
}
if ( $ns == NS_IMAGE ) {
- $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
+ # recursively parse links inside the image caption
+ # actually, this will parse them in any other parameters, too,
+ # but it might be hard to fix that, and it doesn't matter ATM
+ $text = $this->replaceExternalLinks($text);
+ $text = $this->replaceInternalLinks($text);
+
+ # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
+ $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState ) . $trail;
$wgLinkCache->addImageLinkObj( $nt );
continue;
}
if ( $ns == NS_CATEGORY ) {
$t = $nt->getText() ;
- $nnt = Title::newFromText ( Namespace::getCanonicalName(NS_CATEGORY).':'.$t ) ;
$wgLinkCache->suspend(); # Don't save in links/brokenlinks
$pPLC=$sk->postParseLinkColour();
$sk->postParseLinkColour( false );
- $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
+ $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
$sk->postParseLinkColour( $pPLC );
$wgLinkCache->resume();
continue;
}
+ # Special and Media are pseudo-namespaces; no pages actually exist in them
if( $ns == NS_MEDIA ) {
$s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
$wgLinkCache->addImageLinkObj( $nt );