Commit f0fd10e2 authored by Nigel Cunningham's avatar Nigel Cunningham Committed by Robert Lyon
Browse files

(Bug 1364687) Don't look for an artefact record when we know we're making a new one.



Avoid the query for an existing artefact record when we know it doesn't
exist (because we're being called from create_user). This shaves
approximately 40% off the time for creating a user.

Change-Id: I81697dfd0916e4a2933dfe5d1389bdc649e6e856
Signed-off-by: default avatarNigel Cunningham <nigelc@catalyst-au.net>
parent d05c68b8
...@@ -398,15 +398,17 @@ class ArtefactTypeProfile extends ArtefactType { ...@@ -398,15 +398,17 @@ class ArtefactTypeProfile extends ArtefactType {
/** /**
* overriding this because profile fields * overriding this because profile fields
* are unique in that except for email, you only get ONE * are unique in that except for email, you only get ONE
* so if we don't get an id, we still need to go look for it * so if we don't get an id, we still need to go look for it.
* On the other hand, if our caller knows the artefact is new,
* we can skip the query.
*/ */
public function __construct($id=0, $data=null) { public function __construct($id=0, $data=null, $new = FALSE) {
$type = $this->get_artefact_type(); $type = $this->get_artefact_type();
if (!empty($id) || $type == 'email' || $type == 'socialprofile') { if (!empty($id) || $type == 'email' || $type == 'socialprofile') {
return parent::__construct($id, $data); return parent::__construct($id, $data);
} }
if (!empty($data['owner'])) { if (!empty($data['owner'])) {
if ($a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) { if (!$new && $a = get_record('artefact', 'artefacttype', $type, 'owner', $data['owner'])) {
return parent::__construct($a->id, $a); return parent::__construct($a->id, $a);
} }
else { else {
......
...@@ -257,10 +257,15 @@ abstract class ArtefactType implements IArtefactType { ...@@ -257,10 +257,15 @@ abstract class ArtefactType implements IArtefactType {
* If an id is supplied, will query the database * If an id is supplied, will query the database
* to build up the basic information about the object. * to build up the basic information about the object.
* If an id is not supplied, we just create an empty * If an id is not supplied, we just create an empty
* artefact, ready to be filled up * artefact, ready to be filled up.
* @param int $id artefact.id * If the $new parameter is true, we can skip the query
* because we know the artefact is new.
*
* @param int $id artefact.id
* @param mixed $data optional data supplied for artefact
* @param bool $new
*/ */
public function __construct($id=0, $data=null) { public function __construct($id=0, $data=null, $new = FALSE) {
if (!empty($id)) { if (!empty($id)) {
if (empty($data)) { if (empty($data)) {
if (!$data = get_record('artefact','id',$id)) { if (!$data = get_record('artefact','id',$id)) {
......
...@@ -500,18 +500,24 @@ function plugin_account_prefs_submit(Pieform $form, $values) { ...@@ -500,18 +500,24 @@ function plugin_account_prefs_submit(Pieform $form, $values) {
* @param int $userid * @param int $userid
* @param string $field * @param string $field
* @param string (or array for socialprofile) $value * @param string (or array for socialprofile) $value
* @param int $new - Whether the user is new (avoid unnecessary queries)
*/ */
function set_profile_field($userid, $field, $value) { function set_profile_field($userid, $field, $value, $new = FALSE) {
safe_require('artefact', 'internal'); safe_require('artefact', 'internal');
// this is a special case that replaces the primary email address with the // this is a special case that replaces the primary email address with the
// specified one // specified one
if ($field == 'email') { if ($field == 'email') {
try { if (!$new) {
$email = artefact_instance_from_type('email', $userid); try {
$email = artefact_instance_from_type('email', $userid);
}
catch (ArtefactNotFoundException $e) {
// We'll create a new artefact then.
}
} }
catch (ArtefactNotFoundException $e) { if (!isset($email)) {
$email = new ArtefactTypeEmail(); $email = new ArtefactTypeEmail(0, null, TRUE);
$email->set('owner', $userid); $email->set('owner', $userid);
} }
$email->set('title', $value); $email->set('title', $value);
...@@ -519,7 +525,7 @@ function set_profile_field($userid, $field, $value) { ...@@ -519,7 +525,7 @@ function set_profile_field($userid, $field, $value) {
} }
else if ($field == 'socialprofile') { else if ($field == 'socialprofile') {
$classname = generate_artefact_class_name($field); $classname = generate_artefact_class_name($field);
$profile = new $classname(0, array('owner' => $userid)); $profile = new $classname(0, array('owner' => $userid), $new);
$profile->set('title', $value['socialprofile_profileurl']); $profile->set('title', $value['socialprofile_profileurl']);
$profile->set('description', $value['socialprofile_service']); $profile->set('description', $value['socialprofile_service']);
$profile->set('note', $value['socialprofile_profiletype']); $profile->set('note', $value['socialprofile_profiletype']);
...@@ -527,7 +533,7 @@ function set_profile_field($userid, $field, $value) { ...@@ -527,7 +533,7 @@ function set_profile_field($userid, $field, $value) {
} }
else { else {
$classname = generate_artefact_class_name($field); $classname = generate_artefact_class_name($field);
$profile = new $classname(0, array('owner' => $userid)); $profile = new $classname(0, array('owner' => $userid), $new);
$profile->set('title', $value); $profile->set('title', $value);
$profile->commit(); $profile->commit();
} }
...@@ -2281,19 +2287,19 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul ...@@ -2281,19 +2287,19 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
} }
if (isset($user->email) && $user->email != '') { if (isset($user->email) && $user->email != '') {
set_profile_field($user->id, 'email', $user->email); set_profile_field($user->id, 'email', $user->email, TRUE);
} }
if (isset($user->firstname) && $user->firstname != '') { if (isset($user->firstname) && $user->firstname != '') {
set_profile_field($user->id, 'firstname', $user->firstname); set_profile_field($user->id, 'firstname', $user->firstname, TRUE);
} }
if (isset($user->lastname) && $user->lastname != '') { if (isset($user->lastname) && $user->lastname != '') {
set_profile_field($user->id, 'lastname', $user->lastname); set_profile_field($user->id, 'lastname', $user->lastname, TRUE);
} }
foreach ($profile as $k => $v) { foreach ($profile as $k => $v) {
if (in_array($k, array('firstname', 'lastname', 'email'))) { if (in_array($k, array('firstname', 'lastname', 'email'))) {
continue; continue;
} }
set_profile_field($user->id, $k, $v); set_profile_field($user->id, $k, $v, TRUE);
} }
if (!empty($institution)) { if (!empty($institution)) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment