|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Wordlift_Relation_Rebuild_Service extends Wordlift_Listable { |
|
4
|
|
|
/** |
|
5
|
|
|
* @var Wordlift_Content_Filter_Service |
|
6
|
|
|
*/ |
|
7
|
|
|
private $content_filter_service; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var Wordlift_Entity_Service |
|
11
|
|
|
*/ |
|
12
|
|
|
private $entity_service; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 3.14.3 |
|
18
|
|
|
* @access private |
|
19
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $log; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Wordlift_Relation_Rebuild_Service constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Wordlift_Content_Filter_Service $content_filter_service |
|
28
|
|
|
* @param \Wordlift_Entity_Service $entity_service |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( $content_filter_service, $entity_service ) { |
|
31
|
|
|
|
|
32
|
|
|
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Relation_Rebuild_Service' ); |
|
33
|
|
|
|
|
34
|
|
|
$this->content_filter_service = $content_filter_service; |
|
35
|
|
|
$this->entity_service = $entity_service; |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function process_all() { |
|
40
|
|
|
global $wpdb; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
set_time_limit( 21600 ); // 6 hours |
|
43
|
|
|
|
|
44
|
|
|
$this->log->debug( 'Deleting all existing relations...' ); |
|
45
|
|
|
|
|
46
|
|
|
// Delete existing data. |
|
47
|
|
|
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wl_relation_instances" ); |
|
48
|
|
|
|
|
49
|
|
|
$this->log->debug( 'Processing contents...' ); |
|
50
|
|
|
|
|
51
|
|
|
$this->process( array( $this, 'process_single' ) ); |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function process_single( $post_id ) { |
|
56
|
|
|
|
|
57
|
|
|
$this->log->debug( "Processing post $post_id..." ); |
|
58
|
|
|
|
|
59
|
|
|
// Bail out if the post is not found. |
|
60
|
|
|
$post = get_post( $post_id ); |
|
61
|
|
|
|
|
62
|
|
|
if ( null === $post ) { |
|
63
|
|
|
$this->log->error( "Post $post_id not found." ); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Get the URIs from the post content. |
|
69
|
|
|
$uris = $this->content_filter_service->get_entity_uris( $post->post_content ); |
|
70
|
|
|
|
|
71
|
|
|
// Map the URIs to post IDs. |
|
72
|
|
|
$ids = $this->uri_to_post_id( $uris ); |
|
73
|
|
|
|
|
74
|
|
|
$this->log->info( 'Found ' . count( $uris ) . ' annotation(s) and ' . count( $ids ) . ' unique relation(s).' ); |
|
75
|
|
|
|
|
76
|
|
|
// Create the relations. |
|
77
|
|
|
$this->create_relations( $post_id, $ids ); |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function create_relations( $subject_id, $object_ids ) { |
|
82
|
|
|
|
|
83
|
|
|
$this->log->info( "Creating relations for post $subject_id..." ); |
|
84
|
|
|
|
|
85
|
|
|
$entity_service = $this->entity_service; |
|
86
|
|
|
|
|
87
|
|
|
array_walk( $object_ids, function ( $item ) use ( $subject_id, $entity_service ) { |
|
88
|
|
|
wl_core_add_relation_instance( |
|
89
|
|
|
$subject_id, |
|
90
|
|
|
$entity_service->get_classification_scope_for( $item ), |
|
91
|
|
|
$item |
|
92
|
|
|
); |
|
93
|
|
|
} ); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function uri_to_post_id( $uris ) { |
|
98
|
|
|
|
|
99
|
|
|
$entity_service = $this->entity_service; |
|
100
|
|
|
|
|
101
|
|
|
$ids = array_unique( array_map( function ( $item ) use ( $entity_service ) { |
|
102
|
|
|
$post = $entity_service->get_entity_post_by_uri( $item ); |
|
103
|
|
|
|
|
104
|
|
|
return null === $post ? null : $post->ID; |
|
105
|
|
|
}, (array) $uris ) ); |
|
106
|
|
|
|
|
107
|
|
|
return array_filter( $ids, function ( $item ) { |
|
108
|
|
|
return null !== $item; |
|
109
|
|
|
} ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
|
|
function find( $offset = 0, $limit = 10, $args = array() ) { |
|
|
|
|
|
|
116
|
|
|
global $wpdb; |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
return $wpdb->get_col( $wpdb->prepare( |
|
119
|
|
|
" |
|
120
|
|
|
SELECT id |
|
121
|
|
|
FROM $wpdb->posts |
|
122
|
|
|
WHERE post_type NOT IN ( 'attachment', 'revision' ) |
|
123
|
|
|
AND post_content REGEXP %s |
|
124
|
|
|
LIMIT %d OFFSET %d |
|
125
|
|
|
", |
|
126
|
|
|
'<[a-z]+ id="urn:[^"]+" class="[^"]+" itemid="[^"]+">', |
|
127
|
|
|
$limit, |
|
128
|
|
|
$offset |
|
129
|
|
|
) ); |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state