Skip to content

Commit 689ba3e

Browse files
committed
added count method to repository and extended the params that can be passed. This was then used to replace the current stats generation process.
1 parent 06fb256 commit 689ba3e

2 files changed

Lines changed: 139 additions & 79 deletions

File tree

src/Dashboard/Dashboard_Statistics.php

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -124,78 +124,43 @@ private static function normalize_link_statistics( array $stats ): ?array {
124124
* }
125125
*/
126126
private static function compile_link_statistics(): array {
127-
$all_links = ( new Link_Repository() )->query_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null );
128-
129-
// Get all the links stats.
130-
$all_broken = array();
131-
$redirected_broken = array();
132-
$has_archive_link = array();
133-
$not_checked = array();
134-
$process_done = array();
135-
$process_new = array();
136-
$process_pending = array();
137-
$last_checks = array();
138-
139-
// Loop through all links to gather stats.
140-
foreach ( $all_links as $link ) {
141-
if ( $link->is_broken() && ! $link->is_excluded() ) {
142-
$all_broken[] = $link->get_id();
143-
}
144-
145-
if ( $link->is_broken() && $link->has_archived_href() && ! $link->is_excluded() ) {
146-
$redirected_broken[] = $link->get_id();
147-
}
148-
149-
if ( $link->has_archived_href() ) {
150-
$has_archive_link[] = $link->get_id();
151-
}
152-
153-
if ( null === $link->get_last_check() ) {
154-
$not_checked[] = $link->get_id();
155-
} else {
156-
$last = $link->get_last_check();
157-
$last_checks[] = array(
127+
$links = new Link_Repository();
128+
129+
// Get all the link stats.
130+
$all_links = $links->count_links( \PHP_INT_MAX, 1 );
131+
$all_broken = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, false );
132+
$has_archive_link = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array( Link_Repository::LINK_HAS_ARCHIVE ) );
133+
$redirected_broken_ = $links->count_links( \PHP_INT_MAX, 1, array( Link_Repository::LINK_STATUS_BROKEN ), array(), array( Link_Repository::LINK_HAS_ARCHIVE ), Link_Repository::ORDER_DATE_DESC, null, null, false );
134+
$not_checked = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, null, false );
135+
$process_new = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_NEW ) );
136+
$process_done_ = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_DONE ) );
137+
$process_pending = $links->count_links( \PHP_INT_MAX, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, array( Link::PROCESS_PENDING ) );
138+
$last_checks = $links->query_links( 10, 1, array(), array(), array(), Link_Repository::ORDER_DATE_DESC, null, null, null, null, null, true );
139+
140+
// Extract the details from last checks.
141+
$last_checks = array_map(
142+
function ( $link ) {
143+
return array(
158144
'id' => $link->get_id(),
159-
'last_check' => $last,
145+
'last_check' => $link->get_last_check(),
160146
);
161-
}
162-
163-
switch ( $link->get_archive_process() ) {
164-
case Link::PROCESS_NEW:
165-
$process_new[] = $link->get_id();
166-
break;
167-
case Link::PROCESS_PENDING:
168-
$process_pending[] = $link->get_id();
169-
break;
170-
default:
171-
$process_done[] = $link->get_id();
172-
break;
173-
}
174-
}
175-
176-
// Sort the last checks by date desc.
177-
usort(
178-
$last_checks,
179-
function ( $a, $b ) {
180-
return strtotime( $b['last_check']['date'] ) <=> strtotime( $a['last_check']['date'] );
181-
}
147+
},
148+
$last_checks
182149
);
183150

