]>
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 function xml_import ( $args ) {
20 $filename = $args [ 'xml_import' ];
22 if (! is_file ( $filename )) {
23 print "error: input filename ( $filename ) doesn't exist. \n " ;
27 _debug ( "please enter your username:" );
29 $username = db_escape_string ( trim ( read_stdin ()));
31 _debug ( "importing $filename for user $username ... \n " );
33 $result = db_query ( "SELECT id FROM ttrss_users WHERE login = ' $username '" );
35 if ( db_num_rows ( $result ) == 0 ) {
36 print "error: could not find user $username . \n " ;
40 $owner_uid = db_fetch_result ( $result , 0 , "id" );
42 $this -> perform_data_import ( $filename , $owner_uid );
46 $example_value = db_escape_string ( $_POST [ "example_value" ]);
48 echo "Value set to $example_value (not really)" ;
51 function get_prefs_js () {
52 return file_get_contents ( dirname ( __FILE__
) . "/import_export.js" );
55 function hook_prefs_tab ( $args ) {
56 if ( $args != "prefFeeds" ) return ;
58 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Import and export' ). " \" >" ;
60 print_notice ( __ ( "You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version." ));
64 print "<button dojoType= \" dijit.form.Button \" onclick= \" return exportData() \" >" .
65 __ ( 'Export my data' ). "</button> " ;
69 print "<iframe id= \" data_upload_iframe \"
70 name= \" data_upload_iframe \" onload= \" dataImportComplete(this) \"
71 style= \" width: 400px; height: 100px; display: none; \" ></iframe>" ;
73 print "<form name= \" import_form \" style='display : block' target= \" data_upload_iframe \"
74 enctype= \" multipart/form-data \" method= \" POST \"
75 action= \" backend.php \" >
76 <input id= \" export_file \" name= \" export_file \" type= \" file \" >
77 <input type= \" hidden \" name= \" op \" value= \" pluginhandler \" >
78 <input type= \" hidden \" name= \" plugin \" value= \" import_export \" >
79 <input type= \" hidden \" name= \" method \" value= \" dataimport \" >
80 <button dojoType= \" dijit.form.Button \" onclick= \" return importData(); \" type= \" submit \" >" .
81 __ ( 'Import' ) . "</button>" ;
87 print "</div>" ; # pane
90 function csrf_ignore ( $method ) {
91 return in_array ( $method , array ( "exportget" ));
94 function before ( $method ) {
95 return $_SESSION [ "uid" ] != false ;
102 function exportget () {
103 $exportname = CACHE_DIR
. "/export/" .
104 sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]) . ".xml" ;
106 if ( file_exists ( $exportname )) {
107 header ( "Content-type: text/xml" );
109 $timestamp_suffix = date ( "Y-m-d" , filemtime ( $exportname ));
111 if ( function_exists ( 'gzencode' )) {
112 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz" );
113 echo gzencode ( file_get_contents ( $exportname ));
115 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml" );
116 echo file_get_contents ( $exportname );
119 echo "File not found." ;
123 function exportrun () {
124 $offset = ( int ) db_escape_string ( $_REQUEST [ 'offset' ]);
128 if ( $offset < 10000 && is_writable ( CACHE_DIR
. "/export" )) {
129 $result = db_query ( "SELECT
140 ttrss_feeds.title AS feed_title,
141 ttrss_feeds.feed_url AS feed_url,
142 ttrss_entries.updated
144 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
147 (marked = true OR feed_id IS NULL) AND
148 ref_id = ttrss_entries.id AND
149 ttrss_user_entries.owner_uid = " . $_SESSION [ 'uid' ] . "
150 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset " );
152 $exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
155 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "w" );
156 fputs ( $fp , "<articles schema-version= \" " . SCHEMA_VERSION
. " \" >" );
158 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "a" );
163 while ( $line = db_fetch_assoc ( $result )) {
164 fputs ( $fp , "<article>" );
166 foreach ( $line as $k => $v ) {
167 $v = str_replace ( "]]>" , "]]]]><![CDATA[>" , $v );
168 fputs ( $fp , "< $k ><![CDATA[ $v ]]></ $k >" );
171 fputs ( $fp , "</article>" );
174 $exported = db_num_rows ( $result );
176 if ( $exported < $limit && $exported > 0 ) {
177 fputs ( $fp , "</articles>" );
185 print json_encode ( array ( "exported" => $exported ));
188 function perform_data_import ( $filename , $owner_uid ) {
192 $num_feeds_created = 0 ;
194 libxml_disable_entity_loader ( false );
196 $doc = @DOMDocument
:: load ( $filename );
199 $contents = file_get_contents ( $filename );
202 $data = @gzuncompress
( $contents );
206 $data = @gzdecode
( $contents );
210 $doc = DOMDocument
:: loadXML ( $data );
213 libxml_disable_entity_loader ( true );
217 $xpath = new DOMXpath ( $doc );
219 $container = $doc -> firstChild
;
221 if ( $container && $container -> hasAttribute ( 'schema-version' )) {
222 $schema_version = $container -> getAttribute ( 'schema-version' );
224 if ( $schema_version != SCHEMA_VERSION
) {
225 print "<p>" . __ ( "Could not import: incorrect schema version." ) . "</p>" ;
230 print "<p>" . __ ( "Could not import: unrecognized document format." ) . "</p>" ;
234 $articles = $xpath -> query ( "//article" );
236 foreach ( $articles as $article_node ) {
237 if ( $article_node -> childNodes
) {
243 foreach ( $article_node -> childNodes
as $child ) {
244 if ( $child -> nodeName
== 'content' ) {
245 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
, false );
246 } else if ( $child -> nodeName
== 'label_cache' ) {
247 $article [ $child -> nodeName
] = $child -> nodeValue
;
249 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
);
255 if ( $article [ 'guid' ]) {
261 //print 'GUID:' . $article['guid'] . "\n";
263 $result = db_query ( "SELECT id FROM ttrss_entries
264 WHERE guid = '" . $article [ 'guid' ]. "'" );
266 if ( db_num_rows ( $result ) == 0 ) {
269 "INSERT INTO ttrss_entries
283 ('" . $article [ 'title' ]. "',
284 '" . $article [ 'guid' ]. "',
285 '" . $article [ 'link' ]. "',
286 '" . $article [ 'updated' ]. "',
287 '" . $article [ 'content' ]. "',
288 '" . sha1 ( $article [ 'content' ]). "',
296 $result = db_query ( "SELECT id FROM ttrss_entries
297 WHERE guid = '" . $article [ 'guid' ]. "'" );
299 if ( db_num_rows ( $result ) != 0 ) {
300 $ref_id = db_fetch_result ( $result , 0 , "id" );
304 $ref_id = db_fetch_result ( $result , 0 , "id" );
307 //print "Got ref ID: $ref_id\n";
311 $feed_url = $article [ 'feed_url' ];
312 $feed_title = $article [ 'feed_title' ];
316 if ( $feed_url && $feed_title ) {
317 $result = db_query ( "SELECT id FROM ttrss_feeds
318 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
320 if ( db_num_rows ( $result ) != 0 ) {
321 $feed = db_fetch_result ( $result , 0 , "id" );
323 // try autocreating feed in Uncategorized...
325 $result = db_query ( "INSERT INTO ttrss_feeds (owner_uid,
326 feed_url, title) VALUES ( $owner_uid , ' $feed_url ', ' $feed_title ')" );
328 $result = db_query ( "SELECT id FROM ttrss_feeds
329 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
331 if ( db_num_rows ( $result ) != 0 ) {
332 ++
$num_feeds_created ;
334 $feed = db_fetch_result ( $result , 0 , "id" );
340 $feed_qpart = "feed_id = $feed " ;
342 $feed_qpart = "feed_id IS NULL" ;
344 //print "$ref_id / $feed / " . $article['title'] . "\n";
346 $result = db_query ( "SELECT int_id FROM ttrss_user_entries
347 WHERE ref_id = ' $ref_id ' AND owner_uid = ' $owner_uid ' AND $feed_qpart " );
349 if ( db_num_rows ( $result ) == 0 ) {
351 $marked = bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'marked' ]));
352 $published = bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'published' ]));
353 $score = ( int ) $article [ 'score' ];
355 $tag_cache = $article [ 'tag_cache' ];
356 $note = $article [ 'note' ];
358 //print "Importing " . $article['title'] . "<br/>";
363 "INSERT INTO ttrss_user_entries
364 (ref_id, owner_uid, feed_id, unread, last_read, marked,
365 published, score, tag_cache, label_cache, uuid, note)
366 VALUES ( $ref_id , $owner_uid , $feed , false,
367 NULL, $marked , $published , $score , ' $tag_cache ',
370 $label_cache = json_decode ( $article [ 'label_cache' ], true );
372 if ( is_array ( $label_cache ) && $label_cache [ "no-labels" ] != 1 ) {
373 foreach ( $label_cache as $label ) {
375 label_create ( $label [ 1 ],
376 $label [ 2 ], $label [ 3 ], $owner_uid );
378 label_add_article ( $ref_id , $label [ 1 ], $owner_uid );
383 //db_query("COMMIT");
392 vsprintf ( _ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ).
393 vsprintf ( _ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ).
394 vsprintf ( _ngettext ( " %d feed created." , " %d feeds created." , $num_feeds_created ), $num_feeds_created ).
399 print "<p>" . __ ( "Could not load XML document." ) . "</p>" ;
404 function exportData () {
406 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>" ;
408 print "<div align='center'>" ;
409 print "<button dojoType= \" dijit.form.Button \"
410 onclick= \" dijit.byId('dataExportDlg').prepare() \" >" .
411 __ ( 'Prepare data' ). "</button>" ;
413 print "<button dojoType= \" dijit.form.Button \"
414 onclick= \" dijit.byId('dataExportDlg').hide() \" >" .
415 __ ( 'Close this window' ). "</button>" ;
422 function dataImport () {
423 header ( "Content-Type: text/html" ); # required for iframe
425 print "<div style='text-align : center'>" ;
427 if ( $_FILES [ 'export_file' ][ 'error' ] != 0 ) {
428 print_error ( T_sprintf ( "Upload failed with error code %d ( %s )" ,
429 $_FILES [ 'export_file' ][ 'error' ],
430 get_upload_error_message ( $_FILES [ 'export_file' ][ 'error' ])));
435 if ( is_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
436 $tmp_file = tempnam ( CACHE_DIR
. '/upload' , 'export' );
438 $result = move_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ],
442 print_error ( __ ( "Unable to move uploaded file." ));
446 print_error ( __ ( 'Error: please upload OPML file.' ));
450 if ( is_file ( $tmp_file )) {
451 $this -> perform_data_import ( $tmp_file , $_SESSION [ 'uid' ]);
454 print_error ( __ ( 'No file uploaded.' ));
459 print "<button dojoType= \" dijit.form.Button \"
460 onclick= \" dijit.byId('dataImportDlg').hide() \" >" .
461 __ ( 'Close this window' ). "</button>" ;
467 function api_version () {