OSDN Git Service

get_source(): Returns FALSE if error occurerd. Cleanup. Remove redundant is_page()
authorhenoheno <henoheno>
Sun, 19 Aug 2007 14:08:47 +0000 (23:08 +0900)
committerumorigu <umorigu@gmail.com>
Sun, 30 Nov 2014 00:39:28 +0000 (09:39 +0900)
lib/file.php

index 066d100..0b0b304 100644 (file)
@@ -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) {