* Rename / Append Plugin Code

For plugin authors to discuss plugin programming
Post Reply
User avatar
ADC65
Superstar
Posts: 473
Joined: 09 Jul 2007 10:27
Family Historian: V7

Rename / Append Plugin Code

Post by ADC65 »

I would like to write what is probably a simple plugin, but my LUA skills, once basic, have now deserted me, and although I have re-skimmed some of the FH and KB LUA help, it gets too technical too quickly for me and because of other commitments I don't want to spend a large amount of time brushing up for this one exercise. So I am looking for some help in constructing some code please. I am conversant enough to accept hints and snippets and work with that, rather than expect someone to do it all for me. One of my stumbling blocks is also the FH data structure.

I wish to rename some of my Source Records. For Source Records beginning with a particular string (say, "Birth Certificate"), I wish to append the Record ID of the primary person it represents, and by that I mean the ID of the person that has the INDI.BIRT.SOUR data reference for that source record. The Source Record may be attached to a number of people and facts, i.e., primary person's birth place, mother's address, father's occupation, etc., but I'm not interested in those facts.

The way my data is structured, each Birth Certificate source record will have always have one and only one INDI.BIRT.SOUR; there may or not be other facts.

I've started a loop through the SOUR records, and tested SOUR.TITL to check for records that begin with "Birth Certificate". But I'm unsure how to obtain the Person ID which has the INDI.BIRT.SOUR as this appears to be in a different data structure?

A snippet of code just to make sure I was looking at the right records is below. I know this is very simple to our expert programmers here, so be gentle 😳

Code: Select all

pi = fhNewItemPtr()
pi:MoveToFirstRecord("SOUR")
iCountBC = 0
while pi:IsNotNull() do
	ptrTitle = fhGetItemPtr(pi,'SOUR.TITL')
	strTitle = fhGetValueAsText(ptrTitle)
	if string.sub(strTitle,1,20) == "Birth Certificate - " then
		iCountBC = iCountBC + 1
	end
pi:MoveNext()
end
fhMessageBox('Count of BC is '..iCountBC)
Adrian Cook
Researching Cook, Summers, Phipps and Bradford, mainly in Wales and the South West of England
avatar
jelv
Megastar
Posts: 645
Joined: 03 Feb 2020 22:57
Family Historian: V7
Location: Mere, Wiltshire

Re: Rename / Append Plugin Code

Post by jelv »

I think I'd have been tempted to loop the other way round, i.e. loop through all the individuals checking all the sources for birth to see if the name begins "Birth Certificate".
John Elvin
User avatar
ADC65
Superstar
Posts: 473
Joined: 09 Jul 2007 10:27
Family Historian: V7

Re: Rename / Append Plugin Code

Post by ADC65 »

jelv wrote: 13 Apr 2024 09:02 I think I'd have been tempted to loop the other way round, i.e. loop through all the individuals checking all the sources for birth to see if the name begins "Birth Certificate".
I did consider that and realised I might be approaching it the wrong way round, even though I am working on changes to the Sources. But I'm still left puzzling how I access the SOUR.TITL when I am looking at an INDI.BIRT.SOUR, as they appear to be in separate data structures. The LUA pointers in different structures aren't intuitive for me.
Adrian Cook
Researching Cook, Summers, Phipps and Bradford, mainly in Wales and the South West of England
User avatar
Mark1834
Megastar
Posts: 2534
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Rename / Append Plugin Code

Post by Mark1834 »

