1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Kernel\Model;
13:
14: use Xoops\Core\Kernel\XoopsModelAbstract;
15:
16: /**
17: * Object synchronization handler class.
18: *
19: * Usage of methods provided by XoopsModelSync:
20: *
21: * Step #1: set linked table and joint fields through XoopsPersistableObjectHandler:
22: * $handler->table_link = $handler->db2->prefix("the_linked_table");
23: * full name of the linked table that is used for the query
24: * $handler->field_link = "the_linked_field";
25: * name of field in linked table that will be used to link
26: * the linked table with current table
27: * $handler->field_object = "the_object_field";
28: * name of field in current table that will be used to link
29: * the linked table with current table; linked field name will
30: * be used if the field name is not set
31: * Step #2: perform query
32: *
33: * @category Xoops\Core\Kernel\Model\Sync
34: * @package Xoops\Core\Kernel
35: * @author Taiwen Jiang <phppp@users.sourceforge.net>
36: * @copyright 2000-2015 XOOPS Project (http://xoops.org)
37: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
38: * @link http://xoops.org
39: * @since 2.3.0
40: */
41: class Sync extends XoopsModelAbstract
42: {
43: /**
44: * Clean orphan objects in this handler (child table) that are not in parent table
45: *
46: * The parameters can be defined in the handler. Naming should be updated to reflect
47: * standard relational terminology.
48: *
49: * @param string $table_link parent table
50: * @param string $field_link primary key (parent table)
51: * @param string $field_object foreign key (child table)
52: *
53: * @return bool true on success
54: */
55: public function cleanOrphan($table_link = '', $field_link = '', $field_object = '')
56: {
57: if (!empty($table_link)) {
58: $this->handler->table_link = $table_link;
59: }
60: if (!empty($field_link)) {
61: $this->handler->field_link = $field_link;
62: }
63: if (!empty($field_object)) {
64: $this->handler->field_object = $field_object;
65: }
66:
67: if (empty($this->handler->field_object)
68: || empty($this->handler->table_link)
69: || empty($this->handler->field_link)
70: ) {
71: trigger_error(
72: "The link information is not set for '" . get_class($this->handler) . "' yet.",
73: E_USER_WARNING
74: );
75: return false;
76: }
77:
78: // there were two versions of this sql, first for mysql 4.1+ and
79: // the second for earlier versions.
80: // TODO: Need to analyse and find the most portable query to use here
81: /*
82: $sql = "DELETE FROM `{$this->handler->table}`"
83: . " WHERE (`{$this->handler->field_object}` NOT IN "
84: . "( SELECT DISTINCT `{$this->handler->field_link}` FROM `{$this->handler->table_link}`) )";
85: */
86: $sql = "DELETE `{$this->handler->table}` FROM `{$this->handler->table}`"
87: . " LEFT JOIN `{$this->handler->table_link}` AS aa "
88: . " ON `{$this->handler->table}`.`{$this->handler->field_object}` = aa.`{$this->handler->field_link}`"
89: . " WHERE (aa.`{$this->handler->field_link}` IS NULL)";
90:
91: return $this->handler->db2->executeUpdate($sql);
92: }
93: }
94: