diff --git a/webprofiler/config/translations/console.en.yml b/webprofiler/config/translations/console.en.yml index 2f4bfa9..1e4c3cd 100644 --- a/webprofiler/config/translations/console.en.yml +++ b/webprofiler/config/translations/console.en.yml @@ -22,7 +22,6 @@ commands: ip: Filter by IP. url: Filter by URL. method: Filter by HTTP method. - limit: Limit printed profiles. rows: time: D, m/d/Y - H:i:s header: diff --git a/webprofiler/config/translations/console.es.yml b/webprofiler/config/translations/console.es.yml index 71f66ef..fde45ec 100644 --- a/webprofiler/config/translations/console.es.yml +++ b/webprofiler/config/translations/console.es.yml @@ -22,7 +22,6 @@ commands: ip: Filtrar por IP. url: Filtrar por URL. method: Filtrar por método HTTP. - limit: Limitar perfiles a mostrar. rows: time: D, d/m/Y - H:i:s header: diff --git a/webprofiler/src/Controller/DashboardController.php b/webprofiler/src/Controller/DashboardController.php index 2b1d93e..7aae973 100644 --- a/webprofiler/src/Controller/DashboardController.php +++ b/webprofiler/src/Controller/DashboardController.php @@ -153,12 +153,13 @@ public function dashboardAction(Profile $profile) { * @return array */ public function listAction(Request $request) { - $limit = $request->get('limit', 10); + // Don't need the profiler in the profiler page. $this->profiler->disable(); $ip = $request->query->get('ip'); - $method = $request->query->get('method'); $url = $request->query->get('url'); + $limit = $request->get('limit', 50); + $method = $request->query->get('method'); $profiles = $this->profiler->find($ip, $url, $limit, $method, '', ''); @@ -222,6 +223,8 @@ public function listAction(Request $request) { '#sticky' => TRUE, ]; + $build['pager'] = array('#type' => 'pager'); + return $build; } diff --git a/webprofiler/src/Form/ProfilesFilterForm.php b/webprofiler/src/Form/ProfilesFilterForm.php index 87e04dc..7ca84a4 100644 --- a/webprofiler/src/Form/ProfilesFilterForm.php +++ b/webprofiler/src/Form/ProfilesFilterForm.php @@ -49,14 +49,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $this->getRequest()->query->get('method'), ]; - $limits = [10, 50, 100]; - $form['limit'] = [ - '#type' => 'select', - '#title' => $this->t('Limit'), - '#options' => array_combine($limits, $limits), - '#default_value' => $this->getRequest()->query->get('limit'), - ]; - $form['actions'] = ['#type' => 'actions']; $form['actions']['filter'] = [ '#type' => 'submit', diff --git a/webprofiler/src/Profiler/DatabaseProfilerStorage.php b/webprofiler/src/Profiler/DatabaseProfilerStorage.php index 2eb9a17..b7bd7e6 100644 --- a/webprofiler/src/Profiler/DatabaseProfilerStorage.php +++ b/webprofiler/src/Profiler/DatabaseProfilerStorage.php @@ -37,7 +37,8 @@ function __construct(Connection $database) { * {@inheritdoc} */ public function find($ip, $url, $limit, $method, $start = NULL, $end = NULL) { - $select = $this->database->select('webprofiler', 'wp', ['fetch' => \PDO::FETCH_ASSOC]); + $select = $this->database->select('webprofiler', 'wp', ['fetch' => \PDO::FETCH_ASSOC]) + ->extend('\Drupal\Core\Database\Query\PagerSelectExtender'); if (NULL === $start) { $start = 0; @@ -77,7 +78,9 @@ public function find($ip, $url, $limit, $method, $start = NULL, $end = NULL) { 'status_code' ]); $select->orderBy('time', 'DESC'); - $select->range(0, $limit); + + $select->limit($limit); + return $select->execute() ->fetchAllAssoc('token'); } diff --git a/webprofiler/src/Profiler/FileProfilerStorage.php b/webprofiler/src/Profiler/FileProfilerStorage.php index 7eb63ac..f65e97f 100644 --- a/webprofiler/src/Profiler/FileProfilerStorage.php +++ b/webprofiler/src/Profiler/FileProfilerStorage.php @@ -10,6 +10,71 @@ class FileProfilerStorage extends SymfonyFileProfilerStorage { /** * {@inheritdoc} */ + public function find($ip, $url, $limit, $method, $start = null, $end = null) + { + $file = $this->getIndexFilename(); + + if (!file_exists($file)) { + return array(); + } + + $file = fopen($file, 'r'); + fseek($file, 0, SEEK_END); + + // Get all lines + // TODO - Maybe there is another way to get all rows from ($limit * $current_page) to ($limit * ($current_page + 1)) + $lines = array(); + while ($line = $this->readLineFromFile($file)) { + $lines[] = $line; + } + + $total_items = count($lines); + $current_page = (int) pager_default_initialize($total_items, $limit); + + $result = array(); + foreach ($lines as $line_n => $line) { + // Add to $result only lines in the range of the current page + if ($line_n < $limit * $current_page || $line_n >= $limit * ($current_page + 1) ) { + continue; + } + + $values = str_getcsv($line); + list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values; + $csvStatusCode = isset($values[6]) ? $values[6] : null; + + $csvTime = (int) $csvTime; + + if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) { + continue; + } + + if (!empty($start) && $csvTime < $start) { + continue; + } + + if (!empty($end) && $csvTime > $end) { + continue; + } + + $result[$csvToken] = array( + 'token' => $csvToken, + 'ip' => $csvIp, + 'method' => $csvMethod, + 'url' => $csvUrl, + 'time' => $csvTime, + 'parent' => $csvParent, + 'status_code' => $csvStatusCode, + ); + } + + fclose($file); + + return array_values($result); + } + + /** + * {@inheritdoc} + */ protected function createProfileFromData($token, $data, $parent = NULL) { $profile = new Profile($token); $profile->setIp($data['ip']);