For a quick and dirty just for single use, I would be tempted to do it the other way around, but it does involve links between records, which can be more difficult to get your head around.
  1. Loop though Individuals, not sources.
  2. Get the pointer linking to the birth fact source (fhGetItemPtr(pi, '~.BIRT.SOUR). This will return the first source only, but from your description, this is the one you want.
  3. Convert the link to an actual pointer to the record using pSource = fhGetValueAsLink(pLink).
  4. You now have a pointer to the relevant source record, plus the Record ID of the individual (retrieved with fhGetRecordId(pi), so you can proceed to change the source title using a combination of fhGetValueAsText(...) and fhSetValueText(...).
There are no checks here to ensure that all the data are structured as you anticipate, so it depends how robust you want it to be. It's also untested and just off the top of my head, so step through it carefully in the debugger to ensure it is doing what it should be doing!

PS - composed while John posted, but it adds extra details so left unchanged...
Mark Draper
avatar
jelv
Megastar
Posts: 645
Joined: 03 Feb 2020 22:57
Family Historian: V7
Location: Mere, Wiltshire

Re: Rename / Append Plugin Code

Post by jelv »

INDI.BIRT[1].SOUR[1]>TITL

In the plugin editor, Edit, Insert Data Reference is your friend.
John Elvin
User avatar
ADC65
Superstar
Posts: 473
Joined: 09 Jul 2007 10:27
Family Historian: V7

Re: Rename / Append Plugin Code

Post by ADC65 »

Excellent, many thanks both, that's very helpful. Certainly a prod in the right direction helps. I'm out this afternoon but will give it a go this evening and let you know if I cracked it.
Adrian Cook
Researching Cook, Summers, Phipps and Bradford, mainly in Wales and the South West of England
User avatar
Mark1834
Megastar
Posts: 2534
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Rename / Append Plugin Code

Post by Mark1834 »

Yes, John's version is easier as it gets the source title directly without having to worry about converting links. For this exercise, that's all you need.

Any of the following should work:

Code: Select all

pTitle = fhGetItemPtr(pI, '~.BIRT.SOUR>TITL')
pTitle = fhGetItemPtr(pI, '~.BIRT[1].SOUR[1]>TITL')
pTitle = fhGetItemPtr(pI, '%INDI.BIRT.SOUR>TITL%')
pTitle = fhGetItemPtr(pI, '%INDI.BIRT[1].SOUR[1]>TITL%')
Mark Draper
User avatar
ADC65
Superstar
Posts: 473
Joined: 09 Jul 2007 10:27
Family Historian: V7

Re: Rename / Append Plugin Code

Post by ADC65 »

Mark1834 wrote: 13 Apr 2024 09:09 Get the pointer linking to the birth fact source (fhGetItemPtr(pi, '~.BIRT.SOUR). This will return the first source only, but from your description, this is the one you want.
There may be more than one source for the birth, so I guess I may have to loop through these? For example, the first source might be a Census, which was added before I obtained a certificate. Then the certificate was added later, but the census was not removed. So there will be two INDI.BIRT.SOUR records if I am imagining this correctly, and the certificate may or may not be the first of these.

The way I was describing in the post above was from the point of view of a source. If the SOUR.TITL shows it is a Birth Certificate, then for all the facts the source may be attached to, only one of them will be a INDI.SOUR.BIRT

(Your post came through while I was typing, I will have a go later, thanks again for the clues)
Adrian Cook
Researching Cook, Summers, Phipps and Bradford, mainly in Wales and the South West of England
User avatar
Mark1834
Megastar
Posts: 2534
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Rename / Append Plugin Code

Post by Mark1834 »

Ah - if the Birth Certificate is not always the first source, it does get more complicated. However, your approach is the way to go, looping though the sources and testing each in turn with a combination of pSource:MoveTo(pI, Data Reference) and pSource:MoveNext('SAME_TAG'). Examples in the plugin help.
Mark Draper
User avatar
LornaCraig
Megastar
Posts: 3220
Joined: 11 Jan 2005 17:36
Family Historian: V7
Location: Oxfordshire, UK

Re: Rename / Append Plugin Code

Post by LornaCraig »

I've moved this to the Programming Technicalities sub-forum.
Lorna
User avatar
ADC65
Superstar
Posts: 473
Joined: 09 Jul 2007 10:27
Family Historian: V7

Re: Rename / Append Plugin Code

Post by ADC65 »

Thank you both for your help, I sorted it all succesfully. A lot of my knowledge (not a lot to start with) came back once I got started. It is a top tip to use the Edit > Insert Data Reference menu option, I usually use the one in the Query builder which is a faff swapping back and forth.
Adrian Cook
Researching Cook, Summers, Phipps and Bradford, mainly in Wales and the South West of England
Post Reply