Browse Source

Default or custom OPML (#2627)

* Default or custom OPML

Fix https://github.com/FreshRSS/FreshRSS/issues/2075
Replaces https://github.com/FreshRSS/FreshRSS/pull/2515
https://github.com/FreshRSS/FreshRSS/issues/2514

Uses the local ./data/opml.xml if it exists, otherwise
./opml.default.xml

* Better message

* Move to controller
Alexandre Alapetite 6 năm trước cách đây
mục cha
commit
7819a43197

+ 15 - 1
app/Controllers/userController.php

@@ -248,7 +248,21 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 		}
 		if ($ok) {
 			$newUserDAO = FreshRSS_Factory::createUserDao($new_user_name);
-			$ok &= $newUserDAO->createUser($insertDefaultFeeds);
+			$ok &= $newUserDAO->createUser();
+
+			if ($ok && $insertDefaultFeeds) {
+				$opmlPath = DATA_PATH . '/opml.xml';
+				if (!file_exists($opmlPath)) {
+					$opmlPath = FRESHRSS_PATH . '/opml.default.xml';
+				}
+				$importController = new FreshRSS_importExport_Controller();
+				try {
+					$importController->importFile($opmlPath, $opmlPath, $new_user_name);
+				} catch (Exception $e) {
+					Minz_Log::error('Error while importing default OPML for user ' . $new_user_name . ': ' . $e->getMessage());
+				}
+			}
+
 			$ok &= self::updateUser($new_user_name, $email, $passwordPlain, $apiPasswordPlain);
 		}
 		return $ok;

+ 3 - 16
app/Models/UserDAO.php

@@ -1,34 +1,21 @@
 <?php
 
 class FreshRSS_UserDAO extends Minz_ModelPdo {
-	public function createUser($insertDefaultFeeds = false) {
+	public function createUser() {
 		require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
 
 		try {
 			$sql = $SQL_CREATE_TABLES . $SQL_CREATE_TABLE_ENTRYTMP . $SQL_CREATE_TABLE_TAGS;
 			$ok = $this->pdo->exec($sql) !== false;	//Note: Only exec() can take multiple statements safely.
-			if ($ok && $insertDefaultFeeds) {
-				$default_feeds = FreshRSS_Context::$system_conf->default_feeds;
-				$stm = $this->pdo->prepare($SQL_INSERT_FEED);
-				foreach ($default_feeds as $feed) {
-					$parameters = [
-						':url' => $feed['url'],
-						':name' => $feed['name'],
-						':website' => $feed['website'],
-						':description' => $feed['description'],
-					];
-					$ok &= ($stm && $stm->execute($parameters));
-				}
-			}
 		} catch (Exception $e) {
-			Minz_Log::error('Error while creating database for user: ' . $e->getMessage());
+			Minz_Log::error('Error while creating database for user ' . $this->current_user . ': ' . $e->getMessage());
 		}
 
 		if ($ok) {
 			return true;
 		} else {
 			$info = empty($stm) ? $this->pdo->errorInfo() : $stm->errorInfo();
-			Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
+			Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info));
 			return false;
 		}
 	}

+ 0 - 5
app/SQL/install.sql.mysql.php

@@ -112,11 +112,6 @@ CREATE TABLE IF NOT EXISTS `_entrytag` (	-- v1.12
 ENGINE = INNODB;
 SQL;
 
-$SQL_INSERT_FEED = <<<'SQL'
-INSERT IGNORE INTO `_feed` (url, category, name, website, description, ttl)
-	VALUES(:url, 1, :name, :website, :description, 86400);
-SQL;
-
 $SQL_DROP_TABLES = <<<'SQL'
 DROP TABLE IF EXISTS `_entrytag`, `_tag`, `_entrytmp`, `_entry`, `_feed`, `_category`;
 SQL;

+ 0 - 6
app/SQL/install.sql.pgsql.php

@@ -100,12 +100,6 @@ CREATE TABLE IF NOT EXISTS `_entrytag` (
 CREATE INDEX IF NOT EXISTS `_entrytag_id_entry_index` ON `_entrytag` ("id_entry");
 SQL;
 
-$SQL_INSERT_FEED = <<<'SQL'
-INSERT INTO `_feed` (url, category, name, website, description, ttl)
-	SELECT :url::VARCHAR, 1, :name, :website, :description, 86400
-		WHERE NOT EXISTS (SELECT id FROM `_feed` WHERE url = :url);
-SQL;
-
 $SQL_DROP_TABLES = <<<'SQL'
 DROP TABLE IF EXISTS `_entrytag`, `_tag`, `_entrytmp`, `_entry`, `_feed`, `_category`;
 SQL;

+ 0 - 5
app/SQL/install.sql.sqlite.php

@@ -102,11 +102,6 @@ CREATE TABLE IF NOT EXISTS `entrytag` (
 CREATE INDEX IF NOT EXISTS entrytag_id_entry_index ON `entrytag` (`id_entry`);
 SQL;
 
-$SQL_INSERT_FEED = <<<'SQL'
-INSERT OR IGNORE INTO `feed` (url, category, name, website, description, ttl)
-	VALUES(:url, 1, :name, :website, :description, 86400);
-SQL;
-
 $SQL_DROP_TABLES = <<<'SQL'
 DROP TABLE IF EXISTS `entrytag`;
 DROP TABLE IF EXISTS `tag`;

+ 0 - 10
config.default.php

@@ -156,16 +156,6 @@ return array(
 
 	],
 
-	# Configure the default feeds to which users will automatically be subscribed.
-	'default_feeds' => array(
-		array(
-			'url' => 'https://github.com/FreshRSS/FreshRSS/releases.atom',
-			'name' => 'FreshRSS releases',
-			'website' => 'https://github.com/FreshRSS/FreshRSS/',
-			'description' => 'FreshRSS releases @ GitHub',
-		),
-	),
-
 	# Configuration to send emails. Be aware that PHP < 5.5 are not supported.
 	# These options are basically a mapping of the PHPMailer class attributes
 	# from the PHPMailer library.

+ 14 - 0
opml.default.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	Configure the default feeds to which users will automatically be subscribed.
+	Do not edit this file but instead create your own ./data/opml.xml
+-->
+<opml version="2.0">
+	<head>
+		<title>FreshRSS default OPML</title>
+		<dateCreated>Sat, 02 Nov 2019 12:00:00</dateCreated>
+	</head>
+	<body>
+		<outline text="FreshRSS releases" type="rss" xmlUrl="https://github.com/FreshRSS/FreshRSS/releases.atom" htmlUrl="https://github.com/FreshRSS/FreshRSS/" description="FreshRSS releases @ GitHub"/>
+	</body>
+</opml>