Urdu English
English English Deutsch Deutsch Español Español Français Français Italiano Italiano Nederlands Nederlands Português Português Русский Русский Ελληνικά Ελληνικά
Login



 
Welcome to Jumi! >> Tutorial >> Wikipedia Reflection

This script will allow you to display wikipedia content on your site.

wiki_reflect.php

You need to create new Jumi application with the following code below.

You can add the /* config */ part of the script to Custom Script field, for making changes easily from the Joomla! admin interface. Otherwise you need to edit the wiki_reflect.php file every time you need to make changes.

<?php defined("_JEXEC") or die("Restricted access"); ?>
<?php
/* config */
$wiki_url   = 'http://en.wikipedia.org';
$title      = 'Joomla';
$cache_time = 30*24*60*60; // 30 days
$file_name  = 'cache/'.md5($title);

$nice_title = str_replace('_', ' ', stripslashes($title));

// check cache
if(file_exists($file_name) and strtotime('now') - filemtime($file_name) < $cache_time) {
    echo file_get_contents($file_name);
} else {
    // retrieve article from wikipedia
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $wiki_url.'/wiki/'.$title);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $wiki_content = curl_exec($ch);
    curl_close($ch);

    /* modify article for inclusion */
    // separate the article content
    $wiki_content = substr($wiki_content, strpos($wiki_content, '<!-- start content -->'));
    $wiki_content = substr($wiki_content, 0, strpos($wiki_content, '<div class="printfooter">'));
    // replace relative links
    $wiki_content = str_replace('"/w/skin', '"'.$wiki_url.'/w/skin', $wiki_content);
    $wiki_content = str_replace('"/skins', '"'.$wiki_url.'/skins', $wiki_content);
    $wiki_content = str_replace('"/wiki', '"'.$wiki_url.'/wiki', $wiki_content);
    // remove edit links
    $wiki_content = str_replace('>edit<', '><', $wiki_content);
    $wiki_content = str_replace('[<', '<', $wiki_content);
    $wiki_content = str_replace('>]', '>', $wiki_content);
    $wiki_content = str_replace('href="/w/index.php?', 'target="_blank" href="'.$wiki_url.'/w/index.php?', $wiki_content);
    // cache article 
    if(strlen($wiki_content) < 3000) {
        echo '<p>Unfortunately, no content could be extracted! ';
        echo 'You can access wiki article directly <a href="'.$wiki_url.'/wiki/'.$title.'" target="_blank">'.$nice_title.'</a></p>';
    } else {
        file_put_contents($file_name, $wiki_content);
        echo $wiki_content;
    }
}

Your host must support PHP Curl library.

Acknowledgements