2012-12-24 11:03:19 +00:00
< ? php
class Import_Export extends Plugin implements IHandler {
private $host ;
2012-12-25 06:02:08 +00:00
function init ( $host ) {
2012-12-24 11:03:19 +00:00
$this -> host = $host ;
$host -> add_hook ( $host :: HOOK_PREFS_TAB , $this );
2013-04-02 05:28:47 +00:00
$host -> add_command ( " xml-import " , " import articles from XML " , $this , " : " , " FILE " );
2012-12-24 11:03:19 +00:00
}
2012-12-25 06:02:08 +00:00
function about () {
2012-12-24 11:39:42 +00:00
return array ( 1.0 ,
2012-12-25 06:06:35 +00:00
" Imports and exports user data using neutral XML format " ,
2012-12-24 11:39:42 +00:00
" fox " );
}
2012-12-24 11:03:19 +00:00
function xml_import ( $args ) {
2013-04-02 05:28:47 +00:00
$filename = $args [ 'xml_import' ];
2012-12-24 11:03:19 +00:00
if ( ! is_file ( $filename )) {
print " error: input filename ( $filename ) doesn't exist. \n " ;
return ;
}
2018-11-30 05:34:29 +00:00
Debug :: log ( " please enter your username: " );
2013-04-02 05:28:47 +00:00
2018-05-28 13:43:53 +00:00
$username = trim ( read_stdin ());
2013-04-02 05:28:47 +00:00
2018-11-30 05:34:29 +00:00
Debug :: log ( " importing $filename for user $username ... \n " );
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " SELECT id FROM ttrss_users WHERE login = ? " );
$sth -> execute ( $username );
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $row = $sth -> fetch ()) {
$owner_uid = $row [ 'id' ];
$this -> perform_data_import ( $filename , $owner_uid );
} else {
2012-12-24 11:03:19 +00:00
print " error: could not find user $username . \n " ;
return ;
}
}
function get_prefs_js () {
return file_get_contents ( dirname ( __FILE__ ) . " /import_export.js " );
}
function hook_prefs_tab ( $args ) {
if ( $args != " prefFeeds " ) return ;
2019-04-09 04:05:33 +00:00
print " <div dojoType='dijit.layout.AccordionPane'
2018-12-06 05:56:28 +00:00
title = \ " <i class='material-icons'>import_export</i> " . __ ( 'Import and export' ) . " \" > " ;
2012-12-24 11:03:19 +00:00
2013-05-12 08:36:25 +00:00
print_notice ( __ ( " You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version. " ));
2012-12-24 11:03:19 +00:00
2013-05-12 08:36:25 +00:00
print " <p> " ;
2012-12-24 11:03:19 +00:00
2019-04-09 04:05:33 +00:00
print " <button dojoType='dijit.form.Button' class='alt-primary' onclick='return exportData()'> " .
2012-12-24 11:03:19 +00:00
__ ( 'Export my data' ) . " </button> " ;
print " <hr> " ;
2019-04-09 04:05:33 +00:00
print " <iframe id='data_upload_iframe'
name = 'data_upload_iframe' onload = 'dataImportComplete(this)'
style = 'width: 400px; height: 100px; display: none;' ></ iframe > " ;
2012-12-24 11:03:19 +00:00
2019-04-09 04:05:33 +00:00
print " <form name='import_form' style='display : block' target='data_upload_iframe'
enctype = 'multipart/form-data' method = 'POST'
action = 'backend.php' >
< label class = 'dijitButton' > " .__( " Choose file ... " ). "
< input style = 'display : none' id = 'export_file' name = 'export_file' type = 'file' >& nbsp ;
2018-05-28 13:43:53 +00:00
</ label >
2019-04-09 04:05:33 +00:00
< input type = 'hidden' name = 'op' value = 'pluginhandler' >
< input type = 'hidden' name = 'plugin' value = 'import_export' >
< input type = 'hidden' name = 'method' value = 'dataimport' >
< button dojoType = 'dijit.form.Button' onclick = 'return importData();' class = 'alt-primary' type = 'submit' > " .
2012-12-24 11:03:19 +00:00
__ ( 'Import' ) . " </button> " ;
2013-04-11 16:31:24 +00:00
print " </form> " ;
2012-12-24 11:03:19 +00:00
2013-05-12 08:36:25 +00:00
print " </p> " ;
2012-12-24 11:03:19 +00:00
print " </div> " ; # pane
}
function csrf_ignore ( $method ) {
return in_array ( $method , array ( " exportget " ));
}
2017-04-26 17:57:36 +00:00
/**
* @ SuppressWarnings ( PHPMD . UnusedFormalParameter )
*/
2012-12-24 11:03:19 +00:00
function before ( $method ) {
return $_SESSION [ " uid " ] != false ;
}
function after () {
return true ;
}
2017-04-26 17:57:36 +00:00
/**
* @ SuppressWarnings ( unused )
*/
2012-12-24 11:03:19 +00:00
function exportget () {
$exportname = CACHE_DIR . " /export/ " .
sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]) . " .xml " ;
if ( file_exists ( $exportname )) {
header ( " Content-type: text/xml " );
2015-09-21 09:45:25 +00:00
$timestamp_suffix = date ( " Y-m-d " , filemtime ( $exportname ));
2012-12-24 11:03:19 +00:00
if ( function_exists ( 'gzencode' )) {
2015-09-21 09:45:25 +00:00
header ( " Content-Disposition: attachment; filename=TinyTinyRSS_exported_ ${ timestamp_suffix } .xml.gz " );
2012-12-24 11:03:19 +00:00
echo gzencode ( file_get_contents ( $exportname ));
} else {
2015-09-21 09:45:25 +00:00
header ( " Content-Disposition: attachment; filename=TinyTinyRSS_exported_ ${ timestamp_suffix } .xml " );
2012-12-24 11:03:19 +00:00
echo file_get_contents ( $exportname );
}
} else {
echo " File not found. " ;
}
}
function exportrun () {
2018-05-28 13:43:53 +00:00
$offset = ( int ) $_REQUEST [ 'offset' ];
2012-12-24 11:03:19 +00:00
$exported = 0 ;
$limit = 250 ;
if ( $offset < 10000 && is_writable ( CACHE_DIR . " /export " )) {
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " SELECT
2012-12-24 11:03:19 +00:00
ttrss_entries . guid ,
ttrss_entries . title ,
content ,
marked ,
published ,
score ,
note ,
link ,
tag_cache ,
label_cache ,
ttrss_feeds . title AS feed_title ,
ttrss_feeds . feed_url AS feed_url ,
ttrss_entries . updated
FROM
ttrss_user_entries LEFT JOIN ttrss_feeds ON ( ttrss_feeds . id = feed_id ),
ttrss_entries
WHERE
( marked = true OR feed_id IS NULL ) AND
ref_id = ttrss_entries . id AND
2018-05-28 13:43:53 +00:00
ttrss_user_entries . owner_uid = ?
2018-05-28 13:49:39 +00:00
ORDER BY ttrss_entries . id LIMIT $limit OFFSET $offset " );
2018-05-28 13:43:53 +00:00
2018-05-28 13:49:39 +00:00
$sth -> execute ([ $_SESSION [ 'uid' ]]);
2012-12-24 11:03:19 +00:00
$exportname = sha1 ( $_SESSION [ 'uid' ] . $_SESSION [ 'login' ]);
if ( $offset == 0 ) {
$fp = fopen ( CACHE_DIR . " /export/ $exportname .xml " , " w " );
fputs ( $fp , " <articles schema-version= \" " . SCHEMA_VERSION . " \" > " );
} else {
$fp = fopen ( CACHE_DIR . " /export/ $exportname .xml " , " a " );
}
if ( $fp ) {
2018-05-28 13:43:53 +00:00
$exported = 0 ;
while ( $line = $sth -> fetch ( PDO :: FETCH_ASSOC )) {
++ $exported ;
fputs ( $fp , " <article> \n " );
2012-12-24 11:03:19 +00:00
foreach ( $line as $k => $v ) {
2018-05-28 13:43:53 +00:00
fputs ( $fp , " " );
if ( is_bool ( $v ))
$v = ( int ) $v ;
if ( ! $v || is_numeric ( $v )) {
fputs ( $fp , " < $k > $v </ $k > \n " );
} else {
$v = str_replace ( " ]]> " , " ]]]]><![CDATA[> " , $v );
fputs ( $fp , " < $k ><![CDATA[ $v ]]></ $k > \n " );
}
2012-12-24 11:03:19 +00:00
}
2018-05-28 13:43:53 +00:00
fputs ( $fp , " </article> \n " );
2012-12-24 11:03:19 +00:00
}
if ( $exported < $limit && $exported > 0 ) {
fputs ( $fp , " </articles> " );
}
fclose ( $fp );
}
}
print json_encode ( array ( " exported " => $exported ));
}
2013-04-17 14:34:18 +00:00
function perform_data_import ( $filename , $owner_uid ) {
2012-12-24 11:03:19 +00:00
$num_imported = 0 ;
$num_processed = 0 ;
$num_feeds_created = 0 ;
2014-12-26 19:43:38 +00:00
libxml_disable_entity_loader ( false );
2018-05-28 13:43:53 +00:00
$doc = new DOMDocument ();
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( ! $doc_loaded = @ $doc -> load ( $filename )) {
2012-12-24 11:03:19 +00:00
$contents = file_get_contents ( $filename );
if ( $contents ) {
$data = @ gzuncompress ( $contents );
}
if ( ! $data ) {
$data = @ gzdecode ( $contents );
}
if ( $data )
2018-05-28 13:43:53 +00:00
$doc_loaded = $doc -> loadXML ( $data );
2012-12-24 11:03:19 +00:00
}
2014-12-26 19:43:38 +00:00
libxml_disable_entity_loader ( true );
2018-05-28 13:43:53 +00:00
if ( $doc_loaded ) {
2012-12-24 11:03:19 +00:00
$xpath = new DOMXpath ( $doc );
$container = $doc -> firstChild ;
if ( $container && $container -> hasAttribute ( 'schema-version' )) {
$schema_version = $container -> getAttribute ( 'schema-version' );
if ( $schema_version != SCHEMA_VERSION ) {
print " <p> " . __ ( " Could not import: incorrect schema version. " ) . " </p> " ;
return ;
}
} else {
print " <p> " . __ ( " Could not import: unrecognized document format. " ) . " </p> " ;
return ;
}
$articles = $xpath -> query ( " //article " );
foreach ( $articles as $article_node ) {
if ( $article_node -> childNodes ) {
$ref_id = 0 ;
$article = array ();
foreach ( $article_node -> childNodes as $child ) {
2018-05-28 13:43:53 +00:00
if ( $child -> nodeName == 'content' || $child -> nodeName == 'label_cache' ) {
2012-12-24 11:03:19 +00:00
$article [ $child -> nodeName ] = $child -> nodeValue ;
2015-11-02 11:22:46 +00:00
} else {
2018-05-28 13:43:53 +00:00
$article [ $child -> nodeName ] = clean ( $child -> nodeValue );
2015-11-02 11:22:46 +00:00
}
2012-12-24 11:03:19 +00:00
}
//print_r($article);
if ( $article [ 'guid' ]) {
++ $num_processed ;
2018-05-28 13:43:53 +00:00
$this -> pdo -> beginTransaction ();
2012-12-24 11:03:19 +00:00
//print 'GUID:' . $article['guid'] . "\n";
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " SELECT id FROM ttrss_entries
WHERE guid = ? " );
$sth -> execute ([ $article [ 'guid' ]]);
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $row = $sth -> fetch ()) {
$ref_id = $row [ 'id' ];
} else {
$sth = $this -> pdo -> prepare (
2012-12-24 11:03:19 +00:00
" INSERT INTO ttrss_entries
( title ,
guid ,
link ,
updated ,
content ,
content_hash ,
no_orig_date ,
date_updated ,
date_entered ,
comments ,
num_comments ,
author )
VALUES
2018-05-28 13:43:53 +00:00
( ? , ? , ? , ? , ? , ? ,
2012-12-24 11:03:19 +00:00
false ,
NOW (),
NOW (),
'' ,
'0' ,
'' ) " );
2018-05-24 09:31:30 +00:00
2018-05-28 13:43:53 +00:00
$sth -> execute ([
$article [ 'title' ],
$article [ 'guid' ],
$article [ 'link' ],
$article [ 'updated' ],
$article [ 'content' ],
sha1 ( $article [ 'content' ])
]);
$sth = $this -> pdo -> prepare ( " SELECT id FROM ttrss_entries
WHERE guid = ? " );
$sth -> execute ([ $article [ 'guid' ]]);
if ( $row = $sth -> fetch ()) {
$ref_id = $row [ 'id' ];
2012-12-24 11:03:19 +00:00
}
}
//print "Got ref ID: $ref_id\n";
if ( $ref_id ) {
2018-05-28 13:43:53 +00:00
$feed = NULL ;
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $article [ 'feed_url' ] && $article [ 'feed_title' ]) {
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " SELECT id FROM ttrss_feeds
WHERE feed_url = ? AND owner_uid = ? " );
$sth -> execute ([ $article [ 'feed_url' ], $owner_uid ]);
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $row = $sth -> fetch ()) {
$feed = $row [ 'id' ];
2012-12-24 11:03:19 +00:00
} else {
// try autocreating feed in Uncategorized...
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " INSERT INTO ttrss_feeds (owner_uid,
feed_url , title ) VALUES ( ? , ? , ? ) " );
$res = $sth -> execute ([ $owner_uid , $article [ 'feed_url' ], $article [ 'feed_title' ]]);
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $res ) {
$sth = $this -> pdo -> prepare ( " SELECT id FROM ttrss_feeds
WHERE feed_url = ? AND owner_uid = ? " );
$sth -> execute ([ $article [ 'feed_url' ], $owner_uid ]);
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( $row = $sth -> fetch ()) {
++ $num_feeds_created ;
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
$feed = $row [ 'id' ];
}
2012-12-24 11:03:19 +00:00
}
}
}
2018-05-28 13:43:53 +00:00
if ( $feed )
$feed_qpart = " feed_id = " . ( int ) $feed ;
2012-12-24 11:03:19 +00:00
else
$feed_qpart = " feed_id IS NULL " ;
//print "$ref_id / $feed / " . $article['title'] . "\n";
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare ( " SELECT int_id FROM ttrss_user_entries
WHERE ref_id = ? AND owner_uid = ? AND $feed_qpart " );
$sth -> execute ([ $ref_id , $owner_uid ]);
2012-12-24 11:03:19 +00:00
2018-05-28 13:43:53 +00:00
if ( ! $sth -> fetch ()) {
2012-12-24 11:03:19 +00:00
$score = ( int ) $article [ 'score' ];
$tag_cache = $article [ 'tag_cache' ];
$note = $article [ 'note' ];
//print "Importing " . $article['title'] . "<br/>";
++ $num_imported ;
2018-05-28 13:43:53 +00:00
$sth = $this -> pdo -> prepare (
2012-12-24 11:03:19 +00:00
" INSERT INTO ttrss_user_entries
( ref_id , owner_uid , feed_id , unread , last_read , marked ,
published , score , tag_cache , label_cache , uuid , note )
2018-05-28 13:43:53 +00:00
VALUES ( ? , ? , ? , false ,
NULL , ? , ? , ? , ? , '' , '' , ? ) " );
$res = $sth -> execute ([
$ref_id ,
$owner_uid ,
$feed ,
( int ) sql_bool_to_bool ( $article [ 'marked' ]),
( int ) sql_bool_to_bool ( $article [ 'published' ]),
$score ,
$tag_cache ,
$note ]);
if ( $res ) {
2019-04-10 10:18:23 +00:00
if ( DB_TYPE == " pgsql " ) {
$ts_lang = get_pref ( 'DEFAULT_SEARCH_LANGUAGE' , $owner_uid );
// TODO: maybe use per-feed setting if available?
2019-04-11 10:38:47 +00:00
if ( ! $ts_lang )
$ts_lang = 'simple' ;
2019-04-10 10:18:23 +00:00
$sth = $this -> pdo -> prepare ( " UPDATE ttrss_entries
SET tsvector_combined = to_tsvector ( : ts_lang , : ts_content )
WHERE id = : id " );
$sth -> execute ([
" id " => $ref_id ,
" ts_lang " => $ts_lang ,
" ts_content " => mb_substr ( strip_tags ( $article [ 'title' ] . " " . $article [ 'content' ]), 0 , 900000 )
]);
}
2018-05-28 13:43:53 +00:00
$label_cache = json_decode ( $article [ 'label_cache' ], true );
if ( is_array ( $label_cache ) && $label_cache [ " no-labels " ] != 1 ) {
foreach ( $label_cache as $label ) {
Labels :: create ( $label [ 1 ],
$label [ 2 ], $label [ 3 ], $owner_uid );
Labels :: add_article ( $ref_id , $label [ 1 ], $owner_uid );
}
2012-12-24 11:03:19 +00:00
}
}
}
}
2018-05-28 13:43:53 +00:00
$this -> pdo -> commit ();
2012-12-24 11:03:19 +00:00
}
}
}
print " <p> " .
2013-04-02 05:28:47 +00:00
__ ( " Finished: " ) .
2013-09-02 09:09:32 +00:00
vsprintf ( _ngettext ( " %d article processed, " , " %d articles processed, " , $num_processed ), $num_processed ) .
vsprintf ( _ngettext ( " %d imported, " , " %d imported, " , $num_imported ), $num_imported ) .
vsprintf ( _ngettext ( " %d feed created. " , " %d feeds created. " , $num_feeds_created ), $num_feeds_created ) .
2012-12-24 11:03:19 +00:00
" </p> " ;
} else {
print " <p> " . __ ( " Could not load XML document. " ) . " </p> " ;
}
}
function exportData () {
2019-04-09 04:05:33 +00:00
print " <form onsubmit='return false'> " ;
2012-12-24 11:03:19 +00:00
print " <p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p> " ;
2019-02-22 07:48:56 +00:00
print " <footer class='text-center'> " ;
print " <button dojoType='dijit.form.Button'
type = 'submit' class = 'alt-primary'
2012-12-24 11:03:19 +00:00
onclick = \ " dijit.byId('dataExportDlg').prepare() \" > " .
__ ( 'Prepare data' ) . " </button> " ;
2019-02-22 07:48:56 +00:00
print " <button dojoType='dijit.form.Button'
2012-12-24 11:03:19 +00:00
onclick = \ " dijit.byId('dataExportDlg').hide() \" > " .
__ ( 'Close this window' ) . " </button> " ;
2019-02-22 07:48:56 +00:00
print " </footer> " ;
2019-04-09 04:05:33 +00:00
print " </form> " ;
2012-12-24 11:03:19 +00:00
}
function dataImport () {
header ( " Content-Type: text/html " ); # required for iframe
2019-02-22 07:48:56 +00:00
print " <footer class='text-center'> " ;
2012-12-24 11:03:19 +00:00
2013-04-11 16:31:24 +00:00
if ( $_FILES [ 'export_file' ][ 'error' ] != 0 ) {
2016-08-19 15:14:22 +00:00
print_error ( T_sprintf ( " Upload failed with error code %d (%s) " ,
$_FILES [ 'export_file' ][ 'error' ],
get_upload_error_message ( $_FILES [ 'export_file' ][ 'error' ])));
} else {
2013-04-11 16:31:24 +00:00
2016-08-19 15:14:22 +00:00
if ( is_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ])) {
$tmp_file = tempnam ( CACHE_DIR . '/upload' , 'export' );
2012-12-24 11:03:19 +00:00
2016-08-19 15:14:22 +00:00
$result = move_uploaded_file ( $_FILES [ 'export_file' ][ 'tmp_name' ],
$tmp_file );
2013-04-11 16:31:24 +00:00
2016-08-19 15:14:22 +00:00
if ( ! $result ) {
print_error ( __ ( " Unable to move uploaded file. " ));
return ;
}
} else {
print_error ( __ ( 'Error: please upload OPML file.' ));
2013-04-11 16:31:24 +00:00
return ;
}
2012-12-24 11:03:19 +00:00
2016-08-19 15:14:22 +00:00
if ( is_file ( $tmp_file )) {
$this -> perform_data_import ( $tmp_file , $_SESSION [ 'uid' ]);
unlink ( $tmp_file );
} else {
print_error ( __ ( 'No file uploaded.' ));
return ;
}
2012-12-24 11:03:19 +00:00
}
2019-02-22 07:48:56 +00:00
print " <button dojoType='dijit.form.Button'
2012-12-24 11:03:19 +00:00
onclick = \ " dijit.byId('dataImportDlg').hide() \" > " .
__ ( 'Close this window' ) . " </button> " ;
print " </div> " ;
}
2013-04-19 13:31:56 +00:00
function api_version () {
return 2 ;
}
2012-12-24 11:03:19 +00:00
2018-01-30 08:45:15 +00:00
}