From c763775047e9098aa2d438f396bb5bce83d18888 Mon Sep 17 00:00:00 2001 From: henoheno Date: Sun, 19 Aug 2007 23:08:47 +0900 Subject: [PATCH] get_source(): Returns FALSE if error occurerd. Cleanup. Remove redundant is_page() --- lib/file.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/file.php b/lib/file.php index 066d100..0b0b304 100644 --- a/lib/file.php +++ b/lib/file.php @@ -16,31 +16,43 @@ define('PKWK_MAXSHOW_CACHE', 'recent.dat'); define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat'); // Get source(wiki text) data of the page +// Returns FALSE if error occurerd function get_source($page = NULL, $lock = TRUE, $join = FALSE) { + //$result = NULL; // File is not found $result = $join ? '' : array(); + // Compat for "implode('', get_source($file))", + // -- this is slower than "get_source($file, TRUE, TRUE)" + // Compat for foreach(get_source($file) as $line) {} not to warns - if (is_page($page)) { - $path = get_filename($page); + $path = get_filename($page); + if (file_exists($path)) { if ($lock) { $fp = @fopen($path, 'r'); - if ($fp == FALSE) return $result; + if ($fp == FALSE) return FALSE; flock($fp, LOCK_SH); } if ($join) { // Returns a value $size = filesize($path); - if ($size > 0) { - $result = str_replace("\r", '', fread($fp, filesize($path))); + if ($size === FALSE) { + $result = FALSE; } else { - $result = ''; + $result = fread($fp, $size); + if ($result !== FALSE) { + // Removing line-feeds + $result = str_replace("\r", '', $result); + } } } else { // Returns an array - // Removing line-feeds: Because file() doesn't remove them. - $result = str_replace("\r", '', file($path)); + $result = file($path); + if ($result !== FALSE) { + // Removing line-feeds + $result = str_replace("\r", '', $result); + } } if ($lock) { -- 2.11.0