Skip to content

Commit

Permalink
Merge pull request #607 from takeit/console_commands
Browse files Browse the repository at this point in the history
[CS-5270] - Merge cron jobs tasks to console commands
  • Loading branch information
Paweł Mikołajczuk committed Apr 7, 2014
2 parents b8a60cc + fad4107 commit 24568d6
Show file tree
Hide file tree
Showing 49 changed files with 1,261 additions and 836 deletions.
Empty file modified features/bootstrap/FeatureContext.php
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions newscoop/application/console
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ $cli->addCommands(array(
new \Newscoop\Tools\Console\Command\InstallNewscoopCommand(),
new \Newscoop\Tools\Console\Command\CreateOAuthClientCommand(),
new \Newscoop\Tools\Console\Command\GenerateORMSchemaCommand(),
new \Newscoop\Tools\Console\Command\AutopublishCommand(),
new \Newscoop\Tools\Console\Command\ClearOldStatisticsCommand(),
new \Newscoop\Tools\Console\Command\IndexerCommand(),
new \Newscoop\Tools\Console\Command\EventsNotifierCommand(),
new \Newscoop\Tools\Console\Command\SubscriptionsNotifierCommand(),
));

$cli->run($input);
Empty file.
32 changes: 0 additions & 32 deletions newscoop/bin/cli_script_lib.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
<?php

function camp_is_readable($p_fileName)
{
if (!is_readable($p_fileName)) {
echo "\nThis script requires access to the file $p_fileName.\n";
echo "Please run this script as a user with appropriate privileges.\n";
echo "Most often this user is 'root'.\n\n";
return false;
}
return true;
} // fn camp_is_readable


/**
* Execute a command in the shell.
*
Expand Down Expand Up @@ -55,26 +43,6 @@ function camp_readline()
return $in;
} // fn camp_readline


/**
* Create a directory. If this fails, print out the given error
* message or a default one.
*
* @param string $p_dirName
* @param string $p_msg
* @return void
*/
function camp_create_dir($p_dirName, $p_msg = "")
{
if ($p_msg == "") {
$p_msg = "Unable to create directory $p_dirName.";
}
if (!is_dir($p_dirName) && !mkdir($p_dirName)) {
camp_exit_with_error($p_msg);
}
} // fn camp_create_dir


/**
* @return boolean
*/
Expand Down
244 changes: 1 addition & 243 deletions newscoop/bin/events-notifier
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,245 +1,3 @@
#!/usr/bin/env php
<?php

global $CAMPSITE_DIR;

require_once __DIR__ . '/../application.php';

$application->bootstrap();
$GLOBALS['g_campsiteDir'] = $CAMPSITE_DIR = realpath(dirname(dirname(__FILE__)));

if (!defined('WWW_DIR')) {
define('WWW_DIR', $GLOBALS['g_campsiteDir']);
}

require_once __DIR__ . '/cli_script_lib.php';

$ETC_DIR = __DIR__ . '/../conf';

if (!camp_is_readable("$ETC_DIR/install_conf.php")) {
exit;
}

// includes installation configuration file
require_once("$ETC_DIR/install_conf.php");

if (!is_file("$ETC_DIR/database_conf.php")) {
echo "\n";
echo "Database configuration file is missed!\n";
echo "\n";
exit;
}

// includes campsite initialisation
require_once("$CAMPSITE_DIR/include/campsite_init.php");

// defines the notifier template file name
define('NOTIFIER_TEMPLATE', '_events_notifier.tpl');

// connects to database server
if (db_server_connect() == false) {
msg_error('Connecting to DB server');
exit;
}

// sets the array which holds message data
$message = array();

// reads reply address
$message['reply'] = get_reply_address();

// reads events
$tstamp = get_event_timestamp();

$sql_query = "SELECT u.Name AS UserRealName, u.EMail AS UserEMail, "
. "u.UName AS UserName, e.Name AS EventName, l.text AS LogText, "
. "l.time_created AS LogTimeCreated, NOW() AS RightNow "
. "FROM Log AS l, liveuser_users AS u, Events AS e "
. "WHERE l.fk_event_id = e.Id AND e.Notify = 'Y' AND l.fk_user_id = u.Id "
. "AND l.time_created > '" . $tstamp . "' "
. "ORDER BY l.time_created ASC";
if (!$result = mysql_query($sql_query)) {
msg_error('Reading log timestamp');
exit;
}

$i = 0;
$num_rows = mysql_num_rows($result);

// if events log exists, inits smarty template system
if ($num_rows > 0) {
$tpl = init_smarty();
$recipients = get_users_to_notify();
}

$last_tstamp = 0;
while ($i < $num_rows && $row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$last_tstamp = $row['LogTimeCreated'];

// assigns values to smarty template variables
$tpl->assign('user_real_name', $row['UserRealName']);
$tpl->assign('user_name', $row['UserName']);
$tpl->assign('user_email', $row['UserEMail']);
$tpl->assign('event_text', $row['LogText']);
$tpl->assign('event_timestamp', $row['LogTimeCreated']);

// sets the message body text
$message['text'] = '';
$message['text'] = $tpl->fetch(NOTIFIER_TEMPLATE);

// reads users emails to notify to
if (sizeof($recipients) <= 0) {
continue;
}

// sets message recipients and subject
$message['recipients'] = $recipients;
$message['subject'] = $row['EventName'];

// sends email message
send_email($message);
$i++;
}

