mime.types: allow bzip2 upload
[lhc/web/wiklou.git] / includes / cache / bloom / BloomFilters.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Aaron Schulz
20 */
21
22 /**
23 * @since 1.24
24 */
25 class BloomFilterTitleHasLogs {
26 public static function mergeAndCheck(
27 BloomCache $bcache, $domain, $virtualKey, array $status
28 ) {
29 $ttr = 5; // try to refresh before this many seconds
30 $age = microtime( true ) - $status['asOfTime']; // seconds
31 $chance = min( 1, $age / $ttr );
32
33 $scopedLock = ( mt_rand( 1, 1e9 ) <= 1e9 * $chance )
34 ? $bcache->getScopedLock( $virtualKey )
35 : false;
36
37 if ( $scopedLock ) {
38 $updates = self::merge( $bcache, $domain, $virtualKey, $status );
39 if ( isset( $updates['asOfTime'] ) ) {
40 $age = ( microtime( true ) - $updates['asOfTime'] );
41 }
42 }
43
44 return ( $age < 30 );
45 }
46
47 public static function merge(
48 BloomCache $bcache, $domain, $virtualKey, array $status
49 ) {
50 $limit = 1000;
51 $dbr = wfGetDB( DB_SLAVE, array(), $domain );
52 $res = $dbr->select( 'logging',
53 array( 'log_namespace', 'log_title', 'log_id', 'log_timestamp' ),
54 array( 'log_id > ' . $dbr->addQuotes( (int)$status['lastID'] ) ),
55 __METHOD__,
56 array( 'ORDER BY' => 'log_id', 'LIMIT' => $limit )
57 );
58
59 $updates = array();
60 if ( $res->numRows() > 0 ) {
61 $members = array();
62 foreach ( $res as $row ) {
63 $members[] = "$virtualKey:{$row->log_namespace}:{$row->log_title}";
64 }
65 $lastID = $row->log_id;
66 $lastTime = $row->log_timestamp;
67 if ( !$bcache->add( 'shared', $members ) ) {
68 return false;
69 }
70 $updates['lastID'] = $lastID;
71 $updates['asOfTime'] = wfTimestamp( TS_UNIX, $lastTime );
72 } else {
73 $updates['asOfTime'] = microtime( true );
74 }
75
76 $updates['epoch'] = $status['epoch'] ?: microtime( true );
77
78 $bcache->setStatus( $virtualKey, $updates );
79
80 return $updates;
81 }
82 }