Find the oldest living individuals.

@param string $sex   "M", "F" or "BOTH"
@param int    $total

@return array
502740
private function topTenOldestAliveQuery(string $sex, int $total): array
    {
        $query = DB::table('dates')
            ->join('individuals', static function (JoinClause $join): void {
                $join
                    ->on('i_id', '=', 'd_gid')
                    ->on('i_file', '=', 'd_file');
            })
            ->where('d_file', '=', $this->tree->id())
            ->where('d_julianday1', '<>', 0)
            ->where('d_fact', '=', 'BIRT')
            ->where('i_gedcom', 'NOT LIKE', "%\n1 DEAT%")
            ->where('i_gedcom', 'NOT LIKE', "%\n1 BURI%")
            ->where('i_gedcom', 'NOT LIKE', "%\n1 CREM%");

        if ($sex === 'F' || $sex === 'M') {
            $query->where('i_sex', '=', $sex);
        }

        return $query
            ->groupBy(['i_id', 'i_file'])
            ->orderBy(DB::raw('MIN(d_julianday1)'))
            ->select('individuals.*')
            ->take($total)
            ->get()
            ->map(Individual::rowMapper())
            ->filter(GedcomRecord::accessFilter())
            ->map(function (Individual $individual): array {
                return [
                    'person' => $individual,
                    'age'    => $this->calculateAge(Carbon::now()->julianDay() - $individual->getBirthDate()->minimumJulianDay()),
                ];
            })
            ->all();
    }
1114078
public function total()
    {
        $total = array_reduce($this->examples, function($x, $e) {
            $x += $e instanceof Example ? 1 : $e->total();
            return $x;
        }, 0);

        return $total;
    }
31568
public function older()
	{
		return $this->live()
			->where('published_date', '<=', $this->published_date)
			->where('id', '<>', $this->id)
			->orderBy('published_date', 'desc')
			->orderBy('id', 'desc')
			->first();
	}
954770
private function birthAndDeathQuery(string $sex): Builder
    {
        $query = DB::table('individuals')
            ->where('i_file', '=', $this->tree->id())
            ->join('dates AS birth', static function (JoinClause $join): void {
                $join
                    ->on('birth.d_file', '=', 'i_file')
                    ->on('birth.d_gid', '=', 'i_id');
            })
            ->join('dates AS death', static function (JoinClause $join): void {
                $join
                    ->on('death.d_file', '=', 'i_file')
                    ->on('death.d_gid', '=', 'i_id');
            })
            ->where('birth.d_fact', '=', 'BIRT')
            ->where('death.d_fact', '=', 'DEAT')
            ->whereColumn('death.d_julianday1', '>=', 'birth.d_julianday2')
            ->where('birth.d_julianday2', '<>', 0);

        if ($sex === 'M' || $sex === 'F') {
            $query->where('i_sex', '=', $sex);
        }

        return $query;
    }
89080
public function getTotal($duration = null)
    {
        $total = User::getDb()->cache(function ($db) {
            return User::find()->count();
        }, $duration);
        return $total;
    }
1138006
public function getLastChild()
	{
		if ( ! $this->hasChildren()) {
			return null;
		}
		$right = $this->getRightValue() - 1;

		$searchCondition = $this->repository->createSearchCondition()
				->rightEqualsTo($right);

		$lastChild = $this->repository->search($searchCondition);
		if ( ! isset($lastChild[0])) {
			$dump = static::dump($this);
			throw new Exception\InvalidStructure("Could not find the last child of {$dump} but it must exist");
		}
		$lastChild = $lastChild[0];
		
		return $lastChild;
	}
771079
public function findLastEntriesByUser($game, $user, $dateLimit)
    {
        $query = $this->em->createQuery(
            'SELECT e.id FROM PlaygroundGame\Entity\Entry e 
             WHERE e.user = :user AND e.game = :game AND (e.bonus = 0 OR e.bonus IS NULL) AND e.created_at >= :date'
        );
        $query->setParameter('user', $user);
        $query->setParameter('game', $game);
        $query->setParameter('date', $dateLimit);

        $total = $query->getResult();

        return $total;
    }
