]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/Homepage_blacklist.php
99a6786de42ebd84789ee0bfcb9812e02afe50be
[quix0rs-gnu-social.git] / plugins / Blacklist / Homepage_blacklist.php
1 <?php
2 /**
3  * Data class for homepage blacklisting
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
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.
20  *
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.
25  *
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/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for Homepage blacklist
38  *
39  * @category Action
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  *
45  * @see      DB_DataObject
46  */
47 class Homepage_blacklist extends Managed_DataObject
48 {
49     public $__table = 'homepage_blacklist'; // table name
50     public $pattern;                        // string pattern
51     public $created;                        // datetime
52
53     /**
54      * return table definition for DB_DataObject
55      *
56      * DB_DataObject needs to know something about the table to manipulate
57      * instances. This method provides all the DB_DataObject needs to know.
58      *
59      * @return array array of column definitions
60      */
61     function table()
62     {
63         return array('pattern' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
64                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
65     }
66
67     /**
68      * return key definitions for DB_DataObject
69      *
70      * DB_DataObject needs to know about keys that the table has; this function
71      * defines them.
72      *
73      * @return array key definitions
74      */
75     function keys()
76     {
77         return array_keys($this->keyTypes());
78     }
79
80     /**
81      * return key definitions for Memcached_DataObject
82      *
83      * Our caching system uses the same key definitions, but uses a different
84      * method to get them.
85      *
86      * @return array key definitions
87      */
88     function keyTypes()
89     {
90         return array('pattern' => 'K');
91     }
92
93     /**
94      * Return a list of patterns to check
95      *
96      * @return array string patterns to check
97      */
98     static function getPatterns()
99     {
100         $patterns = self::cacheGet('homepage_blacklist:patterns');
101
102         if ($patterns === false) {
103
104             $patterns = array();
105
106             $nb = new Homepage_blacklist();
107
108             $nb->find();
109
110             while ($nb->fetch()) {
111                 $patterns[] = $nb->pattern;
112             }
113
114             self::cacheSet('homepage_blacklist:patterns', $patterns);
115         }
116
117         return $patterns;
118     }
119
120     /**
121      * Save new list of patterns
122      *
123      * @return array of patterns to check
124      */
125     static function saveNew($newPatterns)
126     {
127         $oldPatterns = self::getPatterns();
128
129         // Delete stuff that's old that not in new
130         $toDelete = array_diff($oldPatterns, $newPatterns);
131
132         // Insert stuff that's in new and not in old
133         $toInsert = array_diff($newPatterns, $oldPatterns);
134
135         foreach ($toDelete as $pattern) {
136             $nb = Homepage_blacklist::staticGet('pattern', $pattern);
137             if (!empty($nb)) {
138                 $nb->delete();
139             }
140         }
141
142         foreach ($toInsert as $pattern) {
143             $nb = new Homepage_blacklist();
144             $nb->pattern = $pattern;
145             $nb->created = common_sql_now();
146             $nb->insert();
147         }
148
149         self::blow('homepage_blacklist:patterns');
150     }
151
152     static function ensurePattern($pattern)
153     {
154         $hb = Homepage_blacklist::staticGet('pattern', $pattern);
155
156         if (empty($nb)) {
157             $hb = new Homepage_blacklist();
158             $hb->pattern = $pattern;
159             $hb->created = common_sql_now();
160             $hb->insert();
161             self::blow('homepage_blacklist:patterns');
162         }
163     }
164 }