PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 898 lines | 384 code | 118 blank | 396 comment | 46 complexity | 944ca25b7901747b05406535643d0f81 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The base class for link container managers.
  4. *
  5. * Sub-classes should override at least the get_containers() and resynch() methods.
  6. *
  7. * @package Broken Link Checker
  8. * @access public
  9. */
  10. class blcContainerManager extends blcModule {
  11. var $container_type = '';
  12. var $fields = array();
  13. var $container_class_name = 'blcContainer';
  14. /**
  15. * Do whatever setup necessary that wasn't already done in the constructor.
  16. *
  17. * This method was added so that sub-classes would have something "safe" to
  18. * over-ride without having to deal with PHP4/5 constructors.
  19. *
  20. * @return void
  21. */
  22. function init(){
  23. parent::init();
  24. $this->container_type = $this->module_id;
  25. //Sub-classes might also use it to set up hooks, etc.
  26. }
  27. /**
  28. * Instantiate a link container.
  29. *
  30. * @param array $container An associative array of container data.
  31. * @return blcContainer
  32. */
  33. function get_container($container){
  34. $container['fields'] = $this->get_parseable_fields();
  35. $container_obj = new $this->container_class_name($container);
  36. return $container_obj;
  37. }
  38. /**
  39. * Instantiate multiple containers of the container type managed by this class and optionally
  40. * pre-load container data used for display/parsing.
  41. *
  42. * Sub-classes should, if possible, use the $purpose argument to pre-load any extra data required for
  43. * the specified task right away, instead of making multiple DB roundtrips later. For example, if
  44. * $purpose is set to the BLC_FOR_DISPLAY constant, you might want to preload any DB data that the
  45. * container will need in blcContainer::ui_get_source().
  46. *
  47. * @see blcContainer::make_containers()
  48. * @see blcContainer::ui_get_source()
  49. * @see blcContainer::ui_get_action_links()
  50. *
  51. * @param array $containers Array of assoc. arrays containing container data.
  52. * @param string $purpose An optional code indicating how the retrieved containers will be used.
  53. * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose.
  54. *
  55. * @return array of blcContainer indexed by "container_type|container_id"
  56. */
  57. function get_containers($containers, $purpose = '', $load_wrapped_objects = false){
  58. return $this->make_containers($containers);
  59. }
  60. /**
  61. * Instantiate multiple containers of the container type managed by this class
  62. *
  63. * @param array $containers Array of assoc. arrays containing container data.
  64. * @return array of blcContainer indexed by "container_type|container_id"
  65. */
  66. function make_containers($containers){
  67. $results = array();
  68. foreach($containers as $container){
  69. $key = $container['container_type'] . '|' . $container['container_id'];
  70. $results[ $key ] = $this->get_container($container);
  71. }
  72. return $results;
  73. }
  74. /**
  75. * Create or update synchronization records for all containers managed by this class.
  76. *
  77. * Must be over-ridden in subclasses.
  78. *
  79. * @param bool $forced If true, assume that all synch. records are gone and will need to be recreated from scratch.
  80. * @return void
  81. */
  82. function resynch($forced = false){
  83. trigger_error('Function blcContainerManager::resynch() must be over-ridden in a sub-class', E_USER_ERROR);
  84. }
  85. /**
  86. * Resynch when activated.
  87. *
  88. * @uses blcContainerManager::resynch()
  89. *
  90. * @return void
  91. */
  92. function activated(){
  93. $this->resynch();
  94. blc_got_unsynched_items();
  95. }
  96. /**
  97. * Get a list of the parseable fields and their formats common to all containers of this type.
  98. *
  99. * @return array Associative array of formats indexed by field name.
  100. */
  101. function get_parseable_fields(){
  102. return $this->fields;
  103. }
  104. /**
  105. * Get the message to display after $n containers have been deleted.
  106. *
  107. * @param int $n Number of deleted containers.
  108. * @return string A delete confirmation message, e.g. "5 posts were moved to trash"
  109. */
  110. function ui_bulk_delete_message($n){
  111. return sprintf(
  112. _n(
  113. "%d '%s' has been deleted",
  114. "%d '%s' have been deleted",
  115. $n,
  116. 'broken-link-checker'
  117. ),
  118. $n,
  119. $this->container_type
  120. );
  121. }
  122. /**
  123. * Get the message to display after $n containers have been moved to the trash.
  124. *
  125. * @param int $n Number of trashed containers.
  126. * @return string A delete confirmation message, e.g. "5 posts were moved to trash"
  127. */
  128. function ui_bulk_trash_message($n){
  129. return $this->ui_bulk_delete_message($n);
  130. }
  131. }
  132. /**
  133. * The base class for link containers. All containers should extend this class.
  134. *
  135. * @package Broken Link Checker
  136. * @access public
  137. */
  138. class blcContainer {
  139. var $fields = array();
  140. var $default_field = '';
  141. var $container_type;
  142. var $container_id = 0;
  143. var $synched = false;
  144. var $last_synch = '0000-00-00 00:00:00';
  145. var $wrapped_object = null;
  146. /**
  147. * Constructor
  148. *
  149. * @param array $data
  150. * @param object $wrapped_object
  151. * @return void
  152. */
  153. function __construct( $data = null, $wrapped_object = null ){
  154. $this->wrapped_object = $wrapped_object;
  155. if ( !empty($data) && is_array($data) ){
  156. foreach($data as $name => $value){
  157. $this->$name = $value;
  158. }
  159. }
  160. }
  161. /**
  162. * Get the value of the specified field of the object wrapped by this container.
  163. *
  164. * @access protected
  165. *
  166. * @param string $field Field name. If omitted, the value of the default field will be returned.
  167. * @return string
  168. */
  169. function get_field($field = ''){
  170. if ( empty($field) ){
  171. $field = $this->default_field;
  172. }
  173. $w = &$this->get_wrapped_object();
  174. return $w->$field;
  175. }
  176. /**
  177. * Update the value of the specified field in the wrapped object.
  178. * This method will also immediately save the changed value by calling update_wrapped_object().
  179. *
  180. * @access protected
  181. *
  182. * @param string $field Field name.
  183. * @param string $new_value Set the field to this value.
  184. * @param string $old_value The previous value of the field. Optional, but can be useful for container that need the old value to distinguish between several instances of the same field (e.g. post metadata).
  185. * @return bool|WP_Error True on success, an error object if something went wrong.
  186. */
  187. function update_field($field, $new_value, $old_value = ''){
  188. $w = &$this->get_wrapped_object();
  189. $w->$field = $new_value;
  190. return $this->update_wrapped_object();
  191. }
  192. /**
  193. * Retrieve the entity wrapped by this container.
  194. * The fetched object will also be cached in the $wrapped_object variable.
  195. *
  196. * @access protected
  197. *
  198. * @param bool $ensure_consistency Set this to true to ignore the cached $wrapped_object value and retrieve an up-to-date copy of the wrapped object from the DB (or WP's internal cache).
  199. * @return object The wrapped object.
  200. */
  201. function get_wrapped_object($ensure_consistency = false){
  202. trigger_error('Function blcContainer::get_wrapped_object() must be over-ridden in a sub-class', E_USER_ERROR);
  203. }
  204. /**
  205. * Update the entity wrapped by the container with values currently in the $wrapped_object.
  206. *
  207. * @access protected
  208. *
  209. * @return bool|WP_Error True on success, an error if something went wrong.
  210. */
  211. function update_wrapped_object(){
  212. trigger_error('Function blcContainer::update_wrapped_object() must be over-ridden in a sub-class', E_USER_ERROR);
  213. }
  214. /**
  215. * Parse the container for links and save the results to the DB.
  216. *
  217. * @return void
  218. */
  219. function synch(){
  220. //FB::log("Parsing {$this->container_type}[{$this->container_id}]");
  221. //Remove any existing link instance records associated with the container
  222. $this->delete_instances();
  223. //Load the wrapped object, if not done already
  224. $this->get_wrapped_object();
  225. //FB::log($this->fields, "Parseable fields :");
  226. //Iterate over all parse-able fields
  227. foreach($this->fields as $name => $format){
  228. //Get the field value
  229. $value = $this->get_field($name);
  230. if ( empty($value) ){
  231. //FB::log($name, "Skipping empty field");
  232. continue;
  233. }
  234. //FB::log($name, "Parsing field");
  235. //Get all parsers applicable to this field
  236. $parsers = blcParserHelper::get_parsers( $format, $this->container_type );
  237. //FB::log($parsers, "Applicable parsers");
  238. if ( empty($parsers) ) continue;
  239. $base_url = $this->base_url();
  240. $default_link_text = $this->default_link_text($name);
  241. //Parse the field with each parser
  242. foreach($parsers as $parser){
  243. //FB::log("Parsing $name with '{$parser->parser_type}' parser");
  244. $found_instances = $parser->parse( $value, $base_url, $default_link_text );
  245. //FB::log($found_instances, "Found instances");
  246. //Complete the link instances by adding container info, then save them to the DB.
  247. foreach($found_instances as $instance){
  248. $instance->set_container($this, $name);
  249. $instance->save();
  250. }
  251. }
  252. }
  253. $this->mark_as_synched();
  254. }
  255. /**
  256. * Mark the container as successfully synchronized (parsed for links).
  257. *
  258. * @return bool
  259. */
  260. function mark_as_synched(){
  261. global $wpdb; /* @var wpdb $wpdb */
  262. $this->last_synch = time();
  263. $q = "INSERT INTO {$wpdb->prefix}blc_synch( container_id, container_type, synched, last_synch)
  264. VALUES( %d, %s, %d, NOW() )
  265. ON DUPLICATE KEY UPDATE synched = VALUES(synched), last_synch = VALUES(last_synch)";
  266. $rez = $wpdb->query( $wpdb->prepare( $q, $this->container_id, $this->container_type, 1 ) );
  267. return ($rez !== false);
  268. }
  269. /**
  270. * blcContainer::mark_as_unsynched()
  271. * Mark the container as not synchronized (not parsed, or modified since the last parse).
  272. * The plugin will attempt to (re)parse the container at the earliest opportunity.
  273. *
  274. * @return bool
  275. */
  276. function mark_as_unsynched(){
  277. global $wpdb; /* @var wpdb $wpdb */
  278. $q = "INSERT INTO {$wpdb->prefix}blc_synch( container_id, container_type, synched, last_synch)
  279. VALUES( %d, %s, %d, '0000-00-00 00:00:00' )
  280. ON DUPLICATE KEY UPDATE synched = VALUES(synched)";
  281. $rez = $wpdb->query( $wpdb->prepare( $q, $this->container_id, $this->container_type, 0 ) );
  282. blc_got_unsynched_items();
  283. return ($rez !== false);
  284. }
  285. /**
  286. * Get the base URL of the container. Used to normalize relative URLs found
  287. * in the container. For example, for posts this would be the post permalink.
  288. *
  289. * @return string
  290. */
  291. function base_url(){
  292. return home_url();
  293. }
  294. /**
  295. * Get the default link text to use for links found in a specific container field.
  296. *
  297. * This is generally only meaningful for non-HTML container fields.
  298. * For example, if the container is post metadata, the default
  299. * link text might be equal to the name of the custom field.
  300. *
  301. * @param string $field
  302. * @return string
  303. */
  304. function default_link_text($field = ''){
  305. return '';
  306. }
  307. /**
  308. * Delete the DB record of this container.
  309. * Also deletes the DB records of all link instances associated with it.
  310. * Calling this method will not affect the WP entity (e.g. a post) corresponding to this container.
  311. *
  312. * @return bool
  313. */
  314. function delete(){
  315. global $wpdb; /* @var wpdb $wpdb */
  316. //Delete instances first.
  317. $rez = $this->delete_instances();
  318. if ( !$rez ){
  319. return false;
  320. }
  321. //Now delete the container record.
  322. $q = "DELETE FROM {$wpdb->prefix}blc_synch
  323. WHERE container_id = %d AND container_type = %s";
  324. $q = $wpdb->prepare($q, $this->container_id, $this->container_type);
  325. if ( $wpdb->query( $q ) === false ){
  326. return false;
  327. } else {
  328. return true;
  329. }
  330. }
  331. /**
  332. * Delete all link instance records associated with this container.
  333. * NB: Calling this method will not affect the WP entity (e.g. a post) corresponding to this container.
  334. *
  335. * @return bool
  336. */
  337. function delete_instances(){
  338. global $wpdb; /* @var wpdb $wpdb */
  339. //Remove instances associated with this container
  340. $q = "DELETE FROM {$wpdb->prefix}blc_instances
  341. WHERE container_id = %d AND container_type = %s";
  342. $q = $wpdb->prepare($q, $this->container_id, $this->container_type);
  343. if ( $wpdb->query( $q ) === false ){
  344. return false;
  345. } else {
  346. return true;
  347. }
  348. }
  349. /**
  350. * Delete or trash the WP entity corresponding to this container. Should prefer moving to trash, if possible.
  351. * Also remove the synch. record of the container and all associated instances.
  352. *
  353. * Must be over-ridden in a sub-class.
  354. *
  355. * @return bool|WP_Error
  356. */
  357. function delete_wrapped_object(){
  358. trigger_error('Function blcContainer::delete_wrapped_object() must be over-ridden in a sub-class', E_USER_ERROR);
  359. }
  360. /**
  361. * Move the WP entity corresponding to this container to the Trash.
  362. *
  363. * Must be over-riden in a subclass.
  364. *
  365. * @return bool|WP_Error
  366. */
  367. function trash_wrapped_object(){
  368. trigger_error('Function blcContainer::trash_wrapped_object() must be over-ridden in a sub-class', E_USER_ERROR);
  369. }
  370. /**
  371. * Check if the current user can delete/trash this container.
  372. *
  373. * Should be over-ridden in a subclass.
  374. *
  375. * @return bool
  376. */
  377. function current_user_can_delete(){
  378. return false;
  379. }
  380. /**
  381. * Determine if this container can be moved to the trash.
  382. *
  383. * Should be over-ridden in a subclass.
  384. *
  385. * @return bool
  386. */
  387. function can_be_trashed(){
  388. return false;
  389. }
  390. /**
  391. * Change all links with the specified URL to a new URL.
  392. *
  393. * @param string $field_name
  394. * @param blcParser $parser
  395. * @param string $new_url
  396. * @param string $old_url
  397. * @param string $old_raw_url
  398. * @return array|WP_Error The new value of raw_url on success, or an error object if something went wrong.
  399. */
  400. function edit_link($field_name, $parser, $new_url, $old_url = '', $old_raw_url = ''){
  401. //Ensure we're operating on a consistent copy of the wrapped object.
  402. /*
  403. Explanation
  404. Consider this scenario where the container object wraps a blog post :
  405. 1) The container object gets created and loads the post data.
  406. 2) Someone modifies the DB data corresponding to the post.
  407. 3) The container tries to edit a link present in the post. However, the pots
  408. has changed since the time it was first cached, so when the container updates
  409. the post with it's changes, it will overwrite whatever modifications were made
  410. in step 2.
  411. This would not be a problem if WP entities like posts and comments were
  412. actually real objects, not just bags of key=>value pairs, but oh well.
  413. Therefore, it is necessary to re-load the wrapped object before editing it.
  414. */
  415. $this->get_wrapped_object(true);
  416. //Get the current value of the field that needs to be edited.
  417. $old_value = $this->get_field($field_name);
  418. //Have the parser modify the specified link. If successful, the parser will
  419. //return an associative array with two keys - 'content' and 'raw_url'.
  420. //Otherwise we'll get an instance of WP_Error.
  421. $edit_result = $parser->edit($old_value, $new_url, $old_url, $old_raw_url);
  422. if ( is_wp_error($edit_result) ){
  423. return $edit_result;
  424. }
  425. //Update the field with the new value returned by the parser.
  426. $update_result = $this->update_field( $field_name, $edit_result['content'], $old_value );
  427. if ( is_wp_error($update_result) ){
  428. return $update_result;
  429. }
  430. //Return the new values to the instance.
  431. unset($edit_result['content']); //(Except content, which it doesn't need.)
  432. return $edit_result;
  433. }
  434. /**
  435. * Remove all links with the specified URL, leaving their anchor text intact.
  436. *
  437. * @param string $field_name
  438. * @param blcParser $parser
  439. * @param string $url
  440. * @param string $raw_url
  441. * @return bool|WP_Error True on success, or an error object if something went wrong.
  442. */
  443. function unlink($field_name, $parser, $url, $raw_url =''){
  444. //Ensure we're operating on a consistent copy of the wrapped object.
  445. $this->get_wrapped_object(true);
  446. $old_value = $this->get_field($field_name);
  447. $new_value = $parser->unlink($old_value, $url, $raw_url);
  448. if ( is_wp_error($new_value) ){
  449. return $new_value;
  450. }
  451. return $this->update_field( $field_name, $new_value, $old_value );
  452. }
  453. /**
  454. * Retrieve a list of links found in this container.
  455. *
  456. * @access public
  457. *
  458. * @return array of blcLink
  459. */
  460. function get_links(){
  461. $params = array(
  462. 's_container_type' => $this->container_type,
  463. 's_container_id' => $this->container_id,
  464. );
  465. return blc_get_links($params);
  466. }
  467. /**
  468. * Get action links to display in the "Source" column of the Tools -> Broken Links link table.
  469. *
  470. * @param string $container_field
  471. * @return array
  472. */
  473. function ui_get_action_links($container_field){
  474. return array();
  475. }
  476. /**
  477. * Get the container name to display in the "Source" column of the Tools -> Broken Links link table.
  478. *
  479. * @param string $container_field
  480. * @param string $context
  481. * @return string
  482. */
  483. function ui_get_source($container_field, $context = 'display'){
  484. return sprintf('%s[%d] : %s', $this->container_type, $this->container_id, $container_field);
  485. }
  486. /**
  487. * Get edit URL. Returns the URL of the Dashboard page where the item associated with this
  488. * container can be edited.
  489. *
  490. * HTML entities like '&' will be properly escaped for display.
  491. *
  492. * @access protected
  493. *
  494. * @return string
  495. */
  496. function get_edit_url(){
  497. //Should be over-ridden in a sub-class.
  498. return '';
  499. }
  500. }
  501. /**
  502. * An utility class for working with link container types.
  503. * All methods of this class should be called statically.
  504. *
  505. * @package Broken Link Checker
  506. */
  507. class blcContainerHelper {
  508. /**
  509. * Get the manager associated with a container type.
  510. *
  511. * @param string $container_type
  512. * @param string $fallback If there is no manager associated with $container_type, return the manager of this container type instead.
  513. * @return blcContainerManager|null
  514. */
  515. static function get_manager( $container_type, $fallback = '' ){
  516. $module_manager = blcModuleManager::getInstance();
  517. $container_manager = null;
  518. if ( $container_manager = $module_manager->get_module($container_type, true, 'container') ){
  519. return $container_manager;
  520. } elseif ( !empty($fallback) && ( $container_manager = $module_manager->get_module($fallback, true, 'container') ) ) {
  521. return $container_manager;
  522. } else {
  523. return null;
  524. }
  525. }
  526. /**
  527. * Retrieve or instantiate a container object.
  528. *
  529. * Pass an array containing the container type (string) and ID (int) to retrieve the container
  530. * from the database. Alternatively, pass an associative array to create a new container object
  531. * from the data in the array.
  532. *
  533. * @param array $container Either [container_type, container_id], or an assoc. array of container data.
  534. * @return blcContainer|null
  535. */
  536. static function get_container( $container ){
  537. global $wpdb; /* @var wpdb $wpdb */
  538. if ( !is_array($container) || ( count($container) < 2 ) ){
  539. return null;
  540. }
  541. if ( is_string($container[0]) && is_numeric($container[1]) ){
  542. //The argument is in the [container_type, id] format
  543. //Fetch the container's synch record.
  544. $q = "SELECT * FROM {$wpdb->prefix}blc_synch WHERE container_type = %s AND container_id = %d";
  545. $q = $wpdb->prepare($q, $container[0], $container[1]);
  546. $rez = $wpdb->get_row($q, ARRAY_A);
  547. if ( empty($rez) ){
  548. //The container wasn't found, so we'll create a new one.
  549. $container = array(
  550. 'container_type' => $container[0],
  551. 'container_id' => $container[1],
  552. );
  553. } else {
  554. $container = $rez;
  555. }
  556. }
  557. if ( !($manager = blcContainerHelper::get_manager($container['container_type'])) ){
  558. return null;
  559. }
  560. return $manager->get_container($container);
  561. }
  562. /**
  563. * Retrieve or instantiate multiple link containers.
  564. *
  565. * Takes an array of container specifications and returns an array of container objects.
  566. * Each input array entry should be an array and consist either of a container type (string)
  567. * and container ID (int), or name => value pairs describing a container object.
  568. *
  569. * @see blcContainerHelper::get_container()
  570. *
  571. * @param array $containers
  572. * @param string $purpose Optional code indicating how the retrieved containers will be used.
  573. * @param string $fallback The fallback container type to use for unrecognized containers.
  574. * @param bool $load_wrapped_objects Preload wrapped objects regardless of purpose.
  575. * @return blcContainer[] Array of blcContainer indexed by "container_type|container_id"
  576. */
  577. static function get_containers( $containers, $purpose = '', $fallback = '', $load_wrapped_objects = false ){
  578. global $wpdb; /* @var wpdb $wpdb */
  579. //If the input is invalid or empty, return an empty array.
  580. if ( !is_array($containers) || (count($containers) < 1) ) {
  581. return array();
  582. }
  583. $first = reset($containers);
  584. if ( !is_array($first) ){
  585. return array();
  586. }
  587. if ( isset($first[0]) && is_string($first[0]) && is_numeric($first[1]) ){
  588. //The argument is an array of [container_type, id].
  589. //Divide the container IDs by container type.
  590. $by_type = array();
  591. foreach($containers as $container){
  592. if ( isset($by_type[$container[0]]) ){
  593. array_push($by_type[$container[0]], intval($container[1]));
  594. } else {
  595. $by_type[$container[0]] = array( intval($container[1]) );
  596. }
  597. }
  598. //Build the SQL to fetch all the specified containers
  599. $q = "SELECT *
  600. FROM {$wpdb->prefix}blc_synch
  601. WHERE";
  602. $pieces = array();
  603. foreach($by_type as $container_type => $container_ids){
  604. $pieces[] = '( container_type = "'. $wpdb->escape($container_type) .'" AND container_id IN ('. implode(', ', $container_ids) .') )';
  605. }
  606. $q .= implode("\n\t OR ", $pieces);
  607. //Fetch the container synch. records from the DB.
  608. $containers = $wpdb->get_results($q, ARRAY_A);
  609. }
  610. /*
  611. Divide the inputs into separate arrays by container type (again), then invoke
  612. the appropriate manager for each type to instantiate the container objects.
  613. */
  614. //At this point, $containers is an array of assoc. arrays comprising container data.
  615. $by_type = array();
  616. foreach($containers as $container){
  617. if ( isset($by_type[$container['container_type']]) ){
  618. array_push($by_type[$container['container_type']], $container);
  619. } else {
  620. $by_type[$container['container_type']] = array($container);
  621. }
  622. }
  623. $results = array();
  624. foreach($by_type as $container_type => $entries){
  625. $manager = blcContainerHelper::get_manager($container_type, $fallback);
  626. if ( !is_null($manager) ){
  627. $partial_results = $manager->get_containers($entries, $purpose, $load_wrapped_objects);
  628. $results = array_merge($results, $partial_results);
  629. }
  630. }
  631. return $results;
  632. }
  633. /**
  634. * Retrieve link containers that need to be synchronized (parsed).
  635. *
  636. * @param integer $max_results The maximum number of containers to return. Defaults to returning all unsynched containers.
  637. * @return blcContainer[]
  638. */
  639. static function get_unsynched_containers($max_results = 0){
  640. global $wpdb; /* @var wpdb $wpdb */
  641. $q = "SELECT * FROM {$wpdb->prefix}blc_synch WHERE synched = 0";
  642. if ( $max_results > 0 ){
  643. $q .= " LIMIT $max_results";
  644. }
  645. $container_data = $wpdb->get_results($q, ARRAY_A);
  646. //FB::log($container_data, "Unsynched containers");
  647. if( empty($container_data) ){
  648. return array();
  649. }
  650. $containers = blcContainerHelper::get_containers($container_data, BLC_FOR_PARSING, 'dummy');
  651. return $containers;
  652. }
  653. /**
  654. * (Re)create and update synchronization records for all supported containers.
  655. * Calls the resynch() method of all registered managers.
  656. *
  657. * @param bool $forced If true, assume that no synch. records exist and build all of them from scratch.
  658. * @return void
  659. */
  660. static function resynch($forced = false){
  661. global $wpdb;
  662. $module_manager = blcModuleManager::getInstance();
  663. $active_managers = $module_manager->get_active_by_category('container');
  664. foreach($active_managers as $module_id => $module_data){
  665. $manager = $module_manager->get_module($module_id);
  666. if ( $manager ){
  667. $manager->resynch($forced);
  668. }
  669. }
  670. }
  671. /**
  672. * Mark as unparsed all containers that match one of the the specified formats or
  673. * container types and that were last parsed after a specific timestamp.
  674. *
  675. * Used by newly activated parsers to force the containers they're interested in
  676. * to resynchronize and thus let the parser process them.
  677. *
  678. * @param array $formats Associative array of timestamps, indexed by format IDs.
  679. * @param array $container_types Associative array of timestamps, indexed by container types.
  680. * @return bool
  681. */
  682. function mark_as_unsynched_where($formats, $container_types){
  683. global $wpdb; /* @var wpdb $wpdb */
  684. global $blclog;
  685. //Find containers that match any of the specified formats and add them to
  686. //the list of container types that need to be marked as unsynched.
  687. $module_manager = blcModuleManager::getInstance();
  688. $containers = $module_manager->get_active_by_category('container');
  689. foreach($containers as $module_id => $module_data){
  690. if ( $container_manager = &$module_manager->get_module($module_id) ){
  691. $fields = $container_manager->get_parseable_fields();
  692. $container_type = $container_manager->container_type;
  693. foreach($formats as $format => $timestamp){
  694. if ( in_array($format, $fields) ){
  695. //Choose the earliest timestamp
  696. if ( isset($container_types[$container_type]) ){
  697. $container_types[$container_type] = min($timestamp, $container_types[$container_type]);
  698. } else {
  699. $container_types[$container_type] = $timestamp;
  700. }
  701. }
  702. }
  703. };
  704. }
  705. if ( empty($container_types) ){
  706. return true;
  707. }
  708. //Build the query to update all synch. records that match one of the specified
  709. //container types and have been parsed after the specified time.
  710. $q = "UPDATE {$wpdb->prefix}blc_synch SET synched = 0 WHERE ";
  711. $pieces = array();
  712. foreach($container_types as $container_type => $timestamp){
  713. $pieces[] = $wpdb->prepare(
  714. '(container_type = %s AND last_synch >= %s)',
  715. $container_type,
  716. date('Y-m-d H:i:s', $timestamp)
  717. );
  718. }
  719. $q .= implode(' OR ', $pieces);
  720. $blclog->log('...... Executing query: ' . $q);
  721. $rez = ($wpdb->query($q) !== false);
  722. $blclog->log(sprintf('...... %d rows affected', $wpdb->rows_affected));
  723. blc_got_unsynched_items();
  724. return $rez;
  725. }
  726. /**
  727. * Remove synch. records that reference container types not currently loaded
  728. *
  729. * @return bool
  730. */
  731. static function cleanup_containers(){
  732. global $wpdb; /* @var wpdb $wpdb */
  733. global $blclog;
  734. $module_manager = blcModuleManager::getInstance();
  735. $active_containers = $module_manager->get_escaped_ids('container');
  736. $q = "DELETE synch.*
  737. FROM {$wpdb->prefix}blc_synch AS synch
  738. WHERE
  739. synch.container_type NOT IN ({$active_containers})";
  740. $rez = $wpdb->query($q);
  741. $blclog->log(sprintf('... %d synch records deleted', $wpdb->rows_affected));
  742. return $rez !== false;
  743. }
  744. /**
  745. * Get the message to display after $n containers of a specific type have been deleted.
  746. *
  747. * @param string $container_type
  748. * @param int $n Number of deleted containers.
  749. * @return string A delete confirmation message, e.g. "5 posts were moved to trash"
  750. */
  751. static function ui_bulk_delete_message($container_type, $n){
  752. $manager = blcContainerHelper::get_manager($container_type);
  753. if ( is_null($manager) ){
  754. return sprintf(__("Container type '%s' not recognized", 'broken-link-checker'), $container_type);
  755. } else {
  756. return $manager->ui_bulk_delete_message($n);
  757. }
  758. }
  759. /**
  760. * Get the message to display after $n containers of a specific type have been moved to the trash.
  761. *
  762. * @see blcContainerHelper::ui_bulk_delete_message()
  763. *
  764. * @param string $container_type
  765. * @param int $n
  766. * @return string
  767. */
  768. static function ui_bulk_trash_message($container_type, $n){
  769. $manager = blcContainerHelper::get_manager($container_type);
  770. if ( is_null($manager) ){
  771. return sprintf(__("Container type '%s' not recognized", 'broken-link-checker'), $container_type);
  772. } else {
  773. return $manager->ui_bulk_trash_message($n);
  774. }
  775. }
  776. }
  777. ?>