Created
January 28, 2010 20:48
-
-
Save carylee/289123 to your computer and use it in GitHub Desktop.
A function and subfunctions to find links to the same content on two different sites (of different languages) - for use on myoncofertility.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* get_alternate_language_link | |
* returns a full url to a page on the other language site | |
* or returns false if the page doesn't exist in the other language | |
* | |
* Arguments: $path (optional) | |
* if a path is supplied (e.g. 'node/4') | |
* the alternate link to that path will be returned | |
* $path defaults to the current page | |
* | |
* Example: | |
* $alternate_link = get_alternate_language_link(); | |
* if($alternate_link) | |
* echo "<a href='$alternate_link'>See this page in spanish</a>"; | |
* | |
*/ | |
function get_alternate_language_link( $path=NULL ) { | |
if(empty($path)) | |
$path = get_current_page_path(); | |
$debug = false; // print url whether page exists or not | |
global $base_url; | |
$current_language = get_current_language(); | |
switch( $current_language ) { | |
case 'es': | |
$base = substr($base_url, 0, -3); | |
break; | |
case 'en': | |
$base = $base_url . '/es'; | |
break; | |
} | |
$url = $base . '/' . $path; | |
if($debug) | |
echo "Alternate link: $url\n"; | |
if( page_exists( $url ) ) | |
return $url; | |
else | |
return false; | |
} | |
function get_current_language() { | |
global $base_url; | |
$pattern = '/\/es$/'; | |
if( preg_match( $pattern, $base_url ) ) | |
return 'es'; | |
else | |
return 'en'; | |
} | |
function page_exists( $url ) { | |
// This function will use libcurl if it is installed, | |
// or execute curl at command line if not. | |
// This function is currently untested as bion does not have | |
// libcurl installed. I think it should work as-is though. | |
if( extension_loaded( 'libcurl' ) ) { | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_HEADER, true); | |
curl_setopt($curl, CURLOPT_NOBODY, true); | |
$header = curl_exec($curl); | |
$response = curl_getinfo( $curl ); | |
} | |
else { | |
exec('curl -I "'.$url.'"', $output); | |
// I'd much rather use libcurl than exec curl, | |
// but from what I can tell, libcurl isn't installed | |
// in bion's php build. | |
// if the production server has libcurl, I can rewrite this in pure php | |
$pattern = '/\d{3}/'; | |
preg_match( $pattern, $output[0], $matches ); | |
$response = $matches[0]; | |
} | |
return ($response == 200); | |
} | |
function get_current_page_path() { | |
return implode('/', arg() ); | |
} | |
function print_link_if_exists() { | |
$link = get_alternate_language_link( get_current_page_path() ); | |
if( $link ) | |
echo "Page exists at $link"; | |
else | |
echo "Page does not exist"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment