CHtmlPurifier in ConsoleCommand

Hi!

I tried to use CHtmlPurifier in console command, but failed.

Here is a code:


$purifier = new CHtmlPurifier();

$purifier->options = array(

    'URI.AllowedSchemes' => 'http, https',

    'HTML.Allowed' => 'p,b,i,u,s,strong,strike,big,small,a[href],ul,ol,li,blockquote,h3,h4,h5,h6,br,hr,code,pre,sub,sup'

);

Yii::log($purifier->purify('some html here'), 'info');

When I put this code to an action and run it from browser, it works fine.

But when I paste this code to CConsoleCommand extended class (run method)

I get the error


2011/05/13 09:04:10 [error] [exception.CException] exception 'CException' with message 'CConsoleApplication не содержит метода "getController".' in D:\_projects\External\bankrotstvo.rf\www\framework\base\CComponent.php:266

Stack trace:

#0 D:\_projects\External\bankrotstvo.rf\www\framework\web\widgets\CWidget.php(90): CComponent->__call('getController', Array)

#1 D:\_projects\External\bankrotstvo.rf\www\framework\web\widgets\CWidget.php(90): CConsoleApplication->getController()

#2 D:\_projects\External\bankrotstvo.rf\www\framework\web\widgets\CFilterWidget.php(45): CWidget->__construct(NULL)

#3 D:\_projects\External\bankrotstvo.rf\www\site\protected\commands\FeedAggregateCommand.php(7): CFilterWidget->__construct()

#4 D:\_projects\External\bankrotstvo.rf\www\framework\console\CConsoleCommandRunner.php(63): FeedAggregateCommand->run(Array)

#5 D:\_projects\External\bankrotstvo.rf\www\framework\console\CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)

#6 D:\_projects\External\bankrotstvo.rf\www\framework\base\CApplication.php(155): CConsoleApplication->processRequest()

#7 D:\_projects\External\bankrotstvo.rf\www\framework\yiic.php(33): CApplication->run()

#8 D:\_projects\External\bankrotstvo.rf\www\site\protected\yiic.php(7): require_once('D:\_projects\Ex...')

#9 {main}

I suppose that that is because CHtmlPurifier is inherited from CWidget.

Is there any way to use CHtmlPurifier in console commands? Is it a bug and I should post it to google code issue tracker?




$purifier=new HTMLPurifier($options);

$purifier->config->set('Cache.SerializerPath',Yii::app()->getRuntimePath());

$purified = $purifier->purify($content);



Also consider the fact that purifier is slow so don’t use it on every page load.

Sam Dark,

do I understand you right, that

  1. you propose to use HTMLPurifier instead of CHTMLPurifier?

Will it be loaded automatically? (For now, I downloaded standalone package of HTMLPurifier and use it as independant library as workaround)

  1. I’ll note, that I do not need to use it on page load, instead I try to use in ConsoleCommand (and that gives me an error), which grabs some content from other sites (command is executed with cron).

To be more exact will give you an example

working code:




public function actionTest()

{

    $purifier = new CHTMLPurifier(); // this works fine in action

}



against:




class FeedAggregateCommand extends CConsoleCommand

{

    public function run($args) {

        $purifier = new CHTMLPurifier(); // this causes an error

    }

}



  1. Yes. It’s bundled with Yii actually so you don’t need to download it. Maybe will require additional import. Check CHtmlPurifier code for it, it’s just a simple wrapper.

Thanks, using yii-bundled htmlpurifier works.

I just took code from CHtmlPurifier widget:


if(!class_exists('HTMLPurifier_Bootstrap',false))

{

    require_once(Yii::getPathOfAlias('system.vendors.htmlpurifier').DIRECTORY_SEPARATOR.'HTMLPurifier.standalone.php');

    HTMLPurifier_Bootstrap::registerAutoload();

}

$purifier = new HTMLPurifier();

$purifier->config = HTMLPurifier_config::createDefault();

$purifier->config->set('Cache.SerializerPath',Yii::app()->getRuntimePath());