Extension:AtomExporter

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
AtomExporter

Release status: beta

Implementation Special page
Description Export wiki pages into valid Atom 1.0 feed.
Author(s) Manuel Traverso (mtraversotalk)
Last version 0.1 (21/07/2008)
MediaWiki Tested on 1.12.0
License GPL
Download Extension:AtomExporter#Code
Example http://yourwiki/index.php/Special:AtomExporter/feed
Check usage and version matrix

What can this extension do? [edit]

This extension allows users to export the articles into Atom feeds.

Installation [edit]

  1. Create a folder AtomExporter in your MediaWiki extensions folder.
  2. Copy and paste the PHP code of the files below into a text editor and save them in the folder as AtomExporter.i18n.php, AtomExporter_body.php and AtomExporter.php, respectively.
  3. add the following to LocalSettings.php:
require_once('extensions/AtomExporter/AtomExporter.php');

Code [edit]

AtomExporter.i18n.php

<?php
 
$messages = array();
 
$messages['en'] = array( 
        'atomexporter' => 'Export wiki (Atom 1.0 feed)'
        );
 
?>

AtomExporter_body.php

<?php
 
function efRunAtomExporter($par) 
{
    AtomExporter::run($par);
}
 
class AtomExporter extends SpecialPage 
{
                var $articles = array();
 
        function AtomExporter() 
                {               
                                SpecialPage::SpecialPage("AtomExporter", '', true, 'efRunAtomExporter');
                wfLoadExtensionMessages("AtomExporter");
        }
 
        function execute($params) 
                {
 
                        global $wgRequest, $wgOut;
                        global $wgServer, $wgScript, $wgTitle, $wgSitename;                                              
 
                        $this->setHeaders();
 
                        $params         = split('/',$params);                                           
                        $mode           = $params[0];
                        $page_title = $params[1];       
 
                        $action = $wgServer.$wgScript.'/'.$wgTitle.'/feed';
 
                        if($mode != 'feed')
                        {       
                                $wgOut->addHTML('<div class="toc">');
                                $wgOut->addHTML('<p>Date Format is <b>yyyy-mm-ddd</b> <i>(ex.: 1984-11-07)</i>. <br />');
                                $wgOut->addHTML('<i>Since</i> and <i>until</i> can be null. If you specifed a date in <i>until</i> there must be a <i>since</i>, though.</p>');
                                $wgOut->addHTML('<form action="'.$action.'" method="get">');
                                $wgOut->addHTML('<table border="0">');
                                $wgOut->addHTML('<tr>');
                                $wgOut->addHTML('<td align="right">since: </td>');
                                $wgOut->addHTML('<td><input type="text" name="since"></td>');                                                                           
                                $wgOut->addHTML('</tr>');
                                $wgOut->addHTML('<tr>');
                                $wgOut->addHTML('<td align="right">until: </td>');
                                $wgOut->addHTML('<td><input type="text" name="until"></td>');                           
                                $wgOut->addHTML('</tr>');                       
                                $wgOut->addHTML('<tr>');
                                $wgOut->addHTML('<td colspan="2">');
                                $wgOut->addHTML('<input type="submit" value="Get Atom feeds for this wiki" ');
                                $wgOut->addHTML('</td>');
                                $wgOut->addHTML('</tr>');
                                $wgOut->addHTML('</table>');
                                $wgOut->addHTML('</form>');
                                $wgOut->addHTML('</div>');
                        }
                        else                                    
                                $this->writeXML($since, $until, $page_title);           
        }
 
