Changeset 3730:5c45a5df9a59 for inc/core/class.dc.meta.php
- Timestamp:
- 03/08/18 17:58:39 (7 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.meta.php
r3057 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 /** … … 18 18 19 19 Dotclear metadata class instance is provided by dcCore $meta property. 20 */20 */ 21 21 class dcMeta 22 22 { 23 private $core; ///< <b>dcCore</b> dcCore instance 24 private $con; ///< <b>connection</b> Database connection object 25 private $table; ///< <b>string</b> Media table name 26 27 /** 28 Object constructor. 29 30 @param core <b>dcCore</b> dcCore instance 31 */ 32 public function __construct($core) 33 { 34 $this->core =& $core; 35 $this->con =& $this->core->con; 36 $this->table = $this->core->prefix.'meta'; 37 } 38 39 /** 40 Splits up comma-separated values into an array of 41 unique, URL-proof metadata values. 42 43 @param str <b>string</b> Comma-separated metadata. 44 45 @return <b>Array</b> The array of sanitized metadata 46 */ 47 public function splitMetaValues($str) 48 { 49 $res = array(); 50 foreach (explode(',',$str) as $i => $tag) 51 { 52 $tag = trim($tag); 53 $tag = self::sanitizeMetaID($tag); 54 55 if ($tag != false) { 56 $res[$i] = $tag; 57 } 58 } 59 60 return array_unique($res); 61 } 62 63 /** 64 Make a metadata ID URL-proof. 65 66 @param str <b>string</b> the metadata ID. 67 68 @return <b>string</b> The sanitized metadata 69 */ 70 public static function sanitizeMetaID($str) 71 { 72 return text::tidyURL($str,false,true); 73 } 74 75 /** 76 Converts serialized metadata (for instance in dc_post post_meta) 77 into a meta array. 78 79 @param str <b>string</b> the serialized metadata. 80 81 @return <b>Array</b> the resulting array of post meta 82 */ 83 public function getMetaArray($str) 84 { 85 $meta = @unserialize($str); 86 87 if (!is_array($meta)) { 88 return array(); 89 } 90 91 return $meta; 92 } 93 94 /** 95 Converts serialized metadata (for instance in dc_post post_meta) 96 into a comma-separated meta list for a given type. 97 98 @param str <b>string</b> the serialized metadata. 99 @param type <b>string</b> meta type to retrieve metaIDs from. 100 101 @return <b>string</b> the comma-separated list of meta 102 */ 103 public function getMetaStr($str,$type) 104 { 105 $meta = $this->getMetaArray($str); 106 107 if (!isset($meta[$type])) { 108 return ''; 109 } 110 111 return implode(', ',$meta[$type]); 112 } 113 114 /** 115 Converts serialized metadata (for instance in dc_post post_meta) 116 into a "fetchable" metadata record. 117 118 @param str <b>string</b> the serialized metadata. 119 @param type <b>string</b> meta type to retrieve metaIDs from. 120 121 @return <b>record</b> the meta recordset 122 */ 123 public function getMetaRecordset($str,$type) 124 { 125 $meta = $this->getMetaArray($str); 126 $data = array(); 127 128 if (isset($meta[$type])) 129 { 130 foreach ($meta[$type] as $v) 131 { 132 $data[] = array( 133 'meta_id' => $v, 134 'meta_type' => $type, 135 'meta_id_lower' => mb_strtolower($v), 136 'count' => 0, 137 'percent' => 0, 138 'roundpercent' => 0 139 ); 140 } 141 } 142 143 return staticRecord::newFromArray($data); 144 } 145 146 /** 147 @deprecated since version 2.2 : $core->meta is always defined 148 @see getMetaRecordset 149 static version of getMetaRecordset 150 */ 151 public static function getMetaRecord($core,$str,$type) 152 { 153 $meta = new self($core); 154 return $meta->getMetaRecordset($str,$type); 155 } 156 157 /** 158 Checks whether the current user is allowed to change post meta 159 An exception is thrown if user is not allowed. 160 161 @param post_id <b>string</b> the post_id to check. 162 */ 163 private function checkPermissionsOnPost($post_id) 164 { 165 $post_id = (integer) $post_id; 166 167 if (!$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) { 168 throw new Exception(__('You are not allowed to change this entry status')); 169 } 170 171 #�If user can only publish, we need to check the post's owner 172 if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) 173 { 174 $strReq = 'SELECT post_id '. 175 'FROM '.$this->core->prefix.'post '. 176 'WHERE post_id = '.$post_id.' '. 177 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 178 179 $rs = $this->con->select($strReq); 180 181 if ($rs->isEmpty()) { 182 throw new Exception(__('You are not allowed to change this entry status')); 183 } 184 } 185 } 186 187 /** 188 Updates serialized post_meta information with dc_meta table information. 189 190 @param post_id <b>string</b> the post_id to update. 191 */ 192 private function updatePostMeta($post_id) 193 { 194 $post_id = (integer) $post_id; 195 196 $strReq = 'SELECT meta_id, meta_type '. 197 'FROM '.$this->table.' '. 198 'WHERE post_id = '.$post_id.' '; 199 200 $rs = $this->con->select($strReq); 201 202 $meta = array(); 203 while ($rs->fetch()) { 204 $meta[$rs->meta_type][] = $rs->meta_id; 205 } 206 207 $post_meta = serialize($meta); 208 209 $cur = $this->con->openCursor($this->core->prefix.'post'); 210 $cur->post_meta = $post_meta; 211 212 $cur->update('WHERE post_id = '.$post_id); 213 $this->core->blog->triggerBlog(); 214 } 215 216 /** 217 Retrieves posts corresponding to given meta criteria. 218 <b>$params</b> is an array taking the following optional parameters: 219 - meta_id : get posts having meta id 220 - meta_type : get posts having meta type 221 222 @param params <b>array</b> Parameters 223 @param count_only <b>boolean</b> Only counts results 224 225 @return <b>record</b> the resulting posts record 226 */ 227 public function getPostsByMeta($params=array(),$count_only=false) 228 { 229 if (!isset($params['meta_id'])) { 230 return null; 231 } 232 233 $params['from'] = ', '.$this->table.' META '; 234 $params['sql'] = 'AND META.post_id = P.post_id '; 235 236 $params['sql'] .= "AND META.meta_id = '".$this->con->escape($params['meta_id'])."' "; 237 238 if (!empty($params['meta_type'])) { 239 $params['sql'] .= "AND META.meta_type = '".$this->con->escape($params['meta_type'])."' "; 240 unset($params['meta_type']); 241 } 242 243 unset($params['meta_id']); 244 245 return $this->core->blog->getPosts($params,$count_only); 246 } 247 248 /** 249 Retrieves comments to posts corresponding to given meta criteria. 250 <b>$params</b> is an array taking the following optional parameters: 251 - meta_id : get comments to posts having meta id 252 - meta_type : get comments to posts having meta type 253 254 @param params <b>array</b> Parameters 255 @param count_only <b>boolean</b> Only counts results 256 257 @return <b>record</b> the resulting comments record 258 */ 259 public function getCommentsByMeta($params=array(),$count_only=false) 260 { 261 if (!isset($params['meta_id'])) { 262 return null; 263 } 264 265 $params['from'] = ', '.$this->table.' META '; 266 $params['sql'] = 'AND META.post_id = P.post_id '; 267 $params['sql'] .= "AND META.meta_id = '".$this->con->escape($params['meta_id'])."' "; 268 269 if (!empty($params['meta_type'])) { 270 $params['sql'] .= "AND META.meta_type = '".$this->con->escape($params['meta_type'])."' "; 271 unset($params['meta_type']); 272 } 273 274 return $this->core->blog->getComments($params,$count_only); 275 } 276 277 /** 278 @deprecated since 2.2. Use getMetadata and computeMetaStats instead. 279 Generic-purpose metadata retrieval : gets metadatas according to given 280 criteria. Metadata get enriched with stastistics columns (only relevant 281 if limit parameter is not set). Metadata are sorted by post count 282 descending 283 284 @param type <b>string</b> if not null, get metas having the given type 285 @param limit <b>string</b> if not null, number of max fetched metas 286 @param meta_id <b>string</b> if not null, get metas having the given id 287 @param post_id <b>string</b> if not null, get metas for the given post id 288 289 @return <b>record</b> the meta recordset 290 */ 291 public function getMeta($type=null,$limit=null,$meta_id=null,$post_id=null) { 292 $params = array(); 293 294 if ($type != null) 295 $params['meta_type'] = $type; 296 if ($limit != null) 297 $params['limit'] = $limit; 298 if ($meta_id != null) 299 $params['meta_id'] = $meta_id; 300 if ($meta_id != null) 301 $params['post_id'] = $post_id; 302 $rs = $this->getMetadata($params, false); 303 return $this->computeMetaStats($rs); 304 } 305 306 /** 307 Generic-purpose metadata retrieval : gets metadatas according to given 308 criteria. <b>$params</b> is an array taking the following 309 optionnal parameters: 310 311 - type: get metas having the given type 312 - meta_id: if not null, get metas having the given id 313 - post_id: get metas for the given post id 314 - limit: number of max fetched metas 315 - order: results order (default : posts count DESC) 316 317 @param params <b>array</b> Parameters 318 @param count_only <b>boolean</b> Only counts results 319 320 @return <b>record</b> the resulting comments record 321 */ 322 public function getMetadata($params=array(), $count_only=false) 323 { 324 if ($count_only) { 325 $strReq = 'SELECT count(distinct M.meta_id) '; 326 } else { 327 $strReq = 'SELECT M.meta_id, M.meta_type, COUNT(M.post_id) as count '; 328 } 329 330 $strReq .= 331 'FROM '.$this->table.' M LEFT JOIN '.$this->core->prefix.'post P '. 332 'ON M.post_id = P.post_id '. 333 "WHERE P.blog_id = '".$this->con->escape($this->core->blog->id)."' "; 334 335 if (isset($params['meta_type'])) { 336 $strReq .= " AND meta_type = '".$this->con->escape($params['meta_type'])."' "; 337 } 338 339 if (isset($params['meta_id'])) { 340 $strReq .= " AND meta_id = '".$this->con->escape($params['meta_id'])."' "; 341 } 342 343 if (isset($params['post_id'])) { 344 $strReq .= ' AND P.post_id '.$this->con->in($params['post_id']).' '; 345 } 346 347 if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) { 348 $strReq .= 'AND ((post_status = 1 '; 349 350 if ($this->core->blog->without_password) { 351 $strReq .= 'AND post_password IS NULL '; 352 } 353 $strReq .= ') '; 354 355 if ($this->core->auth->userID()) { 356 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; 357 } else { 358 $strReq .= ') '; 359 } 360 } 361 362 if (!$count_only) { 363 if (!isset($params['order'])) { 364 $params['order'] = 'count DESC'; 365 } 366 367 $strReq .= 368 'GROUP BY meta_id,meta_type,P.blog_id '. 369 'ORDER BY '.$params['order']; 370 371 if (isset($params['limit'])) { 372 $strReq .= $this->con->limit($params['limit']); 373 } 374 } 375 376 $rs = $this->con->select($strReq); 377 return $rs; 378 } 379 380 /** 381 Computes statistics from a metadata recordset. 382 Each record gets enriched with lowercase name, percent and roundpercent columns 383 384 @param rs <b>record</b> recordset to enrich 385 386 @return <b>record</b> the enriched recordset 387 */ 388 public function computeMetaStats($rs) { 389 $rs_static = $rs->toStatic(); 390 391 $max = array(); 392 while ($rs_static->fetch()) 393 { 394 $type = $rs_static->meta_type; 395 if (!isset($max[$type])) { 396 $max[$type] = $rs_static->count; 397 } else { 398 if ($rs_static->count > $max[$type]) { 399 $max[$type] = $rs_static->count; 400 } 401 } 402 } 403 404 while ($rs_static->fetch()) 405 { 406 $rs_static->set('meta_id_lower',dcUtils::removeDiacritics(mb_strtolower($rs_static->meta_id))); 407 408 $count = $rs_static->count; 409 $percent = ((integer) $rs_static->count) * 100 / $max[$rs_static->meta_type]; 410 411 $rs_static->set('percent',(integer) round($percent)); 412 $rs_static->set('roundpercent',round($percent/10)*10); 413 } 414 415 return $rs_static; 416 } 417 418 /** 419 Adds a metadata to a post. 420 421 @param post_id <b>integer</b> the post id 422 @param type <b>string</b> meta type 423 @param value <b>integer</b> meta value 424 */ 425 public function setPostMeta($post_id,$type,$value) 426 { 427 $this->checkPermissionsOnPost($post_id); 428 429 $value = trim($value); 430 if ($value === false) { return; } 431 432 $cur = $this->con->openCursor($this->table); 433 434 $cur->post_id = (integer) $post_id; 435 $cur->meta_id = (string) $value; 436 $cur->meta_type = (string) $type; 437 438 $cur->insert(); 439 $this->updatePostMeta((integer) $post_id); 440 } 441 442 /** 443 Removes metadata from a post. 444 445 @param post_id <b>integer</b> the post id 446 @param type <b>string</b> meta type (if null, delete all types) 447 @param value <b>integer</b> meta value (if null, delete all values) 448 */ 449 public function delPostMeta($post_id,$type=null,$meta_id=null) 450 { 451 $post_id = (integer) $post_id; 452 453 $this->checkPermissionsOnPost($post_id); 454 455 $strReq = 'DELETE FROM '.$this->table.' '. 456 'WHERE post_id = '.$post_id; 457 458 if ($type !== null) { 459 $strReq .= " AND meta_type = '".$this->con->escape($type)."' "; 460 } 461 462 if ($meta_id !== null) { 463 $strReq .= " AND meta_id = '".$this->con->escape($meta_id)."' "; 464 } 465 466 $this->con->execute($strReq); 467 $this->updatePostMeta((integer) $post_id); 468 } 469 470 /** 471 Mass updates metadata for a given post_type. 472 473 @param meta_id <b>integer</b> old value 474 @param new_meta <b>integer</b> new value 475 @param type <b>string</b> meta type (if null, select all types) 476 @param post_type <b>integer</b> impacted post_type (if null, select all types) 477 @return <b>boolean</b> true if at least 1 post has been impacted 478 */ 479 public function updateMeta($meta_id,$new_meta_id,$type=null,$post_type=null) 480 { 481 $new_meta_id = self::sanitizeMetaID($new_meta_id); 482 483 if ($new_meta_id == $meta_id) { 484 return true; 485 } 486 487 $getReq = 'SELECT M.post_id '. 488 'FROM '.$this->table.' M, '.$this->core->prefix.'post P '. 489 'WHERE P.post_id = M.post_id '. 490 "AND P.blog_id = '".$this->con->escape($this->core->blog->id)."' ". 491 "AND meta_id = '%s' "; 492 493 if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) { 494 $getReq .= "AND P.user_id = '".$this->con->escape($this->core->auth->userID())."' "; 495 } 496 if ($post_type !== null) { 497 $getReq .= "AND P.post_type = '".$this->con->escape($post_type)."' "; 498 } 499 500 $delReq = 'DELETE FROM '.$this->table.' '. 501 'WHERE post_id IN (%s) '. 502 "AND meta_id = '%s' "; 503 504 $updReq = 'UPDATE '.$this->table.' '. 505 "SET meta_id = '%s' ". 506 'WHERE post_id IN (%s) '. 507 "AND meta_id = '%s' "; 508 509 if ($type !== null) { 510 $plus = " AND meta_type = '%s' "; 511 $getReq .= $plus; 512 $delReq .= $plus; 513 $updReq .= $plus; 514 } 515 516 $to_update = $to_remove = array(); 517 518 $rs = $this->con->select(sprintf($getReq,$this->con->escape($meta_id), 519 $this->con->escape($type))); 520 521 while ($rs->fetch()) { 522 $to_update[] = $rs->post_id; 523 } 524 525 if (empty($to_update)) { 526 return false; 527 } 528 529 $rs = $this->con->select(sprintf($getReq,$new_meta_id,$type)); 530 while ($rs->fetch()) { 531 if (in_array($rs->post_id,$to_update)) { 532 $to_remove[] = $rs->post_id; 533 unset($to_update[array_search($rs->post_id,$to_update)]); 534 } 535 } 536 537 # Delete duplicate meta 538 if (!empty($to_remove)) 539 { 540 $this->con->execute(sprintf($delReq,implode(',',$to_remove), 541 $this->con->escape($meta_id), 542 $this->con->escape($type))); 543 544 foreach ($to_remove as $post_id) { 545 $this->updatePostMeta($post_id); 546 } 547 } 548 549 # Update meta 550 if (!empty($to_update)) 551 { 552 $this->con->execute(sprintf($updReq,$this->con->escape($new_meta_id), 553 implode(',',$to_update), 554 $this->con->escape($meta_id), 555 $this->con->escape($type))); 556 557 foreach ($to_update as $post_id) { 558 $this->updatePostMeta($post_id); 559 } 560 } 561 562 return true; 563 } 564 565 /** 566 Mass delete metadata for a given post_type. 567 568 @param meta_id <b>integer</b> meta value 569 @param type <b>string</b> meta type (if null, select all types) 570 @param post_type <b>integer</b> impacted post_type (if null, select all types) 571 @return <b>Array</b> the list of impacted post_ids 572 */ 573 public function delMeta($meta_id,$type=null,$post_type=null) 574 { 575 $strReq = 'SELECT M.post_id '. 576 'FROM '.$this->table.' M, '.$this->core->prefix.'post P '. 577 'WHERE P.post_id = M.post_id '. 578 "AND P.blog_id = '".$this->con->escape($this->core->blog->id)."' ". 579 "AND meta_id = '".$this->con->escape($meta_id)."' "; 580 581 if ($type !== null) { 582 $strReq .= " AND meta_type = '".$this->con->escape($type)."' "; 583 } 584 585 if ($post_type !== null) { 586 $strReq .= " AND P.post_type = '".$this->con->escape($post_type)."' "; 587 } 588 589 $rs = $this->con->select($strReq); 590 591 if ($rs->isEmpty()) return array(); 592 593 $ids = array(); 594 while ($rs->fetch()) { 595 $ids[] = $rs->post_id; 596 } 597 598 $strReq = 'DELETE FROM '.$this->table.' '. 599 'WHERE post_id IN ('.implode(',',$ids).') '. 600 "AND meta_id = '".$this->con->escape($meta_id)."' "; 601 602 if ($type !== null) { 603 $strReq .= " AND meta_type = '".$this->con->escape($type)."' "; 604 } 605 606 $rs = $this->con->execute($strReq); 607 608 foreach ($ids as $post_id) { 609 $this->updatePostMeta($post_id); 610 } 611 612 return $ids; 613 } 23 private $core; ///< <b>dcCore</b> dcCore instance 24 private $con; ///< <b>connection</b> Database connection object 25 private $table; ///< <b>string</b> Media table name 26 27 /** 28 Object constructor. 29 30 @param core <b>dcCore</b> dcCore instance 31 */ 32 public function __construct($core) 33 { 34 $this->core = &$core; 35 $this->con = &$this->core->con; 36 $this->table = $this->core->prefix . 'meta'; 37 } 38 39 /** 40 Splits up comma-separated values into an array of 41 unique, URL-proof metadata values. 42 43 @param str <b>string</b> Comma-separated metadata. 44 45 @return <b>Array</b> The array of sanitized metadata 46 */ 47 public function splitMetaValues($str) 48 { 49 $res = array(); 50 foreach (explode(',', $str) as $i => $tag) { 51 $tag = trim($tag); 52 $tag = self::sanitizeMetaID($tag); 53 54 if ($tag != false) { 55 $res[$i] = $tag; 56 } 57 } 58 59 return array_unique($res); 60 } 61 62 /** 63 Make a metadata ID URL-proof. 64 65 @param str <b>string</b> the metadata ID. 66 67 @return <b>string</b> The sanitized metadata 68 */ 69 public static function sanitizeMetaID($str) 70 { 71 return text::tidyURL($str, false, true); 72 } 73 74 /** 75 Converts serialized metadata (for instance in dc_post post_meta) 76 into a meta array. 77 78 @param str <b>string</b> the serialized metadata. 79 80 @return <b>Array</b> the resulting array of post meta 81 */ 82 public function getMetaArray($str) 83 { 84 $meta = @unserialize($str); 85 86 if (!is_array($meta)) { 87 return array(); 88 } 89 90 return $meta; 91 } 92 93 /** 94 Converts serialized metadata (for instance in dc_post post_meta) 95 into a comma-separated meta list for a given type. 96 97 @param str <b>string</b> the serialized metadata. 98 @param type <b>string</b> meta type to retrieve metaIDs from. 99 100 @return <b>string</b> the comma-separated list of meta 101 */ 102 public function getMetaStr($str, $type) 103 { 104 $meta = $this->getMetaArray($str); 105 106 if (!isset($meta[$type])) { 107 return ''; 108 } 109 110 return implode(', ', $meta[$type]); 111 } 112 113 /** 114 Converts serialized metadata (for instance in dc_post post_meta) 115 into a "fetchable" metadata record. 116 117 @param str <b>string</b> the serialized metadata. 118 @param type <b>string</b> meta type to retrieve metaIDs from. 119 120 @return <b>record</b> the meta recordset 121 */ 122 public function getMetaRecordset($str, $type) 123 { 124 $meta = $this->getMetaArray($str); 125 $data = array(); 126 127 if (isset($meta[$type])) { 128 foreach ($meta[$type] as $v) { 129 $data[] = array( 130 'meta_id' => $v, 131 'meta_type' => $type, 132 'meta_id_lower' => mb_strtolower($v), 133 'count' => 0, 134 'percent' => 0, 135 'roundpercent' => 0 136 ); 137 } 138 } 139 140 return staticRecord::newFromArray($data); 141 } 142 143 /** 144 @deprecated since version 2.2 : $core->meta is always defined 145 @see getMetaRecordset 146 static version of getMetaRecordset 147 */ 148 public static function getMetaRecord($core, $str, $type) 149 { 150 $meta = new self($core); 151 return $meta->getMetaRecordset($str, $type); 152 } 153 154 /** 155 Checks whether the current user is allowed to change post meta 156 An exception is thrown if user is not allowed. 157 158 @param post_id <b>string</b> the post_id to check. 159 */ 160 private function checkPermissionsOnPost($post_id) 161 { 162 $post_id = (integer) $post_id; 163 164 if (!$this->core->auth->check('usage,contentadmin', $this->core->blog->id)) { 165 throw new Exception(__('You are not allowed to change this entry status')); 166 } 167 168 #�If user can only publish, we need to check the post's owner 169 if (!$this->core->auth->check('contentadmin', $this->core->blog->id)) { 170 $strReq = 'SELECT post_id ' . 171 'FROM ' . $this->core->prefix . 'post ' . 172 'WHERE post_id = ' . $post_id . ' ' . 173 "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 174 175 $rs = $this->con->select($strReq); 176 177 if ($rs->isEmpty()) { 178 throw new Exception(__('You are not allowed to change this entry status')); 179 } 180 } 181 } 182 183 /** 184 Updates serialized post_meta information with dc_meta table information. 185 186 @param post_id <b>string</b> the post_id to update. 187 */ 188 private function updatePostMeta($post_id) 189 { 190 $post_id = (integer) $post_id; 191 192 $strReq = 'SELECT meta_id, meta_type ' . 193 'FROM ' . $this->table . ' ' . 194 'WHERE post_id = ' . $post_id . ' '; 195 196 $rs = $this->con->select($strReq); 197 198 $meta = array(); 199 while ($rs->fetch()) { 200 $meta[$rs->meta_type][] = $rs->meta_id; 201 } 202 203 $post_meta = serialize($meta); 204 205 $cur = $this->con->openCursor($this->core->prefix . 'post'); 206 $cur->post_meta = $post_meta; 207 208 $cur->update('WHERE post_id = ' . $post_id); 209 $this->core->blog->triggerBlog(); 210 } 211 212 /** 213 Retrieves posts corresponding to given meta criteria. 214 <b>$params</b> is an array taking the following optional parameters: 215 - meta_id : get posts having meta id 216 - meta_type : get posts having meta type 217 218 @param params <b>array</b> Parameters 219 @param count_only <b>boolean</b> Only counts results 220 221 @return <b>record</b> the resulting posts record 222 */ 223 public function getPostsByMeta($params = array(), $count_only = false) 224 { 225 if (!isset($params['meta_id'])) { 226 return; 227 } 228 229 $params['from'] = ', ' . $this->table . ' META '; 230 $params['sql'] = 'AND META.post_id = P.post_id '; 231 232 $params['sql'] .= "AND META.meta_id = '" . $this->con->escape($params['meta_id']) . "' "; 233 234 if (!empty($params['meta_type'])) { 235 $params['sql'] .= "AND META.meta_type = '" . $this->con->escape($params['meta_type']) . "' "; 236 unset($params['meta_type']); 237 } 238 239 unset($params['meta_id']); 240 241 return $this->core->blog->getPosts($params, $count_only); 242 } 243 244 /** 245 Retrieves comments to posts corresponding to given meta criteria. 246 <b>$params</b> is an array taking the following optional parameters: 247 - meta_id : get comments to posts having meta id 248 - meta_type : get comments to posts having meta type 249 250 @param params <b>array</b> Parameters 251 @param count_only <b>boolean</b> Only counts results 252 253 @return <b>record</b> the resulting comments record 254 */ 255 public function getCommentsByMeta($params = array(), $count_only = false) 256 { 257 if (!isset($params['meta_id'])) { 258 return; 259 } 260 261 $params['from'] = ', ' . $this->table . ' META '; 262 $params['sql'] = 'AND META.post_id = P.post_id '; 263 $params['sql'] .= "AND META.meta_id = '" . $this->con->escape($params['meta_id']) . "' "; 264 265 if (!empty($params['meta_type'])) { 266 $params['sql'] .= "AND META.meta_type = '" . $this->con->escape($params['meta_type']) . "' "; 267 unset($params['meta_type']); 268 } 269 270 return $this->core->blog->getComments($params, $count_only); 271 } 272 273 /** 274 @deprecated since 2.2. Use getMetadata and computeMetaStats instead. 275 Generic-purpose metadata retrieval : gets metadatas according to given 276 criteria. Metadata get enriched with stastistics columns (only relevant 277 if limit parameter is not set). Metadata are sorted by post count 278 descending 279 280 @param type <b>string</b> if not null, get metas having the given type 281 @param limit <b>string</b> if not null, number of max fetched metas 282 @param meta_id <b>string</b> if not null, get metas having the given id 283 @param post_id <b>string</b> if not null, get metas for the given post id 284 285 @return <b>record</b> the meta recordset 286 */ 287 public function getMeta($type = null, $limit = null, $meta_id = null, $post_id = null) 288 { 289 $params = array(); 290 291 if ($type != null) { 292 $params['meta_type'] = $type; 293 } 294 295 if ($limit != null) { 296 $params['limit'] = $limit; 297 } 298 299 if ($meta_id != null) { 300 $params['meta_id'] = $meta_id; 301 } 302 303 if ($meta_id != null) { 304 $params['post_id'] = $post_id; 305 } 306 307 $rs = $this->getMetadata($params, false); 308 return $this->computeMetaStats($rs); 309 } 310 311 /** 312 Generic-purpose metadata retrieval : gets metadatas according to given 313 criteria. <b>$params</b> is an array taking the following 314 optionnal parameters: 315 316 - type: get metas having the given type 317 - meta_id: if not null, get metas having the given id 318 - post_id: get metas for the given post id 319 - limit: number of max fetched metas 320 - order: results order (default : posts count DESC) 321 322 @param params <b>array</b> Parameters 323 @param count_only <b>boolean</b> Only counts results 324 325 @return <b>record</b> the resulting comments record 326 */ 327 public function getMetadata($params = array(), $count_only = false) 328 { 329 if ($count_only) { 330 $strReq = 'SELECT count(distinct M.meta_id) '; 331 } else { 332 $strReq = 'SELECT M.meta_id, M.meta_type, COUNT(M.post_id) as count '; 333 } 334 335 $strReq .= 336 'FROM ' . $this->table . ' M LEFT JOIN ' . $this->core->prefix . 'post P ' . 337 'ON M.post_id = P.post_id ' . 338 "WHERE P.blog_id = '" . $this->con->escape($this->core->blog->id) . "' "; 339 340 if (isset($params['meta_type'])) { 341 $strReq .= " AND meta_type = '" . $this->con->escape($params['meta_type']) . "' "; 342 } 343 344 if (isset($params['meta_id'])) { 345 $strReq .= " AND meta_id = '" . $this->con->escape($params['meta_id']) . "' "; 346 } 347 348 if (isset($params['post_id'])) { 349 $strReq .= ' AND P.post_id ' . $this->con->in($params['post_id']) . ' '; 350 } 351 352 if (!$this->core->auth->check('contentadmin', $this->core->blog->id)) { 353 $strReq .= 'AND ((post_status = 1 '; 354 355 if ($this->core->blog->without_password) { 356 $strReq .= 'AND post_password IS NULL '; 357 } 358 $strReq .= ') '; 359 360 if ($this->core->auth->userID()) { 361 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 362 } else { 363 $strReq .= ') '; 364 } 365 } 366 367 if (!$count_only) { 368 if (!isset($params['order'])) { 369 $params['order'] = 'count DESC'; 370 } 371 372 $strReq .= 373 'GROUP BY meta_id,meta_type,P.blog_id ' . 374 'ORDER BY ' . $params['order']; 375 376 if (isset($params['limit'])) { 377 $strReq .= $this->con->limit($params['limit']); 378 } 379 } 380 381 $rs = $this->con->select($strReq); 382 return $rs; 383 } 384 385 /** 386 Computes statistics from a metadata recordset. 387 Each record gets enriched with lowercase name, percent and roundpercent columns 388 389 @param rs <b>record</b> recordset to enrich 390 391 @return <b>record</b> the enriched recordset 392 */ 393 public function computeMetaStats($rs) 394 { 395 $rs_static = $rs->toStatic(); 396 397 $max = array(); 398 while ($rs_static->fetch()) { 399 $type = $rs_static->meta_type; 400 if (!isset($max[$type])) { 401 $max[$type] = $rs_static->count; 402 } else { 403 if ($rs_static->count > $max[$type]) { 404 $max[$type] = $rs_static->count; 405 } 406 } 407 } 408 409 while ($rs_static->fetch()) { 410 $rs_static->set('meta_id_lower', dcUtils::removeDiacritics(mb_strtolower($rs_static->meta_id))); 411 412 $count = $rs_static->count; 413 $percent = ((integer) $rs_static->count) * 100 / $max[$rs_static->meta_type]; 414 415 $rs_static->set('percent', (integer) round($percent)); 416 $rs_static->set('roundpercent', round($percent / 10) * 10); 417 } 418 419 return $rs_static; 420 } 421 422 /** 423 Adds a metadata to a post. 424 425 @param post_id <b>integer</b> the post id 426 @param type <b>string</b> meta type 427 @param value <b>integer</b> meta value 428 */ 429 public function setPostMeta($post_id, $type, $value) 430 { 431 $this->checkPermissionsOnPost($post_id); 432 433 $value = trim($value); 434 if ($value === false) {return;} 435 436 $cur = $this->con->openCursor($this->table); 437 438 $cur->post_id = (integer) $post_id; 439 $cur->meta_id = (string) $value; 440 $cur->meta_type = (string) $type; 441 442 $cur->insert(); 443 $this->updatePostMeta((integer) $post_id); 444 } 445 446 /** 447 Removes metadata from a post. 448 449 @param post_id <b>integer</b> the post id 450 @param type <b>string</b> meta type (if null, delete all types) 451 @param value <b>integer</b> meta value (if null, delete all values) 452 */ 453 public function delPostMeta($post_id, $type = null, $meta_id = null) 454 { 455 $post_id = (integer) $post_id; 456 457 $this->checkPermissionsOnPost($post_id); 458 459 $strReq = 'DELETE FROM ' . $this->table . ' ' . 460 'WHERE post_id = ' . $post_id; 461 462 if ($type !== null) { 463 $strReq .= " AND meta_type = '" . $this->con->escape($type) . "' "; 464 } 465 466 if ($meta_id !== null) { 467 $strReq .= " AND meta_id = '" . $this->con->escape($meta_id) . "' "; 468 } 469 470 $this->con->execute($strReq); 471 $this->updatePostMeta((integer) $post_id); 472 } 473 474 /** 475 Mass updates metadata for a given post_type. 476 477 @param meta_id <b>integer</b> old value 478 @param new_meta <b>integer</b> new value 479 @param type <b>string</b> meta type (if null, select all types) 480 @param post_type <b>integer</b> impacted post_type (if null, select all types) 481 @return <b>boolean</b> true if at least 1 post has been impacted 482 */ 483 public function updateMeta($meta_id, $new_meta_id, $type = null, $post_type = null) 484 { 485 $new_meta_id = self::sanitizeMetaID($new_meta_id); 486 487 if ($new_meta_id == $meta_id) { 488 return true; 489 } 490 491 $getReq = 'SELECT M.post_id ' . 492 'FROM ' . $this->table . ' M, ' . $this->core->prefix . 'post P ' . 493 'WHERE P.post_id = M.post_id ' . 494 "AND P.blog_id = '" . $this->con->escape($this->core->blog->id) . "' " . 495 "AND meta_id = '%s' "; 496 497 if (!$this->core->auth->check('contentadmin', $this->core->blog->id)) { 498 $getReq .= "AND P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 499 } 500 if ($post_type !== null) { 501 $getReq .= "AND P.post_type = '" . $this->con->escape($post_type) . "' "; 502 } 503 504 $delReq = 'DELETE FROM ' . $this->table . ' ' . 505 'WHERE post_id IN (%s) ' . 506 "AND meta_id = '%s' "; 507 508 $updReq = 'UPDATE ' . $this->table . ' ' . 509 "SET meta_id = '%s' " . 510 'WHERE post_id IN (%s) ' . 511 "AND meta_id = '%s' "; 512 513 if ($type !== null) { 514 $plus = " AND meta_type = '%s' "; 515 $getReq .= $plus; 516 $delReq .= $plus; 517 $updReq .= $plus; 518 } 519 520 $to_update = $to_remove = array(); 521 522 $rs = $this->con->select(sprintf($getReq, $this->con->escape($meta_id), 523 $this->con->escape($type))); 524 525 while ($rs->fetch()) { 526 $to_update[] = $rs->post_id; 527 } 528 529 if (empty($to_update)) { 530 return false; 531 } 532 533 $rs = $this->con->select(sprintf($getReq, $new_meta_id, $type)); 534 while ($rs->fetch()) { 535 if (in_array($rs->post_id, $to_update)) { 536 $to_remove[] = $rs->post_id; 537 unset($to_update[array_search($rs->post_id, $to_update)]); 538 } 539 } 540 541 # Delete duplicate meta 542 if (!empty($to_remove)) { 543 $this->con->execute(sprintf($delReq, implode(',', $to_remove), 544 $this->con->escape($meta_id), 545 $this->con->escape($type))); 546 547 foreach ($to_remove as $post_id) { 548 $this->updatePostMeta($post_id); 549 } 550 } 551 552 # Update meta 553 if (!empty($to_update)) { 554 $this->con->execute(sprintf($updReq, $this->con->escape($new_meta_id), 555 implode(',', $to_update), 556 $this->con->escape($meta_id), 557 $this->con->escape($type))); 558 559 foreach ($to_update as $post_id) { 560 $this->updatePostMeta($post_id); 561 } 562 } 563 564 return true; 565 } 566 567 /** 568 Mass delete metadata for a given post_type. 569 570 @param meta_id <b>integer</b> meta value 571 @param type <b>string</b> meta type (if null, select all types) 572 @param post_type <b>integer</b> impacted post_type (if null, select all types) 573 @return <b>Array</b> the list of impacted post_ids 574 */ 575 public function delMeta($meta_id, $type = null, $post_type = null) 576 { 577 $strReq = 'SELECT M.post_id ' . 578 'FROM ' . $this->table . ' M, ' . $this->core->prefix . 'post P ' . 579 'WHERE P.post_id = M.post_id ' . 580 "AND P.blog_id = '" . $this->con->escape($this->core->blog->id) . "' " . 581 "AND meta_id = '" . $this->con->escape($meta_id) . "' "; 582 583 if ($type !== null) { 584 $strReq .= " AND meta_type = '" . $this->con->escape($type) . "' "; 585 } 586 587 if ($post_type !== null) { 588 $strReq .= " AND P.post_type = '" . $this->con->escape($post_type) . "' "; 589 } 590 591 $rs = $this->con->select($strReq); 592 593 if ($rs->isEmpty()) { 594 return array(); 595 } 596 597 $ids = array(); 598 while ($rs->fetch()) { 599 $ids[] = $rs->post_id; 600 } 601 602 $strReq = 'DELETE FROM ' . $this->table . ' ' . 603 'WHERE post_id IN (' . implode(',', $ids) . ') ' . 604 "AND meta_id = '" . $this->con->escape($meta_id) . "' "; 605 606 if ($type !== null) { 607 $strReq .= " AND meta_type = '" . $this->con->escape($type) . "' "; 608 } 609 610 $rs = $this->con->execute($strReq); 611 612 foreach ($ids as $post_id) { 613 $this->updatePostMeta($post_id); 614 } 615 616 return $ids; 617 } 614 618 }
Note: See TracChangeset
for help on using the changeset viewer.