diff --git a/config/dm/modules.yml b/config/dm/modules.yml index c123d68..9ffd57d 100755 --- a/config/dm/modules.yml +++ b/config/dm/modules.yml @@ -5,4 +5,5 @@ Tools: dmBot: front: false admin: true - name: Internal Bot \ No newline at end of file + name: Internal Bot + credentials: bot \ No newline at end of file diff --git a/lib/dmBot.php b/lib/dmBot.php index debd59b..a78b88e 100755 --- a/lib/dmBot.php +++ b/lib/dmBot.php @@ -62,12 +62,12 @@ public function render($options = array()) return $table->render(); } - protected function getPageUrl(DmPage $page) + public function getPageUrl(DmPage $page) { return $this->baseUrl.'/'.$page->_getI18n('slug'); } - protected function getPageStatusCode(DmPage $page) + public function getPageStatusCode(DmPage $page) { $statusCode = 200; diff --git a/lib/task/dmBotTask.class.php b/lib/task/dmBotTask.class.php new file mode 100644 index 0000000..c2179a6 --- /dev/null +++ b/lib/task/dmBotTask.class.php @@ -0,0 +1,86 @@ + + */ +class dmBotTask extends dmContextTask +{ + public function configure() { + $this->namespace = 'project'; + $this->name = 'bot'; + $this->briefDescription = 'Runs dmBot to check if pages load properly and preload cache'; + $this->addArguments(array( + new sfCommandArgument('site-url', sfCommandArgument::REQUIRED, 'Url of site to browse'), + )); + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'admin'), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'), + new sfCommandOption('limit', 'l', sfCommandOption::PARAMETER_OPTIONAL, 'Limit'), + new sfCommandOption('only-active', 'a', sfCommandOption::PARAMETER_OPTIONAL, 'Browse only active pages'), + new sfCommandOption('slug-pattern', 's', sfCommandOption::PARAMETER_OPTIONAL, 'Browse only pages matching given slug'), + new sfCommandOption('title-pattern', 'k', sfCommandOption::PARAMETER_OPTIONAL, 'Browse only pages matching given title'), + new sfCommandOption('name-pattern', 'n', sfCommandOption::PARAMETER_OPTIONAL, 'Browse only pages matching given name'), + new sfCommandOption('verbose', 'v', sfCommandOption::PARAMETER_NONE, 'Complete output'), + )); + } + + public function execute($arguments = array(), $options = array()) + { + + $this->timerStart("bot"); + + $this->withDatabase(); + + $botoptions = array(); + + if (!is_null($options['limit'])) $botoptions['limit'] = $options['limit']; + if (!is_null($options['only-active'])) $botoptions['only_active'] = $options['only-active']; + if (!is_null($options['slug-pattern'])) $botoptions['slug_pattern'] = $options['slug-pattern']; + if (!is_null($options['name-pattern'])) $botoptions['name_pattern'] = $options['name-pattern']; + if (!is_null($options['title-pattern'])) $botoptions['title_pattern'] = $options['title-pattern']; + + $this->bot = $this + ->getContext() + ->getServiceContainer() + ->setParameter('dm_bot.options', $botoptions) + ->getService('dm_bot') + ->setBaseUrl($arguments['site-url']) + ->init(); + + $this->logSection('bot', $this->bot->getNbPages() . " pages to browse.", null, "COMMENT"); + + foreach($this->bot->getPages() as $index => $page) + { + $url = $this->bot->getPageUrl($page); + $statusCode = $this->bot->getPageStatusCode($page); + + if ($options['verbose']) $this->logSection('bot', 'will be browsing ' . $url, null, "INFO"); + $this->browse($url, $statusCode, $options); + } + + $this->logTimersTotal(); + + } + + private function browse($url, $expectedStatus = 200, $options = array()) + { + $browser = $this->getContext()->getServiceContainer()->getService('web_browser'); + + $browsingTime = $this->timerStart('browse-' . $url); + + $browser->get($url); + + if ($browser->getResponseCode() == $expectedStatus) + { + if ($options['verbose']) $this->logSection('bot', 'got expected status code of ' . $browser->getResponseCode() . ' after ' . round($browsingTime->getElapsedTime()*1000, 2) . "ms", null, "COMMENT"); + } + else + { + $this->logSection('bot', $url . ' got unexpected code of ' . $browser->getResponseCode() . " (expected code was " . $expectedStatus . ")", null, "ERROR"); + } + } + +} diff --git a/modules/dmBotAdmin/templates/indexSuccess.php b/modules/dmBotAdmin/templates/indexSuccess.php index 64398af..1b74d8f 100755 --- a/modules/dmBotAdmin/templates/indexSuccess.php +++ b/modules/dmBotAdmin/templates/indexSuccess.php @@ -9,7 +9,7 @@ $form->render('.dm_form.list method=get') ) ). - _tag('div.help_box', markdown('Notes: + _tag('div.help_box', _markdown('Notes: - Find some page with the form above, then run the bot to browse them - The time displayed for each page is not the page creation time, but the time required to send the request and receive the response'