                private function writeXML($since = null, $until = null, $page_title = null)
                {               
                        global $wgRequest, $wgOut, $wgParser;
                        global $wgServer, $wgScript, $wgTitle, $wgSitename, $wgUser;
                        global $wgLocalTZoffset;
 
                        if(!$wgLocalTZoffset)
                                date_default_timezone_set('UTC');
 
                        $wgOut->disable();
                        wfResetOutputBuffers();
 
                        $q_since = $wgRequest->getVal("since");
                        $q_until = $wgRequest->getVal("until");
 
                        $date_format = "YmdHis";
                        $since = ($q_since) ? date($date_format, strtotime($q_since)) : null;
                        $until = ($q_until) ? date($date_format, strtotime($q_until)) : date($date_format, time());
 
                        $wiki_link = $wgServer.$wgScript.'/'.$wgTitle.'/feed';                  
 
                        if($since)
                        {
                                $wiki_link .= "?since=".$q_since;
                                if($q_until)
                                        $wiki_link .= "&amp;until=".$q_until;
                        }       
 
                        header( "Content-type: application/xml; charset=utf-8" );
                        print '<?xml version="1.0" encoding="UTF-8"?>';
                        print "\n";
                        print '<feed xmlns="http://www.w3.org/2005/Atom"';
                        print "\n";
                        print "\t".'xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">';
                        print "\n";
                        print "\t"."<id>".utf8_encode($wiki_link)."</id>";
                        print "\n";
                        print "\t".'<title>'.utf8_encode($wgSitename).'</title>';
                        print "\n";                     
                        print "\t".'<updated>'.$this->getWikiLastTouch().'</updated>';          
                        print "\n";
 
                        if($page_title)
                        {                       
                                print "\t".'<link href="'.utf8_encode($wiki_link).'"/>';                        
                                print "\n";
 
                                $wiki_link .= "/$page_title";
 
                                $dbr =& wfGetDB(DB_SLAVE);                      
 
                                list($page_tbl) = $dbr->tableNamesN('page');
 
                                $page_query  = "SELECT * ";
                                $page_query .= "FROM $page_tbl ";
                                $page_query .= "WHERE page_is_redirect = 0 AND page_namespace = 0 AND page_title = '$page_title' ";  
                                $page_query .= "ORDER BY page_title;";
 
                                $page_res = $dbr->doQuery($page_query);
 
                                if ($dbr->numRows($page_res)) 
                                {
                                        $art_row = $dbr->fetchRow($page_res); 
 
                                        $art_title = Title::newFromID($art_row['page_id']);
 
                                        $article = new Article($art_title);                             
                                        $article->fetchContent($art_row['page_latest']);
 
                                        $content = $article->getContent();                                      
 
                                        $dt_upd = new DateTime($article->getTouched());
                                        $entry_updated = $dt_upd->format(DATE_RFC3339); 
 
                                        $parserOptions = ParserOptions::newFromUser( $wgUser );
                    $parserOptions->setEditSection( false );
                    $parserOptions->setTidy(true);
                    $wgParser->mShowToc = false;                        
                                        $parserOutput = $wgParser->parse($article->preSaveTransform($article->getContent())."\n\n", $art_title, $parserOptions );                     
                                        $bhtml = $parserOutput->getText();
                    $bhtml = $bhtml; 
                    $bhtml = str_replace ($wgScriptPath, $wgServer . $wgScriptPath, $bhtml);
                    $bhtml = str_replace ('/w/',$wgServer . '/w/', $bhtml);
                    $content = $bhtml;                                  
 
                                        print "\t".'<entry>';
                                        print "\n";                     
                                        print "\t\t"."<id>".utf8_encode($wiki_link)."</id>";
                                        print "\n";
                                        print "\t\t".'<title>'.utf8_encode($page_title).'</title>';
                                        print "\n";
                                        print "\t\t".'<link rel="self" type="application/atom+xml" title="article" ';
                                        print "\n";
                                        print "\t\t\t".'href="'.utf8_encode($wiki_link).'" />';
                                        print "\n";                     
                                        print "\t\t".'<link rel="alternate" type="text/html" title="article" ';
                                        print "\n";
                                        print "\t\t\t".'href="'.utf8_encode($art_title->getFullURL()).'" />';
                                        print "\n";     
                                        print "\t\t".'<updated>'.$entry_updated.'</updated>';           
                                        print "\n";     
                                        print "\t\t".'<author>';
                                        print "\n";
                                        print "\t\t\t".'<name>'.$article->getUserText().'</name>';
                                        print "\n";
                                        print "\t\t".'</author>';
                                        print "\n";
                                        print "\t\t".'<content type="html">';
                                        print "\n";     
                                        print htmlspecialchars($content);
                                        print "\n";     
                                        print "\t\t".'</content>';
                                        print "\n";                                     
                                        print "\t".'</entry>';
                                        print "\n";
                                        print "</feed>"; 
 
                                }
                                else
                                {                               
                                        wfHttpError(500, "Internal Server Error", "Article not found.");
                                        $dbr->freeResult($page_res);
                                }
                                $dbr->freeResult($page_res);
                        }
                        else
                        {       
                                print "\t".'<link rel="self" href="'.utf8_encode($wiki_link).'"/>';                     
                                print "\n";
 
                                $dbr =& wfGetDB(DB_SLAVE);
 
                                list($page_tbl) = $dbr->tableNamesN('page');            
 
                                $all_pages_query  = "SELECT * ";
                                $all_pages_query .= "FROM $page_tbl ";
                                $all_pages_query .= "WHERE `page_is_redirect` = 0 AND `page_namespace` = 0 ";                           
                                if($since)
                                        $all_pages_query .= "AND `page_touched` between $since and $until ";
                                $all_pages_query .= "ORDER BY `page_touched`;";
 
                                $all_pages_res = $dbr->doQuery($all_pages_query);
 
                                if ($dbr->numRows($all_pages_res)) 
                                {
                                        $last_updated = "";
                                        while ($art_row = $dbr->fetchRow($all_pages_res)) 
                                        { 
                                                $article = array();
 
                                                $art_title = Title::newFromID($art_row['page_id']);
 
                                                $art_obj = new Article($art_title);                                                     
                                                $art_obj->fetchContent($art_row['page_latest']);
 
                                                $article["obj"] = $art_obj;
                                                $article["row"] = $art_row;
                                                $this->articles[] = $article;
 
                                                $entry_title = $art_title->getPrefixedURL();
                                                $entry_id    = $wgServer.$wgScript.'/'.$wgTitle.'/feed/'.$entry_title;
 
                                                $dt_upd = new DateTime($art_obj->getTouched());
                                                $entry_updated = $dt_upd->format(DATE_RFC3339); 
 
                                                print "\t".'<entry>';
                                                print "\n";                                     
                                                print "\t\t".'<id>'.utf8_encode($entry_id).'</id>';
                                                print "\n";
                                                print "\t\t".'<title>'.utf8_encode($entry_title).'</title>';
                                                print "\n";
                                                print "\t\t".'<link rel="alternate" type="application/atom+xml" title="article" ';
                                                print "\n";
                                                print "\t\t\t".'href="'.utf8_encode($entry_id).'" />';
                                                print "\n";
                                                print "\t\t".'<updated>'.$entry_updated.'</updated>';
                                                print "\n";                                     
                                                print "\t\t".'<author>';
                                                print "\n";
                                                print "\t\t\t".'<name>'.$art_obj->getUserText().'</name>';
                                                print "\n";
                                                print "\t\t".'</author>';
                                                print "\n";                                     
                                                print "\t".'</entry>';
                                                print "\n";
                                        }
                                }
 
                                print "</feed>";
 
                                $dbr->freeResult($all_pages_res);                               
                        }               
 
                }
 
