Completed
Push — develop ( 7afe14...c614b1 )
by David
02:32
created
src/includes/class-wordlift-sparql-service.php 2 patches
Indentation   +307 added lines, -307 removed lines patch added patch discarded remove patch
@@ -18,312 +18,312 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Sparql_Service {
20 20
 
21
-	/**
22
-	 * A {@link Wordlift_Log_Service} instance.
23
-	 *
24
-	 * @since  3.6.0
25
-	 * @access private
26
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
-	 */
28
-	private static $log;
29
-
30
-	/**
31
-	 * The {@link Wordlift_Sparql_Service} singleton instance.
32
-	 *
33
-	 * @since  3.6.0
34
-	 * @access private
35
-	 * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance.
36
-	 */
37
-	private static $instance;
38
-
39
-	/**
40
-	 * Create a {@link Wordlift_Sparql_Service} instance.
41
-	 *
42
-	 * @since 3.6.0
43
-	 */
44
-	public function __construct() {
45
-
46
-		self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' );
47
-
48
-		self::$instance = $this;
49
-
50
-	}
51
-
52
-	/**
53
-	 * Get the singleton instance of the {@link Wordlift_Sparql_Service}.
54
-	 *
55
-	 * @since 3.6.0
56
-	 * @return \Wordlift_Sparql_Service
57
-	 */
58
-	public static function get_instance() {
59
-
60
-		return self::$instance;
61
-	}
62
-
63
-	/**
64
-	 * Queue a SPARQL statement for execution.
65
-	 *
66
-	 * @since 3.6.0
67
-	 *
68
-	 * @param string $stmt The SPARQL statement.
69
-	 * @param bool   $queue Whether to queue the statement for asynchronous
70
-	 *                      execution.
71
-	 */
72
-	public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
73
-
74
-		rl_execute_sparql_update_query( $stmt, $queue );
75
-
76
-	}
77
-
78
-	/**
79
-	 * Run the SPARQL queries buffered for the specified request id.
80
-	 *
81
-	 * @since 3.13.2
82
-	 *
83
-	 * @param string $request_id A unique request id.
84
-	 */
85
-	public function run_sparql_query( $request_id ) {
86
-
87
-		self::$log->debug( "Running SPARQL queries..." );
88
-
89
-		// Look for a free temporary filename.
90
-		for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
91
-			$filename = WL_TEMP_DIR . $request_id . "-$index.sparql";
92
-
93
-			// Bail out if there are no files left.
94
-			if ( ! file_exists( $filename ) ) {
95
-				break;
96
-			}
97
-
98
-			self::$log->debug( "Running SPARQL from $filename..." );
99
-
100
-			// Get the query saved in the file.
101
-			$query = file_get_contents( $filename );
102
-
103
-			// Execute the SPARQL query.
104
-			rl_execute_sparql_update_query( $query, false );
105
-
106
-			// Delete the temporary file.
107
-			unlink( $filename );
108
-		}
109
-
110
-		// Reindex the triple store.
111
-		wordlift_reindex_triple_store();
112
-
113
-	}
114
-
115
-	/**
116
-	 * Queue a SPARQL statement for asynchronous execution.
117
-	 *
118
-	 * @since 3.13.2
119
-	 *
120
-	 * @param string $stmt The SPARQL statement.
121
-	 *
122
-	 * @throws Exception
123
-	 */
124
-	public function queue( $stmt ) {
125
-
126
-		// Get a temporary filename.
127
-		$filename = $this->get_temporary_file_for_sparql();
128
-
129
-		self::$log->debug( "Buffering SPARQL to file $filename..." );
130
-
131
-		// Write the contents to the temporary filename.
132
-		file_put_contents( $filename, $stmt . "\n", FILE_APPEND );
133
-
134
-	}
135
-
136
-	/**
137
-	 * Get a temporary filename where to store SPARQL queries.
138
-	 *
139
-	 * @since 3.13.2
140
-	 *
141
-	 * @return string The filename.
142
-	 * @throws Exception An exception is thrown if there are already 1.000
143
-	 *                   temporary files for this request.
144
-	 */
145
-	private function get_temporary_file_for_sparql() {
146
-
147
-		// Look for a free temporary filename.
148
-		for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
149
-			$filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql";
150
-
151
-			if ( ! file_exists( $filename ) ) {
152
-
153
-				// Only if this it the first buffered SPARQL, then launch the
154
-				// action which will be handled by the Async Task. The Async
155
-				// Task will take care of all the buffered files _on shutdown_.
156
-				if ( 1 === $index ) {
157
-					do_action( 'wl_run_sparql_query', WL_REQUEST_ID );
158
-				}
159
-
160
-				// Return the temporary filename.
161
-				return $filename;
162
-			}
163
-		}
164
-
165
-		throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' );
166
-	}
167
-
168
-	/**
169
-	 * Execute the SELECT query.
170
-	 *
171
-	 * @since 3.12.2
172
-	 *
173
-	 * @param string $query The SELECT query to execute.
174
-	 *
175
-	 * @return WP_Error|array The response or WP_Error on failure.
176
-	 */
177
-	public function select( $query ) {
178
-
179
-		// Prepare the SPARQL statement by prepending the default namespaces.
180
-		$sparql = rl_sparql_prefixes() . "\n" . $query;
181
-
182
-		// Get the SPARQL SELECT URL.
183
-		$url = wl_configuration_get_query_select_url() . urlencode( $sparql );
184
-
185
-		$http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS );
186
-
187
-		/**
188
-		 * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args.
189
-		 *
190
-		 * @since 3.17.0
191
-		 *
192
-		 * @param   array $http_options Current http options.
193
-		 */
194
-		$args = apply_filters( 'wl_sparql_select_http_args', $http_options );
195
-
196
-		return wp_remote_get( $url, $args );
197
-	}
198
-
199
-	/**
200
-	 * Formats the provided value according to the specified type in order to
201
-	 * insert the value using SPARQL. The value is also escaped.
202
-	 *
203
-	 * @since 3.6.0
204
-	 *
205
-	 * @param string      $value The value.
206
-	 * @param string      $type The value type.
207
-	 * @param string|null $language The language tag or null if not set.
208
-	 *
209
-	 * @return string The formatted value for SPARQL statements.
210
-	 */
211
-	public static function format( $value, $type = null, $language = null ) {
212
-
213
-		// see https://www.w3.org/TR/sparql11-query/.
214
-
215
-		switch ( $type ) {
216
-
217
-			case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN:
218
-				// SPARQL supports 'true' and 'false', so we evaluate the $value
219
-				// and return true/false accordingly.
220
-				return $value ? 'true' : 'false';
221
-
222
-			case Wordlift_Schema_Service::DATA_TYPE_DATE:
223
-				$date       = date_create_from_format( 'Y/m/d', $value );
224
-				$date_value = date_format( $date, 'Y-m-d' );
225
-
226
-				return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) );
227
-
228
-			case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME:
229
-				$date       = date_create_from_format( 'Y/m/d H:i', $value );
230
-				$date_value = date_format( $date, 'Y-m-d\TH:i:00' );
231
-
232
-				return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) );
233
-
234
-			case Wordlift_Schema_Service::DATA_TYPE_DURATION:
235
-				$time       = date_create_from_format( 'H:i', $value );
236
-				$time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) );
237
-
238
-				return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) );
239
-
240
-			case Wordlift_Schema_Service::DATA_TYPE_DOUBLE:
241
-				return sprintf( '"%s"^^xsd:double', self::escape( $value ) );
242
-
243
-			case Wordlift_Schema_Service::DATA_TYPE_INTEGER:
244
-				return sprintf( '"%s"^^xsd:integer', self::escape( $value ) );
245
-
246
-			case Wordlift_Schema_Service::DATA_TYPE_STRING:
247
-				return sprintf( '"%s"^^xsd:string', self::escape( $value ) );
248
-
249
-			case Wordlift_Schema_Service::DATA_TYPE_URI:
250
-				/**
251
-				 * Allow 3rd parties to change the uri.
252
-				 *
253
-				 * @since 3.20.0
254
-				 *
255
-				 * @see https://github.com/insideout10/wordlift-plugin/issues/850
256
-				 *
257
-				 * @param string $uri The uri.
258
-				 */
259
-				return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) );
260
-
261
-			case null:
262
-				$language_tag = ( null !== $language ? "@$language" : '' );
263
-
264
-				return sprintf( '"%s"%s', self::escape( $value ), $language_tag );
265
-
266
-			default:
267
-
268
-				self::$log->warn( "Unknown data type [ type :: $type ]" );
269
-
270
-				// Try to insert the value anyway.
271
-				return sprintf( '"%s"', self::escape( $value ) );
272
-		}
273
-
274
-	}
275
-
276
-	/**
277
-	 * Escapes an URI for a SPARQL statement.
278
-	 *
279
-	 * @since 3.6.0
280
-	 *
281
-	 * @param string $uri The URI to escape.
282
-	 *
283
-	 * @return string The escaped URI.
284
-	 */
285
-	public static function escape_uri( $uri ) {
286
-
287
-		// Should we validate the IRI?
288
-		// http://www.w3.org/TR/sparql11-query/#QSynIRI
289
-
290
-		$uri = str_replace( '<', '\<', $uri );
291
-		$uri = str_replace( '>', '\>', $uri );
292
-
293
-		return $uri;
294
-	}
295
-
296
-	/**
297
-	 * Escapes a string for a SPARQL statement.
298
-	 *
299
-	 * @since 3.6.0
300
-	 *
301
-	 * @param string $string The string to escape.
302
-	 *
303
-	 * @return string The escaped string.
304
-	 */
305
-	public static function escape( $string ) {
306
-
307
-		// see http://www.w3.org/TR/rdf-sparql-query/
308
-		//    '\t'	U+0009 (tab)
309
-		//    '\n'	U+000A (line feed)
310
-		//    '\r'	U+000D (carriage return)
311
-		//    '\b'	U+0008 (backspace)
312
-		//    '\f'	U+000C (form feed)
313
-		//    '\"'	U+0022 (quotation mark, double quote mark)
314
-		//    "\'"	U+0027 (apostrophe-quote, single quote mark)
315
-		//    '\\'	U+005C (backslash)
316
-
317
-		$string = str_replace( '\\', '\\\\', $string );
318
-		$string = str_replace( '\'', '\\\'', $string );
319
-		$string = str_replace( '"', '\\"', $string );
320
-		$string = str_replace( "\f", '\\f', $string );
321
-		$string = str_replace( "\b", '\\b', $string );
322
-		$string = str_replace( "\r", '\\r', $string );
323
-		$string = str_replace( "\n", '\\n', $string );
324
-		$string = str_replace( "\t", '\\t', $string );
325
-
326
-		return $string;
327
-	}
21
+    /**
22
+     * A {@link Wordlift_Log_Service} instance.
23
+     *
24
+     * @since  3.6.0
25
+     * @access private
26
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
+     */
28
+    private static $log;
29
+
30
+    /**
31
+     * The {@link Wordlift_Sparql_Service} singleton instance.
32
+     *
33
+     * @since  3.6.0
34
+     * @access private
35
+     * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance.
36
+     */
37
+    private static $instance;
38
+
39
+    /**
40
+     * Create a {@link Wordlift_Sparql_Service} instance.
41
+     *
42
+     * @since 3.6.0
43
+     */
44
+    public function __construct() {
45
+
46
+        self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' );
47
+
48
+        self::$instance = $this;
49
+
50
+    }
51
+
52
+    /**
53
+     * Get the singleton instance of the {@link Wordlift_Sparql_Service}.
54
+     *
55
+     * @since 3.6.0
56
+     * @return \Wordlift_Sparql_Service
57
+     */
58
+    public static function get_instance() {
59
+
60
+        return self::$instance;
61
+    }
62
+
63
+    /**
64
+     * Queue a SPARQL statement for execution.
65
+     *
66
+     * @since 3.6.0
67
+     *
68
+     * @param string $stmt The SPARQL statement.
69
+     * @param bool   $queue Whether to queue the statement for asynchronous
70
+     *                      execution.
71
+     */
72
+    public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
73
+
74
+        rl_execute_sparql_update_query( $stmt, $queue );
75
+
76
+    }
77
+
78
+    /**
79
+     * Run the SPARQL queries buffered for the specified request id.
80
+     *
81
+     * @since 3.13.2
82
+     *
83
+     * @param string $request_id A unique request id.
84
+     */
85
+    public function run_sparql_query( $request_id ) {
86
+
87
+        self::$log->debug( "Running SPARQL queries..." );
88
+
89
+        // Look for a free temporary filename.
90
+        for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
91
+            $filename = WL_TEMP_DIR . $request_id . "-$index.sparql";
92
+
93
+            // Bail out if there are no files left.
94
+            if ( ! file_exists( $filename ) ) {
95
+                break;
96
+            }
97
+
98
+            self::$log->debug( "Running SPARQL from $filename..." );
99
+
100
+            // Get the query saved in the file.
101
+            $query = file_get_contents( $filename );
102
+
103
+            // Execute the SPARQL query.
104
+            rl_execute_sparql_update_query( $query, false );
105
+
106
+            // Delete the temporary file.
107
+            unlink( $filename );
108
+        }
109
+
110
+        // Reindex the triple store.
111
+        wordlift_reindex_triple_store();
112
+
113
+    }
114
+
115
+    /**
116
+     * Queue a SPARQL statement for asynchronous execution.
117
+     *
118
+     * @since 3.13.2
119
+     *
120
+     * @param string $stmt The SPARQL statement.
121
+     *
122
+     * @throws Exception
123
+     */
124
+    public function queue( $stmt ) {
125
+
126
+        // Get a temporary filename.
127
+        $filename = $this->get_temporary_file_for_sparql();
128
+
129
+        self::$log->debug( "Buffering SPARQL to file $filename..." );
130
+
131
+        // Write the contents to the temporary filename.
132
+        file_put_contents( $filename, $stmt . "\n", FILE_APPEND );
133
+
134
+    }
135
+
136
+    /**
137
+     * Get a temporary filename where to store SPARQL queries.
138
+     *
139
+     * @since 3.13.2
140
+     *
141
+     * @return string The filename.
142
+     * @throws Exception An exception is thrown if there are already 1.000
143
+     *                   temporary files for this request.
144
+     */
145
+    private function get_temporary_file_for_sparql() {
146
+
147
+        // Look for a free temporary filename.
148
+        for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
149
+            $filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql";
150
+
151
+            if ( ! file_exists( $filename ) ) {
152
+
153
+                // Only if this it the first buffered SPARQL, then launch the
154
+                // action which will be handled by the Async Task. The Async
155
+                // Task will take care of all the buffered files _on shutdown_.
156
+                if ( 1 === $index ) {
157
+                    do_action( 'wl_run_sparql_query', WL_REQUEST_ID );
158
+                }
159
+
160
+                // Return the temporary filename.
161
+                return $filename;
162
+            }
163
+        }
164
+
165
+        throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' );
166
+    }
167
+
168
+    /**
169
+     * Execute the SELECT query.
170
+     *
171
+     * @since 3.12.2
172
+     *
173
+     * @param string $query The SELECT query to execute.
174
+     *
175
+     * @return WP_Error|array The response or WP_Error on failure.
176
+     */
177
+    public function select( $query ) {
178
+
179
+        // Prepare the SPARQL statement by prepending the default namespaces.
180
+        $sparql = rl_sparql_prefixes() . "\n" . $query;
181
+
182
+        // Get the SPARQL SELECT URL.
183
+        $url = wl_configuration_get_query_select_url() . urlencode( $sparql );
184
+
185
+        $http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS );
186
+
187
+        /**
188
+         * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args.
189
+         *
190
+         * @since 3.17.0
191
+         *
192
+         * @param   array $http_options Current http options.
193
+         */
194
+        $args = apply_filters( 'wl_sparql_select_http_args', $http_options );
195
+
196
+        return wp_remote_get( $url, $args );
197
+    }
198
+
199
+    /**
200
+     * Formats the provided value according to the specified type in order to
201
+     * insert the value using SPARQL. The value is also escaped.
202
+     *
203
+     * @since 3.6.0
204
+     *
205
+     * @param string      $value The value.
206
+     * @param string      $type The value type.
207
+     * @param string|null $language The language tag or null if not set.
208
+     *
209
+     * @return string The formatted value for SPARQL statements.
210
+     */
211
+    public static function format( $value, $type = null, $language = null ) {
212
+
213
+        // see https://www.w3.org/TR/sparql11-query/.
214
+
215
+        switch ( $type ) {
216
+
217
+            case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN:
218
+                // SPARQL supports 'true' and 'false', so we evaluate the $value
219
+                // and return true/false accordingly.
220
+                return $value ? 'true' : 'false';
221
+
222
+            case Wordlift_Schema_Service::DATA_TYPE_DATE:
223
+                $date       = date_create_from_format( 'Y/m/d', $value );
224
+                $date_value = date_format( $date, 'Y-m-d' );
225
+
226
+                return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) );
227
+
228
+            case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME:
229
+                $date       = date_create_from_format( 'Y/m/d H:i', $value );
230
+                $date_value = date_format( $date, 'Y-m-d\TH:i:00' );
231
+
232
+                return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) );
233
+
234
+            case Wordlift_Schema_Service::DATA_TYPE_DURATION:
235
+                $time       = date_create_from_format( 'H:i', $value );
236
+                $time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) );
237
+
238
+                return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) );
239
+
240
+            case Wordlift_Schema_Service::DATA_TYPE_DOUBLE:
241
+                return sprintf( '"%s"^^xsd:double', self::escape( $value ) );
242
+
243
+            case Wordlift_Schema_Service::DATA_TYPE_INTEGER:
244
+                return sprintf( '"%s"^^xsd:integer', self::escape( $value ) );
245
+
246
+            case Wordlift_Schema_Service::DATA_TYPE_STRING:
247
+                return sprintf( '"%s"^^xsd:string', self::escape( $value ) );
248
+
249
+            case Wordlift_Schema_Service::DATA_TYPE_URI:
250
+                /**
251
+                 * Allow 3rd parties to change the uri.
252
+                 *
253
+                 * @since 3.20.0
254
+                 *
255
+                 * @see https://github.com/insideout10/wordlift-plugin/issues/850
256
+                 *
257
+                 * @param string $uri The uri.
258
+                 */
259
+                return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) );
260
+
261
+            case null:
262
+                $language_tag = ( null !== $language ? "@$language" : '' );
263
+
264
+                return sprintf( '"%s"%s', self::escape( $value ), $language_tag );
265
+
266
+            default:
267
+
268
+                self::$log->warn( "Unknown data type [ type :: $type ]" );
269
+
270
+                // Try to insert the value anyway.
271
+                return sprintf( '"%s"', self::escape( $value ) );
272
+        }
273
+
274
+    }
275
+
276
+    /**
277
+     * Escapes an URI for a SPARQL statement.
278
+     *
279
+     * @since 3.6.0
280
+     *
281
+     * @param string $uri The URI to escape.
282
+     *
283
+     * @return string The escaped URI.
284
+     */
285
+    public static function escape_uri( $uri ) {
286
+
287
+        // Should we validate the IRI?
288
+        // http://www.w3.org/TR/sparql11-query/#QSynIRI
289
+
290
+        $uri = str_replace( '<', '\<', $uri );
291
+        $uri = str_replace( '>', '\>', $uri );
292
+
293
+        return $uri;
294
+    }
295
+
296
+    /**
297
+     * Escapes a string for a SPARQL statement.
298
+     *
299
+     * @since 3.6.0
300
+     *
301
+     * @param string $string The string to escape.
302
+     *
303
+     * @return string The escaped string.
304
+     */
305
+    public static function escape( $string ) {
306
+
307
+        // see http://www.w3.org/TR/rdf-sparql-query/
308
+        //    '\t'	U+0009 (tab)
309
+        //    '\n'	U+000A (line feed)
310
+        //    '\r'	U+000D (carriage return)
311
+        //    '\b'	U+0008 (backspace)
312
+        //    '\f'	U+000C (form feed)
313
+        //    '\"'	U+0022 (quotation mark, double quote mark)
314
+        //    "\'"	U+0027 (apostrophe-quote, single quote mark)
315
+        //    '\\'	U+005C (backslash)
316
+
317
+        $string = str_replace( '\\', '\\\\', $string );
318
+        $string = str_replace( '\'', '\\\'', $string );
319
+        $string = str_replace( '"', '\\"', $string );
320
+        $string = str_replace( "\f", '\\f', $string );
321
+        $string = str_replace( "\b", '\\b', $string );
322
+        $string = str_replace( "\r", '\\r', $string );
323
+        $string = str_replace( "\n", '\\n', $string );
324
+        $string = str_replace( "\t", '\\t', $string );
325
+
326
+        return $string;
327
+    }
328 328
 
