urlScrapeID = $row['UrlPostUrlScrapeID']; parent::__construct($row); } /************************************************************************** * CRUD Functions **************************************************************************/ public static function create($userID, $languageID, $text, $scrapeID) { $newPostID = parent::create($userID, $languageID, $text); safe_dbwrite(" UPDATE tig.ProfilePosts SET UrlPostUrlScrapeID = $scrapeID WHERE ID = $newPostID "); return $newPostID; } public static function readByScrapeIDs($scrapeIDs) { if (!is_array($scrapeIDs)) { throw new \InvalidArgumentException("Scrape IDs must be an array."); } if (count($scrapeIDs) === 0) { return array(); } $posts = array(); $rows = safe_dbread(" SELECT * FROM tig.ProfilePosts WHERE UrlPostUrlScrapeID IN (" . implode(',', $scrapeIDs) . ") "); while ($row = safe_fetch_assoc($rows)) { $posts[] = new self($row); } return $posts; } /************************************************************************** * Getters \ Setters \ Checkers **************************************************************************/ public function getUrlScrapeID() { return $this->urlScrapeID; } public function getUrlScrape() { return UrlScrape::readByID($this->urlScrapeID); } /************************************************************************** * Others **************************************************************************/ public static function detectUrl($text) { // This is the PHP equivalent of the Javascript regex that Mike implemented. $regex = '/[-a-zA-Z0-9@:%_\+.~#?&\/=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/=]*)?/i'; if (!preg_match($regex, $text, $matches)) { return false; } return $matches[0]; } /************************************************************************** * Required by Post **************************************************************************/ public function getHtmlizedText() { $escapedText = htmlspecialchars($this->text); $scrape = $this->getUrlScrape(); $escapedUrl = htmlspecialchars($scrape->getUrl()); $link = '' . $escapedUrl . ''; return str_replace($escapedUrl, $link, $escapedText); } public function isRepostableByUser($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } // Users cannot repost their own post if ($this->userID == $userID) { return false; } // Users cannot repost on URLs they've already posted about. If they // are able to do this, they would show up multiple times in the Global // Perspectives listing. $relatedPosts = $this->getRelatedPosts(); foreach ($relatedPosts as $post) { if ($post->getUserID() == $userID) { return false; } } return true; } public function repost($userID, $languageID, $text) { return UrlRepost::create($userID, $languageID, $text, $this); } public function getRelatedPosts() { // Get All Scrape's Related to this Post's URL $url = $this->getUrlScrape()->getCanonicalUrl(); $scrapesOfUrl = UrlScrape::readByCanonicalUrl($url); $scrapeIDs = array(); foreach ($scrapesOfUrl as $scrape) { $scrapeIDs[] = $scrape->getID(); } // Exclude this Post $candidateRelatedPosts = UrlPost::readByScrapeIDs($scrapeIDs); $relatedPosts = array(); foreach ($candidateRelatedPosts as $post) { if ($post->getID() != $this->id) { $relatedPosts[] = $post; } } return $relatedPosts; } }