                private function getWikiLastTouch()
                {
                        $dbr =& wfGetDB(DB_SLAVE);
 
                        list($page_tbl) = $dbr->tableNamesN('page');
 
                        $timespan_query  = "SELECT MAX(page_touched) ";
                        $timespan_query .= "FROM $page_tbl ";
                        $timespan_query .= "WHERE page_is_redirect = 0 AND page_namespace = 0;";
 
                        $timespan_res = $dbr->doQuery($timespan_query);                 
                        $row = $dbr->fetchRow($timespan_res);
 
                        $dt = new DateTime($row['MAX(page_touched)']); 
                        $last_updated = $dt->format(DATE_RFC3339);      
 
                        $dbr->freeResult($timespan_res);
 
                        return $last_updated;                   
                }
}
 
?>

AtomExporter.php

<?php
 
if (!defined('MEDIAWIKI')) 
{
        print <<<EOT
To install AtomExporter extension, put the following line in LocalSettings.php:
require_once("$IP/extensions/AtomExporter/AtomExporter.php");
EOT;
        exit(1);
}
 
# Credits
$wgExtensionCredits['specialpage'][] = array(
    'name'=>'AtomExporter',
    'author'=>'Manuel Traverso (manuel.traverso&lt;at&gt;snoopconsulting.com)',
    'url'=>'http://www.snoopconsulting.com',
    'description'=>'Exports MediaWiki articles into valid Atom 1.0 feeds.',
    'version'=>'0.0.1'
);
 
$dir = dirname(__FILE__) . '/';
 
$wgAutoloadClasses['AtomExporter'] = $dir . 'AtomExporter_body.php';
$wgExtensionMessagesFiles['AtomExporter'] = $dir . 'AtomExporter.i18n.php';
$wgSpecialPages['AtomExporter'] = 'AtomExporter';
 
?>