Extension:AtomExporter

From MediaWiki.org
Jump to: navigation, search
Translate this page; This page contains changes which are not marked for translation.

Other languages:
Deutsch • ‎English • ‎español • ‎日本語 • ‎polski • ‎sicilianu
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
AtomExporter

Release status:Extension status beta

ImplementationTemplate:Extension#type Special page
DescriptionTemplate:Extension#description Export wiki pages into valid Atom 1.0 feed.
Author(s)Template:Extension#username Manuel Traverso (mtraversotalk)
Latest versionTemplate:Extension#version 0.2 (2014-10-10)
MediaWikiTemplate:Extension#mediawiki Tested on 1.12.0
PHPTemplate:Extension#php 5.3+
LicenseTemplate:Extension#license GPL
Download #Code

Translate the AtomExporter extension if it is available at translatewiki.net

Check usage and version matrix.

Details[edit]

The AtomExporter extension allows users to export the articles into Atom feeds.

Installation[edit]

  • Copy the source code below, and place the file(s) in a directory called AtomExporter in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/AtomExporter/AtomExporter.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

AtomExporter.i18n.php

<?php

$messages = array();

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

AtomExporter_body.php

  1 <?php
  2 
  3 function efRunAtomExporter($par) 
  4 {
  5     AtomExporter::run($par);
  6 }
  7 
  8 class AtomExporter extends SpecialPage 
  9 {
 10 		var $articles = array();
 11 		
 12         function AtomExporter() 
 13 		{               
 14 				SpecialPage::SpecialPage("AtomExporter", '', true, 'efRunAtomExporter');
 15                 wfLoadExtensionMessages("AtomExporter");
 16         }
 17  
 18         function execute($params) 
 19 		{
 20 		
 21 			global $wgRequest, $wgOut;
 22 			global $wgServer, $wgScript, $wgTitle, $wgSitename;						 
 23            
 24 			$this->setHeaders();
 25 			
 26 			$params 	= split('/',$params);						
 27 			$mode 		= $params[0];
 28 			$page_title = $params[1];	
 29 			
 30 			$action = $wgServer.$wgScript.'/'.$wgTitle.'/feed';
 31 			
 32 			if($mode != 'feed')
 33 			{	
 34 				$wgOut->addHTML('<div class="toc">');
 35 				$wgOut->addHTML('<p>Date Format is <b>yyyy-mm-dd</b> <i>(ex.: 1984-11-07)</i>. <br />');
 36 				$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>');
 37 				$wgOut->addHTML('<form action="'.$action.'" method="get">');
 38 				$wgOut->addHTML('<table border="0">');
 39 				$wgOut->addHTML('<tr>');
 40 				$wgOut->addHTML('<td align="right">since: </td>');
 41 				$wgOut->addHTML('<td><input type="text" name="since"></td>');										
 42 				$wgOut->addHTML('</tr>');
 43 				$wgOut->addHTML('<tr>');
 44 				$wgOut->addHTML('<td align="right">until: </td>');
 45 				$wgOut->addHTML('<td><input type="text" name="until"></td>');				
 46 				$wgOut->addHTML('</tr>');			
 47 				$wgOut->addHTML('<tr>');
 48 				$wgOut->addHTML('<td colspan="2">');
 49 				$wgOut->addHTML('<input type="submit" value="Get Atom feeds for this wiki" ');
 50 				$wgOut->addHTML('</td>');
 51 				$wgOut->addHTML('</tr>');
 52 				$wgOut->addHTML('</table>');
 53 				$wgOut->addHTML('</form>');
 54 				$wgOut->addHTML('</div>');
 55 			}
 56 			else					
 57 				$this->writeXML($since, $until, $page_title);		
 58         }
 59 		
 60 		private function writeXML($since = null, $until = null, $page_title = null)
 61 		{		
 62 			global $wgRequest, $wgOut, $wgParser;
 63 			global $wgServer, $wgScript, $wgTitle, $wgSitename, $wgUser;
 64 			global $wgLocalTZoffset;
 65 			
 66 			if(!$wgLocalTZoffset)
 67 				date_default_timezone_set('UTC');
 68 			
 69 			$wgOut->disable();
 70 			wfResetOutputBuffers();
 71 			
 72 			$q_since = $wgRequest->getVal("since");
 73 			$q_until = $wgRequest->getVal("until");
 74 			
 75 			$date_format = "YmdHis";
 76 			$since = ($q_since) ? date($date_format, strtotime($q_since)) : null;
 77 			$until = ($q_until) ? date($date_format, strtotime($q_until)) : date($date_format, time());
 78 						
 79 			$wiki_link = $wgServer.$wgScript.'/'.$wgTitle.'/feed';			
 80 			
 81 			if($since)
 82 			{
 83 				$wiki_link .= "?since=".$q_since;
 84 				if($q_until)
 85 					$wiki_link .= "&amp;until=".$q_until;
 86 			}	
 87 				
 88 			header( "Content-type: application/xml; charset=utf-8" );
 89 			print '<?xml version="1.0" encoding="UTF-8"?>';
 90 			print "\n";
 91 			print '<feed xmlns="http://www.w3.org/2005/Atom"';
 92 			print "\n";
 93 			print "\t".'xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">';
 94 			print "\n";
 95 			print "\t"."<id>".utf8_encode($wiki_link)."</id>";
 96 			print "\n";
 97 			print "\t".'<title>'.utf8_encode($wgSitename).'</title>';
 98 			print "\n";			
 99 			print "\t".'<updated>'.$this->getWikiLastTouch().'</updated>';		
100 			print "\n";
101 						
102 			if($page_title)
103 			{			
104 				print "\t".'<link href="'.utf8_encode($wiki_link).'"/>';			
105 				print "\n";
106 			
107 				$wiki_link .= "/$page_title";
108 				
109 				$dbr = wfGetDB(DB_SLAVE);			
110 						
111 				list($page_tbl) = $dbr->tableNamesN('page');
112 					
113 				$page_query  = "SELECT * ";
114 				$page_query .= "FROM $dbr->addIdentifierQuotes($page_tbl) ";
115 				$page_query .= "WHERE page_is_redirect = 0 AND page_namespace = 0 AND page_title = $dbr->addQuotes($page_title) ";  
116 				$page_query .= "ORDER BY page_title;";
117 					
118 				$page_res = $dbr->doQuery($page_query);
119 				
120 				if ($dbr->numRows($page_res)) 
121 				{
122 					$art_row = $dbr->fetchRow($page_res); 
123 					
124 					$art_title = Title::newFromID($art_row['page_id']);
125 
126 					$article = new Article($art_title);				
127 					$article->fetchContent($art_row['page_latest']);
128 
129 					$content = $article->getContent();					
130 					
131 					$dt_upd = new DateTime($article->getTouched());
132 					$entry_updated = $dt_upd->format(DATE_RFC3339);	
133 					
134 					$parserOptions = ParserOptions::newFromUser( $wgUser );
135                     $parserOptions->setEditSection( false );
136                     $parserOptions->setTidy(true);
137                     $wgParser->mShowToc = false;                        
138 					$parserOutput = $wgParser->parse($article->preSaveTransform($article->getContent())."\n\n", $art_title, $parserOptions );                     
139 					$bhtml = $parserOutput->getText();
140                     $bhtml = $bhtml; 
141                     $bhtml = str_replace ($wgScriptPath, $wgServer . $wgScriptPath, $bhtml);
142                     $bhtml = str_replace ('/w/',$wgServer . '/w/', $bhtml);
143                     $content = $bhtml;					
144 									
145 					print "\t".'<entry>';
146 					print "\n";			
147 					print "\t\t"."<id>".utf8_encode($wiki_link)."</id>";
148 					print "\n";
149 					print "\t\t".'<title>'.utf8_encode($page_title).'</title>';
150 					print "\n";
151 					print "\t\t".'<link rel="self" type="application/atom+xml" title="article" ';
152 					print "\n";
153 					print "\t\t\t".'href="'.utf8_encode($wiki_link).'" />';
154 					print "\n";			
155 					print "\t\t".'<link rel="alternate" type="text/html" title="article" ';
156 					print "\n";
157 					print "\t\t\t".'href="'.utf8_encode($art_title->getFullURL()).'" />';
158 					print "\n";	
159 					print "\t\t".'<updated>'.$entry_updated.'</updated>';		
160 					print "\n";	
161 					print "\t\t".'<author>';
162 					print "\n";
163 					print "\t\t\t".'<name>'.$article->getUserText().'</name>';
164 					print "\n";
165 					print "\t\t".'</author>';
166 					print "\n";
167 					print "\t\t".'<content type="html">';
168 					print "\n";	
169 					print htmlspecialchars($content);
170 					print "\n";	
171 					print "\t\t".'</content>';
172 					print "\n";					
173 					print "\t".'</entry>';
174 					print "\n";
175 					print "</feed>"; 
176 					
177 				}
178 				else
179 				{				
180 					wfHttpError(500, "Internal Server Error", "Article not found.");
181 					$dbr->freeResult($page_res);
182 				}
183 				$dbr->freeResult($page_res);
184 			}
185 			else
186 			{	
187 				print "\t".'<link rel="self" href="'.utf8_encode($wiki_link).'"/>';			
188 				print "\n";
189 				
190 				$dbr =& wfGetDB(DB_SLAVE);
191 				
192 				list($page_tbl) = $dbr->tableNamesN('page');		
193 					
194 				$all_pages_query  = "SELECT * ";
195 				$all_pages_query .= "FROM $dbr->addIdentifierQuotes($page_tbl) ";
196 				$all_pages_query .= "WHERE `page_is_redirect` = 0 AND `page_namespace` = 0 ";				
197 				if($since)
198 					$all_pages_query .= "AND `page_touched` between $dbr->addQuotes($since) and $dbr->addQuotes($until) ";
199 				$all_pages_query .= "ORDER BY `page_touched`;";
200 			
201 				$all_pages_res = $dbr->doQuery($all_pages_query);
202 				
203 				if ($dbr->numRows($all_pages_res)) 
204 				{
205 					$last_updated = "";
206 					while ($art_row = $dbr->fetchRow($all_pages_res)) 
207 					{ 
208 						$article = array();
209 						
210 						$art_title = Title::newFromID($art_row['page_id']);
211 						
212 						$art_obj = new Article($art_title);							
213 						$art_obj->fetchContent($art_row['page_latest']);
214 						
215 						$article["obj"] = $art_obj;
216 						$article["row"] = $art_row;
217 						$this->articles[] = $article;
218 						
219 						$entry_title = $art_title->getPrefixedURL();
220 						$entry_id    = $wgServer.$wgScript.'/'.$wgTitle.'/feed/'.$entry_title;
221 					
222 						$dt_upd = new DateTime($art_obj->getTouched());
223 						$entry_updated = $dt_upd->format(DATE_RFC3339);	
224 						
225 						print "\t".'<entry>';
226 						print "\n";					
227 						print "\t\t".'<id>'.utf8_encode($entry_id).'</id>';
228 						print "\n";
229 						print "\t\t".'<title>'.utf8_encode($entry_title).'</title>';
230 						print "\n";
231 						print "\t\t".'<link rel="alternate" type="application/atom+xml" title="article" ';
232 						print "\n";
233 						print "\t\t\t".'href="'.utf8_encode($entry_id).'" />';
234 						print "\n";
235 						print "\t\t".'<updated>'.$entry_updated.'</updated>';
236 						print "\n";					
237 						print "\t\t".'<author>';
238 						print "\n";
239 						print "\t\t\t".'<name>'.$art_obj->getUserText().'</name>';
240 						print "\n";
241 						print "\t\t".'</author>';
242 						print "\n";					
243 						print "\t".'</entry>';
244 						print "\n";
245 					}
246 				}
247 				
248 				print "</feed>";
249 				
250 				$dbr->freeResult($all_pages_res);  				
251 			}            	
252 			
253 		}
254 		
255 		private function getWikiLastTouch()
256 		{
257 			$dbr = wfGetDB(DB_SLAVE);
258 			
259 			list($page_tbl) = $dbr->tableNamesN('page');
260 			
261 			$timespan_query  = "SELECT MAX(page_touched) ";
262 			$timespan_query .= "FROM $dbr->addIdentifierQuotes($page_tbl) ";
263 			$timespan_query .= "WHERE page_is_redirect = 0 AND page_namespace = 0;";
264 								
265 			$timespan_res = $dbr->doQuery($timespan_query);			
266 			$row = $dbr->fetchRow($timespan_res);
267 
268 			$dt = new DateTime($row['MAX(page_touched)']); 
269 			$last_updated = $dt->format(DATE_RFC3339);	
270 		
271 			$dbr->freeResult($timespan_res);
272 		
273 			return $last_updated;			
274 		}
275 }
276 
277 ?>

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'
);


$wgAutoloadClasses['AtomExporter'] = __DIR__ '/AtomExporter_body.php';
$wgExtensionMessagesFiles['AtomExporter'] = __DIR__ . '/AtomExporter.i18n.php';
$wgSpecialPages['AtomExporter'] = 'AtomExporter';

?>