Opening file-link in new tab

Fragment of a discussion from Project:Support desk
Jump to: navigation, search

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';
MarkAHershberger(talk)15:07, 11 June 2013

Thank you, I ll try this! Have a great day

194.29.4.16607:42, 12 June 2013