3 * Data class for homepage blacklisting
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2009, StatusNet, Inc.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
37 * Data class for Homepage blacklist
41 * @author Evan Prodromou <evan@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43 * @link http://status.net/
48 class Homepage_blacklist extends Memcached_DataObject
50 public $__table = 'homepage_blacklist'; // table name
51 public $pattern; // string pattern
52 public $created; // datetime
55 * Get an instance by key
57 * This is a utility method to get a single instance with a given key value.
59 * @param string $k Key to use to lookup (usually 'user_id' for this class)
60 * @param mixed $v Value to lookup
62 * @return Homepage_blacklist object found, or null for no hits
66 function staticGet($k, $v=null)
68 return Memcached_DataObject::staticGet('Homepage_blacklist', $k, $v);
72 * return table definition for DB_DataObject
74 * DB_DataObject needs to know something about the table to manipulate
75 * instances. This method provides all the DB_DataObject needs to know.
77 * @return array array of column definitions
82 return array('pattern' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
83 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
87 * return key definitions for DB_DataObject
89 * DB_DataObject needs to know about keys that the table has; this function
92 * @return array key definitions
97 return array_keys($this->keyTypes());
101 * return key definitions for Memcached_DataObject
103 * Our caching system uses the same key definitions, but uses a different
104 * method to get them.
106 * @return array key definitions
111 return array('pattern' => 'K');
115 * Return a list of patterns to check
117 * @return array string patterns to check
120 static function getPatterns()
122 $patterns = self::cacheGet('homepage_blacklist:patterns');
124 if ($patterns === false) {
128 $nb = new Homepage_blacklist();
132 while ($nb->fetch()) {
133 $patterns[] = $nb->pattern;
136 self::cacheSet('homepage_blacklist:patterns', $patterns);
143 * Save new list of patterns
145 * @return array of patterns to check
148 static function saveNew($newPatterns)
150 $oldPatterns = self::getPatterns();
152 // Delete stuff that's old that not in new
154 $toDelete = array_diff($oldPatterns, $newPatterns);
156 // Insert stuff that's in new and not in old
158 $toInsert = array_diff($newPatterns, $oldPatterns);
160 foreach ($toDelete as $pattern) {
161 $nb = Homepage_blacklist::staticGet('pattern', $pattern);
167 foreach ($toInsert as $pattern) {
168 $nb = new Homepage_blacklist();
169 $nb->pattern = $pattern;
170 $nb->created = common_sql_now();
174 self::blow('homepage_blacklist:patterns');
177 static function ensurePattern($pattern)
179 $hb = Homepage_blacklist::staticGet('pattern', $pattern);
182 $hb = new Homepage_blacklist();
183 $hb->pattern = $pattern;
184 $hb->created = common_sql_now();
186 self::blow('homepage_blacklist:patterns');