[--format=] * * @package Lynkora */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Find orphaned pages, dead ends and broken internal links. */ class Lynkora_CLI { /** * Rebuild the link index from scratch. * * ## EXAMPLES * * wp lynkora scan */ public function scan() { $indexer = new Lynkora_Indexer(); $state = $indexer->start_scan(); $progress = WP_CLI\Utils\make_progress_bar( 'Reading links', max( 1, $state['total'] ) ); $done = 0; while ( 'posts' === $state['phase'] ) { $state = $indexer->step(); $progress->tick( $state['offset'] - $done ); $done = $state['offset']; } $progress->finish(); $unchecked = Lynkora_DB::count_unchecked(); if ( $unchecked ) { WP_CLI::log( sprintf( 'Checking %d addresses that are not pages or posts...', $unchecked ) ); } while ( 'check' === $state['phase'] ) { $state = $indexer->step(); } $report = new Lynkora_Report(); $totals = $report->totals( Lynkora_Settings::get()['depth'] ); WP_CLI::success( sprintf( '%d pages and posts, %d internal links. Orphans: %d. Dead ends: %d. Broken links: %d. Hard to reach: %d.', $totals['content'], $totals['links'], $totals['orphans'], $totals['dead_ends'], $totals['broken'], $totals['deep'] ) ); } /** * Print a report from the current index. * * ## OPTIONS * * * : Which report. * --- * options: * - orphans * - dead-ends * - broken * - deep * - mentions * - all * --- * * [--format=] * : Output format. * --- * default: table * options: * - table * - csv * - json * - count * --- * * ## EXAMPLES * * wp lynkora report orphans * wp lynkora report broken --format=csv > broken-links.csv * * @param array $args Positional arguments. * @param array $assoc_args Named arguments. */ public function report( $args, $assoc_args ) { if ( ! Lynkora_Indexer::get_state()['finished'] ) { WP_CLI::error( 'No scan yet. Run "wp lynkora scan" first.' ); } $report = new Lynkora_Report(); $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; $depth = Lynkora_Settings::get()['depth']; switch ( $args[0] ) { case 'broken': $fields = array( 'source', 'anchor', 'url', 'problem', 'new_url' ); $items = $report->broken(); break; case 'mentions': $fields = array( 'page', 'mentioned_in', 'excerpt' ); $items = array(); foreach ( $report->mentions() as $group ) { foreach ( $group['mentions'] as $mention ) { $items[] = array( 'page' => $group['title'], 'mentioned_in' => $mention['title'], 'excerpt' => $mention['excerpt'], ); } } break; default: $fields = array( 'id', 'title', 'type', 'inbound', 'outbound', 'depth' ); switch ( $args[0] ) { case 'orphans': $items = $report->orphans(); break; case 'dead-ends': $items = $report->dead_ends(); break; case 'deep': $items = $report->deep( $depth ); break; default: $items = array_values( $report->content() ); } foreach ( $items as $i => $item ) { $items[ $i ]['depth'] = null === $item['depth'] ? 'not reachable' : $item['depth']; } } WP_CLI\Utils\format_items( $format, $items, $fields ); } }