Topic on Project:Support desk

Opening file-link in new tab

7
194.29.4.166 (talkcontribs)

File links should open in a new tab in the browser. Do you have suggestions how to change the settings?


MediaWiki 1.19.1 PHP 5.4.9 (cgi-fcgi) MySQL 5.5.21-log

MarkAHershberger (talkcontribs)

I've done this by using a hook. If you want a pointer to the code, feel free to ask.

194.29.4.166 (talkcontribs)
MarkAHershberger (talkcontribs)

I used the following code to open PDFs in a new window. This should give you an idea.

class Hooks {

	static private function doPDFTarget ( $target, &$text, &$attribs ) {
		global $wgExternalLinkTarget;
		wfProfileIn( __METHOD__ );
		if ( strtolower( substr( $target, -4 ) ) === ".pdf" ) {
			if( $wgExternalLinkTarget !== false ) {
				$attribs["target"] = $wgExternalLinkTarget;
			} else {
				$attribs["target"] = "_blank";
			}
		}
		wfProfileOut( __METHOD__ );

		return true;
	}

	static public function ImageParams ( $title, $file, &$params ) {
		self::doPDFTarget( $title, $params['frame']['title'], $params['frame'] );
		# Make all image links go no where.
		if( empty( $params['frame']['link'] ) && empty( $params['frame']['target'] ) ) {
			$params['frame']['no-link'] = 'xxx';
		}
		return true;
	}

	/* may not be necessary? */
	static public function FetchFile ( $parser, $title, &$options, &$desQuery ) {
		$options['target'] = '_blank';
		return true;
	}

	static public function ExternalLink ( &$url, &$text, &$link, &$attribs ) {
		self::doPDFTarget ( $url, $text, $attribs );

		return true;
	}

	static public function InternalLink ( $dummy, $target, $options, $html, &$attribs, &$ret ) {
		self::doPDFTarget ( $target, $text, $attribs );

		return true;

	}
}
$wgHooks['LinkerMakeExternalLink'][] = 'Hooks::ExternalLink';
$wgHooks['ParserMakeImageParams'][] = 'Hooks::ImageParams';
$wgHooks['BeforeParserFetchFileAndTitle'][] = 'Hooks::FetchFile';
$wgHooks['LinkEnd'][] = 'Hooks::InternalLink';
59.160.207.20 (talkcontribs)

Where do I incorporate this code change? In Hooks.php?

Arun

87.123.63.223 (talkcontribs)

I would try adding it to the end of LocalSettings.php. At least this is where you usually put your hook functions... (Although I personally never use a class to put them into, but anyway.)

Reply to "Opening file-link in new tab"