Completed
Push — develop ( d86f0d...0a9e6f )
by David
03:31
created

Wordlift_Rebuild_Service::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Define the Wordlift_Rebuild_Service class.
5
 */
6
7
/**
8
 * The Wordlift_Rebuild_Service allows to rebuild the Linked Data dataset from
9
 * scratch by clearing out data on the remote dataset, parsing all data in WordPress
10
 * and resending data to the remote dataset.
11
 *
12
 * @since 3.6.0
13
 */
14
class Wordlift_Rebuild_Service extends Wordlift_Listable {
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
		// Clear out all generated URIs, since the dataset URI might have changed
66
		// in the process.
67
		$this->uri_service->delete_all();
68
69
		// Delete all the triples in the remote dataset.
70
		$this->sparql_service->queue( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' );
71
72
		// Go through the list of published entities and posts and call the (legacy)
73
		// `wl_linked_data_save_post` function for each one. We're using the `process`
74
		// function which is provided by the parent `Wordlift_Listable` abstract class
75
		// and will cycle through all the posts w/ a very small memory footprint
76
		// in order to avoid memory errors.
77
		$this->process( function ( $post ) {
78
			wl_linked_data_save_post( $post->ID );
79
		}, array(
80
			'post_status' => 'publish',
81
			'post_type'   => array( 'entity', 'post' )
82
		) );
83
84
		// If we're being called as AJAX, die here.
85
		if ( DOING_AJAX ) {
86
			wp_die();
87
		}
88
89
	}
90
91
	/**
92
	 * List the items starting at the specified offset and up to the specified limit.
93
	 *
94
	 * @since 3.6.0
95
	 *
96
	 * @param int $offset The start offset.
97
	 * @param int $limit The maximum number of items to return.
98
	 * @param array $args Additional arguments.
99
	 *
100
	 * @return array A array of items (or an empty array if no items are found).
101
	 */
102
	function find( $offset = 0, $limit = 10, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
104
		return get_posts( wp_parse_args( $args, array(
105
			'offset'      => $offset,
106
			'numberposts' => $limit,
107
			'fields'      => 'all',
108
			'orderby'     => 'ID',
109
			'order'       => 'ASC',
110
			'post_status' => 'any',
111
			'post_type'   => 'post'
112
		) ) );
113
	}
114
115
}
116