summaryrefslogtreecommitdiff
path: root/init.php
blob: e6faed6c708390fcf599ddaa07d0f5c15b883e69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
class Data_Migration extends Plugin {

	private $DATA_FORMAT_VERSION = 1;

	function init($host) {
		$host->add_command("data-user", "set username for import/export", $this, ":", "USER");
		$host->add_command("data-only-marked", "only export starred (or archived) articles", $this, "", "");
		$host->add_command("data-import", "import articles", $this, ":", "FILE.zip");
		$host->add_command("data-export", "export articles", $this, ":", "FILE.zip");
	}

	function about() {
		return array(1.0,
			"Migrates user articles using neutral format",
			"fox",
			true,
			"https://git.tt-rss.org/fox/ttrss-data-migration/wiki");
	}

	function data_only_marked($args) {
		//
	}

	function data_user($args) {
		//
	}

	function data_import($args) {
		$user = $args["data_user"];
		$input_file = $args["data_import"];

		if (!$user) {
			Debug::log("error: please set username using --data_user");
			exit(1);
		}

		$sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
		$sth->execute([$user]);

		if ($row = $sth->fetch()) {
			$owner_uid = $row['id'];

			Debug::log("importing articles of user $user from $input_file...");

			$zip = new ZipArchive();

			if ($zip->open($input_file) !== TRUE) {
				Debug::log("unable to open $input_file");
				exit(3);
			}

			$total_imported = 0;
			$total_processed = 0;
			$total_feeds_created = 0;

			for ($i = 0; $i < $zip->numFiles; $i++) {
				Debug::log("processing " . $zip->getNameIndex($i));

				$batch = json_decode($zip->getFromIndex($i), true);

				if ($batch) {
					if ($batch["version"] == $this->DATA_FORMAT_VERSION) {
						if ($batch["schema-version"] == SCHEMA_VERSION) {
							$total_processed += count($batch["articles"]);

							list ($batch_imported, $batch_feeds_created) = $this->import_article_batch($owner_uid, $batch["articles"]);

							$total_imported += $batch_imported;
							$total_feeds_created += $batch_feeds_created;

						} else {
							Debug::log("batch has incorrect schema format version (expected: " .
								SCHEMA_VERSION . ", got: " . $batch["schema-version"]);
						}
					} else {
						Debug::log("batch has incorrect data format version (expected: " .
							$this->DATA_FORMAT_VERSION . ", got: " . $batch["version"]);
					}


				} else {
					Debug::log("error while decoding JSON data.");
				}
			}

			$zip->close();

			Debug::log("imported $total_imported (out of $total_processed) articles, created $total_feeds_created feeds.");

		} else {
			Debug::log("error: could not find user $user.");
			exit(4);
		}
	}

	function data_export($args) {
		$user = $args["data_user"];
		$output_file = $args["data_export"];
		$only_marked = isset($args["data_only_marked"]);

		if (!$user) {
			Debug::log("error: please set username using --data_user");
			exit(1);
		}

		$sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
		$sth->execute([$user]);

		if ($row = $sth->fetch()) {
			$owner_uid = $row['id'];

			if (stripos($output_file, ".zip") === FALSE)
				$output_file = $output_file . ".zip";

			Debug::log("exporting articles of user $user to $output_file...");

			if ($only_marked)
				Debug::log("limiting export to marked and archived articles.");

			if (file_exists($output_file)) {
				Debug::log("refusing to overwrite existing output file.");
				exit(2);
			}

			$zip = new ZipArchive();

			if ($zip->open($output_file, ZipArchive::CREATE) !== TRUE) {
				Debug::log("unable to create $output_file");
				exit(3);
			}

			$offset = 0;
			$batch_size = 1000;
			$batch_seq = 0;
			$total_processed = 0;

			while (true) {
				$batch_filename = sprintf("%08d.json", $batch_seq);

				$batch = [
					"version" => $this->DATA_FORMAT_VERSION,
					"schema-version" => SCHEMA_VERSION,
					"articles" => $this->get_export_batch($owner_uid, $offset, $batch_size, $only_marked)
				];

				$offset += count($batch["articles"]);
				$total_processed += count($batch["articles"]);
				++$batch_seq;

				$zip->addFromString($batch_filename, json_encode($batch));

				if (count($batch["articles"]) != $batch_size)
					break;
			}

			if ($zip->close() !== TRUE) {
				Debug::log("write error while saving data to $output_file");
				exit(3);
			}

			Debug::log("exported $total_processed articles to $output_file.");

		} else {
			Debug::log("error: could not find user $user.");
			exit(4);
		}
	}

