Revision [12600]

This is an old revision of WikipediaAction made by RolandStens on 2006-01-10 23:05:23.

 

Wikipedia Action

Last edited by RolandStens
Tue, 10 Jan 2006 23:05 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
There are 7 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki