* @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('CLI_SCRIPT', true);
require dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php';
require_once $CFG->libdir . '/clilib.php';
require_once $CFG->dirroot . '/local/linkchecker_robot/lib.php';
list($options, $unrecognized) = cli_get_params(array('help' => false, 'url' => null), array('h' => 'help', 'u' => 'url'));
if ($unrecognized) {
    $unrecognized = implode("\n  ", $unrecognized);
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
$help = "Crawl a url as the robot and parse it.\n\nUseful for when a page has been corrected and you want to instantly reflect this.\n\nOptions:\n-h, --help      Print out this help\n-u, --url       Url to crawl and process\n\nExample:\n\$sudo -u www-data php crawl-as.php --url=http://ford.com/\n";
if ($options['help']) {
    echo $help;
    die;
}
$robot = new \local_linkchecker_robot\robot\crawler();
$error = $robot->is_bot_valid();
if ($error) {
    print "Error: {$error}";
    exit;
}
if (!$options['url']) {
    echo $help;
    die;
}
$url = $options['url'];
$node = $robot->mark_for_crawl($url, $url);
$res = $robot->scrape($url);
$result = $robot->crawl($node, 2);