Advertisement
cmptrwz

MVLC Part Fun

Feb 20th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. -- Fix "two parts on one copy" issues
  2. DELETE FROM asset.copy_part_map WHERE id IN (SELECT acpm.id FROM asset.copy_part_map acpm JOIN asset.copy acp ON acpm.target_copy = acp.id JOIN asset.call_number acn ON acp.call_number = acn.id JOIN biblio.monograph_part bmp ON bmp.id = acpm.part
  3. WHERE bmp.record != acn.record);
  4.  
  5. -- Remove unused parts
  6. DELETE FROM biblio.monograph_part WHERE id NOT IN (select part from asset.copy_part_map UNION select target from action.hold_request where hold_type = 'P' UNION select target from action.aged_hold_request WHERE hold_type = 'P');
  7.  
  8. -- Rename parts!
  9. CREATE OR REPLACE FUNCTION evergreen.mvlc_rename_part(part_id integer, new_label text) RETURNS void AS $func$
  10. DECLARE
  11. record_id bigint;
  12. existing_part integer;
  13. BEGIN
  14. SELECT INTO record_id record FROM biblio.monograph_part WHERE id = part_id;
  15. IF NOT FOUND THEN
  16. RETURN;
  17. END IF;
  18. PERFORM * FROM biblio.monograph_part WHERE id = part_id AND label = new_label;
  19. IF FOUND THEN
  20. RETURN;
  21. END IF;
  22. SELECT INTO existing_part id FROM biblio.monograph_part WHERE label = new_label AND record = record_id;
  23. IF FOUND THEN
  24. UPDATE asset.copy_part_map SET part = existing_part WHERE part = part_id;
  25. UPDATE action.hold_request SET target = existing_part WHERE target = part_id AND hold_type = 'P';
  26. UPDATE action.aged_hold_request SET target = existing_part WHERE target = part_id AND hold_type = 'P';
  27. DELETE FROM biblio.monograph_part WHERE id = part_id;
  28. ELSE
  29. UPDATE biblio.monograph_part SET label = new_label WHERE id = part_id;
  30. END IF;
  31. END;
  32. $func$ LANGUAGE plpgsql;
  33.  
  34. -- Standardize parts
  35. CREATE OR REPLACE FUNCTION evergreen.check_parts() RETURNS void AS $func$
  36. DECLARE
  37. current_part integer;
  38. current_label text;
  39. current_record bigint;
  40. new_label text;
  41. BEGIN
  42. <<partloop>>
  43. FOR current_part, current_label, current_record IN SELECT id, label, record FROM biblio.monograph_part
  44. -- TEST
  45. --WHERE label ~* 'disc'
  46. ORDER BY record, label
  47. --LIMIT 1000
  48. -- END TEST
  49. LOOP
  50. -- Entire-record exceptions
  51. CONTINUE WHEN current_record IN (1245044);
  52. -- Specific part exceptions
  53. CONTINUE WHEN current_part IN (2925,2905,3124,5616,4643,5814,5619,5620,5621,5622,5623,5624,5625,5626,5627,5628,6614,6299,6305,5360);
  54. -- We don't know what to do with bonus/guide/whatever
  55. CONTINUE WHEN current_label ~* ANY(ARRAY['bonus', 'guide']);
  56. new_label := current_label;
  57. IF new_label ~ ANY (ARRAY[
  58. '^((Season|Series|Part|DVD|Blu-ray) [0-9]+, )?Disc [0-9]+(-[0-9]+)?$',
  59. '^DVD$',
  60. '^Blu-ray$',
  61. '^Blu-ray \+ DVD$',
  62. '^v\. [0-9]+(-[0-9]+)?(, pt\. [0-9]+(-[0-9]+)?)?$',
  63. '^(January|February|March|April|May|June|July|August|September|October|November|December|Fall|Spring|Summer|Winter)(/(January|February|March|April|May|June|July|August|September|October|November|December))?$',
  64. '^c\. [0-9]+(-[0-9]+)?$'
  65. ]) THEN
  66. CONTINUE partloop; -- Part is good! Move on
  67. END IF;
  68.  
  69. new_label = regexp_replace(new_label, '^\s+', ''); -- Replace leading spaces
  70. new_label = regexp_replace(new_label, '\s+$', ''); -- Replace trailing spaces
  71.  
  72. -- Generic #-# cleanups?
  73. new_label := regexp_replace(new_label, '0*([0-9]+) *[-&+] *0*([0-9]+)', '\1-\2');
  74.  
  75. -- Digital copies? HAH!
  76. new_label := regexp_replace(new_label, ' *[+&] *(Digital|Ultraviolet) Copy', '', 'i');
  77.  
  78. IF new_label ~* 'blu' THEN -- Some form of Blu-ray?
  79. new_label := regexp_replace(new_label, 'blue?([- ]?rays?)?', 'Blu-ray', 'i');
  80. new_label := regexp_replace(new_label, '^dvd *[+&/] *blu-ray$', 'Blu-ray + DVD', 'i');
  81. new_label := regexp_replace(new_label, '^(1 )?blu-ray( disc)? *([+&/]|and) *(1 )?dvd( disc)?$', 'Blu-ray + DVD', 'i');
  82. new_label := regexp_replace(new_label, '^Blu-ray disc *$','Blu-ray','i');
  83. END IF;
  84.  
  85. -- Disc #(-#)?
  86. IF new_label ~* '^((Season|Series|Part|DVD|Blu-ray) [0-9], )?Disc.*' THEN -- Starts with Disc in some way
  87. new_label := regexp_replace(new_label, '^((Season|Series|Part|DVD|Blu-ray) [0-9], )?[Dd][Ii][Ss][Cc][Ss.]* *0*', '\1Disc '); -- Replace leading Disc variant(s) (and leading zeros)
  88. new_label := regexp_replace(new_label, '^((Season|Series|Part|DVD|Blu-ray) [0-9], )?Disc 0*([0-9]+)(,* *[0-9]+,*)? *[-&+] *0*([0-9]+).*$', '\1Disc \3-\5'); -- Range correction
  89. new_label := regexp_replace(new_label, '^(((Season|Series|Part|DVD|Blu-ray) [0-9], )?Disc [0-9]+(-[0-9]+)?).*$', '\1'); -- Strip trailing
  90. END IF;
  91.  
  92. new_label := regexp_replace(new_label, '^Volumes? ', 'v. ','i'); -- Change Volume into v.
  93. -- v. #(-#)?(, pt. #)?
  94. IF new_label ~* '^v(ol)?\..*' THEN -- Starts with v. or vol. in some way
  95. -- EXCEPTION FUN!
  96. IF current_part = 5303 THEN
  97. new_label := 'v. 1-2 + Guidebook';
  98. ELSIF current_part = 3072 THEN
  99. new_label := 'v. 1-3';
  100. ELSE
  101. new_label := regexp_replace(new_label, '^v(ol)?. *0*', 'v. ','i'); -- Start with "v. " and remove leading zeros
  102. new_label := regexp_replace(new_label, '-v\. *', '-', 'ig'); -- Remove extra v. later (part 4340, 6910 test cases)
  103. new_label := regexp_replace(new_label, '^v\. 0*([0-9]+) *[-&+] *0*([0-9]+) *$', 'v. \1-\2'); -- Range correction
  104. new_label := regexp_replace(new_label, '([0-9]),* *pt\. *([0-9]+(-[0-9]+)?).*', '\1, pt. \2','i'); -- Part designator?
  105. new_label := regexp_replace(new_label, '([0-9]),* *Disc *([0-9]+).*', '\1, Disc \2','i'); -- Disc designator?
  106. new_label := regexp_replace(new_label, '^(v\. [0-9]+(-[0-9]+)?(, (pt\.|Disc) [0-9]+(-[0-9]+)?)?).*', '\1'); -- Remove trailing elements
  107. END IF;
  108. END IF;
  109.  
  110. -- Month fun!
  111. --new_label := regexp_replace(new_label, '(January|February|March|April|May|June|July|August|September|October|November|December) *[-/] *(January|February|March|April|May|June|July|August|September|October|November|December)', '\1/\2', 'i');
  112.  
  113. IF new_label != current_label THEN
  114. RAISE NOTICE 'Change % % % %', current_part, current_record, current_label, new_label;
  115. PERFORM evergreen.mvlc_rename_part(current_part, new_label);
  116. CONTINUE partloop;
  117. END IF;
  118. RAISE NOTICE 'Unknown % % %', current_part, current_record, current_label;
  119. END LOOP;
  120. END;
  121. $func$ LANGUAGE plpgsql;
  122.  
  123. SELECT evergreen.check_parts();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement