Skip to content

Instantly share code, notes, and snippets.

@hissy
Created June 26, 2018 10:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hissy/d85d10d29c406aa3d2a8654e2d2f304e to your computer and use it in GitHub Desktop.
Save hissy/d85d10d29c406aa3d2a8654e2d2f304e to your computer and use it in GitHub Desktop.
#concrete5 Status Code Check Command
<?php
// application/bootstrap/app.php
if ($app->isInstalled() && $app->isRunThroughCommandLineInterface()) {
/** @var \Concrete\Core\Console\Application $console */
$console = $app->make('console');
$console->add(new \Application\Console\Command\StatusCodeCheck());
}
<?php
// application/bootstrap/autoload.php
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader();
$classLoader->addPrefix('Application\\Console', DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/Console');
$classLoader->register();
<?php
// application/src/Console/Command/StatusCodeCheck.php
namespace Application\Console\Command;
use Concrete\Core\Console\Command;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Http\Client\Client;
use Concrete\Core\Page\Page;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Url\Url;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class StatusCodeCheck extends Command
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName('c5:status-code-check')
->setDescription('Check Status Code of all pages');
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$app = Application::getFacadeApplication();
$site = $app['site']->getSite();
if (is_object($site)) {
foreach ($this->getPages() as $cID) {
$c = Page::getByID($cID);
if (!$c->isHomePage() && !$c->isAdminArea() && !$c->isAlias() && !$c->isExternalLink() && !$c->isGeneratedCollection() && !$c->isInTrash() && !$c->isPageDraft() && !$c->isMasterCollection() && !$c->isSystemPage()) {
$url = $c->getCollectionLink();
$output->writeln($url);
/** @var Client $client */
$client = $app->make('http/client');
$client->setUri($url);
$response = $client->send();
$output->writeln($response->getStatusCode());
}
}
}
}
/**
* @return \Generator
*/
protected function getPages()
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make('database')->connection();
$qb = $db->createQueryBuilder();
// Find all pages
$query = $qb
->select('p.cID')
->from('Pages', 'p')
->where('cIsActive = 1')
->execute();
while ($id = $query->fetchColumn()) {
yield $id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment