Point 6 on the below page describes how data can be imported using a script:
http://innoexts.com/currency-pricing/#.UjI1uPlgaHo
The problem is the example provides a shell script so I get an error when trying to run this from a browser. Is there any way that these types of scripts can be run from as a browser as I don't have access to Shell?
I figured out the solution to this. Just needed to comment out the contents of protected function _validate() in /shell/abstract.php
In shell/abstract.php
add a line below
protected $_factory; // Line 76
as
protected $_passvalidation = false;
Then change in function __construct()
$this->_validate();
to
if(!$this->_passvalidation) $this->_validate();
In your shell script add
function __construct() {
$this->_passvalidation = true;
parent::__construct();
}
Related
I've seen numerous posts of running php files without extensions on the browser.My question is,is it possible to do the same when running a file in cli?Example scenario:I have a file called Test.php
that contains this simple code:
class Test
{
public function action()
{
global $argv;
$script = array_shift($argv);
print($argv[0]);
}
}
(new Test)->action();
Now on the terminal,instead of doing
$ php Test.php -calltoAction
I'd like to do:
$ php Test -calltoAction
and get calltoAction printed out.How do I accomplish this.
It is certainly possible. PHP CLI does not care about the file extensions.
Trying to pass argv to your function as a global is not the way to go. Pass it as a function argument instead.
class Test
{
public function action($args)
{
$script = array_shift($args);
print($args[0]);
}
}
(new Test)->action($argv);
~
This question has been asked a few times but none of them solves my issue.
I simply created a file:
/shell/test.php
<?php
require_once 'abstract.php';
class Mage_Shell_Test extends Mage_Shell_Abstract
{
public function run() {
echo 'in-shell-test!';
}
}
$shell = new Mage_Shell_Test();
$shell->run();
and commented out $this->_validate(); in
/shell/abstract.php
Then I still see the page of WHOOPS, OUR BAD...
Appreciate any ideas.
You could override the _validate method which is inherited from Mage_Shell_Abstract. This method checks where you are running your script from and it looks like this:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
die('This script cannot be run from Browser. This is the shell script.');
}
}
You can change this to return true, but this validation was obviously put there for a reason. If you plan on leaving the script there for a longer period, I would recommend looking for a different approach.
I want to run a function in a php script from the linux command line. The php script looks as follows:
<?php
namespace mycompany\admin;
class MyModel
{
public static function myMethod() {
echo 'something';
}
}
Normally I would do something like this: php thefile.php, but since the function is not called anywhere, it isn't run. I have no idea however, how I could call that function from the command line.
Anybody?
create a file run.php:
<?php
require 'thefile.php';
MyModel::myMethod();
While I would not recommend it, you could call it like this:
php -r "include('thefile.php');mycompany\admin\MyModel::MyMethod();"
You cannot run a PHP method from command line. You need to call it from a script. You could do this:
<?php
namespace mycompany\admin;
class MyModel
{
public static function myMethod() {
echo 'something';
}
}
MyModel::myMethod();
and run it from CLI like so:
php -r myscript.php
I'm using CodeIgniter for my project. I have this roadblock in from of me..
There's this method in my controller that I want to call every hour. Let's have that as:
class Notifs extends CI_Controller {
public function __construct() {
parent:: __construct();
// load stuff here
}
public function index() {
}
/* I want to call this function on an hourly basis */
public function check_overdue_stuffs() {
// do the checking here
}
}
I have absolutely no idea on how to implement this. I have tried using sleep($seconds) but that was just a mess.
Any ideas for a perfectly awesome way to do this? A verbose example would be great. Thanks!
This is pretty straight forward, but don't look to just keeping it in your PHP code. You need to use a cronjob script (linux) or windows equivalent (on the server where your software lives).
Here is a simple cron you would add to pull a specific cli method:
# hourly cron
0 * * * * php /www/ciWebsite/index.php [controller] [method] >/dev/null 2>&1
That way, it runs the controller + method you want and output is dumped into /dev/null
Try running /www/ciWebsite/index.php notifs check_overdue_stuff and see if it works (obviously update your path)
edit:
fixed an extra entry (I had cli = folder + controller + method)
Hi I want to execute a function via cronjob to start an csv import. At the moment the import is triggered by accessing a controller in the browser tld.de/Update
The controller has this code http://pastie.org/8351266
How can I execute the function init() via Cronjob?
Thx!
In SilverStripe you can access any route that is accessible via HTTP also by running cli-script.php in the command line
There also is sake which is just a bash wrapper around cli-script.php (but sake needs to be installed)
so, from your project directory, you can run both commands which will perform the same action (in this case, run a dev/build):
php framework/cli-script.php dev/build
sake dev/build
see the docs for commandline ussage of silverstripe: http://doc.silverstripe.org/framework/en/topics/commandline
the 2nd part of your question (how to call a method from a controller) is actually more a question of routing in silverstripe and has nothing to do with how it is called (cronjob)
I assume that your controller is a Page_Controller or a subclass of that (so bound to a SiteTree Model), then routing is done for you (it takes the URLs you set in the CMS).
so lets see some example code and lets assume you have a page with the URLSegment about:
class Page_Controller extends ContentController {
private static $allowed_actions = array('something');
public function init() {
// the init method will run before every action
// this means this code will run, no matter if you visit /about or /about/something
}
public function index() {
// this is the default action (this code is optional and can be removed),
// and will be called if you visit website.com/about
return $this;
}
public function something() {
// this is the somethingaction,
// and will be called if you visit website.com/about/something
// do something here
return $this;
}
}
you can then call run to get the result of the index():
php framework/cli-script.php about
and this to get the result of something():
php framework/cli-script.php about/something
NOTE: the init method itself is not accessable via URL, it is the "setup" that runs before an action
NOTE: all actions other than index() have to be allowed by adding them to $allowed_actions (also note that you need to ?flush=1 after adding to $allowed_actions to reload the config cache)
EDIT: this was actually the response to your first question, after seeing your code example, this addition:
for standalone controllers it works the same way, just that you have to define the routes, and make sure that you have $Action in the route so that something() can be called
You could do this without Silverstripe sake. Install curl and call the URL via a cronjob, ie:
0 0 * * * curl --silent http://tld.de/Update
The proper way to do this would be to write a Silverstripe task, and invoke your controller from within the task. I haven't tested this code but it would go something like this:
class YourTask extends BuildTask {
public $description = "...";
//...
public function run($request) {
YourController::init();
}
}
You can invoke it over sake using:
0 0 * * * /path/to/framework/sake dev/tasks/YourTask
why not create a build task ? which is specially designed for such requirements (at-least that's how I consider build tasks)
<?php
class ArticleCsvUpdateTask extends BuildTask {
protected $title = 'Article Csv Update';
protected $description = 'Build task for article Csv update';
public function run($request) {
$loader = new ArticleCsvBulkLoader('Color');
if($loader->load('import-new.csv')) {
$loader->load('import-new.csv');
}
}
}
Which can be assess both from browser using "yoursite/dev/tasks/ArticleCsvUpdateTask" and from command line using "php framework/cli-script.php dev/tasks/ArticleCsvUpdateTask" OR using "sake dev/tasks/ArticleCsvUpdateTask" (if you have sake installed).
May be I am not getting your exact requirement but I believe this is much cleaner and nicer way of running a cron job with silverstripe.
See Zauberfisch's answer for a complete solution
I'm not familiar with Silverstripe, but if I understand correctly, this controller init function can be called with a HTTP request.
As the silverstripe docs say, you can call any url from the command line:
php framework/cli-script.php Update/init
More information is available here, and consider using sake for this task.
I think the right way do this is create a php file console like:
#!/usr/bin/env php
<?php
require_once "/path/to/your/class/Update.php";
$class = new Update();
$class->init();
Add right perms to this file
chmod 755 consolefile
And finally run this script with cronjob