]>
git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
31e7dd0e27ad3a9b27c2fbef7f5660427d11d3ff
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 = trim ( read_stdin ());
31 _debug ( "importing $filename for user $username ... \n " );
33 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_users WHERE login = ?" );
34 $sth -> execute ( $username );
36 if ( $row = $sth -> fetch ()) {
37 $owner_uid = $row [ 'id' ];
39 $this -> perform_data_import ( $filename , $owner_uid );
41 print "error: could not find user $username . \n " ;
46 function get_prefs_js () {
47 return file_get_contents ( dirname ( __FILE__
) . "/import_export.js" );
50 function hook_prefs_tab ( $args ) {
51 if ( $args != "prefFeeds" ) return ;
53 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Import and export' ). " \" >" ;
55 print_notice ( __ ( "You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version." ));
59 print "<button dojoType= \" dijit.form.Button \" onclick= \" return exportData() \" >" .
60 __ ( 'Export my data' ). "</button> " ;
64 print "<iframe id= \" data_upload_iframe \"
65 name= \" data_upload_iframe \" onload= \" dataImportComplete(this) \"
66 style= \" width: 400px; height: 100px; display: none; \" ></iframe>" ;
68 print "<form name= \" import_form \" style='display : block' target= \" data_upload_iframe \"
69 enctype= \" multipart/form-data \" method= \" POST \"
70 action= \" backend.php \" >
71 <label class= \" dijitButton \" >" . __ ( "Choose file..." ). "
72 <input style= \" display : none \" id= \" export_file \" name= \" export_file \" type= \" file \" >
74 <input type= \" hidden \" name= \" op \" value= \" pluginhandler \" >
75 <input type= \" hidden \" name= \" plugin \" value= \" import_export \" >
76 <input type= \" hidden \" name= \" method \" value= \" dataimport \" >
77 <button dojoType= \" dijit.form.Button \" onclick= \" return importData(); \" type= \" submit \" >" .
78 __ ( 'Import' ) . "</button>" ;
84 print "</div>" ; # pane
87 function csrf_ignore ( $method ) {
88 return in_array ( $method , array ( "exportget" ));
92 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
94 function before ( $method ) {
95 return $_SESSION [ "uid" ] != false ;
103 * @SuppressWarnings(unused)
105 function exportget () {
106 $exportname = CACHE_DIR
. "/export/" .
107 sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]) . ".xml" ;
109 if ( file_exists ( $exportname )) {
110 header ( "Content-type: text/xml" );
112 $timestamp_suffix = date ( "Y-m-d" , filemtime ( $exportname ));
114 if ( function_exists ( 'gzencode' )) {
115 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz" );
116 echo gzencode ( file_get_contents ( $exportname ));
118 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml" );
119 echo file_get_contents ( $exportname );
122 echo "File not found." ;
126 function exportrun () {
127 $offset = ( int ) $_REQUEST [ 'offset' ];
131 if ( $offset < 10000 && is_writable ( CACHE_DIR
. "/export" )) {
133 $sth = $this -> pdo
-> prepare ( "SELECT
144 ttrss_feeds.title AS feed_title,
145 ttrss_feeds.feed_url AS feed_url,
146 ttrss_entries.updated
148 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
151 (marked = true OR feed_id IS NULL) AND
152 ref_id = ttrss_entries.id AND
153 ttrss_user_entries.owner_uid = ?
154 ORDER BY ttrss_entries.id LIMIT ? OFFSET ?" );
156 $sth -> execute ([ $_SESSION [ 'uid' ], $limit , $offset ]);
158 $exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
161 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "w" );
162 fputs ( $fp , "<articles schema-version= \" " . SCHEMA_VERSION
. " \" >" );
164 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "a" );
170 while ( $line = $sth -> fetch ( PDO
:: FETCH_ASSOC
)) {
173 fputs ( $fp , "<article> \n " );
175 foreach ( $line as $k => $v ) {
182 if (! $v ||
is_numeric ( $v )) {
183 fputs ( $fp , "< $k > $v </ $k > \n " );
185 $v = str_replace ( "]]>" , "]]]]><![CDATA[>" , $v );
186 fputs ( $fp , "< $k ><![CDATA[ $v ]]></ $k > \n " );
190 fputs ( $fp , "</article> \n " );
193 if ( $exported < $limit && $exported > 0 ) {
194 fputs ( $fp , "</articles>" );
202 print json_encode ( array ( "exported" => $exported ));
205 function perform_data_import ( $filename , $owner_uid ) {
209 $num_feeds_created = 0 ;
211 libxml_disable_entity_loader ( false );
213 $doc = new DOMDocument ();
215 if (! $doc_loaded = @ $doc -> load ( $filename )) {
216 $contents = file_get_contents ( $filename );
219 $data = @gzuncompress
( $contents );
223 $data = @gzdecode
( $contents );
227 $doc_loaded = $doc -> loadXML ( $data );
230 libxml_disable_entity_loader ( true );
234 $xpath = new DOMXpath ( $doc );
236 $container = $doc -> firstChild
;
238 if ( $container && $container -> hasAttribute ( 'schema-version' )) {
239 $schema_version = $container -> getAttribute ( 'schema-version' );
241 if ( $schema_version != SCHEMA_VERSION
) {
242 print "<p>" . __ ( "Could not import: incorrect schema version." ) . "</p>" ;
247 print "<p>" . __ ( "Could not import: unrecognized document format." ) . "</p>" ;
251 $articles = $xpath -> query ( "//article" );
253 foreach ( $articles as $article_node ) {
254 if ( $article_node -> childNodes
) {
260 foreach ( $article_node -> childNodes
as $child ) {
261 if ( $child -> nodeName
== 'content' ||
$child -> nodeName
== 'label_cache' ) {
262 $article [ $child -> nodeName
] = $child -> nodeValue
;
264 $article [ $child -> nodeName
] = clean ( $child -> nodeValue
);
270 if ( $article [ 'guid' ]) {
274 $this -> pdo
-> beginTransaction ();
276 //print 'GUID:' . $article['guid'] . "\n";
278 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_entries
280 $sth -> execute ([ $article [ 'guid' ]]);
282 if ( $row = $sth -> fetch ()) {
283 $ref_id = $row [ 'id' ];
285 $sth = $this -> pdo
-> prepare (
286 "INSERT INTO ttrss_entries
314 sha1 ( $article [ 'content' ])
317 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_entries
319 $sth -> execute ([ $article [ 'guid' ]]);
321 if ( $row = $sth -> fetch ()) {
322 $ref_id = $row [ 'id' ];
326 //print "Got ref ID: $ref_id\n";
332 if ( $article [ 'feed_url' ] && $article [ 'feed_title' ]) {
334 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_feeds
335 WHERE feed_url = ? AND owner_uid = ?" );
336 $sth -> execute ([ $article [ 'feed_url' ], $owner_uid ]);
338 if ( $row = $sth -> fetch ()) {
341 // try autocreating feed in Uncategorized...
343 $sth = $this -> pdo
-> prepare ( "INSERT INTO ttrss_feeds (owner_uid,
344 feed_url, title) VALUES (?, ?, ?)" );
345 $res = $sth -> execute ([ $owner_uid , $article [ 'feed_url' ], $article [ 'feed_title' ]]);
348 $sth = $this -> pdo
-> prepare ( "SELECT id FROM ttrss_feeds
349 WHERE feed_url = ? AND owner_uid = ?" );
350 $sth -> execute ([ $article [ 'feed_url' ], $owner_uid ]);
352 if ( $row = $sth -> fetch ()) {
353 ++
$num_feeds_created ;
362 $feed_qpart = "feed_id = " . ( int ) $feed ;
364 $feed_qpart = "feed_id IS NULL" ;
366 //print "$ref_id / $feed / " . $article['title'] . "\n";
368 $sth = $this -> pdo
-> prepare ( "SELECT int_id FROM ttrss_user_entries
369 WHERE ref_id = ? AND owner_uid = ? AND $feed_qpart " );
370 $sth -> execute ([ $ref_id , $owner_uid ]);
372 if (! $sth -> fetch ()) {
374 $score = ( int ) $article [ 'score' ];
376 $tag_cache = $article [ 'tag_cache' ];
377 $note = $article [ 'note' ];
379 //print "Importing " . $article['title'] . "<br/>";
383 $sth = $this -> pdo
-> prepare (
384 "INSERT INTO ttrss_user_entries
385 (ref_id, owner_uid, feed_id, unread, last_read, marked,
386 published, score, tag_cache, label_cache, uuid, note)
387 VALUES (?, ?, ?, false,
388 NULL, ?, ?, ?, ?, '', '', ?)" );
390 $res = $sth -> execute ([
394 ( int ) sql_bool_to_bool ( $article [ 'marked' ]),
395 ( int ) sql_bool_to_bool ( $article [ 'published' ]),
402 $label_cache = json_decode ( $article [ 'label_cache' ], true );
404 if ( is_array ( $label_cache ) && $label_cache [ "no-labels" ] != 1 ) {
405 foreach ( $label_cache as $label ) {
406 Labels
:: create ( $label [ 1 ],
407 $label [ 2 ], $label [ 3 ], $owner_uid );
409 Labels
:: add_article ( $ref_id , $label [ 1 ], $owner_uid );
416 $this -> pdo
-> commit ();
423 vsprintf ( _ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ).
424 vsprintf ( _ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ).
425 vsprintf ( _ngettext ( " %d feed created." , " %d feeds created." , $num_feeds_created ), $num_feeds_created ).
430 print "<p>" . __ ( "Could not load XML document." ) . "</p>" ;
435 function exportData () {
437 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>" ;
439 print "<div align='center'>" ;
440 print "<button dojoType= \" dijit.form.Button \"
441 onclick= \" dijit.byId('dataExportDlg').prepare() \" >" .
442 __ ( 'Prepare data' ). "</button>" ;
444 print "<button dojoType= \" dijit.form.Button \"
445 onclick= \" dijit.byId('dataExportDlg').hide() \" >" .
446 __ ( 'Close this window' ). "</button>" ;
453 function dataImport () {
454 header ( "Content-Type: text/html" ); # required for iframe
456 print "<div style='text-align : center'>" ;
458 if ( $_FILES [ 'export_file' ][ 'error' ] != 0 ) {
459 print_error ( T_sprintf ( "Upload failed with error code %d ( %s )" ,
460 $_FILES [ 'export_file' ][ 'error' ],
461 get_upload_error_message ( $_FILES [ 'export_file' ][ 'error' ])));
464 if ( is_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
465 $tmp_file = tempnam ( CACHE_DIR
. '/upload' , 'export' );
467 $result = move_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ],
471 print_error ( __ ( "Unable to move uploaded file." ));
475 print_error ( __ ( 'Error: please upload OPML file.' ));
479 if ( is_file ( $tmp_file )) {
480 $this -> perform_data_import ( $tmp_file , $_SESSION [ 'uid' ]);
483 print_error ( __ ( 'No file uploaded.' ));
488 print "<button dojoType= \" dijit.form.Button \"
489 onclick= \" dijit.byId('dataImportDlg').hide() \" >" .
490 __ ( 'Close this window' ). "</button>" ;
496 function api_version () {