public static function process_scheduled() { /* detatch from invoking HTTP process so we can't be interrupted */ JxBotAsyncLoader::detatch_http_request(); /* ensure we are the only instance running this process */ try { if (!JxBotExclusion::get_exclusive()) { return; } } catch (Exception $err) { JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_ERROR, $err->getMessage(), ''); return; } /* iterate through all scheduled files */ while (true) { $stmt = JxBotDB::$db->prepare('SELECT name FROM file WHERE status = \'Scheduled\' ORDER BY name LIMIT 1'); $stmt->execute(); $next = $stmt->fetchAll(PDO::FETCH_NUM); if (count($next) == 0) { return; } /* we're done */ $file = $next[0][0]; /* flag the file to indicate we're processing it, and prevent a loop if something is amiss */ JxBotAsyncLoader::set_file_status($file, 'Loading'); /* does the file actually exist? */ $path = JxBotConfig::aiml_dir() . $file; if (!file_exists($path)) { JxBotAsyncLoader::set_file_status($file, 'Not Available'); continue; } /* run the AIML importer */ $importer = new JxBotAimlImport(); $result = $importer->import($path); // ! TODO: The notices, warnings and errors of this mechanism should be // sent to us via our log() method, not passed back in an array. ** /* check for errors and notices */ if (is_array($result)) { JxBotAsyncLoader::set_file_status($file, 'Loaded'); JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_NOTICE, 'Loaded.', $file); /* log the results */ foreach ($result as $notice) { JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_WARNING, $notice, $file); } } else { JxBotAsyncLoader::set_file_status($file, 'Load Error'); JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_ERROR, $result, $file); } } }
function server_file_list() { $dir = JxBotConfig::aiml_dir(); $list = array(); $index = array(); $stmt = JxBotDB::$db->prepare('SELECT id,name,status,last_update FROM file ORDER BY name'); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_NUM); foreach ($rows as $row) { $index[$row[1]] = count($list); $path = JxBotConfig::aiml_dir() . $row[1]; if (file_exists($path)) { $list[] = array($row[0], $row[1], $row[2]); } else { $stmt = JxBotDB::$db->prepare('DELETE FROM file WHERE id=?'); $stmt->execute(array($row[0])); } } $dh = opendir($dir); while (($file = readdir($dh)) !== false) { if (substr($file, 0, 1) == '.') { continue; } $info = pathinfo($file); if (!isset($info['extension'])) { continue; } $ext = strtolower($info['extension']); if ($ext != 'aiml') { continue; } if (!isset($index[$file])) { $list[] = array(null, $file, 'Not Loaded'); $stmt = JxBotDB::$db->prepare('INSERT INTO file (name, status) VALUES (?, ?)'); $stmt->execute(array($file, 'Not Loaded')); } } closedir($dh); return $list; }