PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/includes/checkers.php

https://bitbucket.org/lgorence/quickpress
PHP | 121 lines | 38 code | 14 blank | 69 comment | 4 complexity | 8c4ad442345ad1041d331768eb96e698 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Base class for link checking algorithms.
  4. *
  5. * All link checkering algorithms should extend this class.
  6. *
  7. * @package Broken Link Checker
  8. * @access public
  9. */
  10. class blcChecker extends blcModule {
  11. /**
  12. * Priority determines the order in which the plugin will try all registered checkers
  13. * when looking for one that can check a particular URL. Registered checkers will be
  14. * tried in order, from highest to lowest priority, and the first one that returns
  15. * true when its can_check() method is called will be used.
  16. *
  17. * Checker implementations should set their priority depending on how specific they are
  18. * in choosing the URLs that they check.
  19. *
  20. * -10 .. 10 : checks all URLs that have a certain protocol, e.g. all HTTP URLs.
  21. * 11 .. 100 : checks only URLs from a restricted number of domains, e.g. video site URLs.
  22. * 100+ : checks only certain URLs from a certain domain, e.g. YouTube video links.
  23. *
  24. */
  25. var $priority = -100;
  26. /**
  27. * Check if this checker knows how to check a particular URL.
  28. *
  29. * @param string $url
  30. * @param array|false $parsed_url The result of parsing $url with parse_url(). See PHP docs for details.
  31. * @return bool
  32. */
  33. function can_check($url, $parsed_url){
  34. return false;
  35. }
  36. /**
  37. * Check an URL.
  38. *
  39. * This method returns an associative array containing results of
  40. * the check. The following array keys are recognized by the plugin and
  41. * their values will be stored in the link's DB record :
  42. * 'broken' (bool) - True if the URL points to a missing/broken page. Required.
  43. * 'http_code' (int) - HTTP code returned when requesting the URL. Defaults to 0.
  44. * 'redirect_count' (int) - The number of redirects. Defaults to 0.
  45. * 'final_url' (string) - The redirected-to URL. Assumed to be equal to the checked URL by default.
  46. * 'request_duration' (float) - How long it took for the server to respond. Defaults to 0 seconds.
  47. * 'timeout' (bool) - True if checking the URL resulted in a timeout. Defaults to false.
  48. * 'may_recheck' (bool) - Allow the plugin to re-check the URL after 'recheck_threshold' seconds (see broken-link-checker.php).
  49. * 'log' (string) - Free-form log of the performed check. It will be displayed in the "Details" section of the checked link.
  50. * 'result_hash' (string) - A free-form hash or code uniquely identifying the detected link status. See sub-classes for examples. Max 200 characters.
  51. *
  52. * @see blcLink:check()
  53. *
  54. * @param string $url
  55. * @return array
  56. */
  57. function check($url){
  58. trigger_error('Function blcChecker::check() must be over-ridden in a subclass', E_USER_ERROR);
  59. }
  60. }
  61. class blcCheckerHelper {
  62. /**
  63. * Get a reference to a specific checker.
  64. *
  65. * @uses blcModuleManager::get_module()
  66. *
  67. * @param string $checker_id
  68. * @return blcChecker
  69. */
  70. static function get_checker($checker_id){
  71. $manager = blcModuleManager::getInstance();
  72. return $manager->get_module($checker_id, true, 'checker');
  73. }
  74. /**
  75. * Get a checker object that can check the specified URL.
  76. *
  77. * @param string $url
  78. * @return blcChecker|null
  79. */
  80. static function get_checker_for($url){
  81. $parsed = @parse_url($url);
  82. $manager = blcModuleManager::getInstance();
  83. $active_checkers = $manager->get_active_by_category('checker');
  84. foreach($active_checkers as $module_id => $module_data){
  85. //Try the URL pattern in the header first. If it doesn't match,
  86. //we can avoid loading the module altogether.
  87. if ( !empty($module_data['ModuleCheckerUrlPattern']) ){
  88. if ( !preg_match($module_data['ModuleCheckerUrlPattern'], $url) ){
  89. continue;
  90. }
  91. }
  92. $checker = $manager->get_module($module_id);
  93. if ( !$checker ){
  94. continue;
  95. }
  96. //The can_check() method can perform more sophisticated filtering,
  97. //or just return true if the checker thinks matching the URL regex
  98. //is sufficient.
  99. if ( $checker->can_check($url, $parsed) ){
  100. return $checker;
  101. }
  102. }
  103. $checker = null;
  104. return $checker;
  105. }
  106. }
  107. ?>