-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuijiFunctions.php
450 lines (446 loc) · 13.1 KB
/
HuijiFunctions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
Class HuijiFunctions {
/* atomic lock with memecached */
static function addLock( $lockKey, $timeout = 0, $lockExpireTime = 120 ){
$startTime = microtime(TRUE);
$cache = wfGetCache( CACHE_ANYTHING );
$key = wfMemcKey( 'huijimiddleware', 'huijifunctions', 'addLock', $lockKey);
while(!$cache->add( $key, 1, $lockExpireTime )){
$now = microtime(TRUE);
if ( ($now - $startTime) >= $timeout ){
return false;
}
usleep(1000);
}
return true;
}
static function releaseLock($lockKey){
$cache = wfGetCache( CACHE_ANYTHING );
$key = wfMemcKey( 'huijimiddleware', 'huijifunctions', 'addLock', $lockKey);
return $cache->delete($key);
}
/**
* The following four functions are borrowed
* from includes/wikia/GlobalFunctionsNY.php
*/
static function dateDiff( $date1, $date2 ) {
$dtDiff = $date1 - $date2;
$totalDays = intval( $dtDiff / ( 24 * 60 * 60 ) );
$totalSecs = $dtDiff - ( $totalDays * 24 * 60 * 60 );
$dif['mo'] = intval( $totalDays / 30 );
$dif['d'] = $totalDays;
$dif['h'] = $h = intval( $totalSecs / ( 60 * 60 ) );
$dif['m'] = $m = intval( ( $totalSecs - ( $h * 60 * 60 ) ) / 60 );
$dif['s'] = $totalSecs - ( $h * 60 * 60 ) - ( $m * 60 );
return $dif;
}
static function getTimeOffset( $time, $timeabrv, $timename ) {
global $wgUser;
$timeStr = ''; // misza: initialize variables, DUMB FUCKS!
if( $time[$timeabrv] > 0 ) {
// Give grep a chance to find the usages:
// comments-time-days, comments-time-hours, comments-time-minutes, comments-time-seconds, comments-time-months
if ($timeabrv == 's') {
$timeStr = '一个普朗克时间';
}else{
$timeStr = wfMessage( "comments-time-{$timename}", $time[$timeabrv] )->text();
}
}
// if( $timeStr ) {
// $timeStr .= ' ';
// }
return $timeStr;
}
/**
* get time ago
* @param int $time timestamp
* @return string
*/
static function getTimeAgo( $time ) {
$timeArray = self::dateDiff( time(), $time );
$timeStr = '';
$timeStrMo = self::getTimeOffset( $timeArray, 'mo', 'months' );
$timeStrD = self::getTimeOffset( $timeArray, 'd', 'days' );
$timeStrH = self::getTimeOffset( $timeArray, 'h', 'hours' );
$timeStrM = self::getTimeOffset( $timeArray, 'm', 'minutes' );
$timeStrS = self::getTimeOffset( $timeArray, 's', 'seconds' );
if ( $timeStrMo ) {
$timeStr = $timeStrMo;
} else {
$timeStr = $timeStrD;
if( $timeStr < 2 ) {
$timeStr .= $timeStrH;
$timeStr .= $timeStrM;
if( !$timeStr ) {
$timeStr .= $timeStrS;
}
}
}
if( !$timeStr ) {
$timeStr = '一个普朗克时间';
}
return $timeStr;
}
/**
* Makes sure that link text is not too long by changing too long links to
* <a href=#>http://www.abc....xyz.html</a>
*
* @param $matches Array
* @return String: shortened URL
*/
public static function cutCommentLinkText( $matches ) {
$tagOpen = $matches[1];
$linkText = $matches[2];
$tagClose = $matches[3];
$image = preg_match( "/<img src=/i", $linkText );
$isURL = ( preg_match( '%^(?:http|https|ftp)://(?:www\.)?.*$%i', $linkText ) ? true : false );
if( $isURL && !$image && strlen( $linkText ) > 30 ) {
$start = substr( $linkText, 0, ( 30 / 2 ) - 3 );
$end = substr( $linkText, strlen( $linkText ) - ( 30 / 2 ) + 3, ( 30 / 2 ) - 3 );
$linkText = trim( $start ) . wfMessage( 'ellipsis' )->plain() . trim( $end );
}
return $tagOpen . $linkText . $tagClose;
}
/**
* Simple spam check -- checks the supplied text against MediaWiki's
* built-in regex-based spam filters
*
* @param $text String: text to check for spam patterns
* @return Boolean: true if it contains spam, otherwise false
*/
public static function isSpam( $text ) {
global $wgSpamRegex, $wgSummarySpamRegex;
$retVal = false;
// Allow to hook other anti-spam extensions so that sites that use,
// for example, AbuseFilter, Phalanx or SpamBlacklist can add additional
// checks
wfRunHooks( 'Comments::isSpam', array( &$text, &$retVal ) );
if ( $retVal ) {
// Should only be true here...
return $retVal;
}
// Run text through $wgSpamRegex (and $wgSummarySpamRegex if it has been specified)
if ( $wgSpamRegex && preg_match( $wgSpamRegex, $text ) ) {
return true;
}
if ( $wgSummarySpamRegex && is_array( $wgSummarySpamRegex ) ) {
foreach ( $wgSummarySpamRegex as $spamRegex ) {
if ( preg_match( $spamRegex, $text ) ) {
return true;
}
}
}
return $retVal;
}
/**
* Checks the supplied text for links
*
* @param $text String: text to check
* @return Boolean: true if it contains links, otherwise false
*/
public static function haveLinks( $text ) {
$linkPatterns = array(
'/(https?)|(ftp):\/\//',
'/=\\s*[\'"]?\\s*mailto:/',
);
foreach ( $linkPatterns as $linkPattern ) {
if ( preg_match( $linkPattern, $text ) ) {
return true;
}
}
return false;
}
/**
* Blocks comments from a user
*
* @param int $userId User ID of the guy whose comments we want to block
* @param mixed $userName User name of the same guy
*/
public function blockUser( $userId, $userName ) {
$dbw = wfGetDB( DB_MASTER );
wfSuppressWarnings(); // E_STRICT bitching
$date = date( 'Y-m-d H:i:s' );
wfRestoreWarnings();
$dbw->insert(
'Comments_block',
array(
'cb_user_id' => $this->getUser()->getId(),
'cb_user_name' => $this->getUser()->getName(),
'cb_user_id_blocked' => $userId,
'cb_user_name_blocked' => $userName,
'cb_date' => $date
),
__METHOD__
);
$dbw->commit();
}
/**
* Fetches the list of blocked users from the database
*
* @param int $userId User ID for whom we're getting the blocks(?)
* @return array List of comment-blocked users
*/
static function getBlockList( $userId ) {
$blockList = array();
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
'Comments_block',
'cb_user_name_blocked',
array( 'cb_user_id' => $userId ),
__METHOD__
);
foreach ( $res as $row ) {
$blockList[] = $row->cb_user_name_blocked;
}
return $blockList;
}
static function isUserCommentBlocked( $userId, $userIdBlocked ) {
$dbr = wfGetDB( DB_SLAVE );
$s = $dbr->selectRow(
'Comments_block',
array( 'cb_id' ),
array(
'cb_user_id' => $userId,
'cb_user_id_blocked' => $userIdBlocked
),
__METHOD__
);
if ( $s !== false ) {
return true;
} else {
return false;
}
}
/**
* Deletes a user from your personal comment-block list.
*
* @param int $userId Your user ID
* @param int $userIdBlocked User ID of the blocked user
*/
public static function deleteBlock( $userId, $userIdBlocked ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
'Comments_block',
array(
'cb_user_id' => $userId,
'cb_user_id_blocked' => $userIdBlocked
),
__METHOD__
);
$dbw->commit();
}
/**
* Sort threads ascending
*
* @param $x
* @param $y
* @return int
*/
public static function sortAsc( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->timestamp < $y[0]->timestamp ) {
return -1;
} else {
return 1;
}
}
/**
* Sort threads descending
*
* @param $x
* @param $y
* @return int
*/
public static function sortDesc( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->timestamp > $y[0]->timestamp ) {
return -1;
} else {
return 1;
}
}
/**
* Sort threads by score
*
* @param $x
* @param $y
*/
public static function sortScore( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x[0]->currentScore > $y[0]->currentScore ) {
return -1;
} else {
return 1;
}
}
/**
* Sort COMMENTS (not threads) by score
*
* @param $x
* @param $y
*/
public static function sortCommentScore( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x->currentScore > $y->currentScore ) {
return -1;
} else {
return 1;
}
}
/**
* Sort the comments purely by the time, from earliest to latest
*
* @param $x
* @param $y
* @return int
*/
public static function sortTime( $x, $y ) {
// return -1 - x goes above y
// return 1 - x goes below y
// return 0 - order irrelevant (only when x == y)
if ( $x->timestamp == $y->timestamp ) {
return 0;
} elseif ( $x->timestamp < $y->timestamp ) {
return -1;
} else {
return 1;
}
}
# Output easy-to-read numbers
# by james at bandit.co.nz
static function format_nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).'T';
else if($n>1000000000) return round(($n/1000000000),1).'B';
else if($n>1000000) return round(($n/1000000),1).'M';
else if($n>1000) return round(($n/1000),1).'K';
return number_format($n);
}
/**
* Convert all '@' character to user page link. Max ping allowed is 20.
* @param String $text: The message to be converted.
*
* @return String : the converted message.
*/
public static function preprocessText( $message ) {
// convert '@' to wiki link;
$text = $message;
$matches = array();
$t = preg_match_all('/\\@(.+?)\\b/us', $text, $matches);
if ( isset ($matches[1]) ){
$i = 0;
while ( isset($matches[1][$i]) ){
$atWho = User::newFromName( $matches[1][$i] );
if ( !$atWho || $atWho->isAnon() ) {
$i++;
continue;
}
$text = str_replace( '@'.$matches[1][$i], '@[[User:'.$matches[1][$i].'|'.$matches[1][$i].']]', $text );
$i++;
}
}
return $text;
}
/**
* Analyses a PostRevision to determine which users are mentioned.
*
* @param String $text The text where to find mentioned user array.
* @param \Title $title
* @return User[] Array of User objects.
*/
public static function getMentionedUsers($text) {
// At the moment, it is not possible to get a list of mentioned users from HTML
// unless that HTML comes from Parsoid. But VisualEditor (what is currently used
// to convert wikitext to HTML) does not currently use Parsoid.
$mentions = self::getMentionedUsersFromWikitext( $text );
// in the future if we want to add a filter, we can add it here.
// $notifyUsers = $this->filterMentionedUsers( $mentions, $post, $title );
return $mentions;
}
/**
* Examines a wikitext string and finds users that were mentioned
* @param string $wikitext
* @return array Array of User objects
*/
public static function getMentionedUsersFromWikitext( $wikitext ) {
global $wgParser;
$title = Title::newMainPage(); // Bogus title used for parser
$options = new \ParserOptions;
$options->setTidy( true );
$options->setEditSection( false );
$output = $wgParser->parse( $wikitext, $title, $options );
$links = $output->getLinks();
if ( ! isset( $links[NS_USER] ) || ! is_array( $links[NS_USER] ) ) {
// Nothing
return array();
}
$users = array();
foreach ( $links[NS_USER] as $dbk => $page_id ) {
$user = User::newFromName( $dbk );
if ( !$user || $user->isAnon() ) {
continue;
}
$users[$user->getId()] = $user;
// If more than 20 users are being notified this is probably a spam/attack vector.
// Don't send any mention notifications
if ( count( $users ) > 20 ) {
return array();
}
}
return $users;
}
/* Get Real User Ip (By pass proxy) */
public static function getIp() {
//Just get the headers if we can or else use the SERVER global
if ( function_exists( 'apache_request_headers' ) ) {
$headers = apache_request_headers();
} else {
$headers = $_SERVER;
}
//Get the forwarded IP if it exists
if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
$the_ip = $headers['X-Forwarded-For'];
} elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
) {
$the_ip = $headers['HTTP_X_FORWARDED_FOR'];
} else {
$the_ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
}
return $the_ip;
}
/**
* Generate a trade number based on the given type.
*/
public static function getTradeNo($type){
$cache = wfGetCache(CACHE_MEMCACHED);
$key = wfForeignMemcKey('huiji', '', 'Huiji', 'getTradeNo', $type);
if (HuijiFunctions::addLock($key, 1000, 1000)){
$no = $cache->get($key);
if ($no != ''){
++$no;
$cache->set($key, $no, 60);
HuijiFunctions::releaseLock($key);
return $type.$no;
} else {
$no = time() * 1000000;
$cache->set($key, $no, 60);
HuijiFunctions::releaseLock($key);
return $type.$no;
}
}
return false;
}
}
?>