index_all_docs.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. require_once '../../inc/lib/course.lib.php';
  11. require_once '../../inc/lib/document.lib.php';
  12. if (empty($_GET['doc'])) {
  13. echo "To add a document name to search, add ?doc=abc to the URL\n";
  14. } else {
  15. echo "Received param ".$_GET['doc']."<br />\n";
  16. }
  17. $allowed_mime_types = DocumentManager::file_get_mime_type(true);
  18. $allowed_extensions = array('doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'xls', 'xlsx', 'odt', 'odp', 'ods', 'pdf', 'txt', 'rtf', 'msg', 'csv', 'html', 'htm');
  19. $courses_list = CourseManager::get_courses_list();
  20. // Simulating empty specific fields (this is necessary for indexing)
  21. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  22. $specific_fields = get_specific_field_list();
  23. $specific_fields_values = array();
  24. foreach ($specific_fields as $sf) {
  25. $specific_fields_values[$sf['code']] = '';
  26. }
  27. $td = Database::get_course_table(TABLE_DOCUMENT);
  28. foreach ($courses_list as $course) {
  29. $course_dir = $course['directory'].'/document';
  30. $title = Database::escape_string($_GET['doc']);
  31. $sql = "SELECT id, path, session_id FROM $td WHERE c_id = ".$course['id']." AND path LIKE '%$title%' or title LIKE '%$title%'";
  32. $res = Database::query($sql);
  33. if (Database::num_rows($res)>0) {
  34. while ($row = Database::fetch_array($res)) {
  35. $doc_path = api_get_path(SYS_COURSE_PATH).$course_dir.$row['path'];
  36. $extensions = preg_split("/[\/\\.]/", $doc_path);
  37. $doc_ext = strtolower($extensions[count($extensions) - 1]);
  38. if (in_array($doc_ext, $allowed_extensions) && !is_dir($doc_path)) {
  39. $doc_mime = mime_content_type($doc_path);
  40. echo "Indexing doc ".$row['id']." (".$row['path'].") in course ".$course['code']."\n";
  41. $residx = DocumentManager::index_document($row['id'], $course['code'], $row['session_id'], $course['course_language'], $specific_fields_values);
  42. if ($residx) {
  43. echo "Success\n";
  44. } else {
  45. echo "Failure\n";
  46. }
  47. }
  48. }
  49. }
  50. }