Quellcode durchsuchen

Add CLI for user information + Fix last user activity

https://github.com/FreshRSS/FreshRSS/issues/1345
Alexandre Alapetite vor 9 Jahren
Ursprung
Commit
aeda49a7d2
7 geänderte Dateien mit 78 neuen und 2 gelöschten Zeilen
  1. 4 0
      CHANGELOG.md
  2. 4 0
      app/Controllers/feedController.php
  3. 1 1
      app/Models/Auth.php
  4. 5 0
      app/Models/EntryDAO.php
  5. 5 1
      app/Models/UserDAO.php
  6. 16 0
      cli/README.md
  7. 43 0
      cli/user-info.php

+ 4 - 0
CHANGELOG.md

@@ -2,6 +2,10 @@
 
 ## 2016-xx-xx FreshRSS 1.7.0-dev
 
+* CLI
+	* New command `./cli/user-info.php` to get some user information [#1345](https://github.com/FreshRSS/FreshRSS/issues/1345)
+* Bug fixing
+	* Fix bug in estimating last user activity [#1358](https://github.com/FreshRSS/FreshRSS/issues/1358)
 
 
 ## 2016-11-02 FreshRSS 1.6.1

+ 4 - 0
app/Controllers/feedController.php

@@ -27,6 +27,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 	}
 
 	public static function addFeed($url, $title = '', $cat_id = 0, $new_cat_name = '', $http_auth = '') {
+		FreshRSS_UserDAO::touch();
 		@set_time_limit(300);
 
 		$catDAO = new FreshRSS_CategoryDAO();
@@ -484,6 +485,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 		if ($feed_id <= 0 || $feed_name == '') {
 			return false;
 		}
+		FreshRSS_UserDAO::touch();
 		$feedDAO = FreshRSS_Factory::createFeedDao();
 		return $feedDAO->updateFeed($feed_id, array('name' => $feed_name));
 	}
@@ -492,6 +494,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 		if ($feed_id <= 0 || ($cat_id <= 0 && $new_cat_name == '')) {
 			return false;
 		}
+		FreshRSS_UserDAO::touch();
 
 		$catDAO = new FreshRSS_CategoryDAO();
 		if ($cat_id > 0) {
@@ -540,6 +543,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 	}
 
 	public static function deleteFeed($feed_id) {
+		FreshRSS_UserDAO::touch();
 		$feedDAO = FreshRSS_Factory::createFeedDao();
 		if ($feedDAO->deleteFeed($feed_id)) {
 			// TODO: Delete old favicon

+ 1 - 1
app/Models/Auth.php

@@ -25,7 +25,7 @@ class FreshRSS_Auth {
 			self::giveAccess();
 		} elseif (self::accessControl()) {
 			self::giveAccess();
-			FreshRSS_UserDAO::touch($current_user);
+			FreshRSS_UserDAO::touch();
 		} else {
 			// Be sure all accesses are removed!
 			self::removeAccess();

+ 5 - 0
app/Models/EntryDAO.php

@@ -241,6 +241,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 		if (count($ids) < 1) {
 			return 0;
 		}
+		FreshRSS_UserDAO::touch();
 		$sql = 'UPDATE `' . $this->prefix . 'entry` '
 		     . 'SET is_favorite=? '
 		     . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
@@ -315,6 +316,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 	 * @return integer affected rows
 	 */
 	public function markRead($ids, $is_read = true) {
+		FreshRSS_UserDAO::touch();
 		if (is_array($ids)) {	//Many IDs at once (used by API)
 			if (count($ids) < 6) {	//Speed heuristics
 				$affected = 0;
@@ -379,6 +381,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 	 * @return integer affected rows
 	 */
 	public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filter = null, $state = 0) {
+		FreshRSS_UserDAO::touch();
 		if ($idMax == 0) {
 			$idMax = time() . '000000';
 			Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
@@ -421,6 +424,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 	 * @return integer affected rows
 	 */
 	public function markReadCat($id, $idMax = 0, $filter = null, $state = 0) {
+		FreshRSS_UserDAO::touch();
 		if ($idMax == 0) {
 			$idMax = time() . '000000';
 			Minz_Log::debug('Calling markReadCat(0) is deprecated!');
@@ -458,6 +462,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 	 * @return integer affected rows
 	 */
 	public function markReadFeed($id_feed, $idMax = 0, $filter = null, $state = 0) {
+		FreshRSS_UserDAO::touch();
 		if ($idMax == 0) {
 			$idMax = time() . '000000';
 			Minz_Log::debug('Calling markReadFeed(0) is deprecated!');

+ 5 - 1
app/Models/UserDAO.php

@@ -84,7 +84,11 @@ class FreshRSS_UserDAO extends Minz_ModelPdo {
 		return is_dir(join_path(DATA_PATH , 'users', $username));
 	}
 
-	public static function touch($username) {
+	public static function touch($username = '') {
+		if (($username == '') || (!ctype_alnum($username))) {
+			$username = Minz_Session::param('currentUser', '_');
+		}
+		Minz_Log::debug('touch ' . $username);
 		return touch(join_path(DATA_PATH , 'users', $username, 'config.php'));
 	}
 

+ 16 - 0
cli/README.md

@@ -55,4 +55,20 @@ cd /usr/share/FreshRSS
 ./cli/export-opml-for-user.php --user username > /path/to/file.opml.xml
 
 ./cli/export-zip-for-user.php --user username ( --max-feed-entries 100 ) > /path/to/file.zip
+
+./cli/user-info.php -h --user username
+# -h is to use a human-readable format
+# --user can be a username, or '*' to loop on all users
+# Returns a * if the user is admin, the name of the user, the date/time of last action, and the size occupied
+```
+
+
+## Unix piping
+
+It is possible to invoke a command multiple times, e.g. with different usernames, thanks to the `xargs -n1` command.
+
+Example showing user information for all users which username starts with 'a':
+
+```sh
+./cli/list-users.php | grep '^a' | xargs -n1 ./cli/user-info.php -h --user
 ```

+ 43 - 0
cli/user-info.php

@@ -0,0 +1,43 @@
+#!/usr/bin/php
+<?php
+require('_cli.php');
+
+function formatSize($bytes)
+{//http://www.php.net/manual/function.disk-free-space.php#103382
+	$si_prefix = array('', 'k', 'M', 'G', 'T', 'P');
+	$i = min((int)log($bytes, 1024), count($si_prefix) - 1);
+	return ($i <= 0) ? $bytes.'B' :
+		round($bytes / pow(1024, $i), 2).' '.$si_prefix[$i].'B';
+}
+
+$options = getopt('h', array(
+		'user:',
+	));
+
+if (empty($options['user'])) {
+	fail('Usage: ' . basename(__FILE__) . " -h --user username");
+}
+
+$users = $options['user'] === '*' ? listUsers() : array($options['user']);
+
+foreach ($users as $username) {
+	$username = cliInitUser($username);
+
+	$entryDAO = FreshRSS_Factory::createEntryDao($username);
+
+	echo $username === FreshRSS_Context::$system_conf->default_user ? '*' : ' ', "\t";
+
+	if (isset($options['h'])) {	//Human format
+		echo
+			$username, "\t",
+			date('c', FreshRSS_UserDAO::mtime($username)), "\t",
+			formatSize($entryDAO->size()), "\t",
+			"\n";
+	} else {
+		echo
+			$username, "\t",
+			FreshRSS_UserDAO::mtime($username), "\t",
+			$entryDAO->size(), "\t",
+			"\n";
+	}
+}