@@ -18,287 +18,287 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Linked_Data_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Log_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $log; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $entity_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $entity_type_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The {@link Wordlift_Schema_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.15.0 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 54 | - */ |
|
| 55 | - private $schema_service; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 59 | - * |
|
| 60 | - * @since 3.15.0 |
|
| 61 | - * @access private |
|
| 62 | - * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 63 | - */ |
|
| 64 | - private static $instance; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The {@link Wordlift_Sparql_Service} instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.15.0 |
|
| 70 | - * @access private |
|
| 71 | - * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 72 | - */ |
|
| 73 | - private $sparql_service; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Create a {@link Wordlift_Linked_Data_Service} instance. |
|
| 77 | - * |
|
| 78 | - * @since 3.15.0 |
|
| 79 | - * |
|
| 80 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 81 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 82 | - * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 83 | - * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 84 | - */ |
|
| 85 | - public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 86 | - |
|
| 87 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 88 | - |
|
| 89 | - $this->entity_service = $entity_service; |
|
| 90 | - $this->entity_type_service = $entity_type_service; |
|
| 91 | - $this->schema_service = $schema_service; |
|
| 92 | - $this->sparql_service = $sparql_service; |
|
| 93 | - |
|
| 94 | - self::$instance = $this; |
|
| 95 | - |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the singleton instance of {@link Wordlift_Linked_Data_Service}. |
|
| 100 | - * |
|
| 101 | - * @since 3.15.0 |
|
| 102 | - * |
|
| 103 | - * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>. |
|
| 104 | - */ |
|
| 105 | - public static function get_instance() { |
|
| 106 | - |
|
| 107 | - return self::$instance; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Push a {@link WP_Post} to the Linked Data store. |
|
| 112 | - * |
|
| 113 | - * If the {@link WP_Post} is an entity and it's not of the `Article` type, |
|
| 114 | - * then it is pushed to the remote Linked Data store. |
|
| 115 | - * |
|
| 116 | - * @since 3.15.0 |
|
| 117 | - * |
|
| 118 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 119 | - */ |
|
| 120 | - public function push( $post_id ) { |
|
| 121 | - |
|
| 122 | - $this->log->debug( "Pushing post $post_id..." ); |
|
| 123 | - |
|
| 124 | - // Bail out if it's not an entity: we do NOT publish non entities or |
|
| 125 | - // entities of type `Article`s. |
|
| 126 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | - $this->log->debug( "Post $post_id is not an entity." ); |
|
| 128 | - |
|
| 129 | - return; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Bail out if the entity type is `Article`. |
|
| 133 | - if ( $this->entity_type_service->has_entity_type( $post_id, 'http://schema.org/Article' ) ) { |
|
| 134 | - $this->log->debug( "Post $post_id is an `Article`." ); |
|
| 135 | - |
|
| 136 | - return; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Get the post and push it to the Linked Data store. |
|
| 140 | - $this->do_push( $post_id ); |
|
| 141 | - |
|
| 142 | - // Reindex the triple store if buffering is turned off. |
|
| 143 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 144 | - wordlift_reindex_triple_store(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Remove the specified {@link WP_Post} from the Linked Data. |
|
| 151 | - * |
|
| 152 | - * @since 3.15.0 |
|
| 153 | - * |
|
| 154 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 155 | - */ |
|
| 156 | - public function remove( $post_id ) { |
|
| 157 | - |
|
| 158 | - // Get the delete statements. |
|
| 159 | - $deletes = $this->get_delete_statements( $post_id ); |
|
| 160 | - $delete_query = implode( "\n", $deletes ); |
|
| 161 | - $this->sparql_service->execute( $delete_query ); |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Push an entity to the Linked Data store. |
|
| 167 | - * |
|
| 168 | - * @since 3.15.0 |
|
| 169 | - * |
|
| 170 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 171 | - */ |
|
| 172 | - private function do_push( $post_id ) { |
|
| 173 | - $this->log->debug( "Doing post $post_id push..." ); |
|
| 174 | - |
|
| 175 | - // Get the post. |
|
| 176 | - $post = get_post( $post_id ); |
|
| 177 | - |
|
| 178 | - // Bail out if the post isn't found. |
|
| 179 | - if ( null === $post ) { |
|
| 180 | - $this->log->debug( "Post $post_id not found." ); |
|
| 181 | - |
|
| 182 | - return; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Bail out if the post isn't published. |
|
| 186 | - if ( 'publish' !== $post->post_status ) { |
|
| 187 | - $this->log->debug( "Post $post_id not published." ); |
|
| 188 | - |
|
| 189 | - return; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Bail out if the URI isn't valid. |
|
| 193 | - if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 194 | - $this->log->debug( "Post $post_id URI invalid." ); |
|
| 195 | - |
|
| 196 | - return; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // First remove the post data. |
|
| 200 | - $this->remove( $post_id ); |
|
| 201 | - |
|
| 202 | - // Get the insert statements. |
|
| 203 | - $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 204 | - $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 205 | - $insert_query = "INSERT DATA { $insert_query_body };"; |
|
| 206 | - $this->sparql_service->execute( $insert_query ); |
|
| 207 | - |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Check if an entity's {@link WP_Post} has a valid URI. |
|
| 212 | - * |
|
| 213 | - * @since 3.15.0 |
|
| 214 | - * |
|
| 215 | - * @param int $post_id The entity's {@link WP_Post}'s id. |
|
| 216 | - * |
|
| 217 | - * @return bool True if the URI is valid otherwise false. |
|
| 218 | - */ |
|
| 219 | - private function has_valid_uri( $post_id ) { |
|
| 220 | - |
|
| 221 | - // Get the entity's URI. |
|
| 222 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 223 | - |
|
| 224 | - // If the URI isn't found, return false. |
|
| 225 | - if ( null === $uri ) { |
|
| 226 | - return false; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // If the URI ends with a trailing slash, return false. |
|
| 230 | - if ( '/' === substr( $uri, - 1 ) ) { |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // URI is valid. |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Get the delete statements. |
|
| 240 | - * |
|
| 241 | - * @since 3.15.0 |
|
| 242 | - * |
|
| 243 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 244 | - * |
|
| 245 | - * @return array An array of delete statements. |
|
| 246 | - */ |
|
| 247 | - private function get_delete_statements( $post_id ) { |
|
| 248 | - |
|
| 249 | - // Get the entity URI. |
|
| 250 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 251 | - |
|
| 252 | - // Prepare the delete statements with the entity as subject. |
|
| 253 | - $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 254 | - return Wordlift_Query_Builder |
|
| 255 | - ::new_instance() |
|
| 256 | - ->delete() |
|
| 257 | - ->statement( $uri, $item, '?o' ) |
|
| 258 | - ->build(); |
|
| 259 | - }, $this->schema_service->get_all_predicates() ); |
|
| 260 | - |
|
| 261 | - // Prepare the delete statements with the entity as object. |
|
| 262 | - $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 263 | - return Wordlift_Query_Builder |
|
| 264 | - ::new_instance() |
|
| 265 | - ->delete() |
|
| 266 | - ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 267 | - ->build(); |
|
| 268 | - }, $this->schema_service->get_all_predicates() ); |
|
| 269 | - |
|
| 270 | - // Merge the delete statements and return them. |
|
| 271 | - return array_merge( $as_subject, $as_object ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Get the SPARQL insert tuples ( ?s ?p ?o ) for the specified {@link WP_Post}. |
|
| 276 | - * |
|
| 277 | - * @since 3.15.0 |
|
| 278 | - * |
|
| 279 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 280 | - * |
|
| 281 | - * @return array An array of insert tuples. |
|
| 282 | - */ |
|
| 283 | - private function get_insert_tuples( $post_id ) { |
|
| 284 | - |
|
| 285 | - // Get the entity type. |
|
| 286 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 287 | - |
|
| 288 | - // Get the Linked Data properties. |
|
| 289 | - $properties = $type['linked_data']; |
|
| 290 | - |
|
| 291 | - // Accumulate the tuples. |
|
| 292 | - $tuples = array(); |
|
| 293 | - /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
|
| 294 | - foreach ( $properties as $property ) { |
|
| 295 | - foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 296 | - $tuples[] = $tuple; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - // Finally return the tuples. |
|
| 301 | - return $tuples; |
|
| 302 | - } |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Log_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $log; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $entity_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $entity_type_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The {@link Wordlift_Schema_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.15.0 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 54 | + */ |
|
| 55 | + private $schema_service; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 59 | + * |
|
| 60 | + * @since 3.15.0 |
|
| 61 | + * @access private |
|
| 62 | + * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 63 | + */ |
|
| 64 | + private static $instance; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The {@link Wordlift_Sparql_Service} instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.15.0 |
|
| 70 | + * @access private |
|
| 71 | + * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 72 | + */ |
|
| 73 | + private $sparql_service; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Create a {@link Wordlift_Linked_Data_Service} instance. |
|
| 77 | + * |
|
| 78 | + * @since 3.15.0 |
|
| 79 | + * |
|
| 80 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 81 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 82 | + * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 83 | + * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 84 | + */ |
|
| 85 | + public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 86 | + |
|
| 87 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 88 | + |
|
| 89 | + $this->entity_service = $entity_service; |
|
| 90 | + $this->entity_type_service = $entity_type_service; |
|
| 91 | + $this->schema_service = $schema_service; |
|
| 92 | + $this->sparql_service = $sparql_service; |
|
| 93 | + |
|
| 94 | + self::$instance = $this; |
|
| 95 | + |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the singleton instance of {@link Wordlift_Linked_Data_Service}. |
|
| 100 | + * |
|
| 101 | + * @since 3.15.0 |
|
| 102 | + * |
|
| 103 | + * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>. |
|
| 104 | + */ |
|
| 105 | + public static function get_instance() { |
|
| 106 | + |
|
| 107 | + return self::$instance; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Push a {@link WP_Post} to the Linked Data store. |
|
| 112 | + * |
|
| 113 | + * If the {@link WP_Post} is an entity and it's not of the `Article` type, |
|
| 114 | + * then it is pushed to the remote Linked Data store. |
|
| 115 | + * |
|
| 116 | + * @since 3.15.0 |
|
| 117 | + * |
|
| 118 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 119 | + */ |
|
| 120 | + public function push( $post_id ) { |
|
| 121 | + |
|
| 122 | + $this->log->debug( "Pushing post $post_id..." ); |
|
| 123 | + |
|
| 124 | + // Bail out if it's not an entity: we do NOT publish non entities or |
|
| 125 | + // entities of type `Article`s. |
|
| 126 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | + $this->log->debug( "Post $post_id is not an entity." ); |
|
| 128 | + |
|
| 129 | + return; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Bail out if the entity type is `Article`. |
|
| 133 | + if ( $this->entity_type_service->has_entity_type( $post_id, 'http://schema.org/Article' ) ) { |
|
| 134 | + $this->log->debug( "Post $post_id is an `Article`." ); |
|
| 135 | + |
|
| 136 | + return; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Get the post and push it to the Linked Data store. |
|
| 140 | + $this->do_push( $post_id ); |
|
| 141 | + |
|
| 142 | + // Reindex the triple store if buffering is turned off. |
|
| 143 | + if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 144 | + wordlift_reindex_triple_store(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Remove the specified {@link WP_Post} from the Linked Data. |
|
| 151 | + * |
|
| 152 | + * @since 3.15.0 |
|
| 153 | + * |
|
| 154 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 155 | + */ |
|
| 156 | + public function remove( $post_id ) { |
|
| 157 | + |
|
| 158 | + // Get the delete statements. |
|
| 159 | + $deletes = $this->get_delete_statements( $post_id ); |
|
| 160 | + $delete_query = implode( "\n", $deletes ); |
|
| 161 | + $this->sparql_service->execute( $delete_query ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Push an entity to the Linked Data store. |
|
| 167 | + * |
|
| 168 | + * @since 3.15.0 |
|
| 169 | + * |
|
| 170 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 171 | + */ |
|
| 172 | + private function do_push( $post_id ) { |
|
| 173 | + $this->log->debug( "Doing post $post_id push..." ); |
|
| 174 | + |
|
| 175 | + // Get the post. |
|
| 176 | + $post = get_post( $post_id ); |
|
| 177 | + |
|
| 178 | + // Bail out if the post isn't found. |
|
| 179 | + if ( null === $post ) { |
|
| 180 | + $this->log->debug( "Post $post_id not found." ); |
|
| 181 | + |
|
| 182 | + return; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Bail out if the post isn't published. |
|
| 186 | + if ( 'publish' !== $post->post_status ) { |
|
| 187 | + $this->log->debug( "Post $post_id not published." ); |
|
| 188 | + |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // Bail out if the URI isn't valid. |
|
| 193 | + if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 194 | + $this->log->debug( "Post $post_id URI invalid." ); |
|
| 195 | + |
|
| 196 | + return; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // First remove the post data. |
|
| 200 | + $this->remove( $post_id ); |
|
| 201 | + |
|
| 202 | + // Get the insert statements. |
|
| 203 | + $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 204 | + $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 205 | + $insert_query = "INSERT DATA { $insert_query_body };"; |
|
| 206 | + $this->sparql_service->execute( $insert_query ); |
|
| 207 | + |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Check if an entity's {@link WP_Post} has a valid URI. |
|
| 212 | + * |
|
| 213 | + * @since 3.15.0 |
|
| 214 | + * |
|
| 215 | + * @param int $post_id The entity's {@link WP_Post}'s id. |
|
| 216 | + * |
|
| 217 | + * @return bool True if the URI is valid otherwise false. |
|
| 218 | + */ |
|
| 219 | + private function has_valid_uri( $post_id ) { |
|
| 220 | + |
|
| 221 | + // Get the entity's URI. |
|
| 222 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 223 | + |
|
| 224 | + // If the URI isn't found, return false. |
|
| 225 | + if ( null === $uri ) { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // If the URI ends with a trailing slash, return false. |
|
| 230 | + if ( '/' === substr( $uri, - 1 ) ) { |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // URI is valid. |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Get the delete statements. |
|
| 240 | + * |
|
| 241 | + * @since 3.15.0 |
|
| 242 | + * |
|
| 243 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 244 | + * |
|
| 245 | + * @return array An array of delete statements. |
|
| 246 | + */ |
|
| 247 | + private function get_delete_statements( $post_id ) { |
|
| 248 | + |
|
| 249 | + // Get the entity URI. |
|
| 250 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 251 | + |
|
| 252 | + // Prepare the delete statements with the entity as subject. |
|
| 253 | + $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 254 | + return Wordlift_Query_Builder |
|
| 255 | + ::new_instance() |
|
| 256 | + ->delete() |
|
| 257 | + ->statement( $uri, $item, '?o' ) |
|
| 258 | + ->build(); |
|
| 259 | + }, $this->schema_service->get_all_predicates() ); |
|
| 260 | + |
|
| 261 | + // Prepare the delete statements with the entity as object. |
|
| 262 | + $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 263 | + return Wordlift_Query_Builder |
|
| 264 | + ::new_instance() |
|
| 265 | + ->delete() |
|
| 266 | + ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 267 | + ->build(); |
|
| 268 | + }, $this->schema_service->get_all_predicates() ); |
|
| 269 | + |
|
| 270 | + // Merge the delete statements and return them. |
|
| 271 | + return array_merge( $as_subject, $as_object ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Get the SPARQL insert tuples ( ?s ?p ?o ) for the specified {@link WP_Post}. |
|
| 276 | + * |
|
| 277 | + * @since 3.15.0 |
|
| 278 | + * |
|
| 279 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 280 | + * |
|
| 281 | + * @return array An array of insert tuples. |
|
| 282 | + */ |
|
| 283 | + private function get_insert_tuples( $post_id ) { |
|
| 284 | + |
|
| 285 | + // Get the entity type. |
|
| 286 | + $type = $this->entity_type_service->get( $post_id ); |
|
| 287 | + |
|
| 288 | + // Get the Linked Data properties. |
|
| 289 | + $properties = $type['linked_data']; |
|
| 290 | + |
|
| 291 | + // Accumulate the tuples. |
|
| 292 | + $tuples = array(); |
|
| 293 | + /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
|
| 294 | + foreach ( $properties as $property ) { |
|
| 295 | + foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 296 | + $tuples[] = $tuple; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + // Finally return the tuples. |
|
| 301 | + return $tuples; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | 304 | } |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
| 83 | 83 | * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
| 84 | 84 | */ |
| 85 | - public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 85 | + public function __construct($entity_service, $entity_type_service, $schema_service, $sparql_service) { |
|
| 86 | 86 | |
| 87 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 87 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Linked_Data_Service'); |
|
| 88 | 88 | |
| 89 | 89 | $this->entity_service = $entity_service; |
| 90 | 90 | $this->entity_type_service = $entity_type_service; |
@@ -117,30 +117,30 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @param int $post_id The {@link WP_Post}'s id. |
| 119 | 119 | */ |
| 120 | - public function push( $post_id ) { |
|
| 120 | + public function push($post_id) { |
|
| 121 | 121 | |
| 122 | - $this->log->debug( "Pushing post $post_id..." ); |
|
| 122 | + $this->log->debug("Pushing post $post_id..."); |
|
| 123 | 123 | |
| 124 | 124 | // Bail out if it's not an entity: we do NOT publish non entities or |
| 125 | 125 | // entities of type `Article`s. |
| 126 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | - $this->log->debug( "Post $post_id is not an entity." ); |
|
| 126 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 127 | + $this->log->debug("Post $post_id is not an entity."); |
|
| 128 | 128 | |
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // Bail out if the entity type is `Article`. |
| 133 | - if ( $this->entity_type_service->has_entity_type( $post_id, 'http://schema.org/Article' ) ) { |
|
| 134 | - $this->log->debug( "Post $post_id is an `Article`." ); |
|
| 133 | + if ($this->entity_type_service->has_entity_type($post_id, 'http://schema.org/Article')) { |
|
| 134 | + $this->log->debug("Post $post_id is an `Article`."); |
|
| 135 | 135 | |
| 136 | 136 | return; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | // Get the post and push it to the Linked Data store. |
| 140 | - $this->do_push( $post_id ); |
|
| 140 | + $this->do_push($post_id); |
|
| 141 | 141 | |
| 142 | 142 | // Reindex the triple store if buffering is turned off. |
| 143 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 143 | + if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 144 | 144 | wordlift_reindex_triple_store(); |
| 145 | 145 | } |
| 146 | 146 | |
@@ -153,12 +153,12 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @param int $post_id The {@link WP_Post}'s id. |
| 155 | 155 | */ |
| 156 | - public function remove( $post_id ) { |
|
| 156 | + public function remove($post_id) { |
|
| 157 | 157 | |
| 158 | 158 | // Get the delete statements. |
| 159 | - $deletes = $this->get_delete_statements( $post_id ); |
|
| 160 | - $delete_query = implode( "\n", $deletes ); |
|
| 161 | - $this->sparql_service->execute( $delete_query ); |
|
| 159 | + $deletes = $this->get_delete_statements($post_id); |
|
| 160 | + $delete_query = implode("\n", $deletes); |
|
| 161 | + $this->sparql_service->execute($delete_query); |
|
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
@@ -169,41 +169,41 @@ discard block |
||
| 169 | 169 | * |
| 170 | 170 | * @param int $post_id The {@link WP_Post}'s id. |
| 171 | 171 | */ |
| 172 | - private function do_push( $post_id ) { |
|
| 173 | - $this->log->debug( "Doing post $post_id push..." ); |
|
| 172 | + private function do_push($post_id) { |
|
| 173 | + $this->log->debug("Doing post $post_id push..."); |
|
| 174 | 174 | |
| 175 | 175 | // Get the post. |
| 176 | - $post = get_post( $post_id ); |
|
| 176 | + $post = get_post($post_id); |
|
| 177 | 177 | |
| 178 | 178 | // Bail out if the post isn't found. |
| 179 | - if ( null === $post ) { |
|
| 180 | - $this->log->debug( "Post $post_id not found." ); |
|
| 179 | + if (null === $post) { |
|
| 180 | + $this->log->debug("Post $post_id not found."); |
|
| 181 | 181 | |
| 182 | 182 | return; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Bail out if the post isn't published. |
| 186 | - if ( 'publish' !== $post->post_status ) { |
|
| 187 | - $this->log->debug( "Post $post_id not published." ); |
|
| 186 | + if ('publish' !== $post->post_status) { |
|
| 187 | + $this->log->debug("Post $post_id not published."); |
|
| 188 | 188 | |
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | // Bail out if the URI isn't valid. |
| 193 | - if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 194 | - $this->log->debug( "Post $post_id URI invalid." ); |
|
| 193 | + if ( ! $this->has_valid_uri($post_id)) { |
|
| 194 | + $this->log->debug("Post $post_id URI invalid."); |
|
| 195 | 195 | |
| 196 | 196 | return; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // First remove the post data. |
| 200 | - $this->remove( $post_id ); |
|
| 200 | + $this->remove($post_id); |
|
| 201 | 201 | |
| 202 | 202 | // Get the insert statements. |
| 203 | - $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 204 | - $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 203 | + $insert_tuples = $this->get_insert_tuples($post_id); |
|
| 204 | + $insert_query_body = implode("\n", $insert_tuples); |
|
| 205 | 205 | $insert_query = "INSERT DATA { $insert_query_body };"; |
| 206 | - $this->sparql_service->execute( $insert_query ); |
|
| 206 | + $this->sparql_service->execute($insert_query); |
|
| 207 | 207 | |
| 208 | 208 | } |
| 209 | 209 | |
@@ -216,18 +216,18 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @return bool True if the URI is valid otherwise false. |
| 218 | 218 | */ |
| 219 | - private function has_valid_uri( $post_id ) { |
|
| 219 | + private function has_valid_uri($post_id) { |
|
| 220 | 220 | |
| 221 | 221 | // Get the entity's URI. |
| 222 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 222 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 223 | 223 | |
| 224 | 224 | // If the URI isn't found, return false. |
| 225 | - if ( null === $uri ) { |
|
| 225 | + if (null === $uri) { |
|
| 226 | 226 | return false; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | // If the URI ends with a trailing slash, return false. |
| 230 | - if ( '/' === substr( $uri, - 1 ) ) { |
|
| 230 | + if ('/' === substr($uri, - 1)) { |
|
| 231 | 231 | return false; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -244,31 +244,31 @@ discard block |
||
| 244 | 244 | * |
| 245 | 245 | * @return array An array of delete statements. |
| 246 | 246 | */ |
| 247 | - private function get_delete_statements( $post_id ) { |
|
| 247 | + private function get_delete_statements($post_id) { |
|
| 248 | 248 | |
| 249 | 249 | // Get the entity URI. |
| 250 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 250 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 251 | 251 | |
| 252 | 252 | // Prepare the delete statements with the entity as subject. |
| 253 | - $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 253 | + $as_subject = array_map(function($item) use ($uri) { |
|
| 254 | 254 | return Wordlift_Query_Builder |
| 255 | 255 | ::new_instance() |
| 256 | 256 | ->delete() |
| 257 | - ->statement( $uri, $item, '?o' ) |
|
| 257 | + ->statement($uri, $item, '?o') |
|
| 258 | 258 | ->build(); |
| 259 | - }, $this->schema_service->get_all_predicates() ); |
|
| 259 | + }, $this->schema_service->get_all_predicates()); |
|
| 260 | 260 | |
| 261 | 261 | // Prepare the delete statements with the entity as object. |
| 262 | - $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 262 | + $as_object = array_map(function($item) use ($uri) { |
|
| 263 | 263 | return Wordlift_Query_Builder |
| 264 | 264 | ::new_instance() |
| 265 | 265 | ->delete() |
| 266 | - ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 266 | + ->statement('?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI) |
|
| 267 | 267 | ->build(); |
| 268 | - }, $this->schema_service->get_all_predicates() ); |
|
| 268 | + }, $this->schema_service->get_all_predicates()); |
|
| 269 | 269 | |
| 270 | 270 | // Merge the delete statements and return them. |
| 271 | - return array_merge( $as_subject, $as_object ); |
|
| 271 | + return array_merge($as_subject, $as_object); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -280,10 +280,10 @@ discard block |
||
| 280 | 280 | * |
| 281 | 281 | * @return array An array of insert tuples. |
| 282 | 282 | */ |
| 283 | - private function get_insert_tuples( $post_id ) { |
|
| 283 | + private function get_insert_tuples($post_id) { |
|
| 284 | 284 | |
| 285 | 285 | // Get the entity type. |
| 286 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 286 | + $type = $this->entity_type_service->get($post_id); |
|
| 287 | 287 | |
| 288 | 288 | // Get the Linked Data properties. |
| 289 | 289 | $properties = $type['linked_data']; |
@@ -291,8 +291,8 @@ discard block |
||
| 291 | 291 | // Accumulate the tuples. |
| 292 | 292 | $tuples = array(); |
| 293 | 293 | /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
| 294 | - foreach ( $properties as $property ) { |
|
| 295 | - foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 294 | + foreach ($properties as $property) { |
|
| 295 | + foreach ($property->get($post_id) as $tuple) { |
|
| 296 | 296 | $tuples[] = $tuple; |
| 297 | 297 | } |
| 298 | 298 | } |