You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
12 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./import_template_base.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/contrib/import_templates/import_template_base.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 11-Jan-06, 18:36
  15. // Modified: $Date: 2012-02-27 20:25:30 +0000 (Mon, 27 Feb 2012) $
  16. // $Author: msteffens $
  17. // $Revision: 1337 $
  18. // Template for a batch import script.
  19. // Use this script to develop your own batch importer.
  20. // See the scripts 'import.php' and 'import_modify.php' for a working example.
  21. // Incorporate some include files:
  22. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  23. include 'includes/include.inc.php'; // include common functions
  24. include 'initialize/ini.inc.php'; // include common variables
  25. // --------------------------------------------------------------------
  26. // START A SESSION:
  27. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  28. start_session(true);
  29. // --------------------------------------------------------------------
  30. // Initialize preferred display language:
  31. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  32. include 'includes/locales.inc.php'; // include the locales
  33. // --------------------------------------------------------------------
  34. // Get the referring page (or set a default one if no referrer is available):
  35. if (!empty($_SERVER['HTTP_REFERER'])) // if the referrer variable isn't empty
  36. $referer = $_SERVER['HTTP_REFERER']; // on error, redirect to calling page
  37. else
  38. $referer = "import.php"; // on error, redirect to the web import form (if you've got your own import form, insert it's script name here)
  39. // First of all, check if the user is logged in:
  40. if (!isset($_SESSION['loginEmail'])) // -> if the user isn't logged in
  41. {
  42. header("Location: user_login.php?referer=" . rawurlencode($referer)); // ask the user to login first, then he'll get directed back to the calling page
  43. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  44. }
  45. // now, check if the (logged in) user is allowed to import any record into the database:
  46. if (isset($_SESSION['user_permissions']) AND !preg_match("/allow_import|allow_batch_import/", $_SESSION['user_permissions'])) // if the 'user_permissions' session variable does NOT contain either 'allow_import' or 'allow_batch_import'...
  47. {
  48. // return an appropriate error message:
  49. $HeaderString = returnMsg($loc["NoPermission"] . $loc["NoPermission_ForImport"] . "!", "warning", "strong", "HeaderString"); // function 'returnMsg()' is defined in 'include.inc.php'
  50. header("Location: index.php"); // redirect back to main page ('index.php')
  51. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  52. }
  53. // --------------------------------------------------------------------
  54. // PROCESS SOURCE DATA:
  55. $parsedRecordsArray = array(); // initialize array variable which will hold parsed data of all records that shall be imported
  56. // >>> insert your own code here that parses your source data into an array of arrays holding the extracted field data <<<
  57. // ToDo:
  58. // (1) Obtain your source data via a web form, file upload, direct read-in of a local source file, etc
  59. // If your script allows input from the web, make sure to validate your data, see 'import.php' & 'import_modify.php' for an example
  60. // (2) Split your source data into individual bits representing individual records
  61. // (3) Loop over each record and extract the record's field data into an array;
  62. // For each record you should end up with an array structure similar to the one below ('$recordFieldParametersArray'):
  63. // NOTES: - you can safely omit unneeded fields from your data array; for any fields that aren't present in the data array, the database will insert its default values
  64. // - the 'addRecords()' function will take care of the calculation fields ('first_author', 'author_count', 'first_page', 'volume_numeric' and 'series_volume_numeric')
  65. // - similarly, the *date/*time/*by fields ('created_date', 'created_time', 'created_by', 'modified_date', 'modified_time', 'modified_by') will be filled automatically
  66. // if no custom values (in correct date ['YYYY-MM-DD'] and time ['HH:MM:SS'] format) are given in your array
  67. // - you can pass any custom info for the 'location' field in your array; however, if you omit the 'location' field from the array
  68. // the 'addRecords()' function will insert name & email address of the currently logged-in user (e.g. 'Matthias Steffens (refbase@extracts.de)')
  69. // - the serial number(s) will be assigned automatically (and returned by the 'addRecords()' function in form of an array)
  70. // - this import example will only add to the main MySQL table ('refs'), but not to the 'user_data' table
  71. // Example record data array. Commented fields will be filled automatically if not present (see notes above).
  72. // Comments behind field spec give the resulting calculation field values for this example:
  73. $recordFieldParametersArray = array(
  74. 'author' => "FirstAuthor, Initials; SecondAuthor, Initials", // 'first_author' = "FirstAuthor, Initials"; 'author_count' = "2"
  75. 'address' => "Address",
  76. 'corporate_author' => "Corporate Author",
  77. 'title' => "Title",
  78. 'orig_title' => "Orig Title",
  79. 'publication' => "Publication",
  80. 'abbrev_journal' => "Abbrev Journal",
  81. 'year' => "2005",
  82. 'volume' => "2nd Volume", // 'volume_numeric' = "2"
  83. 'issue' => "Issue",
  84. 'pages' => "5 Pages", // 'first_page' = "5"
  85. 'keywords' => "Keywords",
  86. 'abstract' => "Abstract",
  87. 'edition' => "2",
  88. 'editor' => "Editor",
  89. 'publisher' => "Publisher",
  90. 'place' => "Place",
  91. 'medium' => "Medium",
  92. 'series_editor' => "Series Editor",
  93. 'series_title' => "Series Title",
  94. 'abbrev_series_title' => "Abbrev Series Title",
  95. 'series_volume' => "3rd Series Volume", // 'series_volume_numeric' = "3"
  96. 'series_issue' => "Series Issue",
  97. 'issn' => "ISSN",
  98. 'isbn' => "ISBN",
  99. 'language' => "Language",
  100. 'summary_language' => "Summary Language",
  101. 'area' => "Area",
  102. 'type' => "Type",
  103. 'thesis' => "Diploma thesis",
  104. 'expedition' => "Expedition",
  105. 'doi' => "DOI",
  106. 'conference' => "Conference",
  107. 'url' => "URL",
  108. 'call_number' => "Call Number",
  109. // 'location' => "Location",
  110. 'contribution_id' => "Contribution Id",
  111. 'online_publication' => "no",
  112. 'online_citation' => "Online Citation",
  113. 'file' => "File",
  114. 'notes' => "Notes",
  115. 'approved' => "no",
  116. // 'created_date' => "1999-11-30",
  117. // 'created_time' => "00:00:01",
  118. // 'created_by' => "Created By",
  119. // 'modified_date' => "1999-11-31",
  120. // 'modified_time' => "00:00:02",
  121. // 'modified_by' => "Modified By",
  122. 'orig_record' => "-123"
  123. );
  124. // (4) Append the array of extracted field data to the main data array which holds all records to import:
  125. $parsedRecordsArray[] = $recordFieldParametersArray; // in this example, we simply import a single record, adopt to your needs
  126. $recordsCount = count($parsedRecordsArray); // count how many records are available
  127. // check if the current user has batch import permission:
  128. if (($recordsCount > 1) AND isset($_SESSION['user_permissions']) AND !preg_match("/allow_batch_import/", $_SESSION['user_permissions'])) // if we're supposed to import several records BUT the 'user_permissions' session variable does NOT contain 'allow_batch_import'...
  129. {
  130. // return an appropriate error message:
  131. // (note that this error message will overwrite any '$headerMessage' that gets specified below)
  132. $HeaderString = returnMsg($loc["NoPermission"] . $loc["NoPermission_ForBatchImport"] . "!", "warning", "strong", "HeaderString", "", " " . $loc["Warning_OnlyFirstRecordImported"]) . ":"; // function 'returnMsg()' is defined in 'include.inc.php'
  133. array_splice($parsedRecordsArray, 1); // remove all but the first record from the array of records that shall be imported
  134. }
  135. // --------------------------------------------------------------------
  136. // IMPORT RECORDS:
  137. // Build an array structure suitable for passing to the 'addRecords()' function:
  138. $importDataArray = array(); // for an explanation of the structure of '$importDataArray', see the comments above the 'addRecords()' function (in 'include.inc.php')
  139. $importDataArray['type'] = "refbase"; // we use the "refbase" array format
  140. $importDataArray['version'] = "1.0"; // we use version "1.0" of the array structure
  141. $importDataArray['creator'] = "http://refbase.net"; // calling script/importer is "refbase" (insert the unique name of your importer here or give the web address of it's home page)
  142. $importDataArray['author'] = "Matthias Steffens"; // author/contact name of the person who's responsible for this script/importer (insert your own name here)
  143. $importDataArray['contact'] = "refbase@extracts.de"; // author's email/contact address (insert your email address here)
  144. $importDataArray['options'] = array('prefix_call_number' => "true"); // if "true", any 'call_number' string will be prefixed with the correct call number prefix of the currently logged-in user (e.g. 'IP� @ msteffens @ ')
  145. $importDataArray['records'] = $parsedRecordsArray; // this array will hold the record(s) (with each record being a sub-array of fields)
  146. // Add all records to the database (i.e., for each record, add a row entry to MySQL table 'refs'):
  147. // ('$importedRecordsArray' will hold the serial numbers of all newly imported records)
  148. $importedRecordsArray = addRecords($importDataArray); // function 'addRecords()' is defined in 'include.inc.php'
  149. // --------------------------------------------------------------------
  150. // DISPLAY RESULTS:
  151. if (!empty($importedRecordsArray)) // if some records were successfully imported
  152. {
  153. $recordSerialsQueryString = "^(" . implode("|", $importedRecordsArray) . ")$";
  154. $importedRecordsCount = count($importedRecordsArray);
  155. if ($importedRecordsCount == 1)
  156. $headerMessage = $importedRecordsCount . " " . $loc["RecordSuccessfullyImported"] . ":";
  157. else // $importedRecordsCount > 1
  158. $headerMessage = $importedRecordsCount . " " . $loc["RecordsSuccessfullyImported"] . ":";
  159. // display all newly added records:
  160. header("Location: show.php?serial=" . rawurlencode($recordSerialsQueryString) . "&headerMsg=" . rawurlencode($headerMessage));
  161. }
  162. else // nothing imported
  163. {
  164. // return an appropriate error message:
  165. $HeaderString = returnMsg($loc["NoRecordsImported"] . "!", "warning", "strong", "HeaderString"); // function 'returnMsg()' is defined in 'include.inc.php'
  166. if (!empty($_SERVER['HTTP_REFERER'])) // if the referer variable isn't empty
  167. header("Location: " . $_SERVER['HTTP_REFERER']); // redirect to calling page
  168. else
  169. header("Location: index.php"); // redirect to main page ('index.php')
  170. }
  171. // --------------------------------------------------------------------
  172. ?>