Topic on Project:Support desk

Segregated external links

9
Brother Bill (talkcontribs)

I'm looking for a solution that may or may not be viable. I have a new site still under construction with Mediawiki 1.18 installed. This installation is bridged with an SMF 2.0.2 forum installation. I have PHP 5.2.17 installed and MySQL 5.0.91 for a database, if that matters. Here's my issue.

The main website and the forum have links to the wiki and are setup to open in the same window. The wiki will have a number of external links and is set to open these in a new window. However, I have external links to the the main site and the forum built into the sidebar that I would like to open in the same window (i.e. _self). Is this type of segregation of external links possible?

http://theantiquegique.com/index.php - Main site
http://theantiquegique.com/mediawiki/index.php - Wiki
http://theantiquegique.com/forum/index.php - Forum

Thank you,

Emufarmers (talkcontribs)

You could certainly do this with a hook. LinkerMakeExternalLink would probably be good: check whether $url includes your domain, and add _blank or whatever to $attribs if it doesn't.

Brother Bill (talkcontribs)

Thanks for the response, at least now I know that all hope is not lost. There are only two urls that I want to behave differently than the rest, the one to the main site and the one to the forum. I was sure there had to be a way.

I've looked over the LinkerMakeExternalLink page briefly, and the page on hooks, it's all still pretty new to me and somewhat confusing but at least now I have some direction. Thanks again for your help.

Brother Bill (talkcontribs)

I'm no coder, so that will probably explain my confusion. I can't make heads or tails of the LinkerMakeExternalLink as described in the manual. In my LocalSettings, external links are set to open in a separate page as such;


$wgExternalLinkTarget = '_blank';


Nothing else there, just the one line.

I'm guessing that I need some sort of conditional statement, but the best I can come up with is that I need something along this line;


if ($url=='theantiquegique.com/*') $wgExternalLinkTarget = '_self';
else $wgExternalLinkTarget = '_blank';


But I can't get that to work either. I think that since I only have the two urls that need special treatment, I'll probably have to live with them the way they are for now. I don't really want to keep experimenting when I really don't have a clue as to what I'm doing. In the meantime, I'll keep reading.

Thanks again,

Bawolff (talkcontribs)

Try code like this at the bottom of your LocalSettings.php

$wgExternalLinkTarget = '_blank';

$wgHooks['LinkerMakeExternalLink'][] = 'wfMakeSomeLinksNewWindow';
function wfMakeSomeLinksNewWindow( &$url, &$text, &$link, &$attribs, $linktype ) {
        if ( strpos( $url, 'http://theantiquegique.com' ) === 0 ) {
                $attribs['target'] = '_self';
        }
        return true;
}
Brother Bill (talkcontribs)

OK, I was just about ready to give up, but now we're getting somewhere. This seems to work fine for the page and for the logo, but not in the navigation menu. Just the same, I think I can work around that if I move my urls out into the main part of the open page. I think I remember reading somewhere that there's a way to place an item on every page, that should hopefully solve that issue.

Thank you folks so very much. This is a bunch better than anything I was able to come up with on my own.

Brother Bill (talkcontribs)

I'm not going to pretend to understand all of what you did here yet, but if you don't mind, let me offer what I believe happened and maybe I can build from there.

First, you declare the variable $wgExternalLinkTarget and assign it a string value of '_blank'. I'm assuming this is a global variable used to default any external urls into a page of their own.

Next, whenever an external link is selected, the LinkerMakeExternalLink hook calls the function wfMakeSomeLinksNewWindow and tests the link to see if it begins with the string 'http://theantiquegique.com'. If it does, the target attribute of the link is assigned the value '_self'. If it returns false, the function is void and the external link opens in it's own window (the default).

Am I even close?

Bawolff (talkcontribs)

Yep, that's exactly what happens.

The thing with the nav bar - Currently skins do not call Linker::makeExternalLink for links in the sidebar (instead the html is built inside SkinTemplate::makeLink and Skin::addToSidebarPlain, so the LinkerMakeExternalLink hook is not called. At the moment you'd probably have to hook into SkinBuildSidebar which can modify the attributes on individual links in the sidebar, or do something kind of hacky, like make an interwiki that points to http://theantiquegique.com and use that to make an "internal" link to that page which is not subject to $wgExternalLinkTarget .

Brother Bill (talkcontribs)

Well, as I said, I'm no coder. Most of this is just another foreign language to me that maybe in time I'll learn enough to make sense of it all. However, I do want to thank you again, your help has been most appreciated. I've managed a workaround that is quite sufficient for my present needs by inserting a navigational aide at the top of each page. This will do until I can afford more time to experiment.

Again, Thank you so much,

Reply to "Segregated external links"