Example #1
0
 /**
  * Method to get the comments on items from GitHub
  *
  * @return  $this
  *
  * @since   1.0
  */
 protected function fetchData()
 {
     if (!$this->changedIssueNumbers) {
         return $this;
     }
     $this->out(sprintf(g11n4t('Fetch events for one issue from GitHub...', 'Fetch events for <b>%d</b> issues from GitHub...', count($this->changedIssueNumbers)), count($this->changedIssueNumbers)), false);
     $progressBar = $this->getProgressBar(count($this->changedIssueNumbers));
     $this->usePBar ? $this->out() : null;
     foreach ($this->changedIssueNumbers as $count => $issueNumber) {
         $this->usePBar ? $progressBar->update($count + 1) : $this->out(sprintf('%d/%d - # %d: ', $count + 1, count($this->changedIssueNumbers), $issueNumber), false);
         $page = 0;
         $this->items[$issueNumber] = array();
         do {
             $page++;
             $events = $this->github->issues->events->getList($this->project->gh_user, $this->project->gh_project, $issueNumber, $page, 100);
             $this->checkGitHubRateLimit($this->github->issues->events->getRateLimitRemaining());
             $count = is_array($events) ? count($events) : 0;
             if ($count) {
                 $this->items[$issueNumber] = array_merge($this->items[$issueNumber], $events);
                 $this->usePBar ? null : $this->out($count . ' ', false);
             }
         } while ($count);
     }
     // Retrieved items, report status
     $this->out()->outOK();
     return $this;
 }
Example #2
0
 /**
  * Fetch avatars.
  *
  * @return  $this
  *
  * @since   1.0
  * @throws  \UnexpectedValueException
  */
 private function fetchAvatars()
 {
     /* @type \Joomla\Database\DatabaseDriver $db */
     $db = $this->getContainer()->get('db');
     $usernames = $db->setQuery($db->getQuery(true)->from($db->quoteName('#__activities'))->select('DISTINCT ' . $db->quoteName('user'))->order($db->quoteName('user')))->loadColumn();
     if (!count($usernames)) {
         throw new \UnexpectedValueException(g11n3t('No users found in database.'));
     }
     $this->logOut(sprintf(g11n4t('Processing avatars for one user.', 'Processing avatars for %d users.', count($usernames)), count($usernames)));
     $progressBar = $this->getProgressBar(count($usernames));
     $this->usePBar ? $this->out() : null;
     $base = JPATH_THEMES . '/images/avatars/';
     $adds = 0;
     $loginHelper = new GitHubLoginHelper($this->getContainer());
     foreach ($usernames as $i => $username) {
         if (!$username) {
             continue;
         }
         if (file_exists($base . '/' . $username . '.png')) {
             $this->debugOut(sprintf(g11n3t('User avatar already fetched for user %s'), $username));
             $this->usePBar ? $progressBar->update($i + 1) : $this->out('-', false);
             continue;
         }
         $this->debugOut(sprintf(g11n3t('Fetching avatar for user: %s'), $username));
         try {
             $loginHelper->saveAvatar($username);
             ++$adds;
         } catch (\DomainException $e) {
             $this->debugOut($e->getMessage());
             $this->debugOut(sprintf(g11n3t('Copy default image for user: %s'), $username));
             copy(JPATH_THEMES . '/images/avatars/user-default.png', JPATH_THEMES . '/images/avatars/' . $username . '.png');
         }
         $this->usePBar ? $progressBar->update($i + 1) : $this->out('+', false);
     }
     return $this->out()->logOut(sprintf(g11n3t('Added %d new user avatars'), $adds));
 }
Example #3
0
 /**
  * Test method.
  *
  * @return void
  */
 public function testPlural3()
 {
     $this->assertThat(g11n4t('Hey', 'Ho', 3), $this->equalTo('Ho'));
 }
