]>
git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
2 class Import_Export
extends Plugin
implements IHandler
{
7 $this -> pdo
= Db
:: pdo ();
9 $host -> add_hook ( $host :: HOOK_PREFS_TAB
, $this );
10 $host -> add_command ( "xml-import" , "import articles from XML" , $this , ":" , "FILE" );
15 "Imports and exports user data using neutral XML format" ,
19 private function bool_to_sql_bool ( $s ) {
20 return $s ?
'true' : 'false' ;
23 function xml_import ( $args ) {
25 $filename = $args [ 'xml_import' ];
27 if (! is_file ( $filename )) {
28 print "error: input filename ( $filename ) doesn't exist. \n " ;
32 _debug ( "please enter your username:" );
34 $username = db_escape_string ( trim ( read_stdin ()));
36 _debug ( "importing $filename for user $username ... \n " );
38 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_users WHERE login = ?" );
39 $sth -> execute ([ $username ]);
41 if ( $sth -> rowCount () == 0 ) {
42 print "error: could not find user $username . \n " ;
46 $owner_uid = $sth -> fetchColumn ( 0 );
48 $this -> perform_data_import ( $filename , $owner_uid );
52 $example_value = db_escape_string ( $_POST [ "example_value" ]);
54 echo "Value set to $example_value (not really)" ;
57 function get_prefs_js () {
58 return file_get_contents ( dirname ( __FILE__
) . "/import_export.js" );
61 function hook_prefs_tab ( $args ) {
62 if ( $args != "prefFeeds" ) return ;
64 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Import and export' ). " \" >" ;
66 print_notice ( __ ( "You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version." ));
70 print "<button dojoType= \" dijit.form.Button \" onclick= \" return exportData() \" >" .
71 __ ( 'Export my data' ). "</button> " ;
75 print "<iframe id= \" data_upload_iframe \"
76 name= \" data_upload_iframe \" onload= \" dataImportComplete(this) \"
77 style= \" width: 400px; height: 100px; display: none; \" ></iframe>" ;
79 print "<form name= \" import_form \" style='display : block' target= \" data_upload_iframe \"
80 enctype= \" multipart/form-data \" method= \" POST \"
81 action= \" backend.php \" >
82 <input id= \" export_file \" name= \" export_file \" type= \" file \" >
83 <input type= \" hidden \" name= \" op \" value= \" pluginhandler \" >
84 <input type= \" hidden \" name= \" plugin \" value= \" import_export \" >
85 <input type= \" hidden \" name= \" method \" value= \" dataimport \" >
86 <button dojoType= \" dijit.form.Button \" onclick= \" return importData(); \" type= \" submit \" >" .
87 __ ( 'Import' ) . "</button>" ;
93 print "</div>" ; # pane
96 function csrf_ignore ( $method ) {
97 return in_array ( $method , array ( "exportget" ));
101 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
103 function before ( $method ) {
104 return $_SESSION [ "uid" ] != false ;
112 * @SuppressWarnings(unused)
114 function exportget () {
115 $exportname = CACHE_DIR
. "/export/" .
116 sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]) . ".xml" ;
118 if ( file_exists ( $exportname )) {
119 header ( "Content-type: text/xml" );
121 $timestamp_suffix = date ( "Y-m-d" , filemtime ( $exportname ));
123 if ( function_exists ( 'gzencode' )) {
124 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz" );
125 echo gzencode ( file_get_contents ( $exportname ));
127 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml" );
128 echo file_get_contents ( $exportname );
131 echo "File not found." ;
135 function exportrun () {
136 $offset = ( int ) $_REQUEST [ 'offset' ];
140 if ( $offset < 10000 && is_writable ( CACHE_DIR
. "/export" )) {
141 $sth = $this -> pdo
-> prepare ( "SELECT
152 ttrss_feeds.title AS feed_title,
153 ttrss_feeds.feed_url AS feed_url,
154 ttrss_entries.updated
156 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
159 (marked = true OR feed_id IS NULL) AND
160 ref_id = ttrss_entries.id AND
161 ttrss_user_entries.owner_uid = ?
162 ORDER BY ttrss_entries.id LIMIT ? OFFSET ?" );
163 $sth -> execute ([ $_SESSION [ 'uid' ], $limit , $offset ]);
165 $exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
168 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "w" );
169 fputs ( $fp , "<articles schema-version= \" " . SCHEMA_VERSION
. " \" >" );
171 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "a" );
176 while ( $line = $sth -> fetch ( PDO
:: FETCH_ASSOC
)) {
177 fputs ( $fp , "<article>" );
179 foreach ( $line as $k => $v ) {
180 $v = str_replace ( "]]>" , "]]]]><![CDATA[>" , $v );
181 fputs ( $fp , "< $k ><![CDATA[ $v ]]></ $k >" );
184 fputs ( $fp , "</article>" );
187 $exported = $sth -> rowCount ();
189 if ( $exported < $limit && $exported > 0 ) {
190 fputs ( $fp , "</articles>" );
198 print json_encode ( array ( "exported" => $exported ));
201 function perform_data_import ( $filename , $owner_uid ) {
205 $num_feeds_created = 0 ;
207 libxml_disable_entity_loader ( false );
209 $doc = @DOMDocument
:: load ( $filename );
212 $contents = file_get_contents ( $filename );
215 $data = @gzuncompress
( $contents );
219 $data = @gzdecode
( $contents );
223 $doc = DOMDocument
:: loadXML ( $data );
226 libxml_disable_entity_loader ( true );
230 $xpath = new DOMXpath ( $doc );
232 $container = $doc -> firstChild
;
234 if ( $container && $container -> hasAttribute ( 'schema-version' )) {
235 $schema_version = $container -> getAttribute ( 'schema-version' );
237 if ( $schema_version != SCHEMA_VERSION
) {
238 print "<p>" . __ ( "Could not import: incorrect schema version." ) . "</p>" ;
243 print "<p>" . __ ( "Could not import: unrecognized document format." ) . "</p>" ;
247 $articles = $xpath -> query ( "//article" );
249 foreach ( $articles as $article_node ) {
250 if ( $article_node -> childNodes
) {
256 foreach ( $article_node -> childNodes
as $child ) {
257 if ( $child -> nodeName
== 'content' ) {
258 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
, false );
259 } else if ( $child -> nodeName
== 'label_cache' ) {
260 $article [ $child -> nodeName
] = $child -> nodeValue
;
262 $article [ $child -> nodeName
] = db_escape_string ( $child -> nodeValue
);
268 if ( $article [ 'guid' ]) {
274 //print 'GUID:' . $article['guid'] . "\n";
276 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_entries
278 $sth -> execute ([ $article [ 'guid' ]]);
280 if ( $sth -> rowCount () == 0 ) {
282 $sth = $this -> pdo
-> prepare (
283 "INSERT INTO ttrss_entries
315 sha1 ( $article [ 'content' ])
318 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_entries
320 $sth -> execute ([ $article [ 'guid' ]]);
322 if ( $sth -> rowCount () != 0 ) {
323 $ref_id = $sth -> fetchColumn ( 0 );
327 $ref_id = $sth -> fetchColumn ( 0 );
330 //print "Got ref ID: $ref_id\n";
334 $feed_url = $article [ 'feed_url' ];
335 $feed_title = $article [ 'feed_title' ];
339 if ( $feed_url && $feed_title ) {
340 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_feeds
341 WHERE feed_url = ? AND owner_uid = ?" );
342 $sth -> execute ([ $feed_url , $owner_uid ]);
344 if ( $sth -> rowCount () != 0 ) {
345 $feed = $sth -> fetchColumn ( 0 );
347 // try autocreating feed in Uncategorized...
349 $sth = $this -> pdo
-> prepare ( "INSERT INTO ttrss_feeds (owner_uid,
350 feed_url, title) VALUES (?, ?, ?)" );
351 $sth -> execute ([ $owner_uid , $feed_url , $feed_title ]);
353 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_feeds
354 WHERE feed_url = ? AND owner_uid = ?" );
355 $sth -> execute ([ $feed_url , $owner_uid ]);
357 if ( $sth -> rowCount () != 0 ) {
358 ++
$num_feeds_created ;
360 $feed = $sth -> fetchColumn ( 0 );
366 $feed_qpart = "feed_id = $feed " ;
368 $feed_qpart = "feed_id IS NULL" ;
370 //print "$ref_id / $feed / " . $article['title'] . "\n";
372 $sth = $this -> pdo
-> prepare ( "SELECT int_id FROM ttrss_user_entries
373 WHERE ref_id = ? AND owner_uid = ? AND ?" );
374 $sth -> execute ([ $ref_id , $owner_uid , $feed_qpart ]);
376 if ( $sth -> rowCount () == 0 ) {
378 $marked = $this -> bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'marked' ]));
379 $published = $this -> bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'published' ]));
380 $score = ( int ) $article [ 'score' ];
382 $tag_cache = $article [ 'tag_cache' ];
383 $note = $article [ 'note' ];
385 //print "Importing " . $article['title'] . "<br/>";
389 $sth = $this -> pdo
-> prepare (
390 "INSERT INTO ttrss_user_entries
391 (ref_id, owner_uid, feed_id, unread, last_read, marked,
392 published, score, tag_cache, label_cache, uuid, note)
393 VALUES (?, ?, ?, false,
396 $sth -> execute ([ $ref_id , $owner_uid , $feed , $marked , $published , $score , $tag_cache , $note ]);
398 $label_cache = json_decode ( $article [ 'label_cache' ], true );
400 if ( is_array ( $label_cache ) && $label_cache [ "no-labels" ] != 1 ) {
401 foreach ( $label_cache as $label ) {
403 Labels
:: create ( $label [ 1 ],
404 $label [ 2 ], $label [ 3 ], $owner_uid );
406 Labels
:: add_article ( $ref_id , $label [ 1 ], $owner_uid );
411 //db_query("COMMIT");
420 vsprintf ( _ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ).
421 vsprintf ( _ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ).
422 vsprintf ( _ngettext ( " %d feed created." , " %d feeds created." , $num_feeds_created ), $num_feeds_created ).
427 print "<p>" . __ ( "Could not load XML document." ) . "</p>" ;
432 function exportData () {
434 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>" ;
436 print "<div align='center'>" ;
437 print "<button dojoType= \" dijit.form.Button \"
438 onclick= \" dijit.byId('dataExportDlg').prepare() \" >" .
439 __ ( 'Prepare data' ). "</button>" ;
441 print "<button dojoType= \" dijit.form.Button \"
442 onclick= \" dijit.byId('dataExportDlg').hide() \" >" .
443 __ ( 'Close this window' ). "</button>" ;
450 function dataImport () {
451 header ( "Content-Type: text/html" ); # required for iframe
453 print "<div style='text-align : center'>" ;
455 if ( $_FILES [ 'export_file' ][ 'error' ] != 0 ) {
456 print_error ( T_sprintf ( "Upload failed with error code %d ( %s )" ,
457 $_FILES [ 'export_file' ][ 'error' ],
458 get_upload_error_message ( $_FILES [ 'export_file' ][ 'error' ])));
463 if ( is_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
464 $tmp_file = tempnam ( CACHE_DIR
. '/upload' , 'export' );
466 $result = move_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ],
470 print_error ( __ ( "Unable to move uploaded file." ));
474 print_error ( __ ( 'Error: please upload OPML file.' ));
478 if ( is_file ( $tmp_file )) {
479 $this -> perform_data_import ( $tmp_file , $_SESSION [ 'uid' ]);
482 print_error ( __ ( 'No file uploaded.' ));
487 print "<button dojoType= \" dijit.form.Button \"
488 onclick= \" dijit.byId('dataImportDlg').hide() \" >" .
489 __ ( 'Close this window' ). "</button>" ;
495 function api_version () {