329 329
 }
Please login to merge, or discard this patch.
Spacing   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 	 */
44 44
 	public function __construct() {
45 45
 
46
-		self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' );
46
+		self::$log = Wordlift_Log_Service::get_logger('Wordlift_Sparql_Service');
47 47
 
48 48
 		self::$instance = $this;
49 49
 
@@ -69,9 +69,9 @@  discard block
 block discarded – undo
69 69
 	 * @param bool   $queue Whether to queue the statement for asynchronous
70 70
 	 *                      execution.
71 71
 	 */
72
-	public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) {
72
+	public function execute($stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) {
73 73
 
74
-		rl_execute_sparql_update_query( $stmt, $queue );
74
+		rl_execute_sparql_update_query($stmt, $queue);
75 75
 
76 76
 	}
77 77
 
@@ -82,29 +82,29 @@  discard block
 block discarded – undo
82 82
 	 *
83 83
 	 * @param string $request_id A unique request id.
84 84
 	 */
85
-	public function run_sparql_query( $request_id ) {
85
+	public function run_sparql_query($request_id) {
86 86
 
87
-		self::$log->debug( "Running SPARQL queries..." );
87
+		self::$log->debug("Running SPARQL queries...");
88 88
 
89 89
 		// Look for a free temporary filename.
90
-		for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
91
-			$filename = WL_TEMP_DIR . $request_id . "-$index.sparql";
90
+		for ($index = 1; $index < PHP_INT_MAX; $index++) {
91
+			$filename = WL_TEMP_DIR.$request_id."-$index.sparql";
92 92
 
93 93
 			// Bail out if there are no files left.
94
-			if ( ! file_exists( $filename ) ) {
94
+			if ( ! file_exists($filename)) {
95 95
 				break;
96 96
 			}
97 97
 
98
-			self::$log->debug( "Running SPARQL from $filename..." );
98
+			self::$log->debug("Running SPARQL from $filename...");
99 99
 
100 100
 			// Get the query saved in the file.
101
-			$query = file_get_contents( $filename );
101
+			$query = file_get_contents($filename);
102 102
 
103 103
 			// Execute the SPARQL query.
104
-			rl_execute_sparql_update_query( $query, false );
104
+			rl_execute_sparql_update_query($query, false);
105 105
 
106 106
 			// Delete the temporary file.
107
-			unlink( $filename );
107
+			unlink($filename);
108 108
 		}
109 109
 
110 110
 		// Reindex the triple store.
@@ -121,15 +121,15 @@  discard block
 block discarded – undo
121 121
 	 *
122 122
 	 * @throws Exception
123 123
 	 */
124
-	public function queue( $stmt ) {
124
+	public function queue($stmt) {
125 125
 
126 126
 		// Get a temporary filename.
127 127
 		$filename = $this->get_temporary_file_for_sparql();
128 128
 
129
-		self::$log->debug( "Buffering SPARQL to file $filename..." );
129
+		self::$log->debug("Buffering SPARQL to file $filename...");
130 130
 
131 131
 		// Write the contents to the temporary filename.
132
-		file_put_contents( $filename, $stmt . "\n", FILE_APPEND );
132
+		file_put_contents($filename, $stmt."\n", FILE_APPEND);
133 133
 
134 134
 	}
135 135
 
@@ -145,16 +145,16 @@  discard block
 block discarded – undo
145 145
 	private function get_temporary_file_for_sparql() {
146 146
 
147 147
 		// Look for a free temporary filename.
148
-		for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) {
149
-			$filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql";
148
+		for ($index = 1; $index < PHP_INT_MAX; $index++) {
149
+			$filename = WL_TEMP_DIR.WL_REQUEST_ID."-$index.sparql";
150 150
 
151
-			if ( ! file_exists( $filename ) ) {
151
+			if ( ! file_exists($filename)) {
152 152
 
153 153
 				// Only if this it the first buffered SPARQL, then launch the
154 154
 				// action which will be handled by the Async Task. The Async
155 155
 				// Task will take care of all the buffered files _on shutdown_.
156
-				if ( 1 === $index ) {
157
-					do_action( 'wl_run_sparql_query', WL_REQUEST_ID );
156
+				if (1 === $index) {
157
+					do_action('wl_run_sparql_query', WL_REQUEST_ID);
158 158
 				}
159 159
 
160 160
 				// Return the temporary filename.
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 			}
163 163
 		}
164 164
 
165
-		throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' );
165
+		throw new Exception('Cannot create a temporary file [ '.WL_TEMP_DIR.WL_REQUEST_ID.' ].');
166 166
 	}
167 167
 
168 168
 	/**
@@ -174,15 +174,15 @@  discard block
 block discarded – undo
174 174
 	 *
175 175
 	 * @return WP_Error|array The response or WP_Error on failure.
176 176
 	 */
177
-	public function select( $query ) {
177
+	public function select($query) {
178 178
 
179 179
 		// Prepare the SPARQL statement by prepending the default namespaces.
180
-		$sparql = rl_sparql_prefixes() . "\n" . $query;
180
+		$sparql = rl_sparql_prefixes()."\n".$query;
181 181
 
182 182
 		// Get the SPARQL SELECT URL.
183
-		$url = wl_configuration_get_query_select_url() . urlencode( $sparql );
183
+		$url = wl_configuration_get_query_select_url().urlencode($sparql);
184 184
 
185
-		$http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS );
185
+		$http_options = unserialize(WL_REDLINK_API_HTTP_OPTIONS);
186 186
 
187 187
 		/**
188 188
 		 * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args.
@@ -191,9 +191,9 @@  discard block
 block discarded – undo
191 191
 		 *
192 192
 		 * @param   array $http_options Current http options.
193 193
 		 */
194
-		$args = apply_filters( 'wl_sparql_select_http_args', $http_options );
194
+		$args = apply_filters('wl_sparql_select_http_args', $http_options);
195 195
 
196
-		return wp_remote_get( $url, $args );
196
+		return wp_remote_get($url, $args);
197 197
 	}
198 198
 
199 199
 	/**
@@ -208,11 +208,11 @@  discard block
 block discarded – undo
208 208
 	 *
209 209
 	 * @return string The formatted value for SPARQL statements.
210 210
 	 */
211
-	public static function format( $value, $type = null, $language = null ) {
211
+	public static function format($value, $type = null, $language = null) {
212 212
 
213 213
 		// see https://www.w3.org/TR/sparql11-query/.
214 214
 
215
-		switch ( $type ) {
215
+		switch ($type) {
216 216
 
217 217
 			case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN:
218 218
 				// SPARQL supports 'true' and 'false', so we evaluate the $value
@@ -220,31 +220,31 @@  discard block
 block discarded – undo
220 220
 				return $value ? 'true' : 'false';
221 221
 
222 222
 			case Wordlift_Schema_Service::DATA_TYPE_DATE:
223
-				$date       = date_create_from_format( 'Y/m/d', $value );
224
-				$date_value = date_format( $date, 'Y-m-d' );
223
+				$date       = date_create_from_format('Y/m/d', $value);
224
+				$date_value = date_format($date, 'Y-m-d');
225 225
 
226
-				return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) );
226
+				return sprintf('"%s"^^xsd:date', self::escape($date_value));
227 227
 
228 228
 			case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME:
229
-				$date       = date_create_from_format( 'Y/m/d H:i', $value );
230
-				$date_value = date_format( $date, 'Y-m-d\TH:i:00' );
229
+				$date       = date_create_from_format('Y/m/d H:i', $value);
230
+				$date_value = date_format($date, 'Y-m-d\TH:i:00');
231 231
 
232
-				return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) );
232
+				return sprintf('"%s"^^xsd:dateTime', self::escape($date_value));
233 233
 
234 234
 			case Wordlift_Schema_Service::DATA_TYPE_DURATION:
235
-				$time       = date_create_from_format( 'H:i', $value );
236
-				$time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) );
235
+				$time       = date_create_from_format('H:i', $value);
236
+				$time_value = sprintf('PT%dH%dM', date_format($time, 'H'), intval(date_format($time, 'i')));
237 237
 
238
-				return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) );
238
+				return sprintf('"%s"^^xsd:duration', self::escape($time_value));
239 239
 
240 240
 			case Wordlift_Schema_Service::DATA_TYPE_DOUBLE:
241
-				return sprintf( '"%s"^^xsd:double', self::escape( $value ) );
241
+				return sprintf('"%s"^^xsd:double', self::escape($value));
242 242
 
243 243
 			case Wordlift_Schema_Service::DATA_TYPE_INTEGER:
244
-				return sprintf( '"%s"^^xsd:integer', self::escape( $value ) );
244
+				return sprintf('"%s"^^xsd:integer', self::escape($value));
245 245
 
246 246
 			case Wordlift_Schema_Service::DATA_TYPE_STRING:
247
-				return sprintf( '"%s"^^xsd:string', self::escape( $value ) );
247
+				return sprintf('"%s"^^xsd:string', self::escape($value));
248 248
 
249 249
 			case Wordlift_Schema_Service::DATA_TYPE_URI:
250 250
 				/**
@@ -256,19 +256,19 @@  discard block
 block discarded – undo
256 256
 				 *
257 257
 				 * @param string $uri The uri.
258 258
 				 */
259
-				return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) );
259
+				return sprintf('<%s>', self::escape_uri(apply_filters('wl_production_uri', $value)));
260 260
 
261 261
 			case null:
262
-				$language_tag = ( null !== $language ? "@$language" : '' );
262
+				$language_tag = (null !== $language ? "@$language" : '');
263 263
 
264
-				return sprintf( '"%s"%s', self::escape( $value ), $language_tag );
264
+				return sprintf('"%s"%s', self::escape($value), $language_tag);
265 265
 
266 266
 			default:
267 267
 
268
-				self::$log->warn( "Unknown data type [ type :: $type ]" );
268
+				self::$log->warn("Unknown data type [ type :: $type ]");
269 269
 
270 270
 				// Try to insert the value anyway.
271
-				return sprintf( '"%s"', self::escape( $value ) );
271
+				return sprintf('"%s"', self::escape($value));
272 272
 		}
