zettelkasten.citations_ops¶
zettelkasten.citations_ops ¶
Zettelkasten citation-operations business logic.
Citation entity CRUD, dedup, promotion, and OpenAlex citation-graph expansion —
carved out of :mod:zettelkasten.server for clarity. server re-exports
every name here so existing importers keep working unchanged. Server-level graph
cache helpers (_get_graph, _reloaded_graph, …) are imported at module scope;
because server performs its re-export import of this module at end-of-file,
those names are fully defined by the time this import runs (no cycle).
create_citation ¶
create_citation(id: str, title: str, authors: list[str], year: int, doc_type: str = '', doi: str = '', zotero_key: str = '', source: str = '') -> str
Create a lightweight citation entity in _citations/.
Use this for referenced works that don't need a full source graph yet. Notes can link to citations via target_graph='_citations'.
When source is given, the citation id (whether freshly created or matched
to an existing one via dedup) is recorded on that source's _meta.yaml
references list, so the source owns a navigable list of the works it
cites — the same field citation(action="expand") populates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique citation ID (e.g. 'sharpe-1966-mutual-fund') |
required |
title
|
str
|
Full title of the work |
required |
authors
|
list[str]
|
List of author names |
required |
year
|
int
|
Publication year |
required |
doc_type
|
str
|
Document type (e.g. 'journal-article', 'book') |
''
|
doi
|
str
|
Digital Object Identifier |
''
|
zotero_key
|
str
|
Zotero item key |
''
|
source
|
str
|
Optional source graph name that cites this work; the resulting
citation id is appended to its |
''
|
Source code in zettelkasten/citations_ops.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
dedup_citations ¶
Merge duplicate citation entities in _citations/.
Groups citations by canonical key (DOI > arXiv > normalized author+year+ title). For each group with more than one member, keeps a canonical record (prefers one carrying a DOI, then the shortest id), backfills missing metadata fields from the duplicates, repoints all note links from the duplicate ids to the canonical id, and deletes the duplicate files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dry_run
|
bool
|
If true (default), report what would be merged without changing anything. Set false to apply. |
True
|
Source code in zettelkasten/citations_ops.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
unlink_citation ¶
unlink_citation(citation_id: str, sources: list[str] | None = None, remove_global: bool = False, dry_run: bool = False) -> str
Remove a citation's links from the graph — scoped, or globally.
The inverse of the various paths that attach a citation (link_notes
with target_graph="_citations", create_citation/expand recording
it on a source's references/cited_by). Two tiers, selected by
remove_global:
- Scoped unlink (
remove_global=False): drop everygraph="_citations"note link pointing atcitation_idfrom the note graphs named insources, and removecitation_idfrom those sources' meta reference lists. The global_citations/<id>.yamlentity is KEPT (it may still be referenced from other scopes). A WRITE, not a delete. - Global remove (
remove_global=True): scrub the links from EVERY note graph (sources +_cross) and EVERY source's meta lists, then delete the_citations/<id>.yamlfile and drop the node from warm indexes. A DELETE; callers must gate it likepromote/delete_note.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
citation_id
|
str
|
the citation entity id ( |
required |
sources
|
list[str] | None
|
note graphs to scrub for scoped unlink. Ignored (all graphs are
scanned) when |
None
|
remove_global
|
bool
|
also delete the global citation entity after scrubbing. |
False
|
dry_run
|
bool
|
report what WOULD change without writing. |
False
|
Source code in zettelkasten/citations_ops.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
promote_citation ¶
Promote a citation to a full source folder.
Reads the citation metadata from _citations/{id}.yaml, creates a source folder with _meta.yaml, and removes the citation file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
citation_id
|
str
|
ID of the citation to promote |
required |
Source code in zettelkasten/citations_ops.py
expand_corpus ¶
Acquire a cited-but-unowned work into the corpus, end to end.
The executable companion to rank_missing_papers: given an acquisition
target (a cited-but-unowned work id in _citations/, optionally carrying a
zotero_key), this ORCHESTRATES the existing primitives to bring it in —
it does not reimplement any of them. When a zotero_key is available it
FETCHES the PDF from the local Zotero install (:func:zotero_fetch_pdf) and
INGESTS it for a stable content_hash + cached full text
(:func:ingest_source), then PROMOTES the citation into an owned source
folder (:func:promote_citation) and wires the full-text pointer onto the
new source's meta.
Idempotent and partial-failure tolerant:
- Already owned — if a source folder for the target already exists, or its
content is already owned under another source (a
content_hashmatch reported by ingest), this is a safe no-op reportingstatus="already_owned"rather than a duplicate ingest or folder. - Degraded — with no
zotero_key, no local PDF, or a fetch/ingest error, the work is still promoted as a metadata-only owned source and the failure is reported in the structured result, never raised. Promotion is the only graph mutation and is atomic, so the corpus is never left half-mutated.
Returns a structured result describing each step (fetched / ingested
/ promoted), the resulting owned source name, and an overall
status (acquired | already_owned | failed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
citation_id
|
str
|
Id of the cited-but-unowned work to acquire (a |
''
|
zotero_key
|
str
|
Optional Zotero item key; falls back to the citation's stored |
''
|
Source code in zettelkasten/citations_ops.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
promote_citation_to_graph ¶
Promote an OpenAlex citation into an owned source, gated on Zotero ownership.
The executable behind the dashboard's per-citation "promote to a graph"
button. Where :func:expand_corpus degrades to a metadata-only promote when
no PDF is available, this REQUIRES the work to already live in the user's
Zotero library: it resolves a Zotero item key by matching the citation's DOI
or title against the local install (or the Web API) and only then delegates
to :func:expand_corpus to fetch the PDF, ingest it, promote the stub into
an owned source folder (a new graph), and wire the full-text pointer. The
promoted source keeps the citation id, so every source that already links
this work resolves to it as owned — the citation edges are preserved for free.
When the work is NOT yet in Zotero, NOTHING is mutated: the result reports
status="not_in_zotero" with the bibliographic detail the UI needs to
prompt the user to add it to Zotero first, then retry.
Returns a structured result with status one of: acquired |
already_owned | not_in_zotero | zotero_unconfigured | failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
citation_id
|
str
|
Id of the cited-but-unowned work to promote (a |
''
|
zotero_key
|
str
|
Optional Zotero item key; skips the library lookup when supplied. |
''
|
Source code in zettelkasten/citations_ops.py
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
expand_citations ¶
expand_citations(source: str = '', doi: str = '', direction: str = 'both', limit: int = 25, dry_run: bool = False) -> str
Expand a source's citation graph via OpenAlex (free, no API key).
Resolves a work by DOI (from the source's _meta.yaml, or the doi arg),
then pulls its references (backward) and/or the works that cite it
(forward), minting deduplicated citation entities in _citations/. When a
source graph is given, the discovered ids are recorded on its _meta.yaml
under references / cited_by so the raw citation graph is queryable
(e.g. by analyze_gaps) without flooding the note graph with edges.
Set OPENALEX_MAILTO to join OpenAlex's faster polite pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source graph name; its DOI is used and results recorded on it. |
''
|
doi
|
str
|
DOI to expand (overrides the source's DOI when provided). |
''
|
direction
|
str
|
'backward' (references), 'forward' (citing works), or 'both'. |
'both'
|
limit
|
int
|
Max works to fetch per direction (capped at 100 backward / 200 forward). |
25
|
dry_run
|
bool
|
If true, fetch and report without creating citations or editing meta. |
False
|
Source code in zettelkasten/citations_ops.py
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | |