<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[GlotPress][982] trunk: Rewrite how we get recent projects/translation sets of a user.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://glotpress.trac.wordpress.org/changeset/982">982</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://glotpress.trac.wordpress.org/changeset/982","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>markoheijnen</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-11-19 12:21:03 +0000 (Wed, 19 Nov 2014)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Rewrite how we get recent projects/translation sets of a user.
Increases the performance when you are dealing with a lot of translations.
It also stops returning human readable time through the API which should be calculated on display.

See <a href="http://glotpress.trac.wordpress.org/ticket/340">#340</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkgpincludesmiscphp">trunk/gp-includes/misc.php</a></li>
<li><a href="#trunkgpincludesroutesprofilephp">trunk/gp-includes/routes/profile.php</a></li>
<li><a href="#trunkgpincludesthingsuserphp">trunk/gp-includes/things/user.php</a></li>
<li><a href="#trunkgptemplatesprofilepublicphp">trunk/gp-templates/profile-public.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkgpincludesmiscphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/gp-includes/misc.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/gp-includes/misc.php        2014-11-19 10:08:07 UTC (rev 981)
+++ trunk/gp-includes/misc.php  2014-11-19 12:21:03 UTC (rev 982)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -353,3 +353,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                gp_clean_translation_set_cache( $set->id );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+/**
+ * Shows the time past since the given time
+ *
+ * @param int $time Unix time stamp you want to compare against.
+ */
+function gp_time_since( $time ) {
+       $time = time() - $time; // to get the time since that moment
+
+       $tokens = array (
+               31536000 => 'year',
+               2592000 => 'month',
+               604800 => 'week',
+               86400 => 'day',
+               3600 => 'hour',
+               60 => 'minute',
+               1 => 'second'
+       );
+
+       foreach ( $tokens as $unit => $text ) {
+               if ( $time < $unit ) {
+                       continue;
+               }
+
+               $numberOfUnits = floor( $time / $unit );
+
+               return $numberOfUnits . ' ' . $text . ( ( $numberOfUnits > 1 ) ? 's' : '' );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span></span></pre></div>
<a id="trunkgpincludesroutesprofilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/gp-includes/routes/profile.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/gp-includes/routes/profile.php      2014-11-19 10:08:07 UTC (rev 981)
+++ trunk/gp-includes/routes/profile.php        2014-11-19 12:21:03 UTC (rev 982)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> class GP_Route_Profile extends GP_Route_Main {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private $projects = array();
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function profile_get() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( !GP::$user->logged_in() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,70 +29,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $this->die_with_404();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $projects    = $user->get_recent_projects();
-               $locales = $user->locales_known();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $recent_projects = $user->get_recent_translation_sets( 5 );
+               $locales         = $user->locales_known();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $_recent_projects = array_slice( $projects, 0, 5 );
-
-               //recent projects
-               $recent_actions = array();
-               foreach ( $_recent_projects as $recent_project ) {
-                       $set = GP::$translation_set->find_one( array( 'id' => (int) $recent_project->id ) );
-
-                       if ( $set ) {
-                               $action = $this->get_project( $set );
-                               $action->set_id = $set->id;
-                               $action->human_time = $this->time_since( strtotime( $recent_project->last_updated ) );
-                               $action->last_updated = $recent_project->last_updated;
-                               $action->count = $recent_project->count;
-                               $recent_projects[] = $action;
-                       }
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 //validate to
</span><span class="cx" style="display: block; padding: 0 10px">                $permissions = GP::$user->get_permissions();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->tmpl( 'profile-public', get_defined_vars() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function get_project( $set ) {
-               if ( ! isset( $this->projects[ $set->project_id ] ) ) {
-                        $this->projects[ $set->project_id ] = GP::$project->get( $set->project_id );
-               }
-
-               $project = $this->projects[$set->project_id];
-               $project_url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
-               $set_name = gp_project_names_from_root( $project ) . ' | ' . $set->name_with_locale();
-
-               return (object) array(
-                       'project_id' => $project->id,
-                       'project_url' => $project_url,
-                       'set_name' => $set_name
-               );
-       }
-
-       private function time_since( $time ) {
-               $time = time() - $time; // to get the time since that moment
-
-               $tokens = array (
-                       31536000 => 'year',
-                       2592000 => 'month',
-                       604800 => 'week',
-                       86400 => 'day',
-                       3600 => 'hour',
-                       60 => 'minute',
-                       1 => 'second'
-               );
-
-               foreach ( $tokens as $unit => $text ) {
-                       if ( $time < $unit ) {
-                               continue;
-                       }
-
-                       $numberOfUnits = floor( $time / $unit );
-
-                       return $numberOfUnits . ' ' . $text . ( ( $numberOfUnits > 1 ) ? 's' : '' );
-               }
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunkgpincludesthingsuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/gp-includes/things/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/gp-includes/things/user.php 2014-11-19 10:08:07 UTC (rev 981)
+++ trunk/gp-includes/things/user.php   2014-11-19 12:21:03 UTC (rev 982)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,5 +1,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> class GP_User extends GP_Thing {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // For caching purposes
+       private $projects = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        var $table_basename = 'users';
</span><span class="cx" style="display: block; padding: 0 10px">        var $field_names = array( 'id', 'user_login', 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_status', 'display_name' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,40 +140,66 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        function reintialize_wp_users_object() {
+               global $gpdb, $wp_auth_object, $wp_users_object;
+               $wp_users_object = new WP_Users( $gpdb );
+               $wp_auth_object->users = $wp_users_object;
+       }
+
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function get_avatar( $size = 100 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return 'http://www.gravatar.com/avatar/' . md5( strtolower( $this->user_email ) ) . '?s=' . $size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return '//www.gravatar.com/avatar/' . md5( strtolower( $this->user_email ) ) . '?s=' . $size;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_recent_projects() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_recent_translation_sets( $amount = 5 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $gpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $translated = GP::$translation_set->many_no_map("
-                       SELECT translation_set_id, t.user_id, t.date_added, ts.locale, tmax.count
-                       FROM $gpdb->translations AS t
-                       INNER JOIN (
-                               SELECT MAX(date_added) AS date_added, count(*) AS count
-                               FROM $gpdb->translations
-                               WHERE user_id = %s
-                               GROUP BY translation_set_id
-                       ) AS tmax ON tmax.date_added = t.date_added
-                       INNER JOIN $gpdb->translation_sets AS ts WHERE ts.id = t.translation_set_id
-                       ORDER BY t.date_added DESC
-                       LIMIT 10
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $translations = GP::$translation_set->many_no_map("
+                       SELECT translation_set_id, date_added
+                       FROM $gpdb->translations as t
+                       WHERE
+                               date_added >= DATE_SUB(NOW(), INTERVAL 2 MONTH) AND
+                               user_id = %s AND
+                               status != 'rejected'
+                       ORDER BY date_added DESC
</ins><span class="cx" style="display: block; padding: 0 10px">                 ", $this->id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $projects = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $set_ids          = array();
+               $translation_sets = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $translated as $translations ) {
-                       $set = new stdClass;
-                       $set->id = $translations->translation_set_id;
-                       $set->count = $translations->count;
-                       $set->locale = $translations->locale;
-                       $set->last_updated = $translations->date_added;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $i = 0;
+               foreach ( $translations as $translation ) {
+                       if ( in_array( $translation->translation_set_id, $set_ids ) ) {
+                               continue;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $projects[ $translations->translation_set_id ] = $set;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $set_ids[] = $translation->translation_set_id;
+
+                       $set = GP::$translation_set->find_one( array( 'id' => (int) $translation->translation_set_id ) );
+
+                       if ( $set ) {
+                               $translation_set = $this->get_translation_set( $set );
+
+                               if ( ! $project ) {
+                                       continue;
+                               }
+
+                               $translation_set->set_id       = $set->id;
+                               $translation_set->last_updated = $translation->date_added;
+                               $translation_set->count        = $gpdb->get_var( $gpdb->prepare( "SELECT COUNT(*) FROM $gpdb->translations WHERE user_id = %s AND status != 'rejected' AND translation_set_id = %s", $this->id, $translation->translation_set_id ) );
+
+                               $translation_sets[] = $translation_set;
+
+                               $i++;
+
+                               // Bail early if we have already the amount requested
+                               if ( $i >= $amount ) {
+                                       break;
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return $projects;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $translation_sets;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function locales_known() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -200,13 +228,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $locales;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-       function reintialize_wp_users_object() {
-               global $gpdb, $wp_auth_object, $wp_users_object;
-               $wp_users_object = new WP_Users( $gpdb );
-               $wp_auth_object->users = $wp_users_object;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Retrieve a users permissions.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,7 +249,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        unset( $permission->id, $permission->action, $permission->object_type, $permission->object_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $set ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $permission = (object) array_merge( (array) $permission, (array) $this->get_project( $set ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $permission = (object) array_merge( (array) $permission, (array) $this->get_translation_set( $set ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $permission->set_id = $set->id;
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                unset( $permissions[$key] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -238,5 +259,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $permissions;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+       private function get_translation_set( $set ) {
+               if ( ! isset( $this->projects[ $set->project_id ] ) ) {
+                        $this->projects[ $set->project_id ] = GP::$project->get( $set->project_id );
+               }
+
+               $project = $this->projects[$set->project_id];
+
+               if ( ! $project ) {
+                       return false;
+               }
+
+               $project_url = gp_url_project( $project, gp_url_join( $set->locale, $set->slug ) );
+               $set_name = gp_project_names_from_root( $project ) . ' | ' . $set->name_with_locale();
+
+               return (object) array(
+                       'project_id' => $project->id,
+                       'project_url' => $project_url,
+                       'set_name' => $set_name
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> GP::$user = new GP_User();
</span></span></pre></div>
<a id="trunkgptemplatesprofilepublicphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/gp-templates/profile-public.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/gp-templates/profile-public.php     2014-11-19 10:08:07 UTC (rev 981)
+++ trunk/gp-templates/profile-public.php       2014-11-19 12:21:03 UTC (rev 982)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,7 +34,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                sprintf( _n( '%s contribution', '%s contributions',$project->count ), $project->count ) );
</span><span class="cx" style="display: block; padding: 0 10px">                                ?></p>
</span><span class="cx" style="display: block; padding: 0 10px">                                <p class="ago">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        <?php printf( __( 'last translation about %s ago (UTC)' ), $project->human_time ); ?>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 <?php printf( __( 'last translation about %s ago (UTC)' ), gp_time_since( backpress_gmt_strtotime( $project->last_updated ) ) ); ?>
</ins><span class="cx" style="display: block; padding: 0 10px">                                 </p>
</span><span class="cx" style="display: block; padding: 0 10px">                        </li>
</span><span class="cx" style="display: block; padding: 0 10px">                <?php endforeach; ?>
</span></span></pre>
</div>
</div>

</body>
</html>