PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 933 lines | 577 code | 146 blank | 210 comment | 103 complexity | 13773e7f63ecea5b74f0502a528d74c0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @author W-Shadow
  4. * @copyright 2010
  5. */
  6. if (!class_exists('blcLink')){
  7. define('BLC_LINK_STATUS_UNKNOWN', 'unknown');
  8. define('BLC_LINK_STATUS_OK', 'ok');
  9. define('BLC_LINK_STATUS_INFO', 'info');
  10. define('BLC_LINK_STATUS_WARNING', 'warning');
  11. define('BLC_LINK_STATUS_ERROR', 'error');
  12. class blcLink {
  13. //Object state
  14. var $is_new = false;
  15. //DB fields
  16. var $link_id = 0;
  17. var $url = '';
  18. var $being_checked = false;
  19. var $last_check = 0;
  20. var $last_check_attempt = 0;
  21. var $check_count = 0;
  22. var $http_code = 0;
  23. var $request_duration = 0;
  24. var $timeout = false;
  25. var $redirect_count = 0;
  26. var $final_url = '';
  27. var $broken = false;
  28. var $first_failure = 0;
  29. var $last_success = 0;
  30. var $may_recheck = 1;
  31. var $false_positive = false;
  32. var $result_hash = '';
  33. var $dismissed = false;
  34. var $status_text = '';
  35. var $status_code = '';
  36. var $log = '';
  37. //A list of DB fields and their storage formats
  38. var $field_format;
  39. //A cached list of the link's instances
  40. var $_instances = null;
  41. var $http_status_codes = array(
  42. // [Informational 1xx]
  43. 100=>'Continue',
  44. 101=>'Switching Protocols',
  45. // [Successful 2xx]
  46. 200=>'OK',
  47. 201=>'Created',
  48. 202=>'Accepted',
  49. 203=>'Non-Authoritative Information',
  50. 204=>'No Content',
  51. 205=>'Reset Content',
  52. 206=>'Partial Content',
  53. // [Redirection 3xx]
  54. 300=>'Multiple Choices',
  55. 301=>'Moved Permanently',
  56. 302=>'Found',
  57. 303=>'See Other',
  58. 304=>'Not Modified',
  59. 305=>'Use Proxy',
  60. //306=>'(Unused)',
  61. 307=>'Temporary Redirect',
  62. // [Client Error 4xx]
  63. 400=>'Bad Request',
  64. 401=>'Unauthorized',
  65. 402=>'Payment Required',
  66. 403=>'Forbidden',
  67. 404=>'Not Found',
  68. 405=>'Method Not Allowed',
  69. 406=>'Not Acceptable',
  70. 407=>'Proxy Authentication Required',
  71. 408=>'Request Timeout',
  72. 409=>'Conflict',
  73. 410=>'Gone',
  74. 411=>'Length Required',
  75. 412=>'Precondition Failed',
  76. 413=>'Request Entity Too Large',
  77. 414=>'Request-URI Too Long',
  78. 415=>'Unsupported Media Type',
  79. 416=>'Requested Range Not Satisfiable',
  80. 417=>'Expectation Failed',
  81. // [Server Error 5xx]
  82. 500=>'Internal Server Error',
  83. 501=>'Not Implemented',
  84. 502=>'Bad Gateway',
  85. 503=>'Service Unavailable',
  86. 504=>'Gateway Timeout',
  87. 505=>'HTTP Version Not Supported',
  88. 509=>'Bandwidth Limit Exceeded',
  89. 510=>'Not Extended',
  90. );
  91. function __construct($arg = null){
  92. global $wpdb; /** @var wpdb $wpdb */
  93. $this->field_format = array(
  94. 'url' => '%s',
  95. 'first_failure' => 'datetime',
  96. 'last_check' => 'datetime',
  97. 'last_success' => 'datetime',
  98. 'last_check_attempt' => 'datetime',
  99. 'check_count' => '%d',
  100. 'final_url' => '%s',
  101. 'redirect_count' => '%d',
  102. 'log' => '%s',
  103. 'http_code' => '%d',
  104. 'request_duration' => '%F',
  105. 'timeout' => 'bool',
  106. 'result_hash' => '%s',
  107. 'broken' => 'bool',
  108. 'false_positive' => 'bool',
  109. 'may_recheck' => 'bool',
  110. 'being_checked' => 'bool',
  111. 'status_text' => '%s',
  112. 'status_code' => '%s',
  113. 'dismissed' => 'bool',
  114. );
  115. if (is_numeric($arg)){
  116. //Load a link with ID = $arg from the DB.
  117. $q = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}blc_links WHERE link_id=%d LIMIT 1", $arg);
  118. $arr = $wpdb->get_row( $q, ARRAY_A );
  119. if ( is_array($arr) ){ //Loaded successfully
  120. $this->set_values($arr);
  121. } else {
  122. //Link not found. The object is invalid.
  123. //I'd throw an error, but that wouldn't be PHP 4 compatible...
  124. }
  125. } else if (is_string($arg)){
  126. //Load a link with URL = $arg from the DB. Create a new one if the record isn't found.
  127. $q = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}blc_links WHERE url=%s LIMIT 1", $arg);
  128. $arr = $wpdb->get_row( $q, ARRAY_A );
  129. if ( is_array($arr) ){ //Loaded successfully
  130. $this->set_values($arr);
  131. } else { //Link not found, treat as new
  132. $this->url = $arg;
  133. $this->is_new = true;
  134. }
  135. } else if (is_array($arg)){
  136. $this->set_values($arg);
  137. //Is this a new link?
  138. $this->is_new = empty($this->link_id);
  139. } else {
  140. $this->is_new = true;
  141. }
  142. }
  143. function blcLink($arg = null){
  144. $this->__construct($arg);
  145. }
  146. /**
  147. * blcLink::set_values()
  148. * Set the internal values to the ones provided in an array (doesn't sanitize).
  149. *
  150. * @param array $arr An associative array of values
  151. * @return void
  152. */
  153. function set_values($arr){
  154. $arr = $this->to_native_format($arr);
  155. foreach( $arr as $key => $value ){
  156. $this->$key = $value;
  157. }
  158. }
  159. /**
  160. * Check whether the object represents a valid link
  161. *
  162. * @return bool
  163. */
  164. function valid(){
  165. return !empty( $this->url ) && ( !empty($this->link_id) || $this->is_new );
  166. }
  167. /**
  168. * Check if the link is working.
  169. *
  170. * @param bool $save_results Automatically save the results of the check.
  171. * @return bool
  172. */
  173. function check( $save_results = true ){
  174. if ( !$this->valid() ) return false;
  175. $this->last_check_attempt = time();
  176. /*
  177. If the link is stil marked as in the process of being checked, that probably means
  178. that the last time the plugin tried to check it the script got terminated by PHP for
  179. running over the execution time limit or causing a fatal error.
  180. This problem is likely to be temporary for most links, so we leave it be and treat it
  181. as any other link (i.e. check it again later using the default recheck periodicity).
  182. */
  183. if ( $this->being_checked ) {
  184. $this->being_checked = false;
  185. //Add an explanatory notice to the link's log
  186. $error_notice = "[" . __("The plugin script was terminated while trying to check the link.", 'broken-link-checker') . "]";
  187. if ( strpos($this->log, $error_notice) === false ){
  188. $this->log = $error_notice . "\r\n" . $this->log;
  189. }
  190. if ( $save_results ){
  191. $this->save();
  192. }
  193. return false;
  194. }
  195. $this->being_checked = true;
  196. $this->check_count++;
  197. if ( $save_results ) {
  198. //Update the DB record before actually performing the check.
  199. //Useful if something goes terribly wrong while checking this particular URL
  200. //(e.g. the server might kill the script for running over the exec. time limit).
  201. //Note : might be unnecessary.
  202. $this->save();
  203. }
  204. $defaults = array(
  205. 'broken' => false,
  206. 'http_code' => 0,
  207. 'redirect_count' => 0,
  208. 'final_url' => $this->url,
  209. 'request_duration' => 0,
  210. 'timeout' => false,
  211. 'may_recheck' => true,
  212. 'log' => '',
  213. 'result_hash' => '',
  214. 'status_text' => '',
  215. 'status_code' => '',
  216. );
  217. $checker = blcCheckerHelper::get_checker_for($this->get_ascii_url());
  218. if ( is_null($checker) ){
  219. //Oops, there are no checker implementations that can handle this link.
  220. //Assume the link is working, but leave a note in the log.
  221. $this->broken = false;
  222. $this->being_checked = false;
  223. $this->log = __("The plugin doesn't know how to check this type of link.", 'broken-link-checker');
  224. if ( $save_results ){
  225. $this->save();
  226. }
  227. return true;
  228. }
  229. //Check the link
  230. $rez = $checker->check($this->get_ascii_url());
  231. //FB::info($rez, "Check results");
  232. //Filter the returned array to leave only the restricted set of keys that we're interested in.
  233. $results = array();
  234. foreach($rez as $name => $value){
  235. if ( array_key_exists($name, $defaults) ){
  236. $results[$name] = $value;
  237. }
  238. }
  239. $results = array_merge($defaults, $results);
  240. //The result hash is special - see blcLink::status_changed()
  241. $new_result_hash = $results['result_hash'];
  242. unset($results['result_hash']);
  243. //Update the object's fields with the new results
  244. $this->set_values($results);
  245. //Update timestamps & state-dependent fields
  246. $this->status_changed($results['broken'], $new_result_hash);
  247. $this->being_checked = false;
  248. //Save results to the DB
  249. if($save_results){
  250. $this->save();
  251. }
  252. return $this->broken;
  253. }
  254. /**
  255. * A helper method used to update timestamps & other state-dependent fields
  256. * after the state of the link (broken vs working) has just been determined.
  257. *
  258. * @access private
  259. *
  260. * @param bool $broken
  261. * @param string $new_result_hash
  262. * @return void
  263. */
  264. private function status_changed($broken, $new_result_hash = ''){
  265. //If a link's status changes, un-dismiss it.
  266. if ( $this->result_hash != $new_result_hash ) {
  267. $this->dismissed = false;
  268. }
  269. if ( $this->false_positive && !empty($new_result_hash) ){
  270. //If the link has been marked as a (probable) false positive,
  271. //mark it as broken *only* if the new result is different from
  272. //the one that caused the user to mark it as a false positive.
  273. if ( $broken ){
  274. if ( $this->result_hash == $new_result_hash ){
  275. //Got the same result as before, assume it's still incorrect and the link actually works.
  276. $broken = false;
  277. } else {
  278. //Got a new result. Assume (quite optimistically) that it's not a false positive.
  279. $this->false_positive = false;
  280. }
  281. } else {
  282. //The plugin now thinks the link is working,
  283. //so it's no longer a false positive.
  284. $this->false_positive = false;
  285. }
  286. }
  287. $this->broken = $broken;
  288. $this->result_hash = $new_result_hash;
  289. //Update timestamps
  290. $this->last_check = $this->last_check_attempt;
  291. if ( $this->broken ){
  292. if ( empty($this->first_failure) ){
  293. $this->first_failure = $this->last_check;
  294. }
  295. } else {
  296. $this->first_failure = 0;
  297. $this->last_success = $this->last_check;
  298. $this->check_count = 0;
  299. }
  300. //Add a line indicating link status to the log
  301. if ( !$broken ) {
  302. $this->log .= "\n" . __("Link is valid.", 'broken-link-checker');
  303. } else {
  304. $this->log .= "\n" . __("Link is broken.", 'broken-link-checker');
  305. }
  306. }
  307. /**
  308. * blcLink::save()
  309. * Save link data to DB.
  310. *
  311. * @return bool True if saved successfully, false otherwise.
  312. */
  313. function save(){
  314. global $wpdb; /** @var wpdb $wpdb */
  315. if ( !$this->valid() ) return false;
  316. //Make a list of fields to be saved and their values in DB format
  317. $values = array();
  318. foreach($this->field_format as $field => $format){
  319. $values[$field] = $this->$field;
  320. }
  321. $values = $this->to_db_format($values);
  322. if ( $this->is_new ){
  323. //BUG: Technically, there should be a 'LOCK TABLES wp_blc_links WRITE' here. In fact,
  324. //the plugin should probably lock all involved tables whenever it parses something, lest
  325. //the user (ot another plugin) modify the thing being parsed while we're working.
  326. //The problem with table locking, though, is that parsing takes a long time and having
  327. //all of WP freeze while the plugin is working would be a Bad Thing. Food for thought.
  328. //Check if there's already a link with this URL present
  329. $q = $wpdb->prepare(
  330. "SELECT link_id FROM {$wpdb->prefix}blc_links WHERE url = %s",
  331. $this->url
  332. );
  333. $existing_id = $wpdb->get_var($q);
  334. if ( !empty($existing_id) ){
  335. //Dammit.
  336. $this->link_id = $existing_id;
  337. $this->is_new = false;
  338. return true;
  339. }
  340. //Insert a new row
  341. $q = sprintf(
  342. "INSERT INTO {$wpdb->prefix}blc_links( %s ) VALUES( %s )",
  343. implode(', ', array_keys($values)),
  344. implode(', ', array_values($values))
  345. );
  346. //FB::log($q, 'Link add query');
  347. $rez = $wpdb->query($q) !== false;
  348. if ($rez){
  349. $this->link_id = $wpdb->insert_id;
  350. //FB::info($this->link_id, "Link added");
  351. //If the link was successfully saved then it's no longer "new"
  352. $this->is_new = false;
  353. } else {
  354. //FB::error($wpdb->last_error, "Error adding link {$this->url}");
  355. }
  356. return $rez;
  357. } else {
  358. //Generate the field = dbvalue expressions
  359. $set_exprs = array();
  360. foreach($values as $name => $value){
  361. $set_exprs[] = "$name = $value";
  362. }
  363. $set_exprs = implode(', ', $set_exprs);
  364. //Update an existing DB record
  365. $q = sprintf(
  366. "UPDATE {$wpdb->prefix}blc_links SET %s WHERE link_id=%d",
  367. $set_exprs,
  368. intval($this->link_id)
  369. );
  370. //FB::log($q, 'Link update query');
  371. $rez = $wpdb->query($q) !== false;
  372. if ( $rez ){
  373. //FB::log($this->link_id, "Link updated");
  374. } else {
  375. //FB::error($wpdb->last_error, "Error updating link {$this->url}");
  376. }
  377. return $rez;
  378. }
  379. }
  380. /**
  381. * A helper method for converting the link's field values to DB format and escaping them
  382. * for use in SQL queries.
  383. *
  384. * @param array $values
  385. * @return array
  386. */
  387. function to_db_format($values){
  388. global $wpdb;
  389. $dbvalues = array();
  390. foreach($values as $name => $value){
  391. //Skip fields that don't exist in the blc_links table.
  392. if ( !isset($this->field_format[$name]) ){
  393. continue;
  394. }
  395. $format = $this->field_format[$name];
  396. //Convert native values to a format comprehensible to the DB
  397. switch($format){
  398. case 'datetime' :
  399. if ( empty($value) ){
  400. $value = '0000-00-00 00:00:00';
  401. } else {
  402. $value = date('Y-m-d H:i:s', $value);
  403. }
  404. $format = '%s';
  405. break;
  406. case 'bool':
  407. if ( $value ){
  408. $value = 1;
  409. } else {
  410. $value = 0;
  411. }
  412. $format = '%d';
  413. break;
  414. }
  415. //Escapize
  416. $value = $wpdb->prepare($format, $value);
  417. $dbvalues[$name] = $value;
  418. }
  419. return $dbvalues;
  420. }
  421. /**
  422. * A helper method for converting values fetched from the database to native datatypes.
  423. *
  424. * @param array $values
  425. * @return array
  426. */
  427. function to_native_format($values){
  428. foreach($values as $name => $value){
  429. //Don't process ffields that don't exist in the blc_links table.
  430. if ( !isset($this->field_format[$name]) ){
  431. continue;
  432. }
  433. $format = $this->field_format[$name];
  434. //Convert values in DB format to native datatypes.
  435. switch($format){
  436. case 'datetime' :
  437. if ( $value == '0000-00-00 00:00:00' ){
  438. $value = 0;
  439. } elseif (is_string($value)) {
  440. $value = strtotime($value);
  441. }
  442. break;
  443. case 'bool':
  444. $value = (bool)$value;
  445. break;
  446. case '%d':
  447. $value = intval($value);
  448. break;
  449. case '%f':
  450. $value = floatval($value);
  451. break;
  452. }
  453. $values[$name] = $value;
  454. }
  455. return $values;
  456. }
  457. /**
  458. * blcLink::edit()
  459. * Edit all instances of the link by changing the URL.
  460. *
  461. * Here's how this really works : create a new link with the new URL. Then edit()
  462. * all instances and point them to the new link record. If some instance can't be
  463. * edited they will still point to the old record. The old record is deleted
  464. * if all instances were edited successfully.
  465. *
  466. * @param string $new_url
  467. * @return array An associative array with these keys :
  468. * new_link_id - the database ID of the new link.
  469. * new_link - the new link (an instance of blcLink).
  470. * cnt_okay - the number of successfully edited link instances.
  471. * cnt_error - the number of instances that caused problems.
  472. * errors - an array of WP_Error objects corresponding to the failed edits.
  473. */
  474. function edit($new_url){
  475. if ( !$this->valid() ){
  476. return new WP_Error(
  477. 'link_invalid',
  478. __("Link is not valid", 'broken-link-checker')
  479. );
  480. }
  481. //FB::info('Changing link '.$this->link_id .' to URL "'.$new_url.'"');
  482. $instances = $this->get_instances();
  483. //Fail if there are no instances
  484. if (empty($instances)) {
  485. return array(
  486. 'new_link_id' => $this->link_id,
  487. 'new_link' => $this,
  488. 'cnt_okay' => 0,
  489. 'cnt_error' => 0,
  490. 'errors' => array(
  491. new WP_Error(
  492. 'no_instances_found',
  493. __('This link can not be edited because it is not used anywhere on this site.', 'broken-link-checker')
  494. )
  495. )
  496. );
  497. };
  498. //Load or create a link with the URL = $new_url
  499. $new_link = new blcLink($new_url);
  500. $was_new = $new_link->is_new;
  501. if ($new_link->is_new) {
  502. //FB::log($new_link, 'Saving a new link');
  503. $new_link->save(); //so that we get a valid link_id
  504. }
  505. //FB::log("Changing link to $new_url");
  506. if ( empty($new_link->link_id) ){
  507. //FB::error("Failed to create a new link record");
  508. return array(
  509. 'new_link_id' => $this->link_id,
  510. 'new_link' => $this,
  511. 'cnt_okay' => 0,
  512. 'cnt_error' => 0,
  513. 'errors' => array(
  514. new WP_Error(
  515. 'link_creation_failed',
  516. __('Failed to create a DB entry for the new URL.', 'broken-link-checker')
  517. )
  518. )
  519. );;
  520. }
  521. $cnt_okay = $cnt_error = 0;
  522. $errors = array();
  523. //Edit each instance.
  524. //FB::info('Editing ' . count($instances) . ' instances');
  525. foreach ( $instances as $instance ){
  526. $rez = $instance->edit( $new_url, $this->url );
  527. if ( is_wp_error($rez) ){
  528. $cnt_error++;
  529. array_push($errors, $rez);
  530. //FB::error($instance, 'Failed to edit instance ' . $instance->instance_id);
  531. } else {
  532. $cnt_okay++;
  533. $instance->link_id = $new_link->link_id;
  534. $instance->save();
  535. //FB::info($instance, 'Successfully edited instance ' . $instance->instance_id);
  536. }
  537. }
  538. //If all instances were edited successfully we can delete the old link record.
  539. //UNLESS this link is equal to the new link (which should never happen, but whatever).
  540. if ( ( $cnt_error == 0 ) && ( $cnt_okay > 0 ) && ( $this->link_id != $new_link->link_id ) ){
  541. $this->forget( false );
  542. }
  543. //On the other hand, if no instances could be edited and the $new_link was really new,
  544. //then delete it.
  545. if ( ( $cnt_okay == 0 ) && $was_new ){
  546. $new_link->forget( false );
  547. $new_link = $this;
  548. }
  549. return array(
  550. 'new_link_id' => $new_link->link_id,
  551. 'new_link' => $new_link,
  552. 'cnt_okay' => $cnt_okay,
  553. 'cnt_error' => $cnt_error,
  554. 'errors' => $errors,
  555. );
  556. }
  557. /**
  558. * Edit all of of this link's instances and replace the URL with the URL that it redirects to.
  559. * This method does nothing if the link isn't a redirect.
  560. *
  561. * @see blcLink::edit()
  562. *
  563. * @return array|WP_Error
  564. */
  565. function deredirect(){
  566. if ( !$this->valid() ){
  567. return new WP_Error(
  568. 'link_invalid',
  569. __("Link is not valid", 'broken-link-checker')
  570. );
  571. }
  572. if ( ($this->redirect_count <= 0) || empty($this->final_url) ){
  573. return new WP_Error(
  574. 'not_redirect',
  575. __("This link is not a redirect", 'broken-link-checker')
  576. );
  577. }
  578. return $this->edit($this->final_url);
  579. }
  580. /**
  581. * Unlink all instances and delete the link record.
  582. *
  583. * @return array|WP_Error An associative array with these keys :
  584. * cnt_okay - the number of successfully removed instances.
  585. * cnt_error - the number of instances that couldn't be removed.
  586. * link_deleted - true if the link record was deleted.
  587. * errors - an array of WP_Error objects describing the errors that were encountered, if any.
  588. */
  589. function unlink(){
  590. if ( !$this->valid() ){
  591. return new WP_Error(
  592. 'link_invalid',
  593. __("Link is not valid", 'broken-link-checker')
  594. );
  595. }
  596. //FB::info($this, 'Removing link');
  597. $instances = $this->get_instances();
  598. //No instances? Just remove the link then.
  599. if (empty($instances)) {
  600. //FB::warn("This link has no instances. Deleting the link.");
  601. $rez = $this->forget( false ) !== false;
  602. if ( $rez ){
  603. return array(
  604. 'cnt_okay' => 1,
  605. 'cnt_error' => 0,
  606. 'link_deleted' => true,
  607. 'errors' => array(),
  608. );
  609. } else {
  610. return array(
  611. 'cnt_okay' => 0,
  612. 'cnt_error' => 0,
  613. 'link_deleted' => false,
  614. 'errors' => array(
  615. new WP_Error(
  616. "deletion_failed",
  617. __("Couldn't delete the link's database record", 'broken-link-checker')
  618. )
  619. ),
  620. );
  621. }
  622. }
  623. //FB::info('Unlinking ' . count($instances) . ' instances');
  624. $cnt_okay = $cnt_error = 0;
  625. $errors = array();
  626. //Unlink each instance.
  627. foreach ( $instances as $instance ){
  628. $rez = $instance->unlink( $this->url );
  629. if ( is_wp_error($rez) ){
  630. $cnt_error++;
  631. array_push($errors, $rez);
  632. //FB::error( $instance, 'Failed to unlink instance' );
  633. } else {
  634. $cnt_okay++;
  635. //FB::info( $instance, 'Successfully unlinked instance' );
  636. }
  637. }
  638. //If all instances were unlinked successfully we can delete the link record.
  639. if ( ( $cnt_error == 0 ) && ( $cnt_okay > 0 ) ){
  640. //FB::log('Instances removed, deleting the link.');
  641. $link_deleted = $this->forget() !== false;
  642. if ( !$link_deleted ){
  643. array_push(
  644. $errors,
  645. new WP_Error(
  646. "deletion_failed",
  647. __("Couldn't delete the link's database record", 'broken-link-checker')
  648. )
  649. );
  650. }
  651. } else {
  652. //FB::error("Something went wrong. Unlinked instances : $cnt_okay, errors : $cnt_error");
  653. $link_deleted = false;
  654. }
  655. return array(
  656. 'cnt_okay' => $cnt_okay,
  657. 'cnt_error' => $cnt_error,
  658. 'link_deleted' => $link_deleted,
  659. 'errors' => $errors,
  660. );
  661. }
  662. /**
  663. * Remove the link and (optionally) its instance records from the DB. Doesn't alter posts/etc.
  664. *
  665. * @return mixed 1 on success, 0 if link not found, false on error.
  666. */
  667. function forget($remove_instances = true){
  668. global $wpdb;
  669. if ( !$this->valid() ) return false;
  670. if ( !empty($this->link_id) ){
  671. //FB::info($this, 'Deleting link from DB');
  672. if ( $remove_instances ){
  673. //Remove instances, if any
  674. $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->prefix}blc_instances WHERE link_id=%d", $this->link_id) );
  675. }
  676. //Remove the link itself
  677. $rez = $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->prefix}blc_links WHERE link_id=%d", $this->link_id) );
  678. $this->link_id = 0;
  679. return $rez;
  680. } else {
  681. return false;
  682. }
  683. }
  684. /**
  685. * Get a list of the link's instances
  686. *
  687. * @param bool $ignore_cache Don't use the internally cached instance list.
  688. * @param string $purpose
  689. * @return array An array of instance objects or FALSE on failure.
  690. */
  691. function get_instances( $ignore_cache = false, $purpose = '' ){
  692. global $wpdb;
  693. if ( !$this->valid() || empty($this->link_id) ) return false;
  694. if ( $ignore_cache || is_null($this->_instances) ){
  695. $instances = blc_get_instances( array($this->link_id), $purpose );
  696. if ( !empty($instances) ){
  697. $this->_instances = $instances[$this->link_id];
  698. }
  699. }
  700. return $this->_instances;
  701. }
  702. /**
  703. * Determine the status text and status code corresponding to the current state of this link.
  704. *
  705. * @return array Associative array with two keys, 'text' and 'code'.
  706. */
  707. function analyse_status(){
  708. $code = BLC_LINK_STATUS_UNKNOWN;
  709. $text = _x('Unknown', 'link status', 'broken-link-checker');
  710. //Status text
  711. if ( isset($this->status_text) && !empty($this->status_text) && !empty($this->status_code) ){
  712. //Lucky, the checker module has already set it for us.
  713. $text = $this->status_text;
  714. $code = $this->status_code;
  715. } else {
  716. if ( $this->broken ){
  717. $code = BLC_LINK_STATUS_WARNING;
  718. $text = __('Unknown Error', 'link status', 'broken-link-checker');
  719. if ( $this->timeout ){
  720. $text = __('Timeout', 'broken-link-checker');
  721. $code = BLC_LINK_STATUS_WARNING;
  722. } elseif ( $this->http_code ) {
  723. //Only 404 (Not Found) and 410 (Gone) are treated as broken-for-sure.
  724. if ( in_array($this->http_code, array(404, 410)) ){
  725. $code = BLC_LINK_STATUS_ERROR;
  726. } else {
  727. $code = BLC_LINK_STATUS_WARNING;
  728. }
  729. if ( array_key_exists(intval($this->http_code), $this->http_status_codes) ){
  730. $text = $this->http_status_codes[intval($this->http_code)];
  731. }
  732. }
  733. } else {
  734. if ( !$this->last_check ) {
  735. $text = __('Not checked', 'broken-link-checker');
  736. $code = BLC_LINK_STATUS_UNKNOWN;
  737. } elseif ( $this->false_positive ) {
  738. $text = __('False positive', 'broken-link-checker');
  739. $code = BLC_LINK_STATUS_UNKNOWN;
  740. } else {
  741. $text = _x('OK', 'link status', 'broken-link-checker');
  742. $code = BLC_LINK_STATUS_OK;
  743. }
  744. }
  745. }
  746. return compact('text', 'code');
  747. }
  748. /**
  749. * Get the link URL in ASCII-compatible encoding.
  750. *
  751. * @return string
  752. */
  753. function get_ascii_url(){
  754. return blcUtility::idn_to_ascii($this->url);
  755. }
  756. }
  757. } //class_exists
  758. /**
  759. * Remove orphaned links that have no corresponding instances.
  760. *
  761. * @param int|array $link_id (optional) Only check these links
  762. * @return bool
  763. */
  764. function blc_cleanup_links( $link_id = null ){
  765. global $wpdb; /* @var wpdb $wpdb */
  766. global $blclog;
  767. $q = "DELETE FROM {$wpdb->prefix}blc_links
  768. USING {$wpdb->prefix}blc_links LEFT JOIN {$wpdb->prefix}blc_instances
  769. ON {$wpdb->prefix}blc_instances.link_id = {$wpdb->prefix}blc_links.link_id
  770. WHERE
  771. {$wpdb->prefix}blc_instances.link_id IS NULL";
  772. if ( $link_id !== null ) {
  773. if ( !is_array($link_id) ){
  774. $link_id = array( intval($link_id) );
  775. }
  776. $q .= " AND {$wpdb->prefix}blc_links.link_id IN (" . implode(', ', $link_id) . ')';
  777. }
  778. $rez = $wpdb->query( $q );
  779. $blclog->log(sprintf('... %d links deleted', $wpdb->rows_affected));
  780. return $rez !== false;
  781. }
  782. ?>