273 273
 
274 274
 	}
@@ -282,13 +282,13 @@  discard block
 block discarded – undo
282 282
 	 *
283 283
 	 * @return string The escaped URI.
284 284
 	 */
285
-	public static function escape_uri( $uri ) {
285
+	public static function escape_uri($uri) {
286 286
 
287 287
 		// Should we validate the IRI?
288 288
 		// http://www.w3.org/TR/sparql11-query/#QSynIRI
289 289
 
290
-		$uri = str_replace( '<', '\<', $uri );
291
-		$uri = str_replace( '>', '\>', $uri );
290
+		$uri = str_replace('<', '\<', $uri);
291
+		$uri = str_replace('>', '\>', $uri);
292 292
 
293 293
 		return $uri;
294 294
 	}
@@ -302,7 +302,7 @@  discard block
 block discarded – undo
302 302
 	 *
303 303
 	 * @return string The escaped string.
304 304
 	 */
305
-	public static function escape( $string ) {
305
+	public static function escape($string) {
306 306
 
307 307
 		// see http://www.w3.org/TR/rdf-sparql-query/
308 308
 		//    '\t'	U+0009 (tab)
@@ -314,14 +314,14 @@  discard block
 block discarded – undo
314 314
 		//    "\'"	U+0027 (apostrophe-quote, single quote mark)
315 315
 		//    '\\'	U+005C (backslash)
316 316
 
317
-		$string = str_replace( '\\', '\\\\', $string );
318
-		$string = str_replace( '\'', '\\\'', $string );
319
-		$string = str_replace( '"', '\\"', $string );
320
-		$string = str_replace( "\f", '\\f', $string );
321
-		$string = str_replace( "\b", '\\b', $string );
322
-		$string = str_replace( "\r", '\\r', $string );
323
-		$string = str_replace( "\n", '\\n', $string );
324
-		$string = str_replace( "\t", '\\t', $string );
317
+		$string = str_replace('\\', '\\\\', $string);
318
+		$string = str_replace('\'', '\\\'', $string);
319
+		$string = str_replace('"', '\\"', $string);
320
+		$string = str_replace("\f", '\\f', $string);
321
+		$string = str_replace("\b", '\\b', $string);
322
+		$string = str_replace("\r", '\\r', $string);
323
+		$string = str_replace("\n", '\\n', $string);
324
+		$string = str_replace("\t", '\\t', $string);
325 325
 
326 326
 		return $string;
327 327
 	}
Please login to merge, or discard this patch.