Преглед на файлове

Less explicit path building

Zachary Kotlarek преди 13 години
родител
ревизия
e0729445f0
променени са 4 файла, в които са добавени 95 реда и са изтрити 34 реда
  1. 15 9
      web/includes/display.php
  2. 40 0
      web/includes/episode.php
  3. 3 1
      web/includes/main.php
  4. 37 24
      web/includes/season.php

+ 15 - 9
web/includes/display.php

@@ -56,24 +56,30 @@ function printSeries($series) {
 		echo '<h2>Season Parameters</h2>';
 
 		# Existing seasons
-		echo '<p>';
+		echo '<dl>';
 		foreach ($seasons as $season => $monitored) {
 			$season_html = htmlspecialchars($season);
-			echo '<label>Season ' . $season_html . ' ';
-			echo '<input type="checkbox" value="1" name="season_' . $season_html . '" ';
+
+			$episodes = findEpisodes(seasonPath($series, $season));
+
+			echo '<dt>Season ' . $season_html . '</dt>';
+			echo '<dd><label>Monitored: <input type="checkbox" value="1" name="season_' . $season_html . '" ';
 			if ($monitored !== false) {
 				echo 'checked="checked"';
 			}
-			echo '/></label>';
-			echo '<input type="text" size="150" name="url_' . $season_html . '" value="';
+			echo '/></label><br/>';
+			echo '<label>Search URL: <input type="text" size="150" name="url_' . $season_html . '" value="';
 			if ($monitored !== false && $monitored !== true) {
 				echo htmlspecialchars($monitored);
 			}
-			echo '"/>';
-			echo '<input type="submit" name="season_del_' . $season_html . '" value="Delete">';
-			echo '<br/>';
+			echo '"/></label><br/>';
+			echo '<input type="submit" name="season_del_' . $season_html . '" value="Delete"><br/>';
+			echo 'Episode count: <br/>';
+			echo 'Last episode: <br/>';
+			echo 'TheTVDB episode count: <br/>';
+			echo '</dd>';
 		}
-		echo '</p>';
+		echo '</dl>';
 
 		# Add a season
 		$next_season = max(array_keys($seasons)) + 1;

+ 40 - 0
web/includes/episode.php

@@ -0,0 +1,40 @@
+<?
+
+require 'config.php';
+
+# Find all the episodes in a provided season folder
+function findEpisodes($season_path) {
+	$retval = array();
+
+	$dir = opendir($season_path);
+	if ($dir === FALSE) {
+		die('Unable to opendir(): ' . htmlspecialchars($season_path) . "\n");
+	}
+	while (false !== ($episode = readdir($dir))) {
+
+		# Skip junk
+		if (isJunk($episode)) {
+			continue;
+		}
+
+		# We only care about files
+		$episode_path = $path . '/' . $episode;
+		if (!is_file($episode_path)) {
+			continue;
+		}
+
+		# Record the episode number
+		if (preg_match('/^(?:S\d+E)?(\d+)\s*\-\s*\.\w\w\w$/i', $season, $matches)) {
+			$episode_num = intval($matches[1]);
+			$retval[ $episode_num ] = true;
+		}
+	}
+	closedir($dir);
+
+	# Sort numerically; the directory listing typically returns a lexicographic order
+	ksort($retval, SORT_NUMERIC);
+
+	return $retval;
+}
+
+?>

+ 3 - 1
web/includes/main.php

@@ -1,11 +1,13 @@
 <?
 
 require 'config.php';
-require_once 'util.php';
 
+require_once 'util.php';
 require_once 'webloc.php';
+
 require_once 'season.php';
 require_once 'series.php';
+require_once 'episode.php';
 
 require_once 'tvdb.php';
 require_once 'display.php';

+ 37 - 24
web/includes/season.php

@@ -2,33 +2,52 @@
 
 require 'config.php';
 
+function seasonExists($series, $season) {
+        return is_dir(seasonPath($series, $season));
+}
+
+function seasonPath($series, $season) {
+	return seriesPath($series) . '/Season ' . intval($season);
+}
+
 # True if the provided season folder is being monitored
-function isMonitored($season_path) {
-	if (file_exists(dirname($season_path) . '/skip')) {
+function isMonitored($series, $season) {
+
+	# Season 0 is never monitored
+	if (!$season) {
 		return false;
 	}
 
-	if (file_exists($season_path . '/season_done')) {
+	# Imaginary series are not monitored
+	if (!seriesExists($series)) {
 		return false;
 	}
 
-	$season = 0;
-	if (preg_match('/^Season\s+(\d+)$/i', basename($season_path), $matches)) {
-		$season = $matches[1];
+	# Imaginary seasons are not monitored
+	if (!seasonExists($series, $season)) {
+		return false;
 	}
-	if (!$season) {
+
+	# Series marked "skip" are not monitored
+	if (file_exists(seriesPath($series) . '/skip')) {
 		return false;
 	}
 
+	# Seasons marked done are not monitored
+	if (file_exists(seasonPath($series, $season) . '/season_done')) {
+		return false;
+	}
+
+	# The default behavior is to search
 	return true;
 }
 
 # Get the season search status, including the URL (if any) for the provided path
-function seasonSearch($season_path) {
-	$retval = isMonitored($season_path);
+function seasonSearch($series, $season) {
+	$retval = isMonitored($series, $season);
 
 	if ($retval) {
-		$file = $season_path . '/url';
+		$file = seasonPath($series, $season) . '/url';
 		if (is_readable($file)) {
 			$retval = trim(file_get_contents($file));
 		}
@@ -41,12 +60,6 @@ function seasonSearch($season_path) {
 function findSeasons($series) {
 	$retval = array();
 
-	# Check for the skip file
-	$skip = false;
-	if (file_exists($path . '/skip')) {
-		$skip = true;
-	}
-
 	$path = seriesPath($series);
 	$dir = opendir($path);
 	if ($dir === FALSE) {
@@ -67,7 +80,8 @@ function findSeasons($series) {
 
 		# Record the season number and search status
 		if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
-			$retval[ $matches[1] ] = seasonSearch($season_path);
+			$season_num = $matches[1];
+			$retval[ $season_num ] = seasonSearch($series, $season_num);
 		}
 	}
 	closedir($dir);
@@ -88,7 +102,7 @@ function saveSeasons($series, $data, $series_last, $seasons_last) {
 	# For each season
 	$seasons = findSeasons($series);
 	foreach ($seasons as $season => $status) {
-		$season_path = seriesPath($series) . '/Season ' . $season;
+		$season_path = seasonPath($series, $season);
 
 		$monitored = $data[ 'season_' . $season ];
 		$monitored_path = $season_path . '/season_done';
@@ -105,11 +119,11 @@ function saveSeasons($series, $data, $series_last, $seasons_last) {
 		$url = $data[ 'url_' . $season ];
 		$url_path = $season_path . '/url';
 		if ($url) {
-			if (isMonitored($season_path)) {
+			if (isMonitored($series, $season)) {
 				file_put_contents($url_path, $url . "\n");
 			}
 		} else {
-			if ($seasons_last[ $season ] && isMonitored($season_path)) {
+			if ($seasons_last[ $season ] && isMonitored($series, $season)) {
 				if (file_exists($url_path)) {
 					unlink($url_path);
 				}
@@ -130,7 +144,7 @@ function addSeason($series, $season) {
 	}
 
 	# Ensure the season does not exist
-	$season_path = seriesPath($series) . '/Season ' . $season;
+	$season_path = seasonPath($series, $season);
 	if (file_exists($season_path)) {
 		die('Invalid season: ' . htmlspecialchars($season) . "\n");
 	}
@@ -158,13 +172,12 @@ function delSeason($series, $season) {
 	}
 
 	# Ensure the season exists
-	$season_path = seriesPath($series) . '/Season ' . intval($season);
-	if (!file_exists($season_path)) {
+	if (!seasonExists($series, $season)) {
 		die('Invalid season: ' . htmlspecialchars($season) . "\n");
 	}
 
 	# Remove the directory (or fail silently)
-	@rmdir($season_path);
+	@rmdir(seasonPath($series, $season));
 }
 
 ?>