Wikipedia Action

Last edited by RolandStens:
Modified links pointing to docs server
Mon, 28 Jan 2008 00:12 UTC [diff]


This action is meant to automatically incorporate http://wikipedia.org content in your pages.
To use it as a standalone action simply add for instance:
{{wikipedia topic="Workflow"}} in the page body.

Save the code below as actions/wikipedia.php.

The action

Current version: 0.1

Changes:

To do:



actions/wikipedia.php


<?php
    $topic = $vars['topic'];
    $file = file_get_contents('http://en.wikipedia.org/wiki/'.$topic);
    $file = str_replace('href="/', 'href="http://en.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 '';
?>


Extracted and changed from:
Mediawiki MySite class DOCS (ver. 0.1.0)
AUTHOR: Piotr Maliński
Contact: riklaunim@gmail.com
Site: http://www.english.rk.edu.pl | http://www.cms.rk.edu.pl - Polish
License: GPL

Another version of the code that does the same but now with some more error checking and multiple language support as suggested by DarTar.
<?php
    // 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

   
   
    $topic = $vars['topic'];
        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!';
    }
?>



CategoryUserContributions
Comments
Comment by DarTar
2005-12-06 08:08:55
Roland, you might want to make the locale for the Wikipedia server configurable, so for instance using {{wikipedia topic="WikkaWiki" language="es"}} will retrieve content from http://es.wikipedia.org/wiki/WikkaWiki. And maybe add an error message if there is no content to display. My 2 cents.
Comment by YodaHome
2006-01-05 03:40:40
I'm afraid I can't get this working, I always get an error message like:

Warning: file_get_contents(http://en.wikipedia.org/wiki/Workflow): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /homepages/45/d83701170/htdocs/wiki/actions/wikipedia.php on line 3

But I don't see the problem. Why should that be forbidden? Any ideas?
Comment by RolandStens
2006-01-10 00:23:31
Are you running your server inside a corporate network? In other words do you have to go out through a proxy server to get to the Internet?
Comment by s-144-27.resnet.unb.ca
2006-01-10 01:31:09
Hmmm, well I run my own server w/o a proxy and not in a corporate network and I, I'm sorry to say, have the same problem.
Comment by DarTar
2006-01-10 08:05:22
Possibly related to your PHP configuration?
http://www.php.net/manual/en/ref.filesystem.php#ini.allow-url-fopen
Comment by RolandStens
2006-01-10 23:09:14
Added a new version, which gets the info in a slightly different way.
I've also added the multi-lingual facility that DarTar requested. For this to work well for other languages than english, you probably have to change to encoding.
Consider utf-8 encoding.
Comment by YodaHome
2006-01-11 00:48:32
Grabbed the newer version, but unfortunately this one shows nothing at all like on http://yodahome.de/wiki/VisualStudiesTest. Don't know if my hosted webspace is behind a proxy but it's possible. Why would that be a problem?
I'm sure it's not about the allow_url_fopen because as you can see at http://yodahome.de/test.php that's enabled.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki