* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-# Abort if called from a web server
+// Abort if called from a web server
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
require_once INSTALLDIR.'/scripts/commandline.inc';
-if (function_exists('posix_isatty')) {
- define('CONSOLE_INTERACTIVE', posix_isatty(0));
-} else {
- // Windows? Assume we're on a terminal. :P
- define('CONSOLE_INTERACTIVE', false);
-}
-if (CONSOLE_INTERACTIVE) {
- define('CONSOLE_READLINE', function_exists('readline'));
-}
+// Assume we're on a terminal if on Windows, otherwise posix_isatty tells us.
+define('CONSOLE_INTERACTIVE', !function_exists('posix_isatty') || posix_isatty(0));
+define('CONSOLE_READLINE', CONSOLE_INTERACTIVE && function_exists('readline'));
-if (CONSOLE_READLINE && CONSOLE_INTERACTIVE && file_exists(CONSOLE_HISTORY)) {
- define(CONSOLE_HISTORY, getenv("HOME") . "/.statusnet_console_history");
- readline_read_history(CONSOLE_HISTORY);
+if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
+ define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history");
+ if (file_exists(CONSOLE_HISTORY)) {
+ readline_read_history(CONSOLE_HISTORY);
+ }
}
function read_input_line($prompt)
if (CONSOLE_INTERACTIVE) {
if (CONSOLE_READLINE) {
$line = readline($prompt);
- readline_add_history($line);
+ if (trim($line) != '') {
+ readline_add_history($line);
+ if (defined('CONSOLE_HISTORY')) {
+ // Save often; it's easy to hit fatal errors.
+ readline_write_history(CONSOLE_HISTORY);
+ }
+ }
return $line;
} else {
return readline_emulation($prompt);
*/
function readline_emulation($prompt)
{
- if(file_exists(trim(shell_exec('which bash')))) {
+ if(CONSOLE_INTERACTIVE && file_exists(trim(shell_exec('which bash')))) {
$encPrompt = escapeshellarg($prompt);
$command = "read -er -p $encPrompt && echo \"\$REPLY\"";
$encCommand = escapeshellarg($command);
if (feof(STDIN)) {
return false;
}
- print $prompt;
+ if (CONSOLE_INTERACTIVE) {
+ print $prompt;
+ }
return fgets(STDIN);
}
function console_help()
{
- print "Welcome to StatusNet's interactive PHP console!\n";
- print "Type some PHP code and it'll run!\n";
+ print "Welcome to GNU social's interactive PHP console!\n";
+ print "Type some PHP code and it'll execute...\n";
print "\n";
- print "Note that PHP is cranky and you can easily kill your session.\n";
+ print "Hint: return a value of any type to output it via var_export():\n";
+ print " \$profile = new Profile();\n";
+ print " \$profile->find();\n";
+ print " \$profile->fetch();\n";
+ print " return \$profile;\n";
+ print "\n";
+ print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
+ print "\n";
+ print "Type ctrl+D or enter 'exit' to exit.\n";
}
-
-$prompt = common_config('site', 'name') . '> ';
+if (CONSOLE_INTERACTIVE) {
+ print "GNU social interactive PHP console... type ctrl+D or enter 'exit' to exit.\n";
+ $prompt = common_slugify(common_config('site', 'name')) . '> ';
+} else {
+ $prompt = '';
+}
while (!feof(STDIN)) {
$line = read_input_line($prompt);
if ($line === false) {
- print "\n";
+ if (CONSOLE_INTERACTIVE) {
+ print "\n";
+ }
break;
} elseif ($line !== '') {
try {
- if ($line == 'exit') {
+ if (trim($line) == 'exit') {
break;
- } elseif ($line == 'help') {
+ } elseif (trim($line) == 'help') {
console_help();
continue;
}
- // Let's do this
+ // Let's do this!
$result = eval($line);
if ($result === false) {
// parse error
print get_class($e) . ": " . $e->getMessage() . "\n";
}
}
- print "\n";
-}
-
-if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
- @readline_write_history(CONSOLE_HISTORY);
+ if (CONSOLE_INTERACTIVE) {
+ print "\n";
+ }
}