628521
public function topTenLargestFamilyList(int $total = 10): string
    {
        $records = $this->topTenFamilyQuery($total);

        return view('statistics/families/top10-list', [
            'records' => $records,
        ]);
    }
1214470
public function popular($total)
    {
        $results = $response = array();
        $url = '/explore/popular.json?' . http_build_query(array('page' => 1));
        $response['next'] = $this->url($url);

        do {
            $response = $this->request($response['next']);
            $response = $this->parse($response);
            $results = array_merge($results, $this->create($response));
        } while (count($results) < $total && isset($response['next']));

        return array_slice($results, 0, $total);
    }
683951
public static function lastActive()
    {
        $last = Podium::getInstance()->podiumCache->get('forum.lastactive');
        if ($last === false) {
            $time = time() - 15 * 60;
            $last = [
                'count' => static::find()->where(['>', 'updated_at', $time])->count(),
                'members' => static::find()->where(['and',
                        ['>', 'updated_at', $time],
                        ['is not', 'user_id', null],
                        ['anonymous' => 0]
                    ])->count(),
                'anonymous' => static::find()->where(['and',
                        ['>', 'updated_at', $time],
                        ['is not', 'user_id', null],
                        ['anonymous' => 1]
                    ])->count(),
                'guests' => static::find()->where(['and',
                        ['>', 'updated_at', $time],
                        ['user_id' => null]
                    ])->count(),
                'names' => [],
            ];
            $members = static::find()->where(['and',
                    ['>', 'updated_at', $time],
                    ['is not', 'user_id', null],
                    ['anonymous' => 0]
                ]);
            foreach ($members->each() as $member) {
                $last['names'][$member->user_id] = [
                    'name' => $member->username,
                    'role' => $member->user_role,
                    'slug' => $member->user_slug,
                ];
            }
            Podium::getInstance()->podiumCache->set('forum.lastactive', $last, 60);
        }
        return $last;
    }
563862
static function fetchCountByVatType( $vatID )
    {
        $rows = eZPersistentObject::fetchObjectList( eZVatRule::definition(),
                                                     array(),
                                                     array( 'vat_type' => (int) $vatID ),
                                                     false,
                                                     null,
                                                     false,
                                                     false,
                                                     array( array( 'operation' => 'count( * )',
                                                                   'name' => 'count' ) ) );
        return $rows[0]['count'];
    }
1087795
private function extractNumber(string $age, string $suffix): int
    {
        if (preg_match('/(\d+) *' . $suffix . '/', $age, $match)) {
            return (int) $match[1];
        }

        return 0;
    }
474011
private function averageLifespanQuery(string $sex, bool $show_years): string
    {
        $prefix = DB::connection()->getTablePrefix();

        $days = (int) $this->birthAndDeathQuery($sex)
            ->select(DB::raw('AVG(' . $prefix . 'death.d_julianday2 - ' . $prefix . 'birth.d_julianday1) AS days'))
            ->value('days');

        if ($show_years) {
            return $this->calculateAge($days);
        }

        return I18N::number((int) ($days / 365.25));
    }
724808
public function loadTime(bool $totalStringOnly = true)
    {
        $start = APP_START_TIME; $end = microtime(true);

        $total = $end - $start;
        $totalString = sprintf('%.5f', $total);
        if ($totalStringOnly) {
            return $totalString;
        }

        return [$start, $end, $total, $totalString];
    }
1244988
private function getTableCellWidth($total, $cells)
    {
        $widths = [];
        if (is_int($cells)) {
            $width = floor($total/$cells);
            for ($i=0; $i<$cells; ++$i) {
                $widths[] = $width;
            }
        }
        else if (is_array($cells)){
            $width = floor($total/count($cells));
            foreach ($cells as $i) {
                $widths[$i] = $width;
            }
        }
        return $widths;
    }
843245
protected function getRowAttributes($total, $index, $record)
    {
        $attributes = parent::getRowAttributes($total, $index, $record);
        $this->extend('updateRowAttributes', $total, $index, $record, $attributes);

        return $attributes;
    }
586358
protected function getTotal()
	{
		if (null == $this->_total) {
			$this->_total = 0;
			foreach ($this->types as $type) {
				if (isset($type['value'])) {
					$this->_total += $type['value'];
				}
			}
		}
		return $this->_total;
	}