-
-
Save katzueno/33d254afdacde46e383928347c6801b0 to your computer and use it in GitHub Desktop.
#concrete5 Status Code Check Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// application/bootstrap/autoload.php | |
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader(); | |
$classLoader->addPrefix('Application\\Console', DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/Console'); | |
$classLoader->register(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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