<commit_before><?php
namespace IndieWeb\comments;

function truncateString($text, $length) {
  ob_start();
  $short = ellipsize_to_word($text, $length, '...', 10);
  ob_end_clean();
  return $short;
}

function truncate($text, $maxTextLength, $maxLines) {
  $lines = explode("\n", $text);
  $visibleLines = array_filter($lines);
  if(count($visibleLines) > $maxLines) {
    $newContent = array();
    $visibleLinesAdded = 0;
    $i = 0;
    while($visibleLinesAdded < $maxLines && $i < count($lines)) {
      $line = $lines[$i];
      $newContent[] = $line;
      if(trim($line) != '')
        $visibleLinesAdded++;
      $i++;
    }
    $text = implode("\n", $newContent);
    // Tack on extra chars and then tell cassis to ellipsize it shorter to take advantage of proper ellipsizing logic.
    // This is for when the full text is shorter than $maxTextLength but has more lines than $maxLines
    $text .= ' ....';
    $text = truncateString($text, min($maxTextLength, strlen($text)-1));
  } else {
    $text = truncateString($text, $maxTextLength);
  }
  return $text;
}

function removeScheme(&$url) {
  if(is_array($url)) {
    foreach($url as $i=>$u) {
      removeScheme($url[$i]);
    }
  } else {
    $url = preg_replace('/^https?/', '', $url);
  }
}

