@@ -4,44 +4,44 @@ |
||
| 4 | 4 | */ |
| 5 | 5 | abstract class Wordlift_Listable { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 9 | - * |
|
| 10 | - * @param int $offset The start offset. |
|
| 11 | - * @param int $limit The maximum number of items to return. |
|
| 12 | - * @param array $args Additional arguments. |
|
| 13 | - * |
|
| 14 | - * @return array A array of items (or an empty array if no items are found). |
|
| 15 | - */ |
|
| 16 | - abstract function find( $offset = 0, $limit = 10, $args = array() ); |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param callable $callback |
|
| 20 | - * @param array $args |
|
| 21 | - * @param int $offset |
|
| 22 | - * @param int $max |
|
| 23 | - */ |
|
| 24 | - public function process( $callback, $args = array(), $offset = 0, $max = PHP_INT_MAX ) { |
|
| 25 | - |
|
| 26 | - // We process users in chunks in order to avoid using too much memory, |
|
| 27 | - // starting at offset 0, 10 users at a time. |
|
| 28 | - $limit = 10; |
|
| 29 | - |
|
| 30 | - while ( 0 < sizeof( $items = $this->find( $offset, $limit, $args ) ) && $offset < $max ) { |
|
| 31 | - |
|
| 32 | - // Cycle through items and call the callback function. |
|
| 33 | - foreach ( $items as $item ) { |
|
| 34 | - call_user_func_array( $callback, array( $item ) ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - // Clean the cache to avoid memory errors. |
|
| 38 | - wp_cache_flush(); |
|
| 39 | - |
|
| 40 | - // Move to the next offset. |
|
| 41 | - $offset += $limit; |
|
| 42 | - |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - } |
|
| 7 | + /** |
|
| 8 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 9 | + * |
|
| 10 | + * @param int $offset The start offset. |
|
| 11 | + * @param int $limit The maximum number of items to return. |
|
| 12 | + * @param array $args Additional arguments. |
|
| 13 | + * |
|
| 14 | + * @return array A array of items (or an empty array if no items are found). |
|
| 15 | + */ |
|
| 16 | + abstract function find( $offset = 0, $limit = 10, $args = array() ); |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param callable $callback |
|
| 20 | + * @param array $args |
|
| 21 | + * @param int $offset |
|
| 22 | + * @param int $max |
|
| 23 | + */ |
|
| 24 | + public function process( $callback, $args = array(), $offset = 0, $max = PHP_INT_MAX ) { |
|
| 25 | + |
|
| 26 | + // We process users in chunks in order to avoid using too much memory, |
|
| 27 | + // starting at offset 0, 10 users at a time. |
|
| 28 | + $limit = 10; |
|
| 29 | + |
|
| 30 | + while ( 0 < sizeof( $items = $this->find( $offset, $limit, $args ) ) && $offset < $max ) { |
|
| 31 | + |
|
| 32 | + // Cycle through items and call the callback function. |
|
| 33 | + foreach ( $items as $item ) { |
|
| 34 | + call_user_func_array( $callback, array( $item ) ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + // Clean the cache to avoid memory errors. |
|
| 38 | + wp_cache_flush(); |
|
| 39 | + |
|
| 40 | + // Move to the next offset. |
|
| 41 | + $offset += $limit; |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @return array A array of items (or an empty array if no items are found). |
| 15 | 15 | */ |
| 16 | - abstract function find( $offset = 0, $limit = 10, $args = array() ); |
|
| 16 | + abstract function find($offset = 0, $limit = 10, $args = array()); |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * @param callable $callback |
@@ -21,17 +21,17 @@ discard block |
||
| 21 | 21 | * @param int $offset |
| 22 | 22 | * @param int $max |
| 23 | 23 | */ |
| 24 | - public function process( $callback, $args = array(), $offset = 0, $max = PHP_INT_MAX ) { |
|
| 24 | + public function process($callback, $args = array(), $offset = 0, $max = PHP_INT_MAX) { |
|
| 25 | 25 | |
| 26 | 26 | // We process users in chunks in order to avoid using too much memory, |
| 27 | 27 | // starting at offset 0, 10 users at a time. |
| 28 | 28 | $limit = 10; |
| 29 | 29 | |
| 30 | - while ( 0 < sizeof( $items = $this->find( $offset, $limit, $args ) ) && $offset < $max ) { |
|
| 30 | + while (0 < sizeof($items = $this->find($offset, $limit, $args)) && $offset < $max) { |
|
| 31 | 31 | |
| 32 | 32 | // Cycle through items and call the callback function. |
| 33 | - foreach ( $items as $item ) { |
|
| 34 | - call_user_func_array( $callback, array( $item ) ); |
|
| 33 | + foreach ($items as $item) { |
|
| 34 | + call_user_func_array($callback, array($item)); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | // Clean the cache to avoid memory errors. |
@@ -13,127 +13,127 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Rebuild_Service extends Wordlift_Listable { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A {@link Wordlift_Log_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.6.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $log; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Sparql_Service} instance. |
|
| 27 | - * @since 3.6.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $sparql_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var \Wordlift_Uri_Service |
|
| 35 | - */ |
|
| 36 | - private $uri_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create an instance of Wordlift_Rebuild_Service. |
|
| 40 | - * |
|
| 41 | - * @since 3.6.0 |
|
| 42 | - * |
|
| 43 | - * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 44 | - * @param \Wordlift_Uri_Service $uri_service |
|
| 45 | - */ |
|
| 46 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 47 | - |
|
| 48 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 49 | - |
|
| 50 | - $this->sparql_service = $sparql_service; |
|
| 51 | - $this->uri_service = $uri_service; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 56 | - * it with local data. |
|
| 57 | - * |
|
| 58 | - * @since 3.6.0 |
|
| 59 | - */ |
|
| 60 | - public function rebuild() { |
|
| 61 | - |
|
| 62 | - // Give ourselves some time to process the data. |
|
| 63 | - set_time_limit( 21600 ); // 6 hours |
|
| 64 | - |
|
| 65 | - // Send textual output. |
|
| 66 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 67 | - |
|
| 68 | - // We start at 0 by default and get to max. |
|
| 69 | - $offset = $_GET['offset'] ?: 0; |
|
| 70 | - $limit = $_GET['limit'] ?: PHP_INT_MAX; |
|
| 71 | - $max = $offset + $limit; |
|
| 72 | - |
|
| 73 | - // If we're starting at offset 0, then delete existing URIs and data from |
|
| 74 | - // the remote dataset. |
|
| 75 | - if ( 0 === $offset ) { |
|
| 76 | - |
|
| 77 | - // Clear out all generated URIs, since the dataset URI might have changed |
|
| 78 | - // in the process. |
|
| 79 | - $this->uri_service->delete_all(); |
|
| 80 | - |
|
| 81 | - // Delete all the triples in the remote dataset. |
|
| 82 | - $this->sparql_service->queue( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 83 | - |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Go through the list of published entities and posts and call the (legacy) |
|
| 87 | - // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 88 | - // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 89 | - // and will cycle through all the posts w/ a very small memory footprint |
|
| 90 | - // in order to avoid memory errors. |
|
| 91 | - |
|
| 92 | - $count = 0; |
|
| 93 | - $this->process( function ( $post ) use ( &$count ) { |
|
| 94 | - $count ++; |
|
| 95 | - wl_linked_data_save_post( $post->ID ); |
|
| 96 | - }, array( |
|
| 97 | - 'post_status' => 'publish', |
|
| 98 | - 'post_type' => array( 'entity', 'post' ) |
|
| 99 | - ), $offset, $max ); |
|
| 100 | - |
|
| 101 | - // Redirect to the next chunk. |
|
| 102 | - if ( $count == $limit ) { |
|
| 103 | - wp_redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit ) ); |
|
| 104 | - } else { |
|
| 105 | - echo( "done [ count :: $count ][ limit :: $limit ]" ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // If we're being called as AJAX, die here. |
|
| 109 | - if ( DOING_AJAX ) { |
|
| 110 | - wp_die(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 117 | - * |
|
| 118 | - * @since 3.6.0 |
|
| 119 | - * |
|
| 120 | - * @param int $offset The start offset. |
|
| 121 | - * @param int $limit The maximum number of items to return. |
|
| 122 | - * @param array $args Additional arguments. |
|
| 123 | - * |
|
| 124 | - * @return array A array of items (or an empty array if no items are found). |
|
| 125 | - */ |
|
| 126 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 127 | - |
|
| 128 | - return get_posts( wp_parse_args( $args, array( |
|
| 129 | - 'offset' => $offset, |
|
| 130 | - 'numberposts' => $limit, |
|
| 131 | - 'fields' => 'all', |
|
| 132 | - 'orderby' => 'ID', |
|
| 133 | - 'order' => 'ASC', |
|
| 134 | - 'post_status' => 'any', |
|
| 135 | - 'post_type' => 'post' |
|
| 136 | - ) ) ); |
|
| 137 | - } |
|
| 16 | + /** |
|
| 17 | + * A {@link Wordlift_Log_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.6.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $log; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Sparql_Service} instance. |
|
| 27 | + * @since 3.6.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $sparql_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var \Wordlift_Uri_Service |
|
| 35 | + */ |
|
| 36 | + private $uri_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create an instance of Wordlift_Rebuild_Service. |
|
| 40 | + * |
|
| 41 | + * @since 3.6.0 |
|
| 42 | + * |
|
| 43 | + * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 44 | + * @param \Wordlift_Uri_Service $uri_service |
|
| 45 | + */ |
|
| 46 | + public function __construct( $sparql_service, $uri_service ) { |
|
| 47 | + |
|
| 48 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 49 | + |
|
| 50 | + $this->sparql_service = $sparql_service; |
|
| 51 | + $this->uri_service = $uri_service; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 56 | + * it with local data. |
|
| 57 | + * |
|
| 58 | + * @since 3.6.0 |
|
| 59 | + */ |
|
| 60 | + public function rebuild() { |
|
| 61 | + |
|
| 62 | + // Give ourselves some time to process the data. |
|
| 63 | + set_time_limit( 21600 ); // 6 hours |
|
| 64 | + |
|
| 65 | + // Send textual output. |
|
| 66 | + header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 67 | + |
|
| 68 | + // We start at 0 by default and get to max. |
|
| 69 | + $offset = $_GET['offset'] ?: 0; |
|
| 70 | + $limit = $_GET['limit'] ?: PHP_INT_MAX; |
|
| 71 | + $max = $offset + $limit; |
|
| 72 | + |
|
| 73 | + // If we're starting at offset 0, then delete existing URIs and data from |
|
| 74 | + // the remote dataset. |
|
| 75 | + if ( 0 === $offset ) { |
|
| 76 | + |
|
| 77 | + // Clear out all generated URIs, since the dataset URI might have changed |
|
| 78 | + // in the process. |
|
| 79 | + $this->uri_service->delete_all(); |
|
| 80 | + |
|
| 81 | + // Delete all the triples in the remote dataset. |
|
| 82 | + $this->sparql_service->queue( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 83 | + |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Go through the list of published entities and posts and call the (legacy) |
|
| 87 | + // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 88 | + // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 89 | + // and will cycle through all the posts w/ a very small memory footprint |
|
| 90 | + // in order to avoid memory errors. |
|
| 91 | + |
|
| 92 | + $count = 0; |
|
| 93 | + $this->process( function ( $post ) use ( &$count ) { |
|
| 94 | + $count ++; |
|
| 95 | + wl_linked_data_save_post( $post->ID ); |
|
| 96 | + }, array( |
|
| 97 | + 'post_status' => 'publish', |
|
| 98 | + 'post_type' => array( 'entity', 'post' ) |
|
| 99 | + ), $offset, $max ); |
|
| 100 | + |
|
| 101 | + // Redirect to the next chunk. |
|
| 102 | + if ( $count == $limit ) { |
|
| 103 | + wp_redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit ) ); |
|
| 104 | + } else { |
|
| 105 | + echo( "done [ count :: $count ][ limit :: $limit ]" ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // If we're being called as AJAX, die here. |
|
| 109 | + if ( DOING_AJAX ) { |
|
| 110 | + wp_die(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 117 | + * |
|
| 118 | + * @since 3.6.0 |
|
| 119 | + * |
|
| 120 | + * @param int $offset The start offset. |
|
| 121 | + * @param int $limit The maximum number of items to return. |
|
| 122 | + * @param array $args Additional arguments. |
|
| 123 | + * |
|
| 124 | + * @return array A array of items (or an empty array if no items are found). |
|
| 125 | + */ |
|
| 126 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 127 | + |
|
| 128 | + return get_posts( wp_parse_args( $args, array( |
|
| 129 | + 'offset' => $offset, |
|
| 130 | + 'numberposts' => $limit, |
|
| 131 | + 'fields' => 'all', |
|
| 132 | + 'orderby' => 'ID', |
|
| 133 | + 'order' => 'ASC', |
|
| 134 | + 'post_status' => 'any', |
|
| 135 | + 'post_type' => 'post' |
|
| 136 | + ) ) ); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
| 44 | 44 | * @param \Wordlift_Uri_Service $uri_service |
| 45 | 45 | */ |
| 46 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 46 | + public function __construct($sparql_service, $uri_service) { |
|
| 47 | 47 | |
| 48 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 48 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rebuild_Service'); |
|
| 49 | 49 | |
| 50 | 50 | $this->sparql_service = $sparql_service; |
| 51 | 51 | $this->uri_service = $uri_service; |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | public function rebuild() { |
| 61 | 61 | |
| 62 | 62 | // Give ourselves some time to process the data. |
| 63 | - set_time_limit( 21600 ); // 6 hours |
|
| 63 | + set_time_limit(21600); // 6 hours |
|
| 64 | 64 | |
| 65 | 65 | // Send textual output. |
| 66 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 66 | + header('Content-type: text/plain; charset=utf-8'); |
|
| 67 | 67 | |
| 68 | 68 | // We start at 0 by default and get to max. |
| 69 | 69 | $offset = $_GET['offset'] ?: 0; |
@@ -72,14 +72,14 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | // If we're starting at offset 0, then delete existing URIs and data from |
| 74 | 74 | // the remote dataset. |
| 75 | - if ( 0 === $offset ) { |
|
| 75 | + if (0 === $offset) { |
|
| 76 | 76 | |
| 77 | 77 | // Clear out all generated URIs, since the dataset URI might have changed |
| 78 | 78 | // in the process. |
| 79 | 79 | $this->uri_service->delete_all(); |
| 80 | 80 | |
| 81 | 81 | // Delete all the triples in the remote dataset. |
| 82 | - $this->sparql_service->queue( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 82 | + $this->sparql_service->queue('DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };'); |
|
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | |
@@ -90,23 +90,23 @@ discard block |
||
| 90 | 90 | // in order to avoid memory errors. |
| 91 | 91 | |
| 92 | 92 | $count = 0; |
| 93 | - $this->process( function ( $post ) use ( &$count ) { |
|
| 94 | - $count ++; |
|
| 95 | - wl_linked_data_save_post( $post->ID ); |
|
| 93 | + $this->process(function($post) use (&$count) { |
|
| 94 | + $count++; |
|
| 95 | + wl_linked_data_save_post($post->ID); |
|
| 96 | 96 | }, array( |
| 97 | 97 | 'post_status' => 'publish', |
| 98 | - 'post_type' => array( 'entity', 'post' ) |
|
| 99 | - ), $offset, $max ); |
|
| 98 | + 'post_type' => array('entity', 'post') |
|
| 99 | + ), $offset, $max); |
|
| 100 | 100 | |
| 101 | 101 | // Redirect to the next chunk. |
| 102 | - if ( $count == $limit ) { |
|
| 103 | - wp_redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit ) ); |
|
| 102 | + if ($count == $limit) { |
|
| 103 | + wp_redirect(admin_url('admin-ajax.php?action=wl_rebuild&offset='.($offset + $limit).'&limit='.$limit)); |
|
| 104 | 104 | } else { |
| 105 | - echo( "done [ count :: $count ][ limit :: $limit ]" ); |
|
| 105 | + echo("done [ count :: $count ][ limit :: $limit ]"); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // If we're being called as AJAX, die here. |
| 109 | - if ( DOING_AJAX ) { |
|
| 109 | + if (DOING_AJAX) { |
|
| 110 | 110 | wp_die(); |
| 111 | 111 | } |
| 112 | 112 | |
@@ -123,9 +123,9 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @return array A array of items (or an empty array if no items are found). |
| 125 | 125 | */ |
| 126 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 126 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 127 | 127 | |
| 128 | - return get_posts( wp_parse_args( $args, array( |
|
| 128 | + return get_posts(wp_parse_args($args, array( |
|
| 129 | 129 | 'offset' => $offset, |
| 130 | 130 | 'numberposts' => $limit, |
| 131 | 131 | 'fields' => 'all', |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | 'order' => 'ASC', |
| 134 | 134 | 'post_status' => 'any', |
| 135 | 135 | 'post_type' => 'post' |
| 136 | - ) ) ); |
|
| 136 | + ))); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | } |