Mediawiki: remove nofollow tag for selected sites

in

[SEE THE UPDATE BELOW]

By default, Mediawiki (the software that powers Wikipedia and other sites) adds the evil nofollow tag to outbound links. This page describes how to have that tag removed for a pre-defined set of trusted sites.

The setting that controls whether links have that tag is $wgNoFollowLinks, which is located in the LocalSettings.php file. If this is set to true, but you want some links not to have that tag, follow the instructions below. These instructions work with version 1.6.8 and it should work with other versions as well. The function described in Step 4 below is the same in other versions such as: 1.6.9, 1.8.3, and 1.9.2.

1. Just to be on the safe side, backup everything: your database and the directory containing the wiki installation.

2. Add the following near the end of LocalSettings.php. This should be after all the other settings, but before the line containing ?>. If there's no line with that, add it at the end of the file:

$wgFollowmeAcceptableHostnameArray = array(
"tolstoy.com",
"ibm.com",
"24ahead.com"
);

Replace those domain names with the ones that you find acceptable. Note that the 'www' is not needed and should not be used. However, if a site uses other subdomains, and you want those links to be followed, the forms with the non-www subdomains need to be included. Note also that these domain names must be in lower case.

For instance, if you want to allow URLs from 'www.DRuPal.org', 'Drupal.org', and 'cvs.drupal.org', you would only need to use 'drupal.org' and 'cvs.drupal.org' in that array. If you just use 'Drupal.org', or just 'www.drupal.org', it won't work as expected.

3. Add the following somewhere in the file Linker.php, which is located in the includes directory. Right before the line class Linker { might be a good place:

/**
*
* The following class is not part of the MediaWiki distribution.
*
* See this for details:
* http://tolstoy.com/php/mediawiki-remove-nofollow-tag-selected-sites
*
*/
class LinkerAdditions {
function followmeCheckURL( $url ) {
global $wgFollowmeAcceptableHostnameArray;

$parseArray = parse_url( $url );
if ( !$parseArray ) {
return FALSE;
}

$hostname = $parseArray[ 'host' ];
$hostname = strtolower( str_replace( 'www.', '', $hostname ) );

return in_array( $hostname, $wgFollowmeAcceptableHostnameArray );
}
}

4. Then, you need to modify the makeExternalLink function in that same file (Linker.php). Change the line containing:

if( $wgNoFollowLinks ) {

To the following:

if( $wgNoFollowLinks && !LinkerAdditions::followmeCheckURL( $url ) ) {

5. At that point, you'll only need to edit the array in LocalSettings.php when you want to add or delete trusted sites.

And, of course, note that you'll have to re-do these steps when upgrading your system.

Coming soon: a few other ways to do this.

UPDATE: This was written sometime around 2007 or so. In January 2009, Wikimedia added a new configuration option called $wgNoFollowDomainExceptions that's similar to the $wgFollowmeAcceptableHostnameArray above. See the notes about that setting and about $wgNoFollowNsExceptions and $wgNoFollowLinks in DefaultSettings.php.