]>
git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
2 class Import_Export
extends Plugin
implements IHandler
{
8 $this -> link
= $host -> get_link ();
11 $host -> add_hook ( $host :: HOOK_PREFS_TAB
, $this );
12 $host -> add_command ( "xml-import" , "import articles from XML" , $this , ":" , "FILE" );
17 "Imports and exports user data using neutral XML format" ,
21 function xml_import ( $args ) {
23 $filename = $args [ 'xml_import' ];
25 if (! is_file ( $filename )) {
26 print "error: input filename ( $filename ) doesn't exist. \n " ;
30 _debug ( "please enter your username:" );
32 $username = db_escape_string ( $this -> link
, trim ( read_stdin ()));
34 _debug ( "importing $filename for user $username ... \n " );
36 $result = db_query ( $this -> link
, "SELECT id FROM ttrss_users WHERE login = ' $username '" );
38 if ( db_num_rows ( $result ) == 0 ) {
39 print "error: could not find user $username . \n " ;
43 $owner_uid = db_fetch_result ( $result , 0 , "id" );
45 $this -> perform_data_import ( $this -> link
, $filename , $owner_uid );
49 $example_value = db_escape_string ( $this -> link
, $_POST [ "example_value" ]);
51 echo "Value set to $example_value (not really)" ;
54 function get_prefs_js () {
55 return file_get_contents ( dirname ( __FILE__
) . "/import_export.js" );
58 function hook_prefs_tab ( $args ) {
59 if ( $args != "prefFeeds" ) return ;
61 print "<div dojoType= \" dijit.layout.AccordionPane \" title= \" " . __ ( 'Import and export' ). " \" >" ;
63 print "<h3>" . __ ( "Article archive" ) . "</h3>" ;
65 print "<p>" . __ ( "You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances." ) . "</p>" ;
67 print "<button dojoType= \" dijit.form.Button \" onclick= \" return exportData() \" >" .
68 __ ( 'Export my data' ). "</button> " ;
72 print "<iframe id= \" data_upload_iframe \"
73 name= \" data_upload_iframe \" onload= \" dataImportComplete(this) \"
74 style= \" width: 400px; height: 100px; display: none; \" ></iframe>" ;
76 print "<form name= \" import_form \" style='display : block' target= \" data_upload_iframe \"
77 enctype= \" multipart/form-data \" method= \" POST \"
78 action= \" backend.php \" >
79 <input id= \" export_file \" name= \" export_file \" type= \" file \" >
80 <input type= \" hidden \" name= \" op \" value= \" pluginhandler \" >
81 <input type= \" hidden \" name= \" plugin \" value= \" import_export \" >
82 <input type= \" hidden \" name= \" method \" value= \" dataimport \" >
83 <button dojoType= \" dijit.form.Button \" onclick= \" return importData(); \" type= \" submit \" >" .
84 __ ( '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 if ( function_exists ( 'gzencode' )) {
110 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz" );
111 echo gzencode ( file_get_contents ( $exportname ));
113 header ( "Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml" );
114 echo file_get_contents ( $exportname );
117 echo "File not found." ;
121 function exportrun () {
122 $offset = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ 'offset' ]);
126 if ( $offset < 10000 && is_writable ( CACHE_DIR
. "/export" )) {
127 $result = db_query ( $this -> link
, "SELECT
138 ttrss_feeds.title AS feed_title,
139 ttrss_feeds.feed_url AS feed_url,
140 ttrss_entries.updated
142 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
145 (marked = true OR feed_id IS NULL) AND
146 ref_id = ttrss_entries.id AND
147 ttrss_user_entries.owner_uid = " . $_SESSION [ 'uid' ] . "
148 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset " );
150 $exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
153 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "w" );
154 fputs ( $fp , "<articles schema-version= \" " . SCHEMA_VERSION
. " \" >" );
156 $fp = fopen ( CACHE_DIR
. "/export/ $exportname .xml" , "a" );
161 while ( $line = db_fetch_assoc ( $result )) {
162 fputs ( $fp , "<article>" );
164 foreach ( $line as $k => $v ) {
165 fputs ( $fp , "< $k ><![CDATA[ $v ]]></ $k >" );
168 fputs ( $fp , "</article>" );
171 $exported = db_num_rows ( $result );
173 if ( $exported < $limit && $exported > 0 ) {
174 fputs ( $fp , "</articles>" );
182 print json_encode ( array ( "exported" => $exported ));
185 function perform_data_import ( $link , $filename , $owner_uid ) {
189 $num_feeds_created = 0 ;
191 $doc = @DOMDocument
:: load ( $filename );
194 $contents = file_get_contents ( $filename );
197 $data = @gzuncompress
( $contents );
201 $data = @gzdecode
( $contents );
205 $doc = DOMDocument
:: loadXML ( $data );
210 $xpath = new DOMXpath ( $doc );
212 $container = $doc -> firstChild
;
214 if ( $container && $container -> hasAttribute ( 'schema-version' )) {
215 $schema_version = $container -> getAttribute ( 'schema-version' );
217 if ( $schema_version != SCHEMA_VERSION
) {
218 print "<p>" . __ ( "Could not import: incorrect schema version." ) . "</p>" ;
223 print "<p>" . __ ( "Could not import: unrecognized document format." ) . "</p>" ;
227 $articles = $xpath -> query ( "//article" );
229 foreach ( $articles as $article_node ) {
230 if ( $article_node -> childNodes
) {
236 foreach ( $article_node -> childNodes
as $child ) {
237 if ( $child -> nodeName
!= 'label_cache' )
238 $article [ $child -> nodeName
] = db_escape_string ( $this -> link
, $child -> nodeValue
);
240 $article [ $child -> nodeName
] = $child -> nodeValue
;
245 if ( $article [ 'guid' ]) {
249 //db_query($link, "BEGIN");
251 //print 'GUID:' . $article['guid'] . "\n";
253 $result = db_query ( $link , "SELECT id FROM ttrss_entries
254 WHERE guid = '" . $article [ 'guid' ]. "'" );
256 if ( db_num_rows ( $result ) == 0 ) {
258 $result = db_query ( $link ,
259 "INSERT INTO ttrss_entries
273 ('" . $article [ 'title' ]. "',
274 '" . $article [ 'guid' ]. "',
275 '" . $article [ 'link' ]. "',
276 '" . $article [ 'updated' ]. "',
277 '" . $article [ 'content' ]. "',
278 '" . sha1 ( $article [ 'content' ]). "',
286 $result = db_query ( $link , "SELECT id FROM ttrss_entries
287 WHERE guid = '" . $article [ 'guid' ]. "'" );
289 if ( db_num_rows ( $result ) != 0 ) {
290 $ref_id = db_fetch_result ( $result , 0 , "id" );
294 $ref_id = db_fetch_result ( $result , 0 , "id" );
297 //print "Got ref ID: $ref_id\n";
301 $feed_url = $article [ 'feed_url' ];
302 $feed_title = $article [ 'feed_title' ];
306 if ( $feed_url && $feed_title ) {
307 $result = db_query ( $link , "SELECT id FROM ttrss_feeds
308 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
310 if ( db_num_rows ( $result ) != 0 ) {
311 $feed = db_fetch_result ( $result , 0 , "id" );
313 // try autocreating feed in Uncategorized...
315 $result = db_query ( $link , "INSERT INTO ttrss_feeds (owner_uid,
316 feed_url, title) VALUES ( $owner_uid , ' $feed_url ', ' $feed_title ')" );
318 $result = db_query ( $link , "SELECT id FROM ttrss_feeds
319 WHERE feed_url = ' $feed_url ' AND owner_uid = ' $owner_uid '" );
321 if ( db_num_rows ( $result ) != 0 ) {
322 ++
$num_feeds_created ;
324 $feed = db_fetch_result ( $result , 0 , "id" );
330 $feed_qpart = "feed_id = $feed " ;
332 $feed_qpart = "feed_id IS NULL" ;
334 //print "$ref_id / $feed / " . $article['title'] . "\n";
336 $result = db_query ( $link , "SELECT int_id FROM ttrss_user_entries
337 WHERE ref_id = ' $ref_id ' AND owner_uid = ' $owner_uid ' AND $feed_qpart " );
339 if ( db_num_rows ( $result ) == 0 ) {
341 $marked = bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'marked' ]));
342 $published = bool_to_sql_bool ( sql_bool_to_bool ( $article [ 'published' ]));
343 $score = ( int ) $article [ 'score' ];
345 $tag_cache = $article [ 'tag_cache' ];
346 $label_cache = db_escape_string ( $this -> link
, $article [ 'label_cache' ]);
347 $note = $article [ 'note' ];
349 //print "Importing " . $article['title'] . "<br/>";
353 $result = db_query ( $link ,
354 "INSERT INTO ttrss_user_entries
355 (ref_id, owner_uid, feed_id, unread, last_read, marked,
356 published, score, tag_cache, label_cache, uuid, note)
357 VALUES ( $ref_id , $owner_uid , $feed , false,
358 NULL, $marked , $published , $score , ' $tag_cache ',
359 ' $label_cache ', '', ' $note ')" );
361 $label_cache = json_decode ( $label_cache , true );
363 if ( is_array ( $label_cache ) && $label_cache [ "no-labels" ] != 1 ) {
364 foreach ( $label_cache as $label ) {
366 label_create ( $link , $label [ 1 ],
367 $label [ 2 ], $label [ 3 ], $owner_uid );
369 label_add_article ( $link , $ref_id , $label [ 1 ], $owner_uid );
374 //db_query($link, "COMMIT");
383 vsprintf ( ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ).
384 vsprintf ( ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ).
385 vsprintf ( ngettext ( " %d feed created." , " %d feeds created." , $num_feeds_created ), $num_feeds_created ).
390 print "<p>" . __ ( "Could not load XML document." ) . "</p>" ;
395 function exportData () {
397 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>" ;
399 print "<div align='center'>" ;
400 print "<button dojoType= \" dijit.form.Button \"
401 onclick= \" dijit.byId('dataExportDlg').prepare() \" >" .
402 __ ( 'Prepare data' ). "</button>" ;
404 print "<button dojoType= \" dijit.form.Button \"
405 onclick= \" dijit.byId('dataExportDlg').hide() \" >" .
406 __ ( 'Close this window' ). "</button>" ;
413 function dataImport () {
414 header ( "Content-Type: text/html" ); # required for iframe
416 print "<div style='text-align : center'>" ;
418 if ( is_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
420 $this -> perform_data_import ( $this -> link
, $_FILES [ 'export_file' ][ 'tmp_name' ], $_SESSION [ 'uid' ]);
423 print "<p>" . T_sprintf ( "Could not upload file. You might need to adjust upload_max_filesize in PHP.ini (current value = %s )" , ini_get ( "upload_max_filesize" )) . " or use CLI import tool.</p>" ;
427 print "<button dojoType= \" dijit.form.Button \"
428 onclick= \" dijit.byId('dataImportDlg').hide() \" >" .
429 __ ( 'Close this window' ). "</button>" ;