184-
$stats = array(
185-
'total_links' => count( $all_links ),
186-
'all_broken_links' => count( $all_broken ),
187-
'broken_and_redirected_links' => count( $redirected_broken ),
188-
'broken_not_redirected_links' => count( $all_broken ) - count( $redirected_broken ),
189-
'links_with_archive' => count( $has_archive_link ),
190-
'links_without_archive' => count( $all_links ) - count( $has_archive_link ),
191-
'not_checked' => count( $not_checked ),
192-
'process_done' => count( $process_done ),
193-
'process_new' => count( $process_new ),
194-
'process_pending' => count( $process_pending ),
195-
'last_checks' => array_slice( $last_checks, 0, absint( apply_filters( 'iawmlf_dashboard_link_count', 10 ) ) ),
151+
return array(
152+
'total_links' => $all_links,
153+
'all_broken_links' => $all_broken,
154+
'broken_and_redirected_links' => $redirected_broken_,
155+
'broken_not_redirected_links' => $all_broken - $redirected_broken_,
156+
'links_with_archive' => $has_archive_link,
157+
'links_without_archive' => $all_links - $has_archive_link,
158+
'not_checked' => $not_checked,
159+
'process_done' => $process_done_,
160+
'process_new' => $process_new,
161+
'process_pending' => $process_pending,
162+
'last_checks' => $last_checks,
196163
);
197-
198-
return $stats;
199164
}
200165

201166
/**

src/Link/Link_Repository.php

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,17 @@ function ( int $link_id ): ?Link {
379379
*
380380
* @since 1.2.0
381381
*
382-
* @param integer $limit The limit of links to return.
383-
* @param integer $page The page of links to return.
384-
* @param array $status The status of the links to return.
385-
* @param array $link_ids The link ids to query.
386-
* @param array $archive_status The archive status of the links to return.
387-
* @param string $order_by The order by.
388-
* @param string|NULL $search_term The search term to query.
389-
* @param string|NULL $date The date of the links to return (yy-mm).
390-
* @param boolean|NULL $excluded Whether to return excluded links.
382+
* @param integer $limit The limit of links to return.
383+
* @param integer $page The page of links to return.
384+
* @param array $status The status of the links to return.
385+
* @param array $link_ids The link ids to query.
386+
* @param array $archive_status The archive status of the links to return.
387+
* @param string $order_by The order by.
388+
* @param string|NULL $search_term The search term to query.
389+
* @param string|NULL $date The date of the links to return (yy-mm).
390+
* @param boolean|NULL $excluded Whether to return excluded links.
391+
* @param string[]|NULL $snapshot_process The snapshot status of the links to return.
392+
* @param boolean|NULL $has_checks Whether to return links with or without checks.
391393
*
392394
* @return Link[]
393395
*/
@@ -400,9 +402,78 @@ public function query_links(
400402
string $order_by = self::ORDER_DATE_DESC,
401403
?string $search_term = null,
402404
?string $date = null,
403-
?bool $excluded = null
405+
?bool $excluded = null,
406+
?array $snapshot_process = null,
407+
?bool $has_checks = null
404408
): array {
405-
// Remove any invalid statuses.
409+
$rows = $this->perform_query( $limit, $page, $status, $link_ids, $archive_status, $order_by, $search_term, $date, $excluded, $snapshot_process, $has_checks );
410+
return array_map( array( $this, 'map_link' ), $rows );
411+
}
412+
413+
/**
414+
* Gets the count of links for a given query.
415+
*
416+
* @param integer $limit The limit of links to return.
417+
* @param integer $page The page of links to return.
418+
* @param array $status The status of the links to return.
419+
* @param array $link_ids The link ids to query.
420+
* @param array $archive_status The archive status of the links to return.
421+
* @param string $order_by The order by.
422+
* @param string|NULL $search_term The search term to query.
423+
* @param string|NULL $date The date of the links to return (yy-mm).
424+
* @param boolean|NULL $excluded Whether to return excluded links.
425+
* @param string[]|NULL $snapshot_process The snapshot status of the links to return.
426+
* @param boolean|NULL $has_checks Whether to return links with or without checks.
427+
*
428+
* @return integer
429+
*/
430+
public function count_links(
431+
int $limit = 10,
432+
int $page = 1,
433+
array $status = array(),
434+
array $link_ids = array(),
435+
array $archive_status = array(),
436+
string $order_by = self::ORDER_DATE_DESC,
437+
?string $search_term = null,
438+
?string $date = null,
439+
?bool $excluded = null,
440+
?array $snapshot_process = null,
441+
?bool $has_checks = null
442+
): int {
443+
return count( $this->perform_query( $limit, $page, $status, $link_ids, $archive_status, $order_by, $search_term, $date, $excluded, $snapshot_process, $has_checks ) );
444+
}
445+
446+
/**
447+
* Performs a link query.
448+
*
449+
* @param integer $limit The limit of links to return.
450+
* @param integer $page The page of links to return.
451+
* @param array $status The status of the links to return.
452+
* @param array $link_ids The link ids to query.
453+
* @param array $archive_status The archive status of the links to return.
454+
* @param string $order_by The order by.
455+
* @param string|NULL $search_term The search term to query.
456+
* @param string|NULL $date The date of the links to return (yy-mm).
457+
* @param boolean|NULL $excluded Whether to return excluded links.
458+
* @param string[]|NULL $snapshot_process The snapshot status of the links to return.
459+
* @param boolean|NULL $has_checks Whether to return links with or without checks.
460+
*
461+
* @return stdClass[]
462+
*/
463+
private function perform_query(
464+
int $limit = 10,
465+
int $page = 1,
466+
array $status = array(),
467+
array $link_ids = array(),
468+
array $archive_status = array(),
469+
string $order_by = self::ORDER_DATE_DESC,
470+
?string $search_term = null,
471+
?string $date = null,
472+
?bool $excluded = null,
473+
?array $snapshot_process = null,
474+
?bool $has_checks = null
475+
): array {
476+
// Remove any invalid statuses.
406477
$status = array_filter(
407478
$status,
408479
function ( $status ): bool {
@@ -474,6 +545,29 @@ function ( $link_id ): bool {
474545
$where = true;
475546
}
476547

548+
// If we are looking for links with or without checks, add to the query.
549+
if ( null !== $has_checks ) {
550+
$query .= true === $where ? ' AND' : ' WHERE';
551+
$query .= $has_checks ? ' JSON_LENGTH(`checks`) > 0' : ' JSON_LENGTH(`checks`) = 0';
552+
$where = true;
553+
}
554+
555+
// If we have snapshot process status, add to the query.
556+
if ( ! empty( $snapshot_process ) ) {
557+
// Remove any invalid snapshot statuses.
558+
$snapshot_process = array_filter(
559+
$snapshot_process,
560+
function ( $status ): bool {
561+
return is_string( $status ) && in_array( $status, array( Link::PROCESS_NEW, Link::PROCESS_PENDING, Link::PROCESS_DONE ), true );
562+
}
563+
);
564+
565+
$place_holders = join( ',', array_fill( 0, count( $snapshot_process ), '%s' ) );
566+
$snapshot_template = true === $where ? " AND archive_process IN ({$place_holders})" : " WHERE archive_process IN ({$place_holders})";
567+
$query .= $this->wpdb->prepare( $snapshot_template, $snapshot_process ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, Compiled in parts, very hard to escape
568+
$where = true;
569+
}
570+
477571
// If we have a search term, add to the query.
478572
if ( $search_term ) {
479573
// Prepare the search term.
@@ -489,6 +583,7 @@ function ( $link_id ): bool {
489583
if ( null !== $excluded ) {
490584
$query .= true === $where ? ' AND' : ' WHERE';
491585
$query .= $excluded ? ' excluded = 1' : ' excluded = 0';
586+
$where = true;
492587
}
493588

494589
// Add the order by.
@@ -506,7 +601,7 @@ function ( $link_id ): bool {
506601
return array();
507602
}
508603

509-
return array_map( array( $this, 'map_link' ), $rows );
604+
return $rows;
510605
}
511606

512607
/**

0 commit comments

Comments
 (0)