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.

481 lines
21 KiB

  1. <?php
  2. // turn on warnings and notice during developement
  3. include('initialize/PhpErrorSettings.inc.php');
  4. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  5. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  6. // original author(s).
  7. //
  8. // This code is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  10. // License for more details.
  11. //
  12. // File: ./duplicate_search.php
  13. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/duplicate_search.php $
  14. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  15. //
  16. // Created: 28-Jan-07, 09:17
  17. // Modified: $Date: 2015-02-16 20:53:19 +0000 (Mon, 16 Feb 2015) $
  18. // $Author: karnesky $
  19. // $Revision: 1405 $
  20. // This script provides a form that lets you search for duplicate records.
  21. //
  22. //
  23. // TODO: I18n
  24. // Incorporate some include files:
  25. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  26. include 'includes/header.inc.php'; // include header
  27. include 'includes/footer.inc.php'; // include footer
  28. include 'includes/include.inc.php'; // include common functions
  29. include 'initialize/ini.inc.php'; // include common variables
  30. // --------------------------------------------------------------------
  31. // START A SESSION:
  32. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  33. start_session(true);
  34. // --------------------------------------------------------------------
  35. // Initialize preferred display language:
  36. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  37. include 'includes/locales.inc.php'; // include the locales
  38. // --------------------------------------------------------------------
  39. // Extract session variables (only necessary if register globals is OFF!):
  40. if (isset($_SESSION['errors']))
  41. $errors = $_SESSION['errors'];
  42. else
  43. $errors = array(); // initialize variable (in order to prevent 'Undefined index/variable...' messages)
  44. if (isset($_SESSION['formVars']))
  45. $formVars = $_SESSION['formVars'];
  46. else
  47. $formVars = array(); // initialize variable (in order to prevent 'Undefined index/variable...' messages)
  48. // The current values of the session variables 'errors' and 'formVars' get stored in '$errors' or '$formVars', respectively. (either automatically if
  49. // register globals is ON, or explicitly if register globals is OFF [by uncommenting the code above]).
  50. // We need to clear these session variables here, since they would otherwise be still there on a subsequent call of 'duplicate_search.php'!
  51. // Note: though we clear the session variables, the current error message (or form variables) is still available to this script via '$errors' (or '$formVars', respectively).
  52. deleteSessionVariable("errors"); // function 'deleteSessionVariable()' is defined in 'include.inc.php'
  53. deleteSessionVariable("formVars");
  54. // --------------------------------------------------------------------
  55. // Extract the view type requested by the user (either 'Mobile', 'Print', 'Web' or ''):
  56. // ('' will produce the default 'Web' output style)
  57. if (isset($_REQUEST['viewType']))
  58. $viewType = $_REQUEST['viewType'];
  59. else
  60. $viewType = "";
  61. // Check if the script was called with parameters (like: 'duplicate_search.php?customQuery=1&sqlQuery=...')
  62. // If so, the parameter 'customQuery=1' will be set:
  63. if (isset($_REQUEST['customQuery']) AND ($_REQUEST['customQuery'] == "1"))
  64. $customQuery = "1"; // accept any previous SQL queries
  65. else
  66. $customQuery = "0";
  67. // Setup some required variables:
  68. // If there's no stored message available:
  69. if (!isset($_SESSION['HeaderString']))
  70. {
  71. if (empty($errors)) // provide one of the default messages:
  72. {
  73. $errors = array(); // re-assign an empty array (in order to prevent 'Undefined variable "errors"...' messages when calling the 'fieldError' function later on)
  74. if ($customQuery == "1") // the script was called with parameters
  75. $HeaderString = "Find duplicates that match your current query:"; // Provide the default message
  76. else // the script was called without any custom SQL query
  77. $HeaderString = "Find duplicates:"; // Provide the default message
  78. }
  79. else // -> there were errors validating the data entered by the user
  80. $HeaderString = "There were validation errors regarding the data you entered:";
  81. }
  82. else
  83. {
  84. $HeaderString = $_SESSION['HeaderString']; // extract 'HeaderString' session variable (only necessary if register globals is OFF!)
  85. // Note: though we clear the session variable, the current message is still available to this script via '$HeaderString':
  86. deleteSessionVariable("HeaderString"); // function 'deleteSessionVariable()' is defined in 'include.inc.php'
  87. }
  88. // --------------------------------------------------------------------
  89. // Assign correct values to the form variables:
  90. // a) Provide the default query and options:
  91. // list of fields that are selected by default:
  92. $selectedFieldsArray = array("author", "title", "year", "publication", "volume", "pages");
  93. // default SQL query:
  94. // TODO: build the complete SQL query using functions 'buildFROMclause()' and 'buildORDERclause()'
  95. $sqlQuery = buildSELECTclause("", "", "", false, false); // function 'buildSELECTclause()' is defined in 'include.inc.php'
  96. if (isset($_SESSION['loginEmail']))
  97. $sqlQuery .= " FROM $tableRefs WHERE location RLIKE \"" . $loginEmail . "\" ORDER BY year DESC, author"; // '$loginEmail' is defined in function 'start_session()' (in 'include.inc.php')
  98. else
  99. $sqlQuery .= " FROM $tableRefs WHERE serial RLIKE \".+\" ORDER BY year DESC, author";
  100. // default search options:
  101. $ignoreWhitespace = "1";
  102. $ignorePunctuation = "1";
  103. $ignoreCharacterCase = "1";
  104. $ignoreAuthorInitials = "1";
  105. $nonASCIICharsSelected = "strip";
  106. // default display options:
  107. $displayType = $_SESSION['userDefaultView']; // get the default view for the current user
  108. $showLinks = "1";
  109. $showRows = $_SESSION['userRecordsPerPage']; // get the default number of records per page preferred by the current user
  110. $citeStyle = $defaultCiteStyle; // defined in 'ini.inc.php'
  111. $citeOrder = "";
  112. // b) The default query and options are overwritten if the script was called with parameters or if there were some errors on submit:
  113. if ($customQuery == "1") // the script was called with parameters
  114. {
  115. // extract selected fields:
  116. if (isset($_REQUEST['matchFieldsSelector']))
  117. {
  118. if (is_string($_REQUEST['matchFieldsSelector'])) // we accept a string containing a (e.g. comma delimited) list of field names
  119. $selectedFieldsArray = preg_split("/[^a-z_]+/", $_REQUEST['matchFieldsSelector'], -1, PREG_SPLIT_NO_EMPTY); // (the 'PREG_SPLIT_NO_EMPTY' flag causes only non-empty pieces to be returned)
  120. else // the field list is already provided as array:
  121. $selectedFieldsArray = $_REQUEST['matchFieldsSelector'];
  122. }
  123. // extract SQL query:
  124. if (isset($_REQUEST['sqlQuery']))
  125. {
  126. $sqlQuery = $_REQUEST['sqlQuery']; // accept any previous SQL queries
  127. $sqlQuery = stripSlashesIfMagicQuotes($sqlQuery); // function 'stripSlashesIfMagicQuotes()' is defined in 'include.inc.php'
  128. }
  129. // extract search options:
  130. if (isset($_REQUEST['ignoreWhitespace']) AND ($_REQUEST['ignoreWhitespace'] == "0"))
  131. $ignoreWhitespace = "0";
  132. if (isset($_REQUEST['ignorePunctuation']) AND ($_REQUEST['ignorePunctuation'] == "0"))
  133. $ignorePunctuation = "0";
  134. if (isset($_REQUEST['ignoreCharacterCase']) AND ($_REQUEST['ignoreCharacterCase'] == "0"))
  135. $ignoreCharacterCase = "0";
  136. if (isset($_REQUEST['ignoreAuthorInitials']) AND ($_REQUEST['ignoreAuthorInitials'] == "0"))
  137. $ignoreAuthorInitials = "0";
  138. if (isset($_REQUEST['nonASCIIChars']))
  139. $nonASCIICharsSelected = $_REQUEST['nonASCIIChars'];
  140. // extract display options:
  141. if (isset($_REQUEST['originalDisplayType']))
  142. $displayType = $_REQUEST['originalDisplayType']; // extract the type of display requested by the user (either 'Display', 'Cite', 'List' or '')
  143. if (isset($_REQUEST['showLinks']) AND ($_REQUEST['showLinks'] == "0"))
  144. $showLinks = "0";
  145. if (isset($_REQUEST['showRows']) AND preg_match("/^[1-9]+[0-9]*$/", $_REQUEST['showRows']))
  146. $showRows = $_REQUEST['showRows'];
  147. if (isset($_REQUEST['citeStyle']))
  148. $citeStyle = $_REQUEST['citeStyle'];
  149. if (isset($_REQUEST['citeOrder']))
  150. $citeOrder = $_REQUEST['citeOrder'];
  151. }
  152. elseif (!empty($errors)) // there were some errors on submit
  153. {
  154. // load selected fields:
  155. if (isset($formVars['matchFieldsSelector']))
  156. $selectedFieldsArray = $formVars['matchFieldsSelector'];
  157. // load the form data that were entered by the user:
  158. if (isset($formVars['sqlQuery']))
  159. {
  160. $sqlQuery = $formVars['sqlQuery'];
  161. $sqlQuery = stripSlashesIfMagicQuotes($sqlQuery);
  162. }
  163. // load search options:
  164. if (isset($formVars['ignoreWhitespace']))
  165. $ignoreWhitespace = $formVars['ignoreWhitespace'];
  166. if (isset($formVars['ignorePunctuation']))
  167. $ignorePunctuation = $formVars['ignorePunctuation'];
  168. if (isset($formVars['ignoreCharacterCase']))
  169. $ignoreCharacterCase = $formVars['ignoreCharacterCase'];
  170. if (isset($formVars['ignoreAuthorInitials']))
  171. $ignoreAuthorInitials = $formVars['ignoreAuthorInitials'];
  172. if (isset($formVars['nonASCIIChars']))
  173. $nonASCIICharsSelected = $formVars['nonASCIIChars'];
  174. // load display options:
  175. if (isset($formVars['originalDisplayType']))
  176. $displayType = $formVars['originalDisplayType'];
  177. if (isset($formVars['showLinks']))
  178. $showLinks = $formVars['showLinks'];
  179. if (isset($formVars['showRows']))
  180. $showRows = $formVars['showRows'];
  181. if (isset($formVars['citeStyle']))
  182. $citeStyle = $formVars['citeStyle'];
  183. if (isset($formVars['citeOrder']))
  184. $citeOrder = $formVars['citeOrder'];
  185. }
  186. // Set search and display options according to the fetched attribute values:
  187. // 'ignoreWhitespace' option:
  188. if ($ignoreWhitespace == "1")
  189. $ignoreWhitespaceChecked = " checked";
  190. else
  191. $ignoreWhitespaceChecked = "";
  192. // 'ignorePunctuation' option:
  193. if ($ignorePunctuation == "1")
  194. $ignorePunctuationChecked = " checked";
  195. else
  196. $ignorePunctuationChecked = "";
  197. // 'ignoreCharacterCase' option:
  198. if ($ignoreCharacterCase == "1")
  199. $ignoreCharacterCaseChecked = " checked";
  200. else
  201. $ignoreCharacterCaseChecked = "";
  202. // 'ignoreAuthorInitials' option:
  203. if ($ignoreAuthorInitials == "1")
  204. $ignoreAuthorInitialsChecked = " checked";
  205. else
  206. $ignoreAuthorInitialsChecked = "";
  207. // 'showLinks' option:
  208. if ($showLinks == "1")
  209. $checkLinks = " checked";
  210. else
  211. $checkLinks = "";
  212. // Initialize variables used for the multi-select & drop-down form elements:
  213. // specify which fields should be available in the multi-select box:
  214. // (the list of fields is similar to that of the "Search within Results" form; while only
  215. // some fields make sense with regard to duplicate identification, the other fields may be
  216. // useful in finding non-duplicate records with matching field contents)
  217. $dropDownItemArray1 = array("author" => "author",
  218. "title" => "title",
  219. // "type" => "type",
  220. "year" => "year",
  221. "publication" => "publication",
  222. "abbrev_journal" => "abbrev_journal",
  223. "volume" => "volume",
  224. "issue" => "issue",
  225. "pages" => "pages",
  226. // "thesis" => "thesis",
  227. // "keywords" => "keywords",
  228. // "abstract" => "abstract",
  229. // "publisher" => "publisher",
  230. // "place" => "place",
  231. // "editor" => "editor",
  232. // "language" => "language",
  233. // "series_title" => "series_title",
  234. // "area" => "area",
  235. "notes" => "notes");
  236. // if (isset($_SESSION['loginEmail']))
  237. // $dropDownItemArray1["location"] = "location"; // we only add the 'location' field if the user is logged in
  238. // if (isset($_SESSION['loginEmail'])) // add user-specific fields if a user is logged in
  239. // {
  240. // $dropDownItemArray1["marked"] = "marked";
  241. // $dropDownItemArray1["copy"] = "copy";
  242. // $dropDownItemArray1["selected"] = "selected";
  243. // $dropDownItemArray1["user_keys"] = "user_keys";
  244. // $dropDownItemArray1["user_notes"] = "user_notes";
  245. // $dropDownItemArray1["user_file"] = "user_file";
  246. // $dropDownItemArray1["user_groups"] = "user_groups";
  247. // $dropDownItemArray1["cite_key"] = "cite_key";
  248. // }
  249. // build properly formatted <option> tag elements from array items given in '$dropDownItemArray1':
  250. $matchFieldsOptionTags = buildSelectMenuOptions($dropDownItemArray1, "//", "\t\t\t\t", true); // function 'buildSelectMenuOptions()' is defined in 'include.inc.php'
  251. // by default we select all fields that are listed within '$selectedFieldsArray':
  252. $selectedFields = implode("|", $selectedFieldsArray); // merge array of fields that shall be selected
  253. $matchFieldsOptionTags = preg_replace("/<option([^>]*)>($selectedFields)<\\/option>/", "<option\\1 selected>\\2</option>", $matchFieldsOptionTags);
  254. // define variable holding the 'nonASCIIChars' drop-down elements:
  255. $dropDownItemArray2 = array("strip" => "strip",
  256. "transliterate" => "transliterate",
  257. "keep" => "don't change");
  258. // build properly formatted <option> tag elements from array items given in '$dropDownItemArray2':
  259. $nonASCIICharsOptionTags = buildSelectMenuOptions($dropDownItemArray2, "//", "\t\t\t\t", true);
  260. // add 'selected' attribute:
  261. $nonASCIICharsOptionTags = preg_replace("/<option([^>]*)>($dropDownItemArray2[$nonASCIICharsSelected])<\\/option>/", "<option\\1 selected>\\2</option>", $nonASCIICharsOptionTags);
  262. // --------------------------------------------------------------------
  263. // Show the login status:
  264. showLogin(); // (function 'showLogin()' is defined in 'include.inc.php')
  265. // (2a) Display header:
  266. // call the 'displayHTMLhead()' and 'showPageHeader()' functions (which are defined in 'header.inc.php'):
  267. displayHTMLhead(encodeHTML($officialDatabaseName) . " -- " . "Find Duplicates", "index,follow", "Search for duplicates within the " . encodeHTML($officialDatabaseName), "", false, "", $viewType, array());
  268. showPageHeader($HeaderString);
  269. // (2b) Start <form> and <table> holding the form elements:
  270. // note: we provide a default value for the 'submit' form tag so that hitting <enter> within a text entry field will act as if the user clicked the 'Add/Edit Query' button
  271. ?>
  272. <form action="search.php" method="GET">
  273. <input type="hidden" name="formType" value="duplicateSearch">
  274. <input type="hidden" name="originalDisplayType" value="<?php echo $displayType; ?>">
  275. <input type="hidden" name="submit" value="Find Duplicates">
  276. <input type="hidden" name="citeStyle" value="<?php echo rawurlencode($citeStyle); ?>">
  277. <input type="hidden" name="citeOrder" value="<?php echo $citeOrder; ?>">
  278. <table align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds a form that lets you search for duplicate records">
  279. <tr>
  280. <td width="120" valign="top">
  281. <div class="sect"><?php echo $loc["MatchFields"]; ?>:</div>
  282. </td>
  283. <td valign="top"><?php echo fieldError("matchFieldsSelector", $errors); ?>
  284. <select name="matchFieldsSelector[]" multiple><?php echo $matchFieldsOptionTags; ?>
  285. </select>
  286. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Find Duplicates">
  287. </td>
  288. </tr>
  289. </table>
  290. <table class="showhide" align="center" border="0" cellpadding="0" cellspacing="10" width="95%">
  291. <tr>
  292. <td class="small" width="120" valign="top">
  293. <a href="javascript:toggleVisibility('searchopt','optToggleimg','optToggletxt','<?php echo rawurlencode($loc["SearchAndDisplayOptions"]); ?>')"<?php echo addAccessKey("attribute", "search_opt"); ?> title="<?php echo $loc["LinkTitle_ToggleVisibility"] . addAccessKey("title", "search_opt"); ?>">
  294. <img id="optToggleimg" class="toggleimg" src="img/closed.gif" alt="<?php echo $loc["LinkTitle_ToggleVisibility"]; ?>" width="9" height="9" hspace="0" border="0">
  295. <span id="optToggletxt" class="toggletxt"><?php echo $loc["SearchAndDisplayOptions"]; ?></span>
  296. </a>
  297. </td>
  298. </tr>
  299. </table>
  300. <table id="searchopt" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds search &amp; display options" style="display: none;">
  301. <tr>
  302. <td width="120" valign="top" rowspan="5">
  303. <div class="sect"><?php echo $loc["SearchOptions"]; ?>:</div>
  304. </td>
  305. <td valign="top" colspan="2">
  306. <input type="checkbox" name="ignoreWhitespace" value="1"<?php echo $ignoreWhitespaceChecked; ?>>&nbsp;&nbsp;&nbsp;Ignore whitespace
  307. </td>
  308. </tr>
  309. <tr>
  310. <td valign="top" colspan="2">
  311. <input type="checkbox" name="ignorePunctuation" value="1"<?php echo $ignorePunctuationChecked; ?>>&nbsp;&nbsp;&nbsp;Ignore punctuation
  312. </td>
  313. </tr>
  314. <tr>
  315. <td valign="top" colspan="2">
  316. <input type="checkbox" name="ignoreCharacterCase" value="1"<?php echo $ignoreCharacterCaseChecked; ?>>&nbsp;&nbsp;&nbsp;Ignore character case
  317. </td>
  318. </tr>
  319. <tr>
  320. <td valign="top" colspan="2">
  321. <input type="checkbox" name="ignoreAuthorInitials" value="1"<?php echo $ignoreAuthorInitialsChecked; ?>>&nbsp;&nbsp;&nbsp;Ignore initials in author names
  322. </td>
  323. </tr>
  324. <tr>
  325. <td valign="top" colspan="2">
  326. <select name="nonASCIIChars"><?php echo $nonASCIICharsOptionTags; ?>
  327. </select>
  328. &nbsp;&nbsp;&nbsp;non-ASCII characters before comparison
  329. </td>
  330. </tr>
  331. <tr>
  332. <td valign="top">
  333. <div class="sect"><?php echo $loc["SQLQuery"]; ?>:</div>
  334. </td>
  335. <td colspan="2"><?php echo fieldError("sqlQuery", $errors); ?>
  336. <textarea name="sqlQuery" rows="6" cols="60"><?php echo $sqlQuery; ?>
  337. </textarea>
  338. </td>
  339. </tr>
  340. <tr>
  341. <td valign="middle">
  342. <div class="sect"><?php echo $loc["DisplayOptions"]; ?>:</div>
  343. </td>
  344. <td width="205" valign="middle">
  345. <input type="checkbox" id="showLinks" name="showLinks" value="1"<?php echo $checkLinks; ?>>&nbsp;&nbsp;&nbsp;<?php echo $loc["ShowLinks"]; ?>
  346. </td>
  347. <td valign="middle">
  348. <?php echo $loc["ShowRecordsPerPage_Prefix"]; ?>&nbsp;&nbsp;&nbsp;<input type="text" name="showRows" value="<?php echo $showRows; ?>" size="4" title="<?php echo $loc["DescriptionShowRecordsPerPage"]; ?>">&nbsp;&nbsp;&nbsp;<?php echo $loc["ShowRecordsPerPage_Suffix"]; ?>
  349. </td>
  350. </tr>
  351. </table>
  352. <table class="showhide" align="center" border="0" cellpadding="0" cellspacing="10" width="95%">
  353. <tr>
  354. <td class="small" width="120" valign="top">
  355. <a href="javascript:toggleVisibility('helptxt','helpToggleimg','helpToggletxt','<?php echo rawurlencode($loc["Help"]); ?>')"<?php echo addAccessKey("attribute", "search_help"); ?> title="<?php echo $loc["LinkTitle_ToggleVisibility"] . addAccessKey("title", "search_help"); ?>">
  356. <img id="helpToggleimg" class="toggleimg" src="img/closed.gif" alt="<?php echo $loc["LinkTitle_ToggleVisibility"]; ?>" width="9" height="9" hspace="0" border="0">
  357. <span id="helpToggletxt" class="toggletxt"><?php echo $loc["Help"]; ?></span>
  358. </a>
  359. </td>
  360. </tr>
  361. </table>
  362. <table id="helptxt" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds the help text" style="display: none;">
  363. <tr>
  364. <td width="120" valign="top">
  365. <div class="sect"><?php echo $loc["Help"]; ?>:</div>
  366. </td>
  367. <td class="helpbody" valign="top">
  368. <div class="even">
  369. This form allows you to find duplicate records within your current query results. Modify the elements of this form as needed and click the <em>Find Duplicates</em> button. You can use the field selector to specify which fields shall be considered when matching records. The search options allow you to normalize field contents before comparison. The SQL query string defines the scope of the duplicate search and (in case of List view) specifies the columns that will be displayed in the results list.
  370. </div>
  371. <div class="odd">
  372. Some usage tips: Generally it should suffice to simply press the <em>Find Duplicates</em> button. W.r.t. the field selector, it's usually a good idea to leave a few more fields selected. The more fields you select, the more conservative will the dup detection be (and vice versa). The default selection (<code>author</code>, <code>title</code>, <code>year</code>, <code>publication</code>, <code>volume</code>, <code>pages</code>) is rather conservative and should thus avoid false positives. In general, deselecting one or more of the fields <code>author</code>, <code>title</code> and/or <code>publication</code> will often detect additional duplicate entries but may also display some false positives.
  373. </div>
  374. </td>
  375. </tr>
  376. </table>
  377. </form><?php
  378. // --------------------------------------------------------------------
  379. // SHOW ERROR IN RED:
  380. function fieldError($fieldName, $errors)
  381. {
  382. if (isset($errors[$fieldName]))
  383. return "<b><span class=\"warning2\">" . $errors[$fieldName] . "</span></b><br>";
  384. }
  385. // --------------------------------------------------------------------
  386. // DISPLAY THE HTML FOOTER:
  387. // call the 'showPageFooter()' and 'displayHTMLfoot()' functions (which are defined in 'footer.inc.php')
  388. showPageFooter($HeaderString);
  389. displayHTMLfoot();
  390. // --------------------------------------------------------------------
  391. ?>