Deduplicate parsing and fix parsing in emails

This commit is contained in:
Joas Schilling 2016-01-20 13:32:38 +01:00
Родитель 60f16a4070
Коммит 92f6cd05b5
4 изменённых файлов: 150 добавлений и 225 удалений

Просмотреть файл

@ -14,6 +14,7 @@ namespace OCA\Activity\Controller;
use OCA\Activity\Data;
use OCA\Activity\GroupHelper;
use OCA\Activity\PlainTextParser;
use OCA\Activity\UserSettings;
use OCP\Activity\IManager;
use OCP\AppFramework\Controller;
@ -107,6 +108,7 @@ class Feed extends Controller {
// Overwrite user and language in the helper
$this->l = $this->l10nFactory->get('activity', $userLang);
$parser = new PlainTextParser($this->l);
$this->helper->setL10n($this->l);
$this->helper->setUser($user);
@ -116,8 +118,8 @@ class Feed extends Controller {
$activities = [];
foreach ($data as $activity) {
$activity['subject_prepared'] = $this->parseMessage($activity['subject_prepared']);
$activity['message_prepared'] = $this->parseMessage($activity['message_prepared']);
$activity['subject_prepared'] = $parser->parseMessage($activity['subject_prepared']);
$activity['message_prepared'] = $parser->parseMessage($activity['message_prepared']);
$activities[] = $activity;
}
@ -151,113 +153,4 @@ class Feed extends Controller {
return $response;
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
protected function parseMessage($message) {
$message = $this->parseCollections($message);
$message = $this->parseParameters($message);
return $message;
}
/**
* Parse collections
*
* @param string $message
* @return string
*/
protected function parseCollections($message) {
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) {
$parameterList = explode('><', $match[1]);
$parameterListLength = sizeof($parameterList);
$parameters = [];
for ($i = 0; $i < $parameterListLength; $i++) {
$parameter = $parameterList[$i];
if ($i > 0) {
$parameter = '<' . $parameter;
}
if ($i + 1 < $parameterListLength) {
$parameter = $parameter . '>';
}
$parameters[] = $this->parseParameters($parameter);
}
if ($parameterListLength === 1) {
return array_pop($parameters);
} else {
$lastParameter = array_pop($parameters);
return $this->l->t('%s and %s', [
implode($this->l->t(', '), $parameters),
$lastParameter,
]);
}
}, $message);
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
protected function parseParameters($message) {
$message = $this->parseUntypedParameters($message);
$message = $this->parseUserParameters($message);
$message = $this->parseFederatedCloudIDParameters($message);
$message = $this->parseFileParameters($message);
return $message;
}
/**
* Display the parameter value
*
* @param string $message
* @return string
*/
protected function parseUntypedParameters($message) {
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the users display name
*
* @param string $message
* @return string
*/
protected function parseUserParameters($message) {
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the full cloud id
*
* @param string $message
* @return string
*/
protected function parseFederatedCloudIDParameters($message) {
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the path for files
*
* @param string $message
* @return string
*/
protected function parseFileParameters($message) {
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) {
return $match[3];
}, $message);
}
}

Просмотреть файл

@ -50,13 +50,14 @@ class Api
$app->getContainer()->query('UserSettings'),
$user, $start, $count, 'desc', 'all'
);
$parser = new PlainTextParser(\OC::$server->getL10NFactory()->get('activity'));
$entries = array();
foreach($activities['data'] as $entry) {
$entries[] = array(
'id' => $entry['activity_id'],
'subject' => self::parseMessage($entry['subject_prepared']),
'message' => self::parseMessage($entry['message_prepared']),
'subject' => $parser->parseMessage($entry['subject_prepared']),
'message' => $parser->parseMessage($entry['message_prepared']),
'file' => $entry['object_name'],
'link' => $entry['link'],
'date' => date('c', $entry['timestamp']),
@ -90,114 +91,4 @@ class Api
return 0;
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
protected static function parseMessage($message) {
$message = self::parseCollections($message);
$message = self::parseParameters($message);
return $message;
}
/**
* Parse collections
*
* @param string $message
* @return string
*/
protected static function parseCollections($message) {
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) {
$parameterList = explode('><', $match[1]);
$parameterListLength = sizeof($parameterList);
$parameters = [];
for ($i = 0; $i < $parameterListLength; $i++) {
$parameter = $parameterList[$i];
if ($i > 0) {
$parameter = '<' . $parameter;
}
if ($i + 1 < $parameterListLength) {
$parameter = $parameter . '>';
}
$parameters[] = self::parseParameters($parameter);
}
if ($parameterListLength === 1) {
return array_pop($parameters);
} else {
$l = \OC::$server->getL10NFactory()->get('activity');
$lastParameter = array_pop($parameters);
return $l->t('%s and %s', [
implode($l->t(', '), $parameters),
$lastParameter,
]);
}
}, $message);
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
protected static function parseParameters($message) {
$message = self::parseUntypedParameters($message);
$message = self::parseUserParameters($message);
$message = self::parseFederatedCloudIDParameters($message);
$message = self::parseFileParameters($message);
return $message;
}
/**
* Display the parameter value
*
* @param string $message
* @return string
*/
protected static function parseUntypedParameters($message) {
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the users display name
*
* @param string $message
* @return string
*/
protected static function parseUserParameters($message) {
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the full cloud id
*
* @param string $message
* @return string
*/
protected static function parseFederatedCloudIDParameters($message) {
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the path for files
*
* @param string $message
* @return string
*/
protected static function parseFileParameters($message) {
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) {
return $match[3];
}, $message);
}
}

Просмотреть файл

@ -221,6 +221,7 @@ class MailQueueHandler {
list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime);
$l = $this->getLanguage($lang);
$parser = new PlainTextParser($l);
$this->dataHelper->setUser($userName);
$this->dataHelper->setL10n($l);
@ -239,8 +240,10 @@ class MailQueueHandler {
);
$activityList[] = array(
$this->dataHelper->translation(
$activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($event, 'subject', $activity['amq_subjectparams'])
$parser->parseMessage(
$this->dataHelper->translation(
$activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($event, 'subject', $activity['amq_subjectparams'])
)
),
$relativeDateTime,
);

138
lib/plaintextparser.php Normal file
Просмотреть файл

@ -0,0 +1,138 @@
<?php
/**
* ownCloud
*
* @author Joas Schilling
* @copyright 2015 Joas Schilling nickvergessen@owncloud.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Activity;
use OCP\IL10N;
class PlainTextParser {
/** @var IL10N */
protected $l;
/**
* @param IL10N $l
*/
public function __construct(IL10N $l) {
$this->l = $l;
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
public function parseMessage($message) {
$message = $this->parseCollections($message);
$message = $this->parseParameters($message);
return $message;
}
/**
* Parse collections
*
* @param string $message
* @return string
*/
protected function parseCollections($message) {
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) {
$parameterList = explode('><', $match[1]);
$parameterListLength = sizeof($parameterList);
$parameters = [];
for ($i = 0; $i < $parameterListLength; $i++) {
$parameter = $parameterList[$i];
if ($i > 0) {
$parameter = '<' . $parameter;
}
if ($i + 1 < $parameterListLength) {
$parameter = $parameter . '>';
}
$parameters[] = $this->parseParameters($parameter);
}
if ($parameterListLength === 1) {
return array_pop($parameters);
} else {
$lastParameter = array_pop($parameters);
return $this->l->t('%s and %s', [
implode($this->l->t(', '), $parameters),
$lastParameter,
]);
}
}, $message);
}
/**
* Parse the parameters in the subject and message
*
* @param string $message
* @return string
*/
protected function parseParameters($message) {
$message = $this->parseUntypedParameters($message);
$message = $this->parseUserParameters($message);
$message = $this->parseFederatedCloudIDParameters($message);
$message = $this->parseFileParameters($message);
return $message;
}
/**
* Display the parameter value
*
* @param string $message
* @return string
*/
protected function parseUntypedParameters($message) {
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the users display name
*
* @param string $message
* @return string
*/
protected function parseUserParameters($message) {
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the full cloud id
*
* @param string $message
* @return string
*/
protected function parseFederatedCloudIDParameters($message) {
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) {
return $match[1];
}, $message);
}
/**
* Display the path for files
*
* @param string $message
* @return string
*/
protected function parseFileParameters($message) {
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) {
return $match[3];
}, $message);
}
}