function editlog_raw_lines($days, $opts = array()) { global $Config; $ruleset = array(); if (!empty($this->members) && !in_array($this->user->id, $this->members)) { $ruleset = $Config['ruleset']['hidelog']; } $fz = filesize($this->editlog_name); $lines = array(); $time_current = time(); $secs_per_day = 24 * 60 * 60; if (!empty($opts['ago'])) { $date_from = $time_current - $opts['ago'] * $secs_per_day; $date_to = $date_from + $days * $secs_per_day; $seek_start = null; } else { if (!empty($opts['from'])) { $from = strtotime($opts['from']); if ($time_current > $from) { $date_from = $from; } else { $date_from = $time_current - ($from - $time_current); } $date_to = $date_from + $days * $secs_per_day; $seek_start = null; } else { if (!empty($opts['items'])) { $date_from = $time_current - 365 * $secs_per_day; } else { $date_from = $time_current - $days * $secs_per_day; } $date_to = $time_current; $seek_start = 0; } } // check start timestamp if (!empty($opts['start']) && is_numeric($opts['start'])) { $time_seek = $opts['start']; if ($time_seek < $time_current && $time_seek > $date_from) { $date_to = $time_seek; $seek_start = null; } } if ($date_to > $time_current) { $seek_start = 0; $date_to = $time_current; } $check = $date_to; // seek $date_to if ($seek_start === null) { require_once dirname(__FILE__) . '/plugin/editlogbin.php'; $seek = _editlog_seek($this->editlog_name, $date_to); if ($seek < 0) { // fail to seek return array(); } $seek_start = $fz - $seek; } $itemnum = !empty($opts['items']) ? $opts['items'] : 200; $fp = fopen($this->editlog_name, 'r'); while (is_resource($fp) && $fz > 0) { if ($fz <= 1024) { fseek($fp, 0); $ll = rtrim(fread($fp, 1024)); $lines = array_reverse(explode("\n", $ll)); break; } $a = $seek_start - 1; // hack, don't read last \n char. $last = ''; fseek($fp, $seek_start, SEEK_END); while ($date_from < $check and !feof($fp)) { $rlen = $fz + $a; if ($rlen > 1024) { $rlen = 1024; } else { if ($rlen <= 0) { break; } } $a -= $rlen; fseek($fp, $a, SEEK_END); $l = fread($fp, $rlen); if ($rlen != 1024) { $l = "\n" . $l; } // hack, for the first log entry. while (($p = strrpos($l, "\n")) !== false) { $line = substr($l, $p + 1) . $last; $last = ''; $l = substr($l, 0, $p); $dumm = explode("\t", $line, 4); $check = $dumm[2]; if ($date_from > $check) { break; } if ($date_to > $check) { if (!empty($ruleset)) { $page_name = $this->keyToPagename($dumm[0]); if (in_array($page_name, $ruleset)) { continue; } } $lines[] = $line; $pages[$dumm[0]] = 1; if (sizeof($pages) >= $itemnum) { $check = 0; break; } } $last = ''; } $last = $l . $last; } #echo $a; #echo sizeof($lines); #print_r($lines); fclose($fp); break; } if (!empty($opts['quick'])) { $out = array(); foreach ($lines as $line) { $dum = explode("\t", $line, 2); if (!empty($dum[0]) and !empty($keys[$dum[0]])) { continue; } $keys[$dum[0]] = 1; $out[] = $line; } $lines = $out; } return $lines; }
function cached_editlogbin($formatter, $params = array()) { global $Config, $DBInfo; $cache = new Cache_Text('editlogbin'); if (!empty($params['title'])) { if (!$DBInfo->hasPage($params['title'])) { unset($params['title']); } } $args = array(); $user = !empty($params['q']) ? $params['q'] : ''; $start = !empty($params['start']) ? $params['start'] : ''; $domain = !empty($params['domain']) ? $params['domain'] : ''; $title = !empty($params['title']) ? $params['title'] : ''; // get timestamp $oldest_stamp = !empty($params['.oldest']) ? strtotime($params['.oldest']) : strtotime('-1 year'); // round timestamp $tmp = date('Y-m-d 00:00:00', $oldest_stamp); $oldest_stamp = strtotime($tmp); if (!empty($start)) { if (is_numeric($start)) { // timestamp $from = $start; } else { $from = strtotime($start); } // convert to timestamp } else { $from = !empty($params['.max_range']) ? strtotime('-' . $params['.max_range']) : strtotime('-1 year'); // restrict range $params['until'] = null; // reset } // round timestamp $tmp = date('Y-m-d 23:59:59', $from); $from = strtotime($tmp); $max_range = !empty($params['.max_range']) ? strtotime($params['.max_range'], $from) : strtotime('1 year', $from); $params['until'] = $max_range; if ($params['until'] > time()) { $params['until'] = null; } //echo date('Y.m.d H.i.s', $params['until']); $mtime = $DBInfo->mtime(); $key = $user . '.' . $from . '.' . $domain . '.' . $title . $mtime; if (empty($formatter->refresh) && ($data = $cache->fetch($key)) !== false) { return $data; } $fp = fopen($Config['editlog_name'], 'r'); $seek = _editlog_seek($fp, $from); $data = _editlog_binning($fp, $seek, $from, 60 * 60 * 24, $params); fclose($fp); $cache->update($key, $data, 60 * 60 * 24); // TTL to 24 hour return $data; }
function do_editlogbin($formatter, $params = array()) { global $Config, $DBInfo; $cache = new Cache_Text('editlogbin'); if (!empty($params['title'])) { if (!$DBInfo->hasPage($params['title'])) { unset($params['title']); } } $args = array(); $user = !empty($params['q']) ? $params['q'] : ''; $start = !empty($params['start']) ? $params['start'] : ''; $domain = !empty($params['domain']) ? $params['domain'] : ''; $title = !empty($params['title']) ? $params['title'] : ''; // get timestamp $oldest_stamp = !empty($Config['editlogbin_datetime_oldest']) ? strtotime($Config['editlogbin_datetime_oldest']) : strtotime('-1 year'); if ($oldest_stamp < strtotime('-10 years')) { // too old $oldest_stamp = strtotime('-1 year'); } if (!empty($start)) { if (is_numeric($start)) { // timestamp $from = $start; } else { $from = strtotime($start); } // convert to timestamp } else { $from = $oldest_stamp; } // restrict range $params['until'] = null; // reset if ($from < $oldest_stamp) { $max_range = !empty($Config['editlogbin_datetime_max_range']) ? strtotime($Config['editlogbin_datetime_max_range'], $from) : strtotime('1 year', $from); $params['until'] = $max_range; } //echo date('Y.m.d H.i.s', $params['until']); $mtime = $DBInfo->mtime(); $key = $user . '.' . $from . '.' . $domain . '.' . $title . $mtime; if (empty($formatter->refresh) && ($val = $cache->fetch($key)) !== false) { header('Content-Type: text/plain'); echo json_encode($val['data']); return; } $fp = fopen($Config['editlog_name'], 'r'); $seek = _editlog_seek($fp, $from); $data = _editlog_binning($fp, $seek, $from, 60 * 60 * 24, $params); fclose($fp); header('Content-Type: text/plain'); $cache->update($key, $data, 60 * 60 * 24); // TTL to 24 hour echo json_encode($data['data']); }