if ($last_tstamp != 0) {
$sql_query = "UPDATE AutoId SET LogTStamp = '" . $last_tstamp . "'";
mysql_query($sql_query);
}


/**
* Reads reply address
*
* @return string
*/
function get_reply_address()
{
$sql_query = "SELECT EMail FROM liveuser_users WHERE UName = 'admin'";
if (!$result = mysql_query($sql_query)) {
return;
}

if (mysql_num_rows($result) <= 0) {
return;
}

$row = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);

return $row['EMail'];
} // fn get_reply_address


/**
* @return string
*/
function get_event_timestamp()
{
$sql_query = 'SELECT LogTStamp FROM AutoId';
if (!$result = mysql_query($sql_query)) {
msg_error('Getting logtstamp');
exit;
}

if (mysql_num_rows($result) <= 0) {
msg_error('There is no logtstamp');
exit;
}

$row = mysql_fetch_array($result, MYSQL_ASSOC);
mysql_free_result($result);

return $row['LogTStamp'];
} // fn get_event


/**
* Get user emails to notify
*
* @return array $users
* An array with users emails to notify to
*/
function get_users_to_notify()
{
global $application;
$container = $application->getBootstrap()->getResource('container');
return $container->getService('notification')->findRecipients();
}

/**
* @return boolean
* true on success, false on failure
*/
function send_email($p_message)
{
if (!is_array($p_message) || empty($p_message)) {
return false;
}

$mail = new Zend_Mail('utf-8');
$mail->addTo($p_message['recipients']);
$mail->setSubject($p_message['subject']);
$mail->setBodyText($p_message['text']);

if (!empty($p_message['reply'])) {
$mail->setReplyTo($p_message['reply']);
}

return $mail->send();
} // fn send_email


/**
* @return object $tpl
* Smarty object
*/
function init_smarty()
{
global $CAMPSITE_DIR;

// instantiates smarty template system
$tpl = new Smarty();

// inits smarty configuration settings
$tpl->left_delimiter = '{{';
$tpl->right_delimiter = '}}';
$tpl->force_compile = true;
$tpl->config_dir = $CAMPSITE_DIR.'/include/smarty/configs';
$tpl->template_dir = $CAMPSITE_DIR.'/themes/system_templates';
$tpl->compile_dir = $CAMPSITE_DIR.'/cache';
$tpl->auto_literal = false;

return $tpl;
} // fn init_smarty


/**
* @return boolean
*/
function db_server_connect()
{
global $Campsite;

$db_host = $Campsite['DATABASE_SERVER_ADDRESS']
.':'.$Campsite['DATABASE_SERVER_PORT'];
$db_user = $Campsite['DATABASE_USER'];
$db_pass = $Campsite['DATABASE_PASSWORD'];
$db_name = $Campsite['DATABASE_NAME'];
$link = mysql_connect($db_host, $db_user, $db_pass);
if (!$link) {
return false;
}

mysql_select_db($db_name, $link);
return mysql_query("SET NAMES 'utf8'");
} // fn db_server_connect


/**
* @param string $p_msg
*/
function msg_error($p_msg)
{
print('ERROR: ' . $p_msg);
} // fn msg_error

system(__DIR__ . '/../application/console newscoop:notifier:events');
16 changes: 1 addition & 15 deletions newscoop/bin/newscoop-autopublish
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#!/usr/bin/env php
<?php

require_once dirname(__FILE__) . '/newscoop_bootstrap.php';

require_once WWW_DIR . '/include/campsite_init.php';
require_once WWW_DIR . '/classes/ArticlePublish.php';
require_once WWW_DIR . '/classes/IssuePublish.php';

// fill in HTTP_HOST to avoid notices in campsite_constants.php
$_SERVER['HTTP_HOST'] = '';
$issueActions = IssuePublish::DoPendingActions();
$articleActions = ArticlePublish::DoPendingActions();

if ($issueActions > 0 || $articleActions > 0) {
fopen(WWW_DIR.'/reset_cache', 'w');
}
system(__DIR__ . '/../application/console newscoop:autopublish');
13 changes: 11 additions & 2 deletions newscoop/bin/newscoop-backup
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env php
<?php

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Newscoop\Services\FilesystemService;

require_once dirname(__FILE__) . '/newscoop_bootstrap.php';

if (empty($GLOBALS['argv']) && empty($options)) {
Expand All @@ -14,7 +18,7 @@ $ETC_DIR = $CAMPSITE_DIR . '/conf';

require_once("cli_script_lib.php");

if (!camp_is_readable("$ETC_DIR/install_conf.php")) {
if (!FilesystemService::isReadable("$ETC_DIR/install_conf.php")) {
exit(1);
}

Expand Down Expand Up @@ -109,7 +113,12 @@ if (!$silent) {
}

if (!file_exists($backupDirFullPath)) {
camp_create_dir($backupDirFullPath, $adviceOnError);
try {
$filesystem = new Filesystem();
$filesystem->mkdir($backupDirFullPath);
} catch(IOException $e) {
throw new IOException($adviceOnError);
}
}

$tmpVersion = preg_split('/ /', $Campsite["VERSION"]);
Expand Down
Loading

0 comments on commit 24568d6

Please sign in to comment.