Code Duplication    Length = 30-30 lines in 2 locations

src/Xtools/EditCounterRepository.php 2 locations

@@ 459-488 (lines=30) @@
456
     * @param string $username The username.
457
     * @return string[]
458
     */
459
    public function getMonthCounts(Project $project, User $user)
460
    {
461
        $username = $user->getUsername();
462
        $cacheKey = "monthcounts.$username";
463
        if ($this->cache->hasItem($cacheKey)) {
464
            return $this->cache->getItem($cacheKey)->get();
465
        }
466
467
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
468
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
469
        $sql =
470
            "SELECT " . "     YEAR(rev_timestamp) AS `year`," .
471
            "     MONTH(rev_timestamp) AS `month`," . "     page_namespace," .
472
            "     COUNT(rev_id) AS `count` "
473
            . " FROM $revisionTable    JOIN $pageTable ON (rev_page = page_id)" .
474
            " WHERE rev_user_text = :username" .
475
            " GROUP BY YEAR(rev_timestamp), MONTH(rev_timestamp), page_namespace " .
476
            " ORDER BY rev_timestamp DESC";
477
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
478
        $resultQuery->bindParam(":username", $username);
479
        $resultQuery->execute();
480
        $totals = $resultQuery->fetchAll();
481
        
482
        $cacheItem = $this->cache->getItem($cacheKey);
483
        $cacheItem->expiresAfter(new DateInterval('PT10M'));
484
        $cacheItem->set($totals);
485
        $this->cache->save($cacheItem);
486
487
        return $totals;
488
    }
489
490
    /**
491
     * Get yearly edit totals for this user, grouped by namespace.
@@ 495-524 (lines=30) @@
492
     * @param string $username
493
     * @return string[] ['<namespace>' => ['<year>' => 'total', ... ], ... ]
494
     */
495
    public function getYearCounts(Project $project, User $user)
496
    {
497
        $username = $user->getUsername();
498
        $cacheKey = "yearcounts.$username";
499
        if ($this->cache->hasItem($cacheKey)) {
500
            return $this->cache->getItem($cacheKey)->get();
501
        }
502
503
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
504
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
505
        $sql = "SELECT "
506
            . "     SUBSTR(CAST(rev_timestamp AS CHAR(4)), 1, 4) AS `year`,"
507
            . "     page_namespace,"
508
            . "     COUNT(rev_id) AS `count` "
509
            . " FROM $revisionTable    JOIN $pageTable ON (rev_page = page_id)"
510
            . " WHERE rev_user_text = :username"
511
            . " GROUP BY SUBSTR(CAST(rev_timestamp AS CHAR(4)), 1, 4), page_namespace "
512
            . " ORDER BY rev_timestamp DESC ";
513
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
514
        $resultQuery->bindParam(":username", $username);
515
        $resultQuery->execute();
516
        $totals = $resultQuery->fetchAll();
517
518
        $cacheItem = $this->cache->getItem($cacheKey);
519
        $cacheItem->set($totals);
520
        $cacheItem->expiresAfter(new DateInterval('P10M'));
521
        $this->cache->save($cacheItem);
522
523
        return $totals;
524
    }
525
526
    /**
527
     * Get data for the timecard chart, with totals grouped by day and to the nearest two-hours.