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/
47 class Homepage_blacklist extends Memcached_DataObject
49 public $__table = 'homepage_blacklist'; // table name
50 public $pattern; // string pattern
51 public $created; // datetime
54 * Get an instance by key
56 * This is a utility method to get a single instance with a given key value.
58 * @param string $k Key to use to lookup (usually 'user_id' for this class)
59 * @param mixed $v Value to lookup
61 * @return Homepage_blacklist object found, or null for no hits
64 function staticGet($k, $v=null)
66 return Memcached_DataObject::staticGet('Homepage_blacklist', $k, $v);
70 * return table definition for DB_DataObject
72 * DB_DataObject needs to know something about the table to manipulate
73 * instances. This method provides all the DB_DataObject needs to know.
75 * @return array array of column definitions
79 return array('pattern' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
80 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
84 * return key definitions for DB_DataObject
86 * DB_DataObject needs to know about keys that the table has; this function
89 * @return array key definitions
93 return array_keys($this->keyTypes());
97 * return key definitions for Memcached_DataObject
99 * Our caching system uses the same key definitions, but uses a different
100 * method to get them.
102 * @return array key definitions
106 return array('pattern' => 'K');
110 * Return a list of patterns to check
112 * @return array string patterns to check
114 static function getPatterns()
116 $patterns = self::cacheGet('homepage_blacklist:patterns');
118 if ($patterns === false) {
122 $nb = new Homepage_blacklist();
126 while ($nb->fetch()) {
127 $patterns[] = $nb->pattern;
130 self::cacheSet('homepage_blacklist:patterns', $patterns);
137 * Save new list of patterns
139 * @return array of patterns to check
141 static function saveNew($newPatterns)
143 $oldPatterns = self::getPatterns();
145 // Delete stuff that's old that not in new
146 $toDelete = array_diff($oldPatterns, $newPatterns);
148 // Insert stuff that's in new and not in old
149 $toInsert = array_diff($newPatterns, $oldPatterns);
151 foreach ($toDelete as $pattern) {
152 $nb = Homepage_blacklist::staticGet('pattern', $pattern);
158 foreach ($toInsert as $pattern) {
159 $nb = new Homepage_blacklist();
160 $nb->pattern = $pattern;
161 $nb->created = common_sql_now();
165 self::blow('homepage_blacklist:patterns');
168 static function ensurePattern($pattern)
170 $hb = Homepage_blacklist::staticGet('pattern', $pattern);
173 $hb = new Homepage_blacklist();
174 $hb->pattern = $pattern;
175 $hb->created = common_sql_now();
177 self::blow('homepage_blacklist:patterns');