Skip to content

Commit

Permalink
Extractors are working, sort of
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Apr 21, 2015
1 parent 4cedff8 commit e680cdb
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class AppleMailExtractor extends Extractor
{
// /**
// * Extract Reply from Apple MAil mail
// */
// protected function processLines()
// {
// $splitters = $this->getOriginalMessageSplitters();
//
// if (!empty($splitters)) {
// $this->stripOriginalMessage($splitters);
// }
//
// $this->body = implode("\n", $this->body);
// if (preg_match('/(.*)(On)(.*) at (.*) wrote\:(.*)/mis', $this->body, $matches, PREG_OFFSET_CAPTURE)) {
// $match_index = $matches[2][1];
// $this->body = trim(mb_substr($this->body, 0, $match_index));
// $this->body = explode("\n", $this->body);
// }
//
// $unwanted_text_patterns = $this->getUnwantedTextPatterns();
//
// if (!empty($unwanted_text_patterns)) {
// self::stripUnwantedText($unwanted_text_patterns);
// }
//
// self::stripSignature();
// self::convertPlainTextQuotesToBlockquotes();
// }
/**
* Extract Reply from Apple MAil mail
*/
protected function processLines()
{
$splitters = $this->getOriginalMessageSplitters();

if (!empty($splitters)) {
$this->stripOriginalMessage($splitters);
}

$this->body = implode("\n", $this->body);
if (preg_match('/(.*)(On)(.*) at (.*) wrote\:(.*)/mis', $this->body, $matches, PREG_OFFSET_CAPTURE)) {
$match_index = $matches[2][1];
$this->body = trim(mb_substr($this->body, 0, $match_index));
}
$this->body = explode("\n", $this->body);

$unwanted_text_patterns = $this->getUnwantedTextPatterns();

if (!empty($unwanted_text_patterns)) {
$this->stripUnwantedText($unwanted_text_patterns);
}

$this->stripSignature();
$this->convertPlainTextQuotesToBlockquotes();
}
}
32 changes: 21 additions & 11 deletions src/ActiveCollab/EmailReplyExtractor/Extractor/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ protected function getOriginalMessageSplitters()
*
* @param array $splitters
* @param integer $trim_previous_lines
* @return boolean
*/
function stripOriginalMessage(array &$splitters, $trim_previous_lines = 0) {
protected function stripOriginalMessage(array &$splitters, $trim_previous_lines = 0) {
$stripped = [];

foreach ($this->body as $line) {
Expand All @@ -117,21 +116,31 @@ function stripOriginalMessage(array &$splitters, $trim_previous_lines = 0) {
if ($trim_previous_lines == 0) {
$this->body = $stripped;
} else {
$this->body= array_slice($stripped, 0, count($stripped) - $trim_previous_lines);
$this->body = array_slice($stripped, 0, count($stripped) - $trim_previous_lines);
}

$last_line = trim($this->body[count($this->body) - 1]);
if (in_array($last_line, [ '>', '&gt;', '> **', '&gt; **' ])) {
$this->body = array_slice($this->body, 0, count($this->body) - 1); // sometimes > sign appears in front of reply so we need to strip it
}

return true;
$this->stripEmptyLinesFromTheEnd();
return;
}
}
$stripped[] = $line;
}
}

/**
* Remove empty or quote lines from the end of the mail
*/
protected function stripEmptyLinesFromTheEnd()
{
for ($i = count($this->body) - 1; $i >= 0; $i--) {
$line = trim($this->body[$i]);

return true;
if (empty($line) || in_array($line, [ '>', '&gt;', '> **', '&gt; **' ])) {
unset($this->body[$i]);
} else {
break;
}
}
}

/**
Expand Down Expand Up @@ -238,7 +247,8 @@ function convertPlainTextQuotesToBlockquotes() {

for ($x = 0, $lines_count = count($this->body); $x < $lines_count; $x++) {
$line = $this->body[$x];
if ((mb_substr($line,0,1) == '>') || (mb_substr($line,0,4) == '&gt;')) {

if (mb_substr($line, 0, 1) == '>' || mb_substr($line, 0, 4) == '&gt;') {
if (!$block_quote_opened) {
$lines[] = "<blockquote>\n";
$block_quote_opened = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class GoogleMailExtractor extends Extractor
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class HotmailExtractor extends Extractor
{

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class HushmailExtractor extends Extractor
{

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class OutlookExtractor extends Extractor
{

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class YahooExtractor extends Extractor
{

/**
* Return splitters
*
* @return array
*/
protected function getOriginalMessageSplitters()
{
return array_merge(parent::getOriginalMessageSplitters(), [
'/On(.*?)wrote\:(.*?)/is'
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace ActiveCollab\EmailReplyExtractor\Extractor;

/**
* @package ActiveCollab\EmailReplyExtractor\Extractor
*/
final class iOSExtractor extends Extractor
{

}

0 comments on commit e680cdb

Please sign in to comment.