Example #1
0
 public static function Update()
 {
     static::Init();
     if (count(self::$queue) > 0) {
         $query = 'SELECT "posts".*,GROUP_CONCAT("postgroup"."groupid") AS "groups" FROM "posts" LEFT JOIN "postgroup" ON ("postgroup"."postid" = "posts"."id") ' . 'WHERE "posts"."id" IN (' . implode(',', self::$queue) . ') GROUP BY "posts"."id"';
         $rs = static::$conn->Execute($query);
         while ($row = $rs->Fetch()) {
             $status = true;
             // If last article was posted in the past hour and not yet complete = DELETE
             if ($row['post_date'] > time() - 3600 && $row['parts_found'] != $row['parts_total']) {
                 $status = false;
             }
             // If last article was posted in the past 10 minutes then post may not yet be complete = DELETE
             if ($row['post_date'] > time() - 600) {
                 $status = false;
             }
             // If hidden = DELETE
             if ($row['hidden']) {
                 $status = false;
             }
             // If less than 80% complete = DELETE
             if ($row['parts_found'] < (double) $row['parts_total'] * 0.8) {
                 $status = false;
             }
             // If just a single file
             if ($row['files'] <= 1) {
                 $status = false;
             }
             // If to small = DELETE
             if ($row['size'] < 1048576) {
                 $status = false;
             }
             // If only PAR or PAR2 = DELETE
             if (!empty($row['stats'])) {
                 parse_str($row['stats'], $files);
                 if (isset($files['par'])) {
                     unset($files['par']);
                 }
                 if (isset($files['par2'])) {
                     unset($files['par2']);
                 }
                 if (count($files) == 0) {
                     $status = false;
                 }
             }
             if ($status) {
                 // Add or update
                 $groups = explode(',', $row['groups']);
                 $cats = array();
                 foreach ($groups as $group) {
                     $cats[$GLOBALS['groupcat'][$group]] = $GLOBALS['groupcat'][$group];
                 }
                 arsort($cats);
                 // Latter categories are more important; Anime = least specific category
                 $set_primary = true;
                 // Last category (first in the array) will be set to primary
                 foreach ($cats as $cat) {
                     try {
                         $postcat = PostCat::Find(null, 'SELECT * FROM "postcat" WHERE ("postid" = ?) AND ("catid" = ?)', $row['id'], $cat);
                     } catch (ActiveRecord_NotFoundException $e) {
                         $postcat = new PostCat();
                     }
                     $postcat->catid = $cat;
                     $postcat->postid = $row['id'];
                     $postcat->primarycat = (int) $set_primary;
                     $postcat->post_date = $row['post_date'];
                     $postcat->updated = time();
                     $postcat->Save();
                     unset($postcat);
                     if ($set_primary) {
                         // Disable flag to mark only first as primary
                         $set_primary = false;
                     }
                 }
             } else {
                 static::$conn->Execute('DELETE FROM "postcat" WHERE "postid" = ?', $row['id']);
                 // Remove cached NZB file too
                 try {
                     if (file_exists(Post::NZBFile($row['id']))) {
                         unlink(Post::NZBFile($row['id']));
                     }
                 } catch (Exception $e) {
                     // Ignore
                 }
             }
         }
     }
 }