]>
git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
2 class Import_Export
extends Plugin
implements IHandler
{
8 $host -> add_hook ( $host :: HOOK_PREFS_TAB
, $this );
9 $host -> add_command ( "xml-import" , "import articles from XML" , $this , ":" , "FILE" );
14 "Imports and exports user data using neutral XML format" ,
18 private function bool_to_sql_bool ( $s ) {
19 return $s ?
'true' : 'false' ;
22 function xml_import ( $args ) {
24 $filename = $args [ 'xml_import' ];
26 if (! is_file ( $filename )) {
27 print "error: input filename ( $filename ) doesn't exist. \n " ;
31 _debug ( "please enter your username:" );
33 $username = db_escape_string ( trim ( read_stdin ()));
35 _debug ( "importing $filename for user $username ... \n " );
37 $result = db_query ( "SELECT id FROM ttrss_users WHERE login = ' $username '" );
39 if ( db_num_rows ( $result ) == 0 ) {
40 print "error: could not find user $username . \n " ;
44 $owner_uid = db_fetch_result ( $result , 0 , "id" );
46 $this -> perform_data_import ( $filename , $owner_uid );
50 $example_value = db_escape_string ( $_POST [ "example_value" ]);
52 echo "Value set to $example_value (not really)" ;
55 function get_prefs_js () {
56 return file_get_contents ( dirname ( __FILE__
) . "/import_export.js" );
59 function hook_prefs_tab ( $args ) {
60 if ( $args != "prefFeeds" ) return ;
62 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Import and export' ). " \" >" ;
64 print_notice ( __ ( "You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version." ));
68 print "<button dojoType= \" dijit.form.Button \" onclick= \" return exportData() \" >" .
69 __ ( 'Export my data' ). "</button> " ;
73 print "<iframe id= \" data_upload_iframe \"
74 name= \" data_upload_iframe \" onload= \" dataImportComplete(this) \"
75 style= \" width: 400px; height: 100px; display: none; \" ></iframe>" ;
77 print "<form name= \" import_form \" style='display : block' target= \" data_upload_iframe \"
78 enctype= \" multipart/form-data \" method= \" POST \"
79 action= \" backend.php \" >
80 <input id= \" export_file \" name= \" export_file \" type= \" file \" >
81 <input type= \" hidden \" name= \" op \" value= \" pluginhandler \" >
82 <input type= \" hidden \" name= \" plugin \" value= \" import_export \" >
83 <input type= \" hidden \" name= \" method \" value= \" dataimport \" >
84 <button dojoType= \" dijit.form.Button \" onclick= \" return importData(); \" type= \" submit \" >" .
85 __ ( 'Import' ) . "</button>" ;
91 print "</div>" ; # pane
94 function csrf_ignore ( $method ) {
95 return in_array ( $method , array ( "exportget" ));
99 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
101 function before ( $method ) {
102 return $_SESSION [ "uid" ] != false ;
110 * @SuppressWarnings(unused)
112 function exportget () {
113 $exportname = CACHE_DIR
. "/export/" .
114 sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]) . ".xml" ;
116 if ( file_exists ( $exportname )) {
117 header ( "Content-type: text/xml" );
119 $timestamp_suffix = date ( "Y-m-d" , filemtime ( $exportname ));
121 if ( function_exists ( 'gzencode' )) {
122 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz" );
123 echo gzencode ( file_get_contents ( $exportname ));
125 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml" );
126 echo file_get_contents ( $exportname );
129 echo "File not found." ;
133 function exportrun () {
134 $offset = ( int ) db_escape_string ( $_REQUEST [ 'offset' ]);
138 if ( $offset < 10000 && is_writable ( CACHE_DIR
. "/export" )) {
139 $result = db_query ( "SELECT
150 ttrss_feeds.title AS feed_title,
151 ttrss_feeds.feed_url AS feed_url,
152 ttrss_entries.updated
154 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
157 (marked = true OR feed_id IS NULL) AND
158 ref_id = ttrss_entries.id AND
159 ttrss_user_entries.owner_uid = " . $_SESSION [ 'uid' ] . "
160 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset " );
162 $exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
165 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "w" );
166 fputs ( $fp , "<articles schema-version= \" " . SCHEMA_VERSION
. " \" >" );
168 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "a" );
173 while ( $line = db_fetch_assoc ( $result )) {
174 fputs ( $fp , "<article>" );
176 foreach ( $line as $k => $v ) {
177 $v = str_replace ( "]]>" , "]]]]><![CDATA[>" , $v );
178 fputs ( $fp , "< $k ><![CDATA[ $v ]]></ $k >" );
181 fputs ( $fp , "</article>" );
184 $exported = db_num_rows ( $result );
186 if ( $exported < $limit && $exported > 0 ) {
187 fputs ( $fp , "</articles>" );
195 print json_encode ( array ( "exported" => $exported ));
198 function perform_data_import ( $filename , $owner_uid ) {
202 $num_feeds_created = 0 ;
204 libxml_disable_entity_loader ( false );
206 $doc = @DOMDocument
:: load ( $filename );
209 $contents = file_get_contents ( $filename );
212 $data = @gzuncompress
( $contents );
216 $data = @gzdecode
( $contents );
220 $doc = DOMDocument
:: loadXML ( $data );
223 libxml_disable_entity_loader ( true );
227 $xpath = new DOMXpath ( $doc );
229 $container = $doc -> firstChild
;
231 if ( $container && $container -> hasAttribute ( 'schema-version' )) {
232 $schema_version = $container -> getAttribute ( 'schema-version' );
234 if ( $schema_version != SCHEMA_VERSION
) {
235 print "<p>" . __ ( "Could not import: incorrect schema version." ) . "</p>" ;
240 print "<p>" . __ ( "Could not import: unrecognized document format." ) . "</p>" ;
244 $articles = $xpath -> query ( "//article" );
246 foreach ( $articles as $article_node ) {
247 if ( $article_node -> childNodes
) {
253 foreach ( $article_node -> childNodes
as $child ) {
254 if ( $child -> nodeName
== 'content' ) {
255 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
, false );
256 } else if ( $child -> nodeName
== 'label_cache' ) {
257 $article [ $child -> nodeName
] = $child -> nodeValue
;
259 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
);
265 if ( $article [ 'guid' ]) {
271 //print 'GUID:' . $article['guid'] . "\n";
273 $result = db_query ( "SELECT id FROM ttrss_entries
274 WHERE guid = '" . $article [ 'guid' ]. "'" );
276 if ( db_num_rows ( $result ) == 0 ) {
279 "INSERT INTO ttrss_entries
293 ('" . $article [ 'title' ]. "',
294 '" . $article [ 'guid' ]. "',
295 '" . $article [ 'link' ]. "',
296 '" . $article [ 'updated' ]. "',
297 '" . $article [ 'content' ]. "',
298 '" . sha1 ( $article [ 'content' ]). "',
306 $result = db_query ( "SELECT id FROM ttrss_entries
307 WHERE guid = '" . $article [ 'guid' ]. "'" );
309 if ( db_num_rows ( $result ) != 0 ) {
310 $ref_id = db_fetch_result ( $result , 0 , "id" );
314 $ref_id = db_fetch_result ( $result , 0 , "id" );
317 //print "Got ref ID: $ref_id\n";
321 $feed_url = $article [ 'feed_url' ];
322 $feed_title = $article [ 'feed_title' ];
326 if ( $feed_url && $feed_title ) {
327 $result = db_query ( "SELECT id FROM ttrss_feeds
328 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
330 if ( db_num_rows ( $result ) != 0 ) {
331 $feed = db_fetch_result ( $result , 0 , "id" );
333 // try autocreating feed in Uncategorized...
335 $result = db_query ( "INSERT INTO ttrss_feeds (owner_uid,
336 feed_url, title) VALUES ( $owner_uid , ' $feed_url ', ' $feed_title ')" );
338 $result = db_query ( "SELECT id FROM ttrss_feeds
339 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
341 if ( db_num_rows ( $result ) != 0 ) {
342 ++
$num_feeds_created ;
344 $feed = db_fetch_result ( $result , 0 , "id" );
350 $feed_qpart = "feed_id = $feed " ;
352 $feed_qpart = "feed_id IS NULL" ;
354 //print "$ref_id / $feed / " . $article['title'] . "\n";
356 $result = db_query ( "SELECT int_id FROM ttrss_user_entries
357 WHERE ref_id = ' $ref_id ' AND owner_uid = ' $owner_uid ' AND $feed_qpart " );
359 if ( db_num_rows ( $result ) == 0 ) {
361 $marked = $this -> bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'marked' ]));
362 $published = $this -> bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'published' ]));
363 $score = ( int ) $article [ 'score' ];
365 $tag_cache = $article [ 'tag_cache' ];
366 $note = $article [ 'note' ];
368 //print "Importing " . $article['title'] . "<br/>";
373 "INSERT INTO ttrss_user_entries
374 (ref_id, owner_uid, feed_id, unread, last_read, marked,
375 published, score, tag_cache, label_cache, uuid, note)
376 VALUES ( $ref_id , $owner_uid , $feed , false,
377 NULL, $marked , $published , $score , ' $tag_cache ',
380 $label_cache = json_decode ( $article [ 'label_cache' ], true );
382 if ( is_array ( $label_cache ) && $label_cache [ "no-labels" ] != 1 ) {
383 foreach ( $label_cache as $label ) {
385 Labels
:: create ( $label [ 1 ],
386 $label [ 2 ], $label [ 3 ], $owner_uid );
388 Labels
:: add_article ( $ref_id , $label [ 1 ], $owner_uid );
393 //db_query("COMMIT");
402 vsprintf ( _ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ).
403 vsprintf ( _ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ).
404 vsprintf ( _ngettext ( " %d feed created." , " %d feeds created." , $num_feeds_created ), $num_feeds_created ).
409 print "<p>" . __ ( "Could not load XML document." ) . "</p>" ;
414 function exportData () {
416 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>" ;
418 print "<div align='center'>" ;
419 print "<button dojoType= \" dijit.form.Button \"
420 onclick= \" dijit.byId('dataExportDlg').prepare() \" >" .
421 __ ( 'Prepare data' ). "</button>" ;
423 print "<button dojoType= \" dijit.form.Button \"
424 onclick= \" dijit.byId('dataExportDlg').hide() \" >" .
425 __ ( 'Close this window' ). "</button>" ;
432 function dataImport () {
433 header ( "Content-Type: text/html" ); # required for iframe
435 print "<div style='text-align : center'>" ;
437 if ( $_FILES [ 'export_file' ][ 'error' ] != 0 ) {
438 print_error ( T_sprintf ( "Upload failed with error code %d ( %s )" ,
439 $_FILES [ 'export_file' ][ 'error' ],
440 get_upload_error_message ( $_FILES [ 'export_file' ][ 'error' ])));
445 if ( is_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
446 $tmp_file = tempnam ( CACHE_DIR
. '/upload' , 'export' );
448 $result = move_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ],
452 print_error ( __ ( "Unable to move uploaded file." ));
456 print_error ( __ ( 'Error: please upload OPML file.' ));
460 if ( is_file ( $tmp_file )) {
461 $this -> perform_data_import ( $tmp_file , $_SESSION [ 'uid' ]);
464 print_error ( __ ( 'No file uploaded.' ));
469 print "<button dojoType= \" dijit.form.Button \"
470 onclick= \" dijit.byId('dataImportDlg').hide() \" >" .
471 __ ( 'Close this window' ). "</button>" ;
477 function api_version () {