Example #4
0
 /**
  * Method to process the list of issues and inject into the database as needed
  *
  * @return  $this
  *
  * @since   1.0
  */
 protected function processData()
 {
     if (!$this->items) {
         $this->logOut(g11n3t('Everything is up to date.'));
         return $this;
     }
     /* @type \Joomla\Database\DatabaseDriver $db */
     $db = $this->getContainer()->get('db');
     // Initialize our query object
     $query = $db->getQuery(true);
     $this->out(sprintf(g11n4t('Processing comments for one modified issue...', 'Processing comments for %d modified issues...', count($this->items)), count($this->items)));
     $adds = 0;
     $updates = 0;
     $count = 1;
     // Initialize our ActivitiesTable instance to insert the new record
     $table = new ActivitiesTable($db);
     // Comments ids for computing the difference
     $commentsIds = array();
     // Comments ids to delete
     $toDelete = array();
     // Start processing the comments now
     foreach ($this->items as $issueNumber => $comments) {
         if (!count($comments)) {
             $this->out()->out(sprintf(g11n3t('No comments for issue # %d'), $issueNumber));
         } else {
             $this->out()->out(sprintf(g11n4t('Processing one comment for issue # %2$d (%3$d/%4$d)', 'Processing %1$d comments for issue # %2$d (%3$d/%4$d)', count($comments)), count($comments), $issueNumber, $count, count($this->items)));
             $progressBar = $this->getProgressBar(count($comments));
             $this->usePBar ? $this->out() : null;
             foreach ($comments as $i => $comment) {
                 // Store the comment id for computing the difference
                 $commentsIds[] = $comment->id;
                 $check = $db->setQuery($query->clear()->select($table->getKeyName())->select($db->quoteName('updated_date'))->from($db->quoteName($table->getTableName()))->where($db->quoteName('gh_comment_id') . ' = ' . (int) $comment->id)->where($db->quoteName('project_id') . ' = ' . (int) $this->project->project_id))->loadObject();
                 if ($check) {
                     if (!$this->force) {
                         // If we have something already, check if it needs an update...
                         $d1 = new Date($check->updated_date);
                         $d2 = new Date($comment->updated_at);
                         if ($d1 == $d2) {
                             // No update required
                             $this->usePBar ? $progressBar->update($i + 1) : $this->out('-', false);
                             continue;
                         }
                     }
                     $table->load($check->{$table->getKeyName()});
                     $this->usePBar ? null : $this->out($this->force ? 'F ' : '~ ', false);
                 } else {
                     // New item
                     $table->reset();
                     $table->{$table->getKeyName()} = null;
                     $this->usePBar ? null : $this->out('+', false);
                 }
                 $table->gh_comment_id = $comment->id;
                 $table->issue_number = (int) $issueNumber;
                 $table->project_id = $this->project->project_id;
                 $table->user = $comment->user->login;
                 $table->event = 'comment';
                 $table->text_raw = $comment->body;
                 $table->text = $this->github->markdown->render($comment->body, 'gfm', $this->project->gh_user . '/' . $this->project->gh_project);
                 $this->checkGitHubRateLimit($this->github->markdown->getRateLimitRemaining());
                 $table->created_date = (new Date($comment->created_at))->format('Y-m-d H:i:s');
                 $table->updated_date = (new Date($comment->updated_at))->format('Y-m-d H:i:s');
                 $table->store();
                 if ($check) {
                     ++$updates;
                 } else {
                     ++$adds;
                 }
                 $this->usePBar ? $progressBar->update($i + 1) : null;
             }
             ++$count;
         }
         // Compute the difference between GitHub comments and issue comments
         $issueComments = $this->getIssueCommentsIds($issueNumber);
         $commentsToDelete = array_diff($issueComments, $commentsIds);
         $toDelete = array_merge($toDelete, $commentsToDelete);
     }
     // Delete comments which does not exist on GitHub
     if (!empty($toDelete)) {
         $this->deleteIssuesComments($toDelete);
     }
     $this->out()->outOK()->logOut(sprintf(g11n3t('%1$d added, %2$d updated, %3$d deleted.'), $adds, $updates, count($toDelete)));
     return $this;
 }
Example #5
0
 /**
  * Render database information.
  *
  * @return  string  HTML markup for database debug
  *
  * @since   1.0
  */
 protected function renderDatabase()
 {
     $debug = array();
     $dbLog = $this->getLog('db');
     if (!$dbLog) {
         return '';
     }
     $tableFormat = new TableFormat();
     $sqlFormat = new SqlFormat();
     $dbDebugger = new DatabaseDebugger($this->container->get('db'));
     $debug[] = sprintf(g11n4t('One database query', '%d database queries', count($dbLog)), count($dbLog));
     $prefix = $dbDebugger->getPrefix();
     foreach ($dbLog as $i => $entry) {
         $explain = $dbDebugger->getExplain($entry->sql);
         $debug[] = '<pre class="dbQuery">' . $sqlFormat->highlightQuery($entry->sql, $prefix) . '</pre>';
         if (isset($entry->times) && is_array($entry->times)) {
             $debug[] = sprintf('Query Time: %.3f ms', ($entry->times[1] - $entry->times[0]) * 1000) . '<br />';
         }
         // Tabs headers
         $debug[] = '<ul class="nav nav-tabs">';
         if ($explain) {
             $debug[] = '<li><a data-toggle="tab" href="#queryExplain-' . $i . '">Explain</a></li>';
         }
         if (isset($entry->trace) && is_array($entry->trace)) {
             $debug[] = '<li><a data-toggle="tab" href="#queryTrace-' . $i . '">Trace</a></li>';
         }
         if (isset($entry->profile) && is_array($entry->profile)) {
             $debug[] = '<li><a data-toggle="tab" href="#queryProfile-' . $i . '">Profile</a></li>';
         }
         $debug[] = '</ul>';
         // Tabs contents
         $debug[] = '<div class="tab-content">';
         if ($explain) {
             $debug[] = '<div id="queryExplain-' . $i . '" class="tab-pane">';
             $debug[] = $explain;
             $debug[] = '</div>';
         }
         if (isset($entry->trace) && is_array($entry->trace)) {
             $debug[] = '<div id="queryTrace-' . $i . '" class="tab-pane">';
             $debug[] = $tableFormat->fromTrace($entry->trace);
             $debug[] = '</div>';
         }
         if (isset($entry->profile) && is_array($entry->profile)) {
             $debug[] = '<div id="queryProfile-' . $i . '" class="tab-pane">';
             $debug[] = $tableFormat->fromArray($entry->profile);
             $debug[] = '</div>';
         }
         $debug[] = '</div>';
     }
     return implode("\n", $debug);
 }