index_all_docs.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Script to find a document with a specific title or path in all courses
  4. */
  5. /**
  6. * Code init - comment die() call to enable
  7. */
  8. die();
  9. require '../../inc/global.inc.php';
  10. if (empty($_GET['doc'])) {
  11. echo "To add a document name to search, add ?doc=abc to the URL\n";
  12. } else {
  13. echo "Received param ".$_GET['doc']."<br />\n";
  14. }
  15. $allowed_mime_types = DocumentManager::file_get_mime_type(true);
  16. $allowed_extensions = array(
  17. 'doc',
  18. 'docx',
  19. 'ppt',
  20. 'pptx',
  21. 'pps',
  22. 'ppsx',
  23. 'xls',
  24. 'xlsx',
  25. 'odt',
  26. 'odp',
  27. 'ods',
  28. 'pdf',
  29. 'txt',
  30. 'rtf',
  31. 'msg',
  32. 'csv',
  33. 'html',
  34. 'htm',
  35. );
  36. $courses_list = CourseManager::get_courses_list();
  37. // Simulating empty specific fields (this is necessary for indexing)
  38. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  39. $specific_fields = get_specific_field_list();
  40. $specific_fields_values = array();
  41. foreach ($specific_fields as $sf) {
  42. $specific_fields_values[$sf['code']] = '';
  43. }
  44. $td = Database::get_course_table(TABLE_DOCUMENT);
  45. foreach ($courses_list as $course) {
  46. $course_dir = $course['directory'].'/document';
  47. $title = Database::escape_string($_GET['doc']);
  48. $sql = "SELECT id, path, session_id FROM $td WHERE c_id = ".$course['id']." AND path LIKE '%$title%' or title LIKE '%$title%'";
  49. $res = Database::query($sql);
  50. if (Database::num_rows($res) > 0) {
  51. while ($row = Database::fetch_array($res)) {
  52. $doc_path = api_get_path(SYS_COURSE_PATH).$course_dir.$row['path'];
  53. $extensions = preg_split("/[\/\\.]/", $doc_path);
  54. $doc_ext = strtolower($extensions[count($extensions) - 1]);
  55. if (in_array($doc_ext, $allowed_extensions) && !is_dir($doc_path)) {
  56. $doc_mime = mime_content_type($doc_path);
  57. echo "Indexing doc ".$row['id']." (".$row['path'].") in course ".$course['code']."\n";
  58. $residx = DocumentManager::index_document($row['id'], $course['code'], $row['session_id'], $course['course_language'], $specific_fields_values);
  59. if ($residx) {
  60. echo "Success\n";
  61. } else {
  62. echo "Failure\n";
  63. }
  64. }
  65. }
  66. }
  67. }