function parse($mf, $refURL=false, $maxTextLength=150, $maxLines=2) {
  // When parsing a comment, the $refURL is the URL being commented on.
  // This is used to check for an explicit in-reply-to property set to this URL.

  // Remove the scheme from the refURL and treat http and https links as the same
  removeScheme($refURL);

  $type = 'mention';
  $published = false;
  $name = false;
  $text = false;
  $url = false;
  $author = array(
    'name' => false,
    'photo' => false,
    'url' => false
  );
  $rsvp = null;

  if(array_key_exists('type', $mf) && in_array('h-entry', $mf['type']) && array_key_exists('properties', $mf)) {
    $properties = $mf['properties'];

    if(array_key_exists('author', $properties)) {
      $authorProperty = $properties['author'][0];
      if(is_array($authorProperty)) {

        if(array_key_exists('name', $authorProperty['properties'])) {
          $author['name'] = $authorProperty['properties']['name'][0];
        }

        if(array_key_exists('url', $authorProperty['properties'])) {
          $author['url'] = $authorProperty['properties']['url'][0];
        }

        if(array_key_exists('photo', $authorProperty['properties'])) {
          $author['photo'] = $authorProperty['properties']['photo'][0];
        }

      } elseif(is_string($authorProperty)) {
        $author['url'] = $authorProperty;
      }
    }

    if(array_key_exists('published', $properties)) {
      $published = $properties['published'][0];
    }

    if(array_key_exists('url', $properties)) {
      $url = $properties['url'][0];
    }

    // If the post has an explicit in-reply-to property, verify it matches $refURL and set the type to "reply"
    if($refURL && array_key_exists('in-reply-to', $properties)) {
      // in-reply-to may be a string or an h-cite
      foreach($properties['in-reply-to'] as $check) {
        removeScheme($check);
        if(is_string($check) && $check == $refURL) {
          $type = 'reply';
          continue;
        } elseif(is_array($check)) {
          if(array_key_exists('type', $check) && in_array('h-cite', $check['type'])) {
            if(array_key_exists('properties', $check) && array_key_exists('url', $check['properties'])) {
              if(in_array($refURL, $check['properties']['url'])) {
                $type = 'reply';
              }
            }
          }
        }
      }
    }

    // Check if the reply is an RSVP
    if(array_key_exists('rsvp', $properties)) {
      $rsvp = $properties['rsvp'][0];
      $type = 'rsvp';
    }

    // Check if this post is a "repost"
    if($refURL && array_key_exists('repost-of', $properties)) {
      removeScheme($properties['repost-of']);
      if(in_array($refURL, $properties['repost-of']))
        $type = 'repost';
    }

    // Also check for "u-repost" since some people are sending that. Probably "u-repost-of" will win out.
    if($refURL && array_key_exists('repost', $properties)) {
      removeScheme($properties['repost']);
      if(in_array($refURL, $properties['repost']))
        $type = 'repost';
    }

    // Check if this post is a "like"
    if($refURL && array_key_exists('like', $properties)) {
      removeScheme($properties['like']);
      if(in_array($refURL, $properties['like']))
        $type = 'like';
    }

    // From http://indiewebcamp.com/comments-presentation#How_to_display

    // If the entry has an e-content, and if the content is not too long, use that
    if(array_key_exists('content', $properties)) {
      $content = $properties['content'][0]['value'];
      $visibleLines = array_filter(explode("\n", $content));
      if(strlen($content) <= $maxTextLength && count($visibleLines) <= $maxLines) {
        $text = $content;
      }
    }<commit_msg>check for like-of<commit_after><?php
namespace IndieWeb\comments;

function truncateString($text, $length) {
  ob_start();
  $short = ellipsize_to_word($text, $length, '...', 10);
  ob_end_clean();
  return $short;
}

function truncate($text, $maxTextLength, $maxLines) {
  $lines = explode("\n", $text);
  $visibleLines = array_filter($lines);
  if(count($visibleLines) > $maxLines) {
    $newContent = array();
    $visibleLinesAdded = 0;
    $i = 0;
    while($visibleLinesAdded < $maxLines && $i < count($lines)) {
      $line = $lines[$i];
      $newContent[] = $line;
      if(trim($line) != '')
        $visibleLinesAdded++;
      $i++;
    }
    $text = implode("\n", $newContent);
    // Tack on extra chars and then tell cassis to ellipsize it shorter to take advantage of proper ellipsizing logic.
    // This is for when the full text is shorter than $maxTextLength but has more lines than $maxLines
    $text .= ' ....';
    $text = truncateString($text, min($maxTextLength, strlen($text)-1));
  } else {
    $text = truncateString($text, $maxTextLength);
  }
  return $text;
}

function removeScheme(&$url) {
  if(is_array($url)) {
    foreach($url as $i=>$u) {
      removeScheme($url[$i]);
    }
  } else {
    $url = preg_replace('/^https?/', '', $url);
  }
}

function parse($mf, $refURL=false, $maxTextLength=150, $maxLines=2) {
  // When parsing a comment, the $refURL is the URL being commented on.
  // This is used to check for an explicit in-reply-to property set to this URL.

  // Remove the scheme from the refURL and treat http and https links as the same
  removeScheme($refURL);

  $type = 'mention';
  $published = false;
  $name = false;
  $text = false;
  $url = false;
  $author = array(
    'name' => false,
    'photo' => false,
    'url' => false
  );
  $rsvp = null;

  if(array_key_exists('type', $mf) && in_array('h-entry', $mf['type']) && array_key_exists('properties', $mf)) {
    $properties = $mf['properties'];

    if(array_key_exists('author', $properties)) {
      $authorProperty = $properties['author'][0];
      if(is_array($authorProperty)) {

        if(array_key_exists('name', $authorProperty['properties'])) {
          $author['name'] = $authorProperty['properties']['name'][0];
        }

        if(array_key_exists('url', $authorProperty['properties'])) {
          $author['url'] = $authorProperty['properties']['url'][0];
        }

        if(array_key_exists('photo', $authorProperty['properties'])) {
          $author['photo'] = $authorProperty['properties']['photo'][0];
        }

      } elseif(is_string($authorProperty)) {
        $author['url'] = $authorProperty;
      }
    }

    if(array_key_exists('published', $properties)) {
      $published = $properties['published'][0];
    }

    if(array_key_exists('url', $properties)) {
      $url = $properties['url'][0];
    }

    // If the post has an explicit in-reply-to property, verify it matches $refURL and set the type to "reply"
    if($refURL && array_key_exists('in-reply-to', $properties)) {
      // in-reply-to may be a string or an h-cite
      foreach($properties['in-reply-to'] as $check) {
        removeScheme($check);
        if(is_string($check) && $check == $refURL) {
          $type = 'reply';
          continue;
        } elseif(is_array($check)) {
          if(array_key_exists('type', $check) && in_array('h-cite', $check['type'])) {
            if(array_key_exists('properties', $check) && array_key_exists('url', $check['properties'])) {
              if(in_array($refURL, $check['properties']['url'])) {
                $type = 'reply';
              }
            }
          }
        }
      }
    }

    // Check if the reply is an RSVP
    if(array_key_exists('rsvp', $properties)) {
      $rsvp = $properties['rsvp'][0];
      $type = 'rsvp';
    }

    // Check if this post is a "repost"
    if($refURL && array_key_exists('repost-of', $properties)) {
      removeScheme($properties['repost-of']);
      if(in_array($refURL, $properties['repost-of']))
        $type = 'repost';
    }

    // Also check for "u-repost" since some people are sending that. Probably "u-repost-of" will win out.
    if($refURL && array_key_exists('repost', $properties)) {
      removeScheme($properties['repost']);
      if(in_array($refURL, $properties['repost']))
        $type = 'repost';
    }

    if($refURL && array_key_exists('like-of', $properties)) {
      removeScheme($properties['like-of']);
      if(in_array($refURL, $properties['like-of']))
        $type = 'like';
    }

    // Check if this post is a "like"
    if($refURL && array_key_exists('like', $properties)) {
      removeScheme($properties['like']);
      if(in_array($refURL, $properties['like']))
        $type = 'like';
    }

    // From http://indiewebcamp.com/comments-presentation#How_to_display

    // If the entry has an e-content, and if the content is not too long, use that
    if(array_key_exists('content', $properties)) {
      $content = $properties['content'][0]['value'];
      $visibleLines = array_filter(explode("\n", $content));
      if(strlen($content) <= $maxTextLength && count($visibleLines) <= $maxLines) {
        $text = $content;
      }
    }
{'fraction_non_alphanumeric': 0.1767245608870572, 'fraction_numerical': 0.0033362771072514962, 'mean_word_length': 3.0573248407643314, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1043319', 'n_tokens_mistral': 3265, 'n_tokens_neox': 3226, 'n_words': 720}