	private function get_export_batch($owner_uid, $offset, $batch_size, $only_marked)	{
		$rv = [];

		Debug::log("processing articles, offset: $offset");

		if ($only_marked)
			$export_filter_qpart = "(marked = true OR feed_id IS NULL) AND";
		else
			$export_filter_qpart = "";

		$sth = $this->pdo->prepare("SELECT
					ttrss_entries.guid,
					ttrss_entries.title,
					content,
					marked,
					published,
					score,
					note,
					link,
					tag_cache,
					label_cache,
					author,
					unread,
					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
				    $export_filter_qpart
					ref_id = ttrss_entries.id AND
					ttrss_user_entries.owner_uid = ?
				ORDER BY ttrss_entries.id LIMIT $batch_size OFFSET $offset");

		$sth->execute([$owner_uid]);

		while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

			foreach ($row as $k => $v) {
				if (is_bool($v)) {
					$row[$k] = (int)$v;
				}
			}

			array_push($rv, $row);
		}

		return $rv;
	}

	private function import_article_batch($owner_uid, $articles) {
		$total_imported = 0;
		$total_feeds_created = 0;

		foreach ($articles as $article) {

			$this->pdo->beginTransaction();

			$sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE guid = ?");
			$sth->execute([$article['guid']]);

			$ref_id = false;

			if ($row = $sth->fetch()) {
				$ref_id = $row['id'];
			} else {
				$sth = $this->pdo->prepare(
					"INSERT INTO ttrss_entries
									(title, guid, link, updated, content, content_hash,
									no_orig_date,
									date_updated,
									date_entered,
									comments,
									num_comments,
									author)
								VALUES
									(:title, :guid, :link, :updated, :content, :content_hash,
									false,
									 NOW(),
									NOW(),
									'',
									'0',
									:author)");

				$sth->execute([
					"title" => $article['title'],
					"guid" => $article['guid'],
					"link" => $article['link'],
					"updated" => $article['updated'],
					"content" => $article['content'],
					"content_hash" => sha1($article['content']),
					"author" => $article["author"]
				]);

				$sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE guid = ?");
				$sth->execute([$article['guid']]);

				if ($row = $sth->fetch()) {
					$ref_id = $row['id'];
				}
			}

			//print "Got ref ID: $ref_id\n";

			if ($ref_id) {

				$feed = NULL;

				if ($article['feed_url'] && $article['feed_title']) {

					$sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
									WHERE feed_url = ? AND owner_uid = ?");
					$sth->execute([$article['feed_url'], $owner_uid]);

					if ($row = $sth->fetch()) {
						$feed = $row['id'];
					} else {
						// try autocreating feed in Uncategorized...

						$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']]);

						if ($res) {
							$sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
											WHERE feed_url = ? AND owner_uid = ?");
							$sth->execute([$article['feed_url'], $owner_uid]);

							if ($row = $sth->fetch()) {
								++$total_feeds_created;

								$feed = $row['id'];
							}
						}
					}
				}

				if ($feed)
					$feed_qpart = "feed_id = " . (int) $feed;
				else
					$feed_qpart = "feed_id IS NULL";

				//print "$ref_id / $feed / " . $article['title'] . "\n";

				$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]);

				if (!$sth->fetch()) {

					$score = (int) $article['score'];

					$tag_cache = $article['tag_cache'];
					$note = $article['note'];

					//print "Importing " . $article['title'] . "<br/>";

					++$total_imported;

					$sth = $this->pdo->prepare(
						"INSERT INTO ttrss_user_entries
									(ref_id, owner_uid, feed_id, unread, last_read,
									 	marked, published, score, tag_cache, label_cache, uuid, note)
									VALUES (:ref_id, :owner_uid, :feed_id, :unread, NULL,
									    :marked, :published, :score, :tag_cache, '', '', :note)");

					$res = $sth->execute([
						"ref_id" => $ref_id,
						"owner_uid" => $owner_uid,
						"feed_id" => $feed,
						"marked" => (int)sql_bool_to_bool($article['marked']),
						"published" => (int)sql_bool_to_bool($article['published']),
						"unread" => (int)sql_bool_to_bool($article['unread']),
						"score" => $score,
						"tag_cache" => $tag_cache,
						"note" => $note]);

					if ($res) {

						if (Config::get(Config::DB_TYPE) == "pgsql") {
							$ts_lang = get_pref('DEFAULT_SEARCH_LANGUAGE', $owner_uid);
							// TODO: maybe use per-feed setting if available?

							if (!$ts_lang)
								$ts_lang = 'simple';

							$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)
							]);
						}

						$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);
							}
						}
					}
				}
			}

			$this->pdo->commit();
		}

		return [$total_imported, $total_feeds_created];
	}

	function api_version() {
		return 2;
	}

}