@@ -7,310 +7,310 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Query_Builder { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The INSERT statement template. |
|
| 12 | - * |
|
| 13 | - * @since 3.1.7 |
|
| 14 | - */ |
|
| 15 | - const INSERT = 'INSERT DATA { %s };'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The DELETE statement template (it repeats the statements in the WHERE clause. |
|
| 19 | - * |
|
| 20 | - * @since 3.1.7 |
|
| 21 | - */ |
|
| 22 | - const DELETE = 'DELETE { %s } WHERE { %1$s };'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Tell the statement function to guess the object type (URI, value or parameter). |
|
| 26 | - * |
|
| 27 | - * @since 3.1.7 |
|
| 28 | - */ |
|
| 29 | - const OBJECT_AUTO = - 1; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Tell the statement function that the object is a URI. |
|
| 33 | - * |
|
| 34 | - * @since 3.1.7 |
|
| 35 | - */ |
|
| 36 | - const OBJECT_URI = 0; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Tell the statement function that the object is a value. |
|
| 40 | - * |
|
| 41 | - * @since 3.1.7 |
|
| 42 | - */ |
|
| 43 | - const OBJECT_VALUE = 1; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Tell the statement function that the object is a parameter. |
|
| 47 | - * |
|
| 48 | - * @since 3.1.7 |
|
| 49 | - */ |
|
| 50 | - const OBJECT_PARAMETER = 2; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The RDFS type. |
|
| 54 | - * |
|
| 55 | - * @since 3.1.7 |
|
| 56 | - */ |
|
| 57 | - const RDFS_TYPE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The schema.org/Person type. |
|
| 61 | - * |
|
| 62 | - * @since 3.1.7 |
|
| 63 | - */ |
|
| 64 | - const SCHEMA_PERSON_URI = 'http://schema.org/Person'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The schema.org given name predicate. |
|
| 68 | - * |
|
| 69 | - * @since 3.1.7 |
|
| 70 | - */ |
|
| 71 | - const SCHEMA_GIVEN_NAME_URI = 'http://schema.org/givenName'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The schema.org family name predicate. |
|
| 75 | - * |
|
| 76 | - * @since 3.1.7 |
|
| 77 | - */ |
|
| 78 | - const SCHEMA_FAMILY_NAME_URI = 'http://schema.org/familyName'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The schema.org url predicate. |
|
| 82 | - * |
|
| 83 | - * @since 3.1.7 |
|
| 84 | - */ |
|
| 85 | - const SCHEMA_URL_URI = 'http://schema.org/url'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @since 3.14.0 |
|
| 89 | - */ |
|
| 90 | - const SCHEMA_IMAGE_URI = 'http://schema.org/image'; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * The location created predicate. |
|
| 94 | - * |
|
| 95 | - * @since 3.14.0 |
|
| 96 | - */ |
|
| 97 | - const SCHEMA_LOCATION_CREATED_URI = 'http://schema.org/locationCreated'; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @since 3.14.0 |
|
| 101 | - */ |
|
| 102 | - const SCHEMA_AUTHOR_URI = 'http://schema.org/author'; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @since 3.14.0 |
|
| 106 | - */ |
|
| 107 | - const SCHEMA_INTERACTION_COUNT_URI = 'http://schema.org/interactionCount'; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @since 3.14.0 |
|
| 111 | - */ |
|
| 112 | - const DCTERMS_SUBJECT_URI = 'http://purl.org/dc/terms/subject'; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @since 3.14.0 |
|
| 116 | - */ |
|
| 117 | - const DCTERMS_REFERENCES_URI = 'http://purl.org/dc/terms/references'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The RDF label. |
|
| 121 | - * |
|
| 122 | - * @since 3.1.7 |
|
| 123 | - */ |
|
| 124 | - const RDFS_LABEL_URI = 'http://www.w3.org/2000/01/rdf-schema#label'; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Hold the template (INSERT or DELETE). |
|
| 128 | - * |
|
| 129 | - * @since 3.1.7 |
|
| 130 | - * @access private |
|
| 131 | - * @var string $template The query template. |
|
| 132 | - */ |
|
| 133 | - private $template; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * An array of statements (in the form of subject, predicate, object). |
|
| 137 | - * |
|
| 138 | - * @since 3.1.7 |
|
| 139 | - * @access private |
|
| 140 | - * @var array $statements An array of statements. |
|
| 141 | - */ |
|
| 142 | - private $statements = array(); |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Create a new instance of the Query builder (compatible with PHP 5.3). |
|
| 146 | - * |
|
| 147 | - * @since 3.1.7 |
|
| 148 | - * @return Wordlift_Query_Builder A new instance of the Query builder. |
|
| 149 | - */ |
|
| 150 | - public static function new_instance() { |
|
| 151 | - |
|
| 152 | - return new Wordlift_Query_Builder(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Set the query to INSERT. |
|
| 157 | - * |
|
| 158 | - * @since 3.1.7 |
|
| 159 | - * @return Wordlift_Query_Builder The Query builder. |
|
| 160 | - */ |
|
| 161 | - public function insert() { |
|
| 162 | - |
|
| 163 | - $this->template = self::INSERT; |
|
| 164 | - |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Set the query to DELETE. |
|
| 170 | - * |
|
| 171 | - * @since 3.1.7 |
|
| 172 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 173 | - */ |
|
| 174 | - public function delete() { |
|
| 175 | - |
|
| 176 | - $this->template = self::DELETE; |
|
| 177 | - |
|
| 178 | - return $this; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Set the query to SELECT. |
|
| 183 | - * |
|
| 184 | - * @since 3.12.2 |
|
| 185 | - * |
|
| 186 | - * @param string $props The list of properties to read. |
|
| 187 | - * |
|
| 188 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 189 | - */ |
|
| 190 | - public function select( $props = '*' ) { |
|
| 191 | - |
|
| 192 | - $this->template = "SELECT $props WHERE { %s }"; |
|
| 193 | - |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Add a statement. |
|
| 199 | - * |
|
| 200 | - * @since 3.1.7 |
|
| 201 | - * |
|
| 202 | - * @param string $subject The subject of the statement (must be a URI). |
|
| 203 | - * @param string $predicate The predicate (must be a URI). |
|
| 204 | - * @param string $object The object, can be a URI or a value. |
|
| 205 | - * @param int $object_type The object type, either a {@link OBJECT_URI} or a value {@link OBJECT_VALUE}. If set to {@link OBJECT_AUTO}, the Query builder will try to guess. |
|
| 206 | - * @param string|null $data_type The data type (or null). |
|
| 207 | - * @param string|null $language The language code (or null). |
|
| 208 | - * |
|
| 209 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 210 | - */ |
|
| 211 | - public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 212 | - |
|
| 213 | - // If no value has been provided, we don't set any statement. |
|
| 214 | - if ( empty( $object ) ) { |
|
| 215 | - return $this; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // Guess the subject type. |
|
| 219 | - $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 220 | - |
|
| 221 | - // Get the object type if set, otherwise try to guess it. |
|
| 222 | - $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 223 | - |
|
| 224 | - // Prepare the statement template. |
|
| 225 | - $template = |
|
| 226 | - // Subject as a parameter, no `<`, `>`. |
|
| 227 | - ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 228 | - // Predicate. |
|
| 229 | - ' <%2$s> ' . |
|
| 230 | - // Object. |
|
| 231 | - ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 232 | - ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 233 | - // self::OBJECT_VALUE === $object_value_type |
|
| 234 | - '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 235 | - |
|
| 236 | - // Escape the subject, predicate and object. |
|
| 237 | - $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 238 | - $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 239 | - $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 240 | - |
|
| 241 | - // Prepare the statement and add it to the list of statements. |
|
| 242 | - $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 243 | - |
|
| 244 | - return $this; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Build the query. |
|
| 249 | - * |
|
| 250 | - * @since 3.1.7 |
|
| 251 | - * @return string The query string. |
|
| 252 | - */ |
|
| 253 | - public function build() { |
|
| 254 | - |
|
| 255 | - // If there are no statements return an empty string. |
|
| 256 | - if ( 0 === count( $this->statements ) ) { |
|
| 257 | - return ''; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Guess the statement object type. |
|
| 265 | - * |
|
| 266 | - * @since 3.1.7 |
|
| 267 | - * |
|
| 268 | - * @param string $predicate The predicate. |
|
| 269 | - * @param string $object The object. |
|
| 270 | - * |
|
| 271 | - * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
|
| 272 | - */ |
|
| 273 | - private function guess_object_type( $predicate, $object ) { |
|
| 274 | - |
|
| 275 | - // If the object starts with a question mark, it's a parameter. |
|
| 276 | - if ( 0 === strpos( $object, '?' ) ) { |
|
| 277 | - return self::OBJECT_PARAMETER; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // Guess based on the predicate. |
|
| 281 | - switch ( $predicate ) { |
|
| 282 | - |
|
| 283 | - case self::DCTERMS_REFERENCES_URI: |
|
| 284 | - case self::DCTERMS_SUBJECT_URI: |
|
| 285 | - case self::RDFS_TYPE_URI: |
|
| 286 | - case self::SCHEMA_AUTHOR_URI: |
|
| 287 | - case self::SCHEMA_LOCATION_CREATED_URI: |
|
| 288 | - case self::SCHEMA_URL_URI: |
|
| 289 | - case self::SCHEMA_IMAGE_URI: |
|
| 290 | - return self::OBJECT_URI; |
|
| 291 | - |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return self::OBJECT_VALUE; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Guess the subject type. |
|
| 299 | - * |
|
| 300 | - * @since 3.12.3 |
|
| 301 | - * |
|
| 302 | - * @param string $subject The subject string. |
|
| 303 | - * |
|
| 304 | - * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
|
| 305 | - */ |
|
| 306 | - private function guess_subject_type( $subject ) { |
|
| 307 | - |
|
| 308 | - // If the object starts with a question mark, it's a parameter. |
|
| 309 | - if ( 0 === strpos( $subject, '?' ) ) { |
|
| 310 | - return self::OBJECT_PARAMETER; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - return self::OBJECT_URI; |
|
| 314 | - } |
|
| 10 | + /** |
|
| 11 | + * The INSERT statement template. |
|
| 12 | + * |
|
| 13 | + * @since 3.1.7 |
|
| 14 | + */ |
|
| 15 | + const INSERT = 'INSERT DATA { %s };'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The DELETE statement template (it repeats the statements in the WHERE clause. |
|
| 19 | + * |
|
| 20 | + * @since 3.1.7 |
|
| 21 | + */ |
|
| 22 | + const DELETE = 'DELETE { %s } WHERE { %1$s };'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Tell the statement function to guess the object type (URI, value or parameter). |
|
| 26 | + * |
|
| 27 | + * @since 3.1.7 |
|
| 28 | + */ |
|
| 29 | + const OBJECT_AUTO = - 1; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Tell the statement function that the object is a URI. |
|
| 33 | + * |
|
| 34 | + * @since 3.1.7 |
|
| 35 | + */ |
|
| 36 | + const OBJECT_URI = 0; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Tell the statement function that the object is a value. |
|
| 40 | + * |
|
| 41 | + * @since 3.1.7 |
|
| 42 | + */ |
|
| 43 | + const OBJECT_VALUE = 1; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Tell the statement function that the object is a parameter. |
|
| 47 | + * |
|
| 48 | + * @since 3.1.7 |
|
| 49 | + */ |
|
| 50 | + const OBJECT_PARAMETER = 2; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The RDFS type. |
|
| 54 | + * |
|
| 55 | + * @since 3.1.7 |
|
| 56 | + */ |
|
| 57 | + const RDFS_TYPE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The schema.org/Person type. |
|
| 61 | + * |
|
| 62 | + * @since 3.1.7 |
|
| 63 | + */ |
|
| 64 | + const SCHEMA_PERSON_URI = 'http://schema.org/Person'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The schema.org given name predicate. |
|
| 68 | + * |
|
| 69 | + * @since 3.1.7 |
|
| 70 | + */ |
|
| 71 | + const SCHEMA_GIVEN_NAME_URI = 'http://schema.org/givenName'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The schema.org family name predicate. |
|
| 75 | + * |
|
| 76 | + * @since 3.1.7 |
|
| 77 | + */ |
|
| 78 | + const SCHEMA_FAMILY_NAME_URI = 'http://schema.org/familyName'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The schema.org url predicate. |
|
| 82 | + * |
|
| 83 | + * @since 3.1.7 |
|
| 84 | + */ |
|
| 85 | + const SCHEMA_URL_URI = 'http://schema.org/url'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @since 3.14.0 |
|
| 89 | + */ |
|
| 90 | + const SCHEMA_IMAGE_URI = 'http://schema.org/image'; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * The location created predicate. |
|
| 94 | + * |
|
| 95 | + * @since 3.14.0 |
|
| 96 | + */ |
|
| 97 | + const SCHEMA_LOCATION_CREATED_URI = 'http://schema.org/locationCreated'; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @since 3.14.0 |
|
| 101 | + */ |
|
| 102 | + const SCHEMA_AUTHOR_URI = 'http://schema.org/author'; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @since 3.14.0 |
|
| 106 | + */ |
|
| 107 | + const SCHEMA_INTERACTION_COUNT_URI = 'http://schema.org/interactionCount'; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @since 3.14.0 |
|
| 111 | + */ |
|
| 112 | + const DCTERMS_SUBJECT_URI = 'http://purl.org/dc/terms/subject'; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @since 3.14.0 |
|
| 116 | + */ |
|
| 117 | + const DCTERMS_REFERENCES_URI = 'http://purl.org/dc/terms/references'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The RDF label. |
|
| 121 | + * |
|
| 122 | + * @since 3.1.7 |
|
| 123 | + */ |
|
| 124 | + const RDFS_LABEL_URI = 'http://www.w3.org/2000/01/rdf-schema#label'; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Hold the template (INSERT or DELETE). |
|
| 128 | + * |
|
| 129 | + * @since 3.1.7 |
|
| 130 | + * @access private |
|
| 131 | + * @var string $template The query template. |
|
| 132 | + */ |
|
| 133 | + private $template; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * An array of statements (in the form of subject, predicate, object). |
|
| 137 | + * |
|
| 138 | + * @since 3.1.7 |
|
| 139 | + * @access private |
|
| 140 | + * @var array $statements An array of statements. |
|
| 141 | + */ |
|
| 142 | + private $statements = array(); |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Create a new instance of the Query builder (compatible with PHP 5.3). |
|
| 146 | + * |
|
| 147 | + * @since 3.1.7 |
|
| 148 | + * @return Wordlift_Query_Builder A new instance of the Query builder. |
|
| 149 | + */ |
|
| 150 | + public static function new_instance() { |
|
| 151 | + |
|
| 152 | + return new Wordlift_Query_Builder(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Set the query to INSERT. |
|
| 157 | + * |
|
| 158 | + * @since 3.1.7 |
|
| 159 | + * @return Wordlift_Query_Builder The Query builder. |
|
| 160 | + */ |
|
| 161 | + public function insert() { |
|
| 162 | + |
|
| 163 | + $this->template = self::INSERT; |
|
| 164 | + |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Set the query to DELETE. |
|
| 170 | + * |
|
| 171 | + * @since 3.1.7 |
|
| 172 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 173 | + */ |
|
| 174 | + public function delete() { |
|
| 175 | + |
|
| 176 | + $this->template = self::DELETE; |
|
| 177 | + |
|
| 178 | + return $this; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Set the query to SELECT. |
|
| 183 | + * |
|
| 184 | + * @since 3.12.2 |
|
| 185 | + * |
|
| 186 | + * @param string $props The list of properties to read. |
|
| 187 | + * |
|
| 188 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 189 | + */ |
|
| 190 | + public function select( $props = '*' ) { |
|
| 191 | + |
|
| 192 | + $this->template = "SELECT $props WHERE { %s }"; |
|
| 193 | + |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Add a statement. |
|
| 199 | + * |
|
| 200 | + * @since 3.1.7 |
|
| 201 | + * |
|
| 202 | + * @param string $subject The subject of the statement (must be a URI). |
|
| 203 | + * @param string $predicate The predicate (must be a URI). |
|
| 204 | + * @param string $object The object, can be a URI or a value. |
|
| 205 | + * @param int $object_type The object type, either a {@link OBJECT_URI} or a value {@link OBJECT_VALUE}. If set to {@link OBJECT_AUTO}, the Query builder will try to guess. |
|
| 206 | + * @param string|null $data_type The data type (or null). |
|
| 207 | + * @param string|null $language The language code (or null). |
|
| 208 | + * |
|
| 209 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 210 | + */ |
|
| 211 | + public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 212 | + |
|
| 213 | + // If no value has been provided, we don't set any statement. |
|
| 214 | + if ( empty( $object ) ) { |
|
| 215 | + return $this; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // Guess the subject type. |
|
| 219 | + $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 220 | + |
|
| 221 | + // Get the object type if set, otherwise try to guess it. |
|
| 222 | + $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 223 | + |
|
| 224 | + // Prepare the statement template. |
|
| 225 | + $template = |
|
| 226 | + // Subject as a parameter, no `<`, `>`. |
|
| 227 | + ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 228 | + // Predicate. |
|
| 229 | + ' <%2$s> ' . |
|
| 230 | + // Object. |
|
| 231 | + ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 232 | + ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 233 | + // self::OBJECT_VALUE === $object_value_type |
|
| 234 | + '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 235 | + |
|
| 236 | + // Escape the subject, predicate and object. |
|
| 237 | + $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 238 | + $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 239 | + $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 240 | + |
|
| 241 | + // Prepare the statement and add it to the list of statements. |
|
| 242 | + $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 243 | + |
|
| 244 | + return $this; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Build the query. |
|
| 249 | + * |
|
| 250 | + * @since 3.1.7 |
|
| 251 | + * @return string The query string. |
|
| 252 | + */ |
|
| 253 | + public function build() { |
|
| 254 | + |
|
| 255 | + // If there are no statements return an empty string. |
|
| 256 | + if ( 0 === count( $this->statements ) ) { |
|
| 257 | + return ''; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Guess the statement object type. |
|
| 265 | + * |
|
| 266 | + * @since 3.1.7 |
|
| 267 | + * |
|
| 268 | + * @param string $predicate The predicate. |
|
| 269 | + * @param string $object The object. |
|
| 270 | + * |
|
| 271 | + * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
|
| 272 | + */ |
|
| 273 | + private function guess_object_type( $predicate, $object ) { |
|
| 274 | + |
|
| 275 | + // If the object starts with a question mark, it's a parameter. |
|
| 276 | + if ( 0 === strpos( $object, '?' ) ) { |
|
| 277 | + return self::OBJECT_PARAMETER; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // Guess based on the predicate. |
|
| 281 | + switch ( $predicate ) { |
|
| 282 | + |
|
| 283 | + case self::DCTERMS_REFERENCES_URI: |
|
| 284 | + case self::DCTERMS_SUBJECT_URI: |
|
| 285 | + case self::RDFS_TYPE_URI: |
|
| 286 | + case self::SCHEMA_AUTHOR_URI: |
|
| 287 | + case self::SCHEMA_LOCATION_CREATED_URI: |
|
| 288 | + case self::SCHEMA_URL_URI: |
|
| 289 | + case self::SCHEMA_IMAGE_URI: |
|
| 290 | + return self::OBJECT_URI; |
|
| 291 | + |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return self::OBJECT_VALUE; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Guess the subject type. |
|
| 299 | + * |
|
| 300 | + * @since 3.12.3 |
|
| 301 | + * |
|
| 302 | + * @param string $subject The subject string. |
|
| 303 | + * |
|
| 304 | + * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
|
| 305 | + */ |
|
| 306 | + private function guess_subject_type( $subject ) { |
|
| 307 | + |
|
| 308 | + // If the object starts with a question mark, it's a parameter. |
|
| 309 | + if ( 0 === strpos( $subject, '?' ) ) { |
|
| 310 | + return self::OBJECT_PARAMETER; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + return self::OBJECT_URI; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | 316 | } |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return $this \Wordlift_Query_Builder The Query builder. |
| 189 | 189 | */ |
| 190 | - public function select( $props = '*' ) { |
|
| 190 | + public function select($props = '*') { |
|
| 191 | 191 | |
| 192 | 192 | $this->template = "SELECT $props WHERE { %s }"; |
| 193 | 193 | |
@@ -208,38 +208,36 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @return $this \Wordlift_Query_Builder The Query builder. |
| 210 | 210 | */ |
| 211 | - public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 211 | + public function statement($subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null) { |
|
| 212 | 212 | |
| 213 | 213 | // If no value has been provided, we don't set any statement. |
| 214 | - if ( empty( $object ) ) { |
|
| 214 | + if (empty($object)) { |
|
| 215 | 215 | return $this; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | // Guess the subject type. |
| 219 | - $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 219 | + $subject_value_type = $this->guess_subject_type($subject); |
|
| 220 | 220 | |
| 221 | 221 | // Get the object type if set, otherwise try to guess it. |
| 222 | - $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 222 | + $object_value_type = (self::OBJECT_AUTO === $object_type ? $this->guess_object_type($predicate, $object) : $object_type); |
|
| 223 | 223 | |
| 224 | 224 | // Prepare the statement template. |
| 225 | 225 | $template = |
| 226 | 226 | // Subject as a parameter, no `<`, `>`. |
| 227 | - ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 227 | + (self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>'). |
|
| 228 | 228 | // Predicate. |
| 229 | - ' <%2$s> ' . |
|
| 229 | + ' <%2$s> '. |
|
| 230 | 230 | // Object. |
| 231 | - ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 232 | - ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 233 | - // self::OBJECT_VALUE === $object_value_type |
|
| 234 | - '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 231 | + (self::OBJECT_URI === $object_value_type ? '<%3$s>' : (self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : // self::OBJECT_VALUE === $object_value_type |
|
| 232 | + '"%3$s"'.(isset($data_type) ? '^^%4$s' : '').(isset($language) ? '@%5$s' : ''))); |
|
| 235 | 233 | |
| 236 | 234 | // Escape the subject, predicate and object. |
| 237 | - $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 238 | - $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 239 | - $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 235 | + $escaped_subject = Wordlift_Sparql_Service::escape_uri($subject); |
|
| 236 | + $escaped_predicate = Wordlift_Sparql_Service::escape_uri($predicate); |
|
| 237 | + $escaped_object = (self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri($object) : Wordlift_Sparql_Service::escape($object)); |
|
| 240 | 238 | |
| 241 | 239 | // Prepare the statement and add it to the list of statements. |
| 242 | - $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 240 | + $this->statements[] = sprintf($template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language); |
|
| 243 | 241 | |
| 244 | 242 | return $this; |
| 245 | 243 | } |
@@ -253,11 +251,11 @@ discard block |
||
| 253 | 251 | public function build() { |
| 254 | 252 | |
| 255 | 253 | // If there are no statements return an empty string. |
| 256 | - if ( 0 === count( $this->statements ) ) { |
|
| 254 | + if (0 === count($this->statements)) { |
|
| 257 | 255 | return ''; |
| 258 | 256 | } |
| 259 | 257 | |
| 260 | - return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 258 | + return sprintf($this->template, implode(' . ', $this->statements))."\n"; |
|
| 261 | 259 | } |
| 262 | 260 | |
| 263 | 261 | /** |
@@ -270,15 +268,15 @@ discard block |
||
| 270 | 268 | * |
| 271 | 269 | * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
| 272 | 270 | */ |
| 273 | - private function guess_object_type( $predicate, $object ) { |
|
| 271 | + private function guess_object_type($predicate, $object) { |
|
| 274 | 272 | |
| 275 | 273 | // If the object starts with a question mark, it's a parameter. |
| 276 | - if ( 0 === strpos( $object, '?' ) ) { |
|
| 274 | + if (0 === strpos($object, '?')) { |
|
| 277 | 275 | return self::OBJECT_PARAMETER; |
| 278 | 276 | } |
| 279 | 277 | |
| 280 | 278 | // Guess based on the predicate. |
| 281 | - switch ( $predicate ) { |
|
| 279 | + switch ($predicate) { |
|
| 282 | 280 | |
| 283 | 281 | case self::DCTERMS_REFERENCES_URI: |
| 284 | 282 | case self::DCTERMS_SUBJECT_URI: |
@@ -303,10 +301,10 @@ discard block |
||
| 303 | 301 | * |
| 304 | 302 | * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
| 305 | 303 | */ |
| 306 | - private function guess_subject_type( $subject ) { |
|
| 304 | + private function guess_subject_type($subject) { |
|
| 307 | 305 | |
| 308 | 306 | // If the object starts with a question mark, it's a parameter. |
| 309 | - if ( 0 === strpos( $subject, '?' ) ) { |
|
| 307 | + if (0 === strpos($subject, '?')) { |
|
| 310 | 308 | return self::OBJECT_PARAMETER; |
| 311 | 309 | } |
| 312 | 310 | |
@@ -16,23 +16,23 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | function wl_admin_permalink_html( $html, $post_id, $new_title, $new_slug ) { |
| 18 | 18 | |
| 19 | - // If the post is published, add the button to view Redlink's linked data. |
|
| 20 | - if ( 'publish' == get_post_status( $post_id ) ) { |
|
| 21 | - if ( $uri = wl_get_entity_uri( $post_id ) ) { |
|
| 22 | - $uri_esc = esc_attr( wl_get_entity_uri( $post_id ) ); |
|
| 23 | - $lod_view_href = 'http://lodview.it/lodview/?IRI=' . $uri_esc; |
|
| 24 | - $html .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" . |
|
| 25 | - esc_html__( 'View Linked Data', 'wordlift' ) . |
|
| 26 | - "</a></span>\n"; |
|
| 27 | - } |
|
| 28 | - $html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL . |
|
| 29 | - urlencode( get_permalink( $post_id ) ) . |
|
| 30 | - "' class='button button-small wl-button' target='_blank'>" . |
|
| 31 | - esc_html__( 'Test Google Rich Snippets', 'wordlift' ) . |
|
| 32 | - "</a></span>\n"; |
|
| 33 | - } |
|
| 19 | + // If the post is published, add the button to view Redlink's linked data. |
|
| 20 | + if ( 'publish' == get_post_status( $post_id ) ) { |
|
| 21 | + if ( $uri = wl_get_entity_uri( $post_id ) ) { |
|
| 22 | + $uri_esc = esc_attr( wl_get_entity_uri( $post_id ) ); |
|
| 23 | + $lod_view_href = 'http://lodview.it/lodview/?IRI=' . $uri_esc; |
|
| 24 | + $html .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" . |
|
| 25 | + esc_html__( 'View Linked Data', 'wordlift' ) . |
|
| 26 | + "</a></span>\n"; |
|
| 27 | + } |
|
| 28 | + $html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL . |
|
| 29 | + urlencode( get_permalink( $post_id ) ) . |
|
| 30 | + "' class='button button-small wl-button' target='_blank'>" . |
|
| 31 | + esc_html__( 'Test Google Rich Snippets', 'wordlift' ) . |
|
| 32 | + "</a></span>\n"; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - return $html; |
|
| 35 | + return $html; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | add_filter( 'get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4 ); |
@@ -14,25 +14,25 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @return The enhanced html. |
| 16 | 16 | */ |
| 17 | -function wl_admin_permalink_html( $html, $post_id, $new_title, $new_slug ) { |
|
| 17 | +function wl_admin_permalink_html($html, $post_id, $new_title, $new_slug) { |
|
| 18 | 18 | |
| 19 | 19 | // If the post is published, add the button to view Redlink's linked data. |
| 20 | - if ( 'publish' == get_post_status( $post_id ) ) { |
|
| 21 | - if ( $uri = wl_get_entity_uri( $post_id ) ) { |
|
| 22 | - $uri_esc = esc_attr( wl_get_entity_uri( $post_id ) ); |
|
| 23 | - $lod_view_href = 'http://lodview.it/lodview/?IRI=' . $uri_esc; |
|
| 24 | - $html .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>" . |
|
| 25 | - esc_html__( 'View Linked Data', 'wordlift' ) . |
|
| 20 | + if ('publish' == get_post_status($post_id)) { |
|
| 21 | + if ($uri = wl_get_entity_uri($post_id)) { |
|
| 22 | + $uri_esc = esc_attr(wl_get_entity_uri($post_id)); |
|
| 23 | + $lod_view_href = 'http://lodview.it/lodview/?IRI='.$uri_esc; |
|
| 24 | + $html .= "<span id='view-post-btn'><a href='$lod_view_href' class='button button-small wl-button' target='_blank'>". |
|
| 25 | + esc_html__('View Linked Data', 'wordlift'). |
|
| 26 | 26 | "</a></span>\n"; |
| 27 | 27 | } |
| 28 | - $html .= "<span id='view-post-btn'><a href='" . WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL . |
|
| 29 | - urlencode( get_permalink( $post_id ) ) . |
|
| 30 | - "' class='button button-small wl-button' target='_blank'>" . |
|
| 31 | - esc_html__( 'Test Google Rich Snippets', 'wordlift' ) . |
|
| 28 | + $html .= "<span id='view-post-btn'><a href='".WL_CONFIG_TEST_GOOGLE_RICH_SNIPPETS_URL. |
|
| 29 | + urlencode(get_permalink($post_id)). |
|
| 30 | + "' class='button button-small wl-button' target='_blank'>". |
|
| 31 | + esc_html__('Test Google Rich Snippets', 'wordlift'). |
|
| 32 | 32 | "</a></span>\n"; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | return $html; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | -add_filter( 'get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4 ); |
|
| 38 | +add_filter('get_sample_permalink_html', 'wl_admin_permalink_html', PHP_INT_MAX, 4); |
|