Revision history for WikipediaAction
Revision [18729]
Last edited on 2008-01-28 00:12:34 by RolandStens [Modified links pointing to docs server]No Differences
Revision [12600]
Edited on 2006-01-10 23:05:23 by RolandStens [Modified links pointing to docs server]Additions:
Another version of the code that does the same but now with some more error checking and multiple language support as suggested by DarTar.
// wikipedia action
// This action will insert the content of the supplied topic in your page.
// Usage examples: {{wikipedia topic="Workflow"}}
// {{wikipedia topic="Madrid" lang="es"}}
function fetchUrlWithoutHanging($url)
{
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
print("WikiPedia Error: ");
} // end if
else {
$xml = '';
fputs($socketConnection, "GET /$resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 128);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} // end function
if (!isset($vars['lang'])) $lang = 'en';
else $lang = $vars['lang'];
if ($file = fetchUrlWithoutHanging('http://'.$lang.'.wikipedia.org/wiki/'.$topic))
{
$file = str_replace('href="/', 'href="http://'.$lang.'.wikipedia.org/', $file);
preg_match_all('#<!-- start content -->(.*?)<!-- end content -->#es', $file, $ar);
unset($file);
if (is_array($ar[1])) echo $ar[1][0];
else echo '';
}
else
{
echo 'http://'.$lang.'.wikipedia.org/wiki/'.$topic.' could not be found!';
}
// wikipedia action
// This action will insert the content of the supplied topic in your page.
// Usage examples: {{wikipedia topic="Workflow"}}
// {{wikipedia topic="Madrid" lang="es"}}
function fetchUrlWithoutHanging($url)
{
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
print("WikiPedia Error: ");
} // end if
else {
$xml = '';
fputs($socketConnection, "GET /$resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 128);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} // end function
if (!isset($vars['lang'])) $lang = 'en';
else $lang = $vars['lang'];
if ($file = fetchUrlWithoutHanging('http://'.$lang.'.wikipedia.org/wiki/'.$topic))
{
$file = str_replace('href="/', 'href="http://'.$lang.'.wikipedia.org/', $file);
preg_match_all('#<!-- start content -->(.*?)<!-- end content -->#es', $file, $ar);
unset($file);
if (is_array($ar[1])) echo $ar[1][0];
else echo '';
}
else
{
echo 'http://'.$lang.'.wikipedia.org/wiki/'.$topic.' could not be found!';
}
Additions:
CategoryUserContributions
Additions:
Extracted and changed from:
Mediawiki MySite class DOCS (ver. 0.1.0)
AUTHOR: Piotr Maliński
Contact: [email protected]
Site: http://www.english.rk.edu.pl | http://www.cms.rk.edu.pl - Polish
License: GPL
Mediawiki MySite class DOCS (ver. 0.1.0)
AUTHOR: Piotr Maliński
Contact: [email protected]
Site: http://www.english.rk.edu.pl | http://www.cms.rk.edu.pl - Polish
License: GPL