CSV person importer
View_Admin__Import (views/view_10_admin__7_import.class.php, ?view=admin__import, sysadmin
only) imports persons, families, groups and notes. User-facing documentation:
Importing Persons.
The parse and apply logic lives in Person_Importer (include/person_importer.class.php); the
view is a thin UI wrapper around it, and scripts/import_persons.php drives the same class
from cron.
Stages
- begin: the upload form. Upload is CSV (
,); pasted text is TSV (\t); a Google Sheets link is fetched as CSV (the named tab, the tab named by a#gid=, or the last tab). The Google fields mirror the script's options: URL, optional tab, and an optional service-account key upload that is used for that request only. - preview:
Person_Importer::parse()builds the plan into$_SESSION['import'](new_families,new_persons,person_updates,family_updates,new_groups,matched_groups, ...) and the view renders_printConfirmView(). Row problems areE_USER_NOTICE/E_USER_WARNINGs captured by_captureErrors()/_handleError(). Any captured error sends the user back to begin with a per-row error list. - import:
Person_Importer::apply()replays the plan inside one transaction. Any error rolls everything back. The view then redirects by<script>to the import's group. Each updated family and person is edit-locked only while it is saved, then released, so imports (notably the CLI, which never logs out) leave no lock rows behind.
Sources
The begin view renders one radio per Import_Source (include/import_source.class.php).
Import_Source::printChooser() prints the radios and a show/hide container per source; each
source prints its own fields (printFields()) and turns the submitted request into a temporary
CSV plus separator (fetch()), which Person_Importer::parse() consumes.
Adding a source
Subclass Import_Source, return an instance from getSources(), and implement
printFields()/fetch(). The view and the preview/confirm stages stay unchanged.
Layering: UI adapter vs transport
Import_Source is a web adapter, not a general data-source layer. It exists so the begin view
can render fields and read them from $_REQUEST/$_FILES; printFields() has no meaning in a
CLI context.
The reusable transport belongs in a client class: Google_Sheets_Client resolves links, fetches
CSV and handles service-account auth. Person_Importer is shared by both front ends.
scripts/import_persons.php calls those directly and keeps its own flag parsing; it does not use
Import_Source.
Routing the CLI through Import_Source would mean translating flags into a $_REQUEST-shaped
array and fabricating a $_FILES entry — an adapter for an adapter — while the only shared step
would be the thin fetch() wrapper around the client. Keep shared behaviour in the transport
client and a thin adapter at each front end.
For a Wufoo source, that means a Wufoo_Client (API calls, pagination, entry-to-CSV, field
mapping) shared by Import_Source_Wufoo and a --wufoo script option, rather than compelling
the CLI to use the source class.
Columns
Person_Importer::getSampleHeader() is the single list of recognised columns.
getSampleHeader(TRUE) returns the required subset (last_name, first_name, age_bracket).
call_sample_import builds the downloadable sample file from the full list. Headers are
normalised by _stringToKey() (lower case, spaces → _). The header id is renamed to
personid, because Jethro's own CSV exports (call_csv, report CSV) head the person ID column
ID. The aliases mobile and notes are accepted for mobile_tel and note, so form exports
need no renaming.
Optional columns fall back as follows:
family_name: when the file has nofamily_namecolumn at all, the row'slast_nameis used, so form exports that collect only a surname still group people into families. Files that do have the column keep the usual continuation semantics: a blank cell stays in the family above.status: blank status is only defaulted for new persons (to the status markedis_default); a blank value on a matched row leaves the existing status unchanged. A non-blank value must be an existing status label (case-insensitive); an unknown label is a row error that lists the available labels.congregation: may be blank. It is still validated against the person's status, so a status that requires a congregation raises a row error when the cell is empty.
Matching existing persons — _findExistingPerson()
- Non-blank
personid: authoritative. It returns that person, archived or not, ignoring names. It raises a row error, and never falls back to name matching, when the value is not a positive integer, when no such person exists, or whenmatch_existingis off. A wrong explicit ID would otherwise silently create a duplicate of the person the file named. - Blank or absent
personid: whenmatch_existingis on, it matches by exact first + last name.match_email/match_mobile_teldiscard candidates whose non-blank value differs. Only a unique match counts.
A matched row updates the person and their family (person_updates/family_updates). Following
unmatched rows of the same family become new_persons in that existing family. The
overwrite_existing flag is passed to DB_Object::fromCsvRow(), which never overwrites with a
blank value.
Headless use (cron)
scripts/import_persons.php fetches a CSV from a Google Sheet or local file, validates it with
Person_Importer, and applies it in one transaction. Typical target: a Google Sheet tab exported
as CSV.
php scripts/import_persons.php \
--google-spreadsheet='https://docs.google.com/spreadsheets/d/<id>/edit?usp=sharing' \
--create-group='Signup sheet import' \
--match-existing
--google-spreadsheet=<url>accepts the share/edit link shown in the browser, an existing export/gviz/pub link, or a published (/d/e/<id>/pubhtml) link. Share/edit links are rewritten to.../export?format=csv, preserving agidtab selector if present.--google-spreadsheet-tab=<name>imports a named tab (added assheet=); without it, a#gid=in the link wins, otherwise the last tab is used. Google's plain CSV export would return the first tab. Hidden tabs are not included in that lookup, so name one explicitly. Published links need agid, because they have no tab-name lookup.--file=<path>reads a local CSV instead; other source-specific options can be added later. The script never echoes the URL in its output. A response that starts with HTML (for example a link that is not publicly readable) is rejected before parsing.- The CSV must contain the required columns (
last_name,first_name,age_bracket); optional columns fall back as described under Columns. Extra columns are ignored with a warning, so a source-specific key column (for example a form response ID) can be carried through harmlessly. --group=<id>or--create-group=<name>is required. Every imported person is added to that group, which gives an audit trail and a practical undo (remove the group membership).--create-groupcreates a new group on every run; for a recurring import that keeps a per-run audit trail, or pass--group=<id>to accumulate one stable group.--match-existingupdates persons matched by name;--match-emailand--match-mobilenarrow the match. Without matching, rerunning the same file creates duplicates.--no-overwritekeeps existing non-blank values when updating; by default the file overwrites them.--creator=<staff-id>sets the staff account credited for the import. Default: the lowest staff id. A staff account is required becausefamily,personand note rows have a non-nullcreator, and adding group members checksPERM_EDITGROUP.--config=<path>uses an alternate Jethroconf.php(default: the checkout's own). The functional test (tests/functional/import/import-cli.spec.ts) points it atdevbox.d/functest_jethro_server/conf.phpto import intojethro_functest.--dry-runvalidates and reports without writing.--verboseprints the same detail as the Admin import preview: the audit group, groups to be created/matched, and every new or updated family and person. Parse messages and per-row errors are printed regardless; progress goes to stderr. On success the finalImported:line reports the committed counts.- The whole file is one transaction. Any row error rolls everything back and the script exits non-zero; there is no partial import.
- Without
--google-service-account, the URL must be readable without OAuth (Share → Anyone with the link → Viewer); the script uses the anonymous CSV export endpoint. With a service-account key the sheet can stay private; see Private sheets (service account).
Private sheets (service account)
Pass --google-service-account=<path> to read the sheet through the Sheets API instead of the
public CSV export. The spreadsheet stays private: share it with the service account as Viewer
and enable the Sheets API in that project. Setup, key handling and troubleshooting:
Google Sheets service account.
The client signs a JWT with the key, exchanges it for a short-lived access token, and reads the
tab with spreadsheets.values.get. Only
https://www.googleapis.com/auth/spreadsheets.readonly is requested. The script warns if the key
path is inside the checkout. Without --google-spreadsheet-tab, the last visible tab is used (as
with the CSV export path); a #gid= in the URL selects that sheet. Published (/d/e/...) links
are rejected in this mode.
History
- #1577:
personidwas briefly a required column, and the matching code readid/person_idinstead ofpersonid, so documented ID matching never happened. ID matching also ignoredmatch_existing.
Tests
Playwright project import has two specs:
tests/functional/import/import.spec.tscovers the Admin → Import upload → preview → import flow, includingpersonid/IDmatching and a preview with nostatuscolumn.tests/functional/import/import-cli.spec.tsrunsscripts/import_persons.php --fileagainstjethro_functest(via--config), asserting the created person/family/group and that an invalid row writes nothing.
The walkthrough spec checks that testdata/import-persons.csv reaches the preview stage.