]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/Homepage_blacklist.php
fb2712f5658189c7e25eaff70ed37bfaae0ab461
[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;                        // varchar(255) pattern
51     public $created;                        // datetime not_null
52     public $modified;                       // timestamp()   not_null default_CURRENT_TIMESTAMP
53
54     public static function schemaDef()
55     {
56         return array(
57             'fields' => array(
58                 'pattern' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'blacklist pattern'),
59                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
60                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
61             ),
62             'primary key' => array('pattern'),
63         );
64     }
65
66     /**
67      * Return a list of patterns to check
68      *
69      * @return array string patterns to check
70      */
71     static function getPatterns()
72     {
73         $patterns = self::cacheGet('homepage_blacklist:patterns');
74
75         if ($patterns === false) {
76
77             $patterns = array();
78
79             $nb = new Homepage_blacklist();
80
81             $nb->find();
82
83             while ($nb->fetch()) {
84                 $patterns[] = $nb->pattern;
85             }
86
87             self::cacheSet('homepage_blacklist:patterns', $patterns);
88         }
89
90         return $patterns;
91     }
92
93     /**
94      * Save new list of patterns
95      *
96      * @return array of patterns to check
97      */
98     static function saveNew($newPatterns)
99     {
100         $oldPatterns = self::getPatterns();
101
102         // Delete stuff that's old that not in new
103         $toDelete = array_diff($oldPatterns, $newPatterns);
104
105         // Insert stuff that's in new and not in old
106         $toInsert = array_diff($newPatterns, $oldPatterns);
107
108         foreach ($toDelete as $pattern) {
109             $nb = Homepage_blacklist::getKV('pattern', $pattern);
110             if (!empty($nb)) {
111                 $nb->delete();
112             }
113         }
114
115         foreach ($toInsert as $pattern) {
116             $nb = new Homepage_blacklist();
117             $nb->pattern = $pattern;
118             $nb->created = common_sql_now();
119             $nb->insert();
120         }
121
122         self::blow('homepage_blacklist:patterns');
123     }
124
125     static function ensurePattern($pattern)
126     {
127         $hb = Homepage_blacklist::getKV('pattern', $pattern);
128
129         if (empty($nb)) {
130             $hb = new Homepage_blacklist();
131             $hb->pattern = $pattern;
132             $hb->created = common_sql_now();
133             $hb->insert();
134             self::blow('homepage_blacklist:patterns');
135         }
136     }
137 }