I am trying to just simply write "hello world" to a file, from a cakephp shell, with plans to eventually write a sitemap.xml file using our product models. I found this: Question which got me started...
But i am thinking either ConsoleOutput is not supported in Cake 1.3.6 (which i'm using), or i need to include the class that holds it.
The error i get when trying to run the file from terminal:
PHP Fatal error: Class 'ConsoleOutput' not found in /public/vendors/shells/sitemap.php on line 7
Here is my code:
class SitemapShell extends Shell {
public function __construct($stdout = null, $stderr = null, $stdin = null) {
// This will cause all Shell outputs, eg. from $this->out(), to be written to
// TMP.'shell.out'
$stdout = new ConsoleOutput('file://'.TMP.'shell.out');
// You can do the same for stderr too if you wish
// $stderr = new ConsoleOutput('file://'.TMP.'shell.err');
parent::__construct($stdout, $stderr, $stdin);
}
public function main() {
// The following output will not be printed on your console
// but be written to TMP.'shell.out'
$this->out('Hello world');
}
}
You are correct that ConsoleOutput didn't feature in CakePHP 1.3 - can you upgrade to a version 2.*?
If not you could just use regular PHP:
$fp = fopen('hello.txt', 'w');
fwrite($fp, 'hello world');
fclose($fp);
Hope this helps.
Toby
Related
The goal is to create a session context via the PHP V2 SDK like this:
$session = $this->contextsClient->sessionName($this->projectId, $this->sessionId);
$contextName = $this->contextsClient->contextName($this->projectId, $this->sessionId, 'test-context-name');
$context = new Context();
$context->setName($contextName);
$context->setLifespanCount(2);
$context->setParameters(["test-param-key" => "test-param-value"]);
return $this->contextsClient->createContext($session, $context);
The code works fine without the $context->setParameters(["test-param-key" => "test-param-value"]); part. I need to add parameters to the context though.
The error I get is:
Exception {#3554
#message: "Expect message.",
#file: "/home/vagrant/code/vendor/google/protobuf/php/src/Google/Protobuf/Internal/GPBUtil.php",
#line: 197,
}
I followed the errors trail and the problem is Google's code in line 197:
public static function checkMessage(&$var, $klass)
{
if (!$var instanceof $klass && !is_null($var)) {
throw new \Exception("Expect message.");
}
}
is trying to assert if the array passed to the setParameters function is an instance of \Google\Protobuf\Struct class in this snippet right here
public function setParameters($var)
{
GPBUtil::checkMessage($var, \Google\Protobuf\Struct::class);
$this->parameters = $var;
return $this;
}
I would be really glad if someone could help me. I spent a lot of hours trying to figure this out and nothing yet
As the error message states, the parameters need to be of type \Google\Protobuf\Struct. Also, each of the values need to be of type \Google\Protobuf\Value. For your particular example, you could do the following:
$paramValue = new \Google\Protobuf\Value();
$paramValue->setStringValue("test-param-value");
$parameters = new \Google\Protobuf\Struct();
$parameters->setFields(["test-param-key" => $paramValue]);
$context->setParameters($parameters);
You can look the implementation of these two classes here:
https://github.com/google/protobuf/blob/master/php/src/Google/Protobuf/Value.php
https://github.com/google/protobuf/blob/master/php/src/Google/Protobuf/Struct.php
For time-consuming tasks (email sending, image manipulation… you get the point), I want to run asynchronous PHP tasks.
It is quite easy on Linux, but I'm looking for a method that works on Windows too.
I want it to be simple, as it should be. No artillery, no SQL queueing, no again and again installing stuff… I just want to run a goddamn asynchronous task.
So I tried the Symfony Process Component.
Problem is, running the task synchronously works fine, but when running it asynchronously it exits along the main script.
Is there a way to fix this?
composer require symfony/process
index.php
<?php
require './bootstrap.php';
$logFile = './log.txt';
file_put_contents($logFile, '');
append($logFile, 'script (A) : '.timestamp());
$process = new Process('php subscript.php');
$process->start(); // async, subscript exits prematurely…
//$process->run(); // sync, works fine
append($logFile, 'script (B) : '.timestamp());
subscript.php
<?php
require './bootstrap.php';
$logFile = './log.txt';
//ignore_user_abort(true); // doesn't solve issue…
append($logFile, 'subscript (A) : '.timestamp());
sleep(2);
append($logFile, 'subscript (B) : '.timestamp());
bootstrap.php
<?php
require './vendor/autoload.php';
class_alias('Symfony\Component\Process\Process', 'Process');
function append($file, $content) {
file_put_contents($file, $content."\n", FILE_APPEND);
}
function timestamp() {
list($usec, $sec) = explode(' ', microtime());
return date('H:i:s', $sec) . ' ' . sprintf('%03d', floor($usec * 1000));
}
result
script (A) : 02:36:10 491
script (B) : 02:36:10 511
subscript (A) : 02:36:10 581
// subscript (B) is missing
Main script must be waiting when async process will be completed. Try this code:
$process = new Process('php subscript.php');
$process->start();
do {
$process->checkTimeout();
} while ($process->isRunning() && (sleep(1) !== false));
if (!$process->isSuccessful()) {
throw new \Exception($process->getErrorOutput());
}
If php supports fpm for Windows, you can listen to kernel.terminate event to provide all expensive tasks after response has been sent.
Service:
app.some_listener:
class: SomeBundle\EventListener\SomeListener
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }
Listener:
<?php
namespace SomeBundle\EventListener;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
class SomeListener
{
public function onKernelTerminate(PostResponseEvent $event)
{
// provide time consuming tasks here
}
}
Not the best solution, but:
$process = new Process('nohup php subscript.php &');
$process->start();
I just moved to another server and cannot reindex with Magmi, I receive the error below:
This script cannot be run from Browser. This is the shell script.
Thanks!
This error occurs when you run Magmi from the browser, because Magmi runs the indexer using shell_exec command, and the $_SERVER['REQUEST_METHOD'] doesn't get unset.
You can try one of two things.
Method 1. Unset the $_SERVER['REQUEST_METHOD'] variable Magento uses to check if the shell file is being run from the browser.
To do this, open magmi/plugins/base/general/reindex/magmi_reindexing_plugin.php
Find:
public function updateIndexes()
{
At the top of the updateIndexes() function, add the following:
if(isset($_SERVER['REQUEST_METHOD']))
{
unset($_SERVER['REQUEST_METHOD']);
}
So it will look like this:
public function updateIndexes()
{
if(isset($_SERVER['REQUEST_METHOD']))
{
unset($_SERVER['REQUEST_METHOD']);
}
Method 2: Modify the _validate() function in [magento_root]/shell/abstract.php
Open [magento_root]/shell/abstract.php
Find:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
die('This script cannot be run from Browser. This is the shell script.');
}
}
Replace with:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
//die('This script cannot be run from Browser. This is the shell script.');
}
}
I have some question related with Magento's free extension OnePica ImageCdn.
A broken image appear in frontend when I uploaded "corrupt image".
Ok, let's start the long story:
I notice it is happened because of ImageCdn extension and "corrupt image".
In some part of ImageCdn's code:
OnePica_ImageCdn_Helper_Image
/**
* In older versions of Magento (<1.1.3) this method was used to get an image URL.
* However, 1.1.3 now uses the getUrl() method in the product > image model. This code
* was added for backwards compatibility.
*
* #return string
*/
public function __toString()
{
parent::__toString();
return $this->_getModel()->getUrl();
}
My question is, anybody know what is the purpose of that code?
I don't understand what is the meaning of their comment above.
I think it is a bug as it always return $this->_getModel()->getUrl();
Is is really a bug or it is just my wrong interpretation?
This is what I've done so far:
I have an image dummy.jpeg
After some investigation, I just realized that is a "corrupt image".
I tested using: <?php print_r(getimagesize('dummy.jpeg')); ?>
Result:
Array
(
[0] => 200
[1] => 200
[2] => 6
[3] => width="200" height="200"
[bits] => 24
[mime] => image/x-ms-bmp
)
Of course I was surprised by the result because it looks good when I open it using Preview (on Mac OSX)
Then I open it using hex editor, the first two bytes is : BM which is BMP's identifier
I tried to upload .bmp image for product -> failed, can not select the image
I asked my colleague to upload it too (on Ubuntu), he was able to change the choices for file type into "any files". When he click "Upload Files", error message shown state that that type of file is not allowed.
What crossed on my mind is: an admin tried to upload .bmp image and failed. Then he rename it into .jpeg and successful. Though I don't get it what kind of images can be renamed without showing broken image logo (out of topic).
Those scenarios trigger an Exception, I'll break down what I've traced.
Trace of the codes:
app/design/frontend/base/default/catalog/product/view/media.phtml
<?php
$_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
echo $_helper->productAttribute($_product, $_img, 'image');
?>
From that code, I know that image url is generated using: $this->helper('catalog/image')->init($_product, 'image')
I did Mage::log((string)$this->helper('catalog/image')->init($_product, 'image'));
Result:
http://local.m.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/d/u/dummy.jpeg
.
Mage_Catalog_Helper_Image
public function __toString()
{
try {
if( $this->getImageFile() ) {
$this->_getModel()->setBaseFile( $this->getImageFile() );
} else {
$this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
}
if( $this->_getModel()->isCached() ) {
return $this->_getModel()->getUrl();
} else {
if( $this->_scheduleRotate ) {
$this->_getModel()->rotate( $this->getAngle() );
}
if ($this->_scheduleResize) {
$this->_getModel()->resize();
}
if( $this->getWatermark() ) {
$this->_getModel()->setWatermark($this->getWatermark());
}
Mage::log('pass');
$url = $this->_getModel()->saveFile()->getUrl();
Mage::log('not pass');
}
} catch( Exception $e ) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
The error triggered in $this->_getModel()->saveFile()->getUrl(). In some part of the code, it will eventually reach:
Varien_Image_Adapter_Gd2
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
{
if (null === $fileType) {
$fileType = $this->_fileType;
}
if (empty(self::$_callbacks[$fileType])) {
//reach this line -> exception thrown
throw new Exception($unsupportedText);
}
if (empty(self::$_callbacks[$fileType][$callbackType])) {
throw new Exception('Callback not found.');
}
return self::$_callbacks[$fileType][$callbackType];
}
The exception was catched in the previous code:
Mage_Catalog_Helper_Image
public function __toString()
{
...
} catch( Exception $e ) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
...
}
the $url became:
http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg
So, it should have generated placeholder image right?
(without ImageCdn extension)
No, because
Mage_Catalog_Helper_Image was rewritten by
OnePica_ImageCdn_Helper_Image
public function __toString()
{
parent::__toString(); //the result is http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg but no variable store/process its value
return $this->_getModel()->getUrl(); //in the end it will return http://local.m.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/d/u/dummy.jpeg
}
In case you all already forgot the question:
Anybody know what is the purpose of that code? I don't understand what is the meaning of their comment above.
Is it really a bug or it is just my wrong interpretation?
No it isn't a bug. It's just legacy support for older Magento systems. I'm wondering, have you ever got around to snoop around earlier versions of magento (as the inline documentation comment references to, < 1.1.3)?
The gist of the matter is before Mage 1.1.3, Mage_Catalog_Helper_Image instances happen to produce URL's from to-string casts e.g.
$image = (some instance of Mage_Catalog_Helper_Image).. ;
$imageUrl = (string) $image;
__toString is probably either protected or private, i'm not sure but what I'm sure is the usual practice is to always code up this Magic Method in order to use it in a class that you are meaning to rewrite something with that expects to use this kind data cast.
I have a custom class, we'll call it FileProcessUpload and it extends RunBaseBatch. It more or less creates a CSV file and then uploads it to an FTP server. When the class is run manually, everything works fine. However, when submitted as a Batch Job, there is an error in the infolog stating "AsciiIO object not initialized".
Probably the most important thing to note here is that this Batch Job is being delegated to a different AOS.
Here is a cropped down version of the offending code:
void CreateFiles()
{
#File
AsciiIO asciiio;
FileIOPermission permission;
ATable aTable;
str outputFile;
str directory;
;
directory = #'C:\Uploads';
ouptutFile = directory + #'\output.csv';
if (!WinAPI::folderExists(directory))
{
WinAPI::createDirectory(directory);
}
// Try to assert the appropriate file access mode
permission = new FileIOPermission(outputFile, #io_write);
permission.assert();
// Try to open the file for writing
asciiio = new AsciiIO(outputFile, #io_write);
if (asciiio != null)
{
while select aTable
{
// Write the necessary lines into the file
asciiio.write(aTable.field1 + ',' + aTable.field2);
}
}
else
{
error('Could not create file: ' + outputFile);
}
// Close file and release permission assertion
asciiio = null;
CodeAccessPermission::revertAssert();
}
Does the service user that Ax is running under have permissions to read/write the file?
You are using the WinAPI class, but should you be using WinAPIServer class instead? You may be executing on the server of course.
Do you need to add to your class the following public boolean runsImpersonated() { return false; } and run this class on a client?
Good luck
Edit: Executing your code via the server static void mainOnServer(Args args) method signature is commonly used (see PurchFormLetter class for it's usage) to make sure that you execute on the server. It is called from static void main(Args args)
Use file path and file name instead of str as directory and name
If runbasebatch then should put pack/uppack filePath and fileName and put it into currentVersion control at classdeclaration.
If you move/delete/encrytion/read file, using system.io.file /system.io.stream, or streamreader, or system.net.ftpwebrequest, and system.net.ftpwebresponse, remember to run on server static void method for this...
Any file format I have done, txt, .csv, .gpg, I move around file easily in/out ax to other server, no problem to just write a file inside of AX by fellowing the above rule..