diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 4665cc8..4e957c4 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -106,6 +106,21 @@ public function feedAction( // Add the logged in user's rights to the response, so we can conditionally show links for sysops. $ret['user_rights'] = $currentUser ? $wikiRepo->getUserRights( $currentUser->username ) : []; + $open_records_count = $copyPatrolRepo->getPlagiarismRecordsCount( + array_merge( + $options, + [ + 'filter' => CopyPatrolRepository::FILTER_OPEN + ] + ) + ); + // If there are 200 (or more) open records, we just show "200+". + if ( $open_records_count >= 200 ) { + $ret['open_records_count'] = "200+"; + } else { + $ret['open_records_count'] = $open_records_count; + } + return $this->render( 'feed.html.twig', $ret ); } diff --git a/src/Repository/CopyPatrolRepository.php b/src/Repository/CopyPatrolRepository.php index 98d158a..483da74 100644 --- a/src/Repository/CopyPatrolRepository.php +++ b/src/Repository/CopyPatrolRepository.php @@ -4,6 +4,7 @@ namespace App\Repository; +use DateInterval; use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ParameterType; @@ -128,6 +129,42 @@ public function getPlagiarismRecords( array $options = [], int $limit = 50 ): ar ->fetchAllAssociative(); } + /** + * Fetch the count of records from the CopyPatrol database, up to $limit. + * + * @param array $options filter and filter user options, should look like: + * string 'filter' Filter to show a certain status: 'all', 'open', or 'reviewed' + * string 'filter_user' Filter SQL to only return records reviewed by given user + * string 'filter_page' Search string (page title) + * boolean 'drafts' returns only records that are in the Draft namespace + * integer 'last_id' offset of where to start fetching records, going by 'diff_id' + * integer 'id' exact submission_id of a record. This will override all other filter options + * string 'lang' The language code of the Wikipedia to query for + * @param int $limit Number of records asked for + * @param string $cacheExpiry Cache expiry time (default 15 minutes) + * @return int Count of CopyPatrol db records. + */ + public function getPlagiarismRecordsCount( + array $options = [], + int $limit = 200, + string $cacheExpiry = 'PT15M' + ): int { + // If the count is already cached, return it + if ( $this->cache->hasItem( md5( serialize( $options ) . $limit ) ) ) { + return $this->cache->getItem( md5( serialize( $options ) . $limit ) )->get(); + } + + // Else, calculate the count and cache it + $count = count( $this->getPlagiarismRecords( $options, $limit ) ); + $cacheItem = $this->cache + ->getItem( md5( serialize( $options ) . $limit ) ) + ->set( $count ) + // cache for 15 minutes + ->expiresAfter( new DateInterval( $cacheExpiry ) ); + $this->cache->save( $cacheItem ); + return $this->cache->getItem( md5( serialize( $options ) . $limit ) )->get(); + } + /** * Get the top reviewers over the past last 7 days, 30 days, and all-time. * diff --git a/templates/base.html.twig b/templates/base.html.twig index bbf0933..9f90575 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -52,7 +52,7 @@ {% endfor %}