function DisplayStopWatch() { global $StopWatch; StopWatch('now'); $out[] = "<pre>"; foreach ((array) $StopWatch as $k => $x) { $out[] = "{$x}\n"; } array_pop($StopWatch); $out[] = '</pre>'; return implode('', $out); }
function StopWatchHTML($pagename, $print = 0) { global $StopWatch; StopWatch('now'); $l = strlen(count($StopWatch)); $out = '<pre>'; foreach((array)$StopWatch as $i => $x) $out .= sprintf("%{$l}d: %s\n", $i, $x); $out .= '</pre>'; if (is_array($StopWatch)) array_pop($StopWatch); if ($print) print $out; return $out; }
function ReadApprovedUrls($pagename) { global $ApprovedUrlPagesFmt,$ApproveUrlPattern,$WhiteUrlPatterns; foreach((array)$ApprovedUrlPagesFmt as $p) { $pn = FmtPageName($p, $pagename); StopWatch("ReadApprovedUrls $pn begin"); $apage = ReadPage($pn, READPAGE_CURRENT); preg_match_all("/$ApproveUrlPattern/",@$apage['text'],$match); foreach($match[0] as $a) $WhiteUrlPatterns[] = preg_quote($a,'!'); StopWatch("ReadApprovedUrls $pn end"); } }
function StopWatchHTML($pagename, $print = 0) { global $StopWatch; StopWatch('now'); $out = '<pre>' . implode("\n", (array) $StopWatch) . "\n</pre>"; if (is_array($StopWatch)) { array_pop($StopWatch); } if ($print) { print $out; } return $out; }
function DisplayStopWatch() { global $StopWatch; StopWatch('now'); $out = "<pre>"; foreach ((array) $StopWatch as $k => $x) { $out .= "{$x}\n"; } if (is_array($StopWatch)) { array_pop($StopWatch); } $out .= '</pre>'; return $out; }
function DisplayStopWatch() { global $StopWatch; StopWatch('now'); $u = $StopWatch[0]; $au = $u; $out[] = "<table><tr><th>Event</th><th>Time</th><th>Cumulative</th></tr>\n"; for ($i = 0; $i < count($StopWatch); $i++) { list($bu, $bevent) = explode(' ', $StopWatch[$i], 2); $t = sprintf('%.02f', $bu - $au); $c = sprintf('%.02f', $bu - $u); $out[] = "<tr><td>{$bevent}</td>\n <td align='right'>{$t}</td><td align='right'>{$c}</td></tr>"; $au = $bu; } array_pop($StopWatch); $out[] = '</table>'; return implode('', $out); }
function Blocklist($pagename, $text) { global $BlocklistPages, $BlockedMessagesFmt, $BlocklistDownload, $BlocklistDownloadRefresh, $Now, $EnablePost, $WhyBlockedFmt, $MessagesFmt, $BlocklistMessageFmt, $EnableWhyBlocked, $IsBlocked; StopWatch("Blocklist: begin $pagename"); $BlocklistDownload = (array)@$BlocklistDownload; SDV($BlocklistPages, array_merge(array('$SiteAdminGroup.Blocklist', '$SiteAdminGroup.Blocklist-Farm'), array_keys($BlocklistDownload))); SDV($BlocklistMessageFmt, "<h3 class='wikimessage'>$[This post has been blocked by the administrator]</h3>"); SDVA($BlockedMessagesFmt, array( 'ip' => '$[Address blocked from posting]: ', 'text' => '$[Text blocked from posting]: ')); SDV($BlocklistDownloadRefresh, 86400); ## Loop over all blocklist pages foreach((array)$BlocklistPages as $b) { ## load the current blocklist page $pn = FmtPageName($b, $pagename); $page = ReadPage($pn, READPAGE_CURRENT); if (!$page) continue; ## if the page being checked is a blocklist page, stop blocking if ($pagename == $pn) return; ## If the blocklist page is managed by automatic download, ## schedule any new downloads here if (@$BlocklistDownload[$pn]) { $bd = &$BlocklistDownload[$pn]; SDVA($bd, array( 'refresh' => $BlocklistDownloadRefresh, 'url' => "http://www.pmwiki.org/blocklists/$pn" )); if (!@$page['text'] || $page['time'] < $Now - $bd['refresh']) register_shutdown_function('BlocklistDownload', $pn, getcwd()); } ## If the blocklist is simply a list of regexes to be matched, load ## them into $terms['block'] and continue to the next blocklist page. ## Some regexes from remote sites aren't well-formed, so we have ## to escape any slashes that aren't already escaped. if (strpos(@$page['text'], 'blocklist-format: regex') !==false) { if (preg_match_all('/^([^\\s#].+)/m', $page['text'], $match)) foreach($match[0] as $m) { $m = preg_replace('#(?<!\\\\)/#', '\\/', trim($m)); $terms['block'][] = "/$m/"; } continue; } ## Treat the page as a pmwiki-format blocklist page, with ## IP addresses and "block:"-style declarations. First, see ## if we need to block the author based on a.b.c.d or a.b.c.* ## IP addresses. $ip = preg_quote($_SERVER['REMOTE_ADDR']); $ip = preg_replace('/\\d+$/', '($0\\b|\\*)', $ip); if (preg_match("/\\b$ip/", @$page['text'], $match)) { $EnablePost = 0; $IsBlocked = 1; $WhyBlockedFmt[] = $BlockedMessagesFmt['ip'] . $match[0]; } ## Now we'll load any "block:" or "unblock:" specifications ## from the page text. if (preg_match_all('/(un)?(?:block|regex):(.*)/', @$page['text'], $match, PREG_SET_ORDER)) foreach($match as $m) $terms[$m[1].'block'][] = trim($m[2]); } ## okay, we've loaded all of the terms, now subtract any 'unblock' ## terms from the block set. StopWatch("Blocklist: diff unblock"); $blockterms = array_diff((array)@$terms['block'], (array)@$terms['unblock']); ## go through each of the remaining blockterms and see if it matches the ## text -- if so, disable posting and add a message to $WhyBlockedFmt. StopWatch('Blocklist: blockterms (count='.count($blockterms).')'); $itext = strtolower($text); foreach($blockterms as $b) { if ($b{0} == '/') { if (!preg_match($b, $text)) continue; } else if (strpos($itext, strtolower($b)) === false) continue; $EnablePost = 0; $IsBlocked = 1; $WhyBlockedFmt[] = $BlockedMessagesFmt['text'] . $b; } StopWatch('Blocklist: blockterms done'); ## If we came across any reasons to block, let's provide a message ## to the author that it was blocked. If $EnableWhyBlocked is set, ## we'll even tell the author why. :-) if (@$WhyBlockedFmt) { $MessagesFmt[] = $BlocklistMessageFmt; if (IsEnabled($EnableWhyBlocked, 0)) foreach((array)$WhyBlockedFmt as $why) $MessagesFmt[] = "<pre class='blocklistmessage'>$why</pre>\n"; } StopWatch("Blocklist: end $pagename"); }
function HandleUpgrade($pagename, $auth = 'ALWAYS') { global $SiteGroup, $SiteAdminGroup, $StatusPageName, $ScriptUrl, $AuthUserPageFmt, $VersionNum, $Version; StopWatch('HandleUpgrade: begin'); $message = ''; $done = ''; ## check for Site.* --> SiteAdmin.* foreach(array('AuthUser', 'NotifyList', 'Blocklist', 'ApprovedUrls') as $n) { $n0 = "$SiteGroup.$n"; $n1 = "$SiteAdminGroup.$n"; StopWatch("HandleUpgrade: checking $n0 -> $n1"); ## checking AuthUser is special, because Site.AuthUser comes with the ## distribution. if ($n == 'AuthUser') { ## if we already have a user-modified SiteAdmin.AuthUser, we can skip SDV($AuthUserPageFmt, '$SiteAdminGroup.AuthUser'); $n1 = FmtPageName($AuthUserPageFmt, $pagename); $page = ReadPage($n1, READPAGE_CURRENT); if (@$page['time'] > 1000000000) continue; ## if there's not a user-modified Site.AuthUser, we can skip $page = ReadPage($n0, READPAGE_CURRENT); if (@$page['time'] == 1000000000) continue; } else if (!PageExists($n0) || PageExists($n1)) continue; if (@$_REQUEST['migrate'] == 'yes') { ## if the admin wants PmWiki to migrate, do it. $page = RetrieveAuthPage($n0, 'admin', true); StopWatch("HandleUpgrade: copying $n0 -> $n1"); if ($page) { WritePage($n1, $page); $done .= "<li>Copied $n0 to $n1</li>"; continue; } } $message .= "<li>$n0 -> $n1</li>"; } if ($message) { $migrateurl = "$ScriptUrl?action=upgrade&migrate=yes"; $infourl = 'http://www.pmwiki.org/wiki/PmWiki/UpgradeToSiteAdmin'; $message = "<h2>Upgrade notice -- SiteAdmin group</h2> <p>This version of PmWiki expects several administrative pages from the <em>Site</em> group to be found in a new <em>SiteAdmin</em> group. On this site, the following pages appear to need to be relocated:</p> <ul>$message</ul> <p>For more information about this change, including the various options for proceeding, see</p> <blockquote><a target='_blank' href='$infourl'>$infourl</a></blockquote> <form action='$ScriptUrl' method='post'> <p>If you would like PmWiki to attempt to automatically copy these pages into their new <br /> locations for you, try <input type='hidden' name='action' value='upgrade' /> <input type='hidden' name='migrate' value='yes' /> <input type='submit' value='Relocate pages listed above' /> (admin password required) </p> </form> <p>If you want to configure PmWiki so that it continues to look for the above pages in <em>$SiteGroup</em>, add the following line near the top of <em>local/config.php</em>:</p> <blockquote><pre>\$SiteAdminGroup = \$SiteGroup;</pre></blockquote> $Version "; print $message; exit; } StopWatch("UpgradeCheck: writing $StatusPageName"); Lock(2); SDV($StatusPageName, "$SiteAdminGroup.Status"); $page = ReadPage($StatusPageName); $page['updatedto'] = $VersionNum; WritePage($StatusPageName, $page); if ($done) { $done .= "<li>Updated $StatusPageName</li>"; echo "<h2>Upgrade to $Version ... ok</h2><ul>$done</ul>"; $GLOBALS['EnableRedirect'] = 0; } Redirect($pagename); }
function MarkupToHTML($pagename, $text, $escape = true) { # convert wiki markup text to HTML output global $MarkupRules, $MarkupFrame, $MarkupFrameBase, $WikiWordCount, $K0, $K1, $RedoMarkupLine; StopWatch('MarkupToHTML begin'); array_unshift($MarkupFrame, $MarkupFrameBase); $MarkupFrame[0]['wwcount'] = $WikiWordCount; $markrules = BuildMarkupRules(); foreach ((array) $text as $l) { $lines[] = $escape ? PVS(htmlspecialchars($l, ENT_NOQUOTES)) : $l; } $lines[] = '(:closeall:)'; $out = ''; while (count($lines) > 0) { $x = array_shift($lines); $RedoMarkupLine = 0; foreach ($markrules as $p => $r) { if ($p[0] == '/') { $x = preg_replace($p, $r, $x); } elseif (strstr($x, $p) !== false) { $x = eval($r); } if (isset($php_errormsg)) { echo "pat={$p}"; unset($php_errormsg); } if ($RedoMarkupLine) { $lines = array_merge((array) $x, $lines); continue 2; } } if ($x > '') { $out .= "{$x}\n"; } } foreach ((array) @$MarkupFrame[0]['posteval'] as $v) { eval($v); } array_shift($MarkupFrame); StopWatch('MarkupToHTML end'); return $out; }
function MxPageList($pagename, $args) { StopWatch('pagelist start'); $opt = array('o' => $args, 'fmt' => 'csv'); $out = FmtPageList('$MatchList', $pagename, $opt, 0); $out = preg_replace("/[\n]+/s", "\n", $out); StopWatch('pagelist end'); return $out; }
function ls($pats=NULL) { global $GroupPattern, $NamePattern; StopWatch("PageStore::ls begin {$this->dir}"); $pats=(array)$pats; array_push($pats, "/^$GroupPattern\.$NamePattern$/"); $dir = $this->pagefile('$Group.$Name'); $dirlist = array(preg_replace('!/*[^/]*\\$.*$!','',$dir)); $out = array(); while (count($dirlist)>0) { $dir = array_shift($dirlist); $dfp = @opendir($dir); if (!$dfp) { continue; } $o = array(); while ( ($pagefile = readdir($dfp)) !== false) { if ($pagefile{0} == '.') continue; if (is_dir("$dir/$pagefile")) { array_push($dirlist,"$dir/$pagefile"); continue; } $o[] = $pagefile; } closedir($dfp); StopWatch("PageStore::ls merge {$this->dir}"); $out = array_merge($out, MatchPageNames($o, $pats)); } StopWatch("PageStore::ls end {$this->dir}"); return $out; }
function PHPDiff($old, $new) { StopWatch("PHPDiff: begin"); # split the source text into arrays of lines $t1 = explode("\n", $old); $x = array_pop($t1); if ($x > '') { $t1[] = "{$x}\n\\ No newline at end of file"; } $t2 = explode("\n", $new); $x = array_pop($t2); if ($x > '') { $t2[] = "{$x}\n\\ No newline at end of file"; } $t1_start = 0; $t1_end = count($t1); $t2_start = 0; $t2_end = count($t2); # stop with a common ending while ($t1_start < $t1_end && $t2_start < $t2_end && $t1[$t1_end - 1] == $t2[$t2_end - 1]) { $t1_end--; $t2_end--; } # skip over any common beginning while ($t1_start < $t1_end && $t2_start < $t2_end && $t1[$t1_start] == $t2[$t2_start]) { $t1_start++; $t2_start++; } # build a reverse-index array using the line as key and line number as value # don't store blank lines, so they won't be targets of the shortest distance # search for ($i = $t1_start; $i < $t1_end; $i++) { if ($t1[$i] > '') { $r1[$t1[$i]][] = $i; } } for ($i = $t2_start; $i < $t2_end; $i++) { if ($t2[$i] > '') { $r2[$t2[$i]][] = $i; } } $a1 = $t1_start; $a2 = $t2_start; # start at beginning of each list $actions = array(); # walk this loop until we reach the end of one of the lists while ($a1 < $t1_end && $a2 < $t2_end) { # if we have a common element, save it and go to the next if ($t1[$a1] == $t2[$a2]) { $actions[] = 4; $a1++; $a2++; continue; } # otherwise, find the shortest move (Manhattan-distance) from the # current location $best1 = $t1_end; $best2 = $t2_end; $s1 = $a1; $s2 = $a2; while ($s1 + $s2 - $a1 - $a2 < $best1 + $best2 - $a1 - $a2) { $d = -1; foreach ((array) @$r1[$t2[$s2]] as $n) { if ($n >= $s1) { $d = $n; break; } } if ($d >= $s1 && $d + $s2 - $a1 - $a2 < $best1 + $best2 - $a1 - $a2) { $best1 = $d; $best2 = $s2; } $d = -1; foreach ((array) @$r2[$t1[$s1]] as $n) { if ($n >= $s2) { $d = $n; break; } } if ($d >= $s2 && $s1 + $d - $a1 - $a2 < $best1 + $best2 - $a1 - $a2) { $best1 = $s1; $best2 = $d; } $s1++; $s2++; } while ($a1 < $best1) { $actions[] = 1; $a1++; } # deleted elements while ($a2 < $best2) { $actions[] = 2; $a2++; } # added elements } # we've reached the end of one list, now walk to the end of the other while ($a1 < $t1_end) { $actions[] = 1; $a1++; } # deleted elements while ($a2 < $t2_end) { $actions[] = 2; $a2++; } # added elements # and this marks our ending point $actions[] = 8; # now, let's follow the path we just took and report the added/deleted # elements into $out. $op = 0; $x0 = $x1 = $t1_start; $y0 = $y1 = $t2_start; $out = array(); foreach ($actions as $act) { if ($act == 1) { $op |= $act; $x1++; continue; } if ($act == 2) { $op |= $act; $y1++; continue; } if ($op > 0) { $xstr = $x1 == $x0 + 1 ? $x1 : $x0 + 1 . ",{$x1}"; $ystr = $y1 == $y0 + 1 ? $y1 : $y0 + 1 . ",{$y1}"; if ($op == 1) { $out[] = "{$xstr}d{$y1}"; } elseif ($op == 3) { $out[] = "{$xstr}c{$ystr}"; } while ($x0 < $x1) { $out[] = '< ' . $t1[$x0]; $x0++; } # deleted elems if ($op == 2) { $out[] = "{$x1}a{$ystr}"; } elseif ($op == 3) { $out[] = '---'; } while ($y0 < $y1) { $out[] = '> ' . $t2[$y0]; $y0++; } # added elems } $x1++; $x0 = $x1; $y1++; $y0 = $y1; $op = 0; } $out[] = ''; StopWatch("PHPDiff: end"); return join("\n", $out); }
function ls($pats = NULL) { global $UploadDir, $UploadPrefixFmt; global $GroupPattern, $NamePattern; global $ThumbShoeThumbPrefix, $ThumbShoePageSep; StopWatch("ThumbShoePageStore::ls begin {$this->galleryGroup}"); $pats = (array) $pats; $topdir = PageVar($this->galleryGroup . '.' . $this->galleryGroup, '$TSAttachTopDir'); StopWatch("ThumbShoePageStore::ls topdir={$topdir}"); $out = array(); $o = array(); $dfp = @opendir($topdir); if ($dfp) { while (($file = readdir($dfp)) !== false) { if ($file[0] == '.') { continue; } if (is_dir("{$topdir}/{$file}")) { $sub_dfp = @opendir("{$topdir}/{$file}"); while (($subfile = readdir($sub_dfp)) !== false) { if ($subfile[0] == '.') { continue; } if (is_dir("{$topdir}/{$file}/{$subfile}")) { continue; } if (!preg_match("/{$this->ImgRx}\$/", $subfile)) { continue; } if (preg_match("/^{$ThumbShoeThumbPrefix}/", $subfile)) { continue; } $pn = str_replace('.', '_', $subfile); $pn = "{$file}{$ThumbShoePageSep}" . $pn; $pn = MakePageName($this->galleryGroup . '.' . $this->galleryGroup, $pn); $o[] = $pn; StopWatch("ThumbShoePageStore::ls pn={$pn}"); } closedir($sub_dfp); } else { if (!preg_match("/{$this->ImgRx}\$/", $file)) { continue; } if (preg_match("/^{$ThumbShoeThumbPrefix}/", $file)) { continue; } $pn = str_replace('.', '_', $file); $pn = MakePageName($this->galleryGroup . '.' . $this->galleryGroup, $pn); $o[] = $pn; StopWatch("ThumbShoePageStore::ls pn={$pn}"); } } closedir($dfp); } StopWatch("ThumbShoePageStore::ls merge {$this->galleryGroup}"); $out = array_merge($out, MatchPageNames($o, $pats)); StopWatch("ThumbShoePageStore::ls end {$this->galleryGroup}"); return $out; }
function FPLTextExtract($pagename, &$matches, $opt) { ##DEBUG echo "<pre>OPT "; print_r($opt); echo "</pre>"; global $FmtV, $EnableStopWatch, $KeepToken, $KPV; $EnableStopWatch = 1; StopWatch('TextExtract pagelist begin'); $opt['stime'] = strtok(microtime(), ' ') + strtok(''); $opt['q'] = ltrim($opt['q']); foreach ($opt[''] as $k => $v) { $opt[''][$k] = htmlspecialchars_decode($v); } //treat single . search term as request for regex 'all characters' if ($opt[''][0] == '.' || $opt['pattern'] == '.') { $opt['regex'] = 1; } //MakePageList() does not evaluate terms as regular expressions, so we save them for later if (@$opt['regex'] == 1) { $opt['pattern'] = implode(' ', $opt['']); unset($opt['']); } if (@$opt['page']) { $opt['name'] .= "," . $opt['page']; } //allow search of anchor sections if ($sa = strpos($opt['name'], '#')) { $opt['section'] = strstr($opt['name'], '#'); $opt['name'] = substr($opt['name'], 0, $sa); } $list = MakePageList($pagename, $opt, 0); //extract page subset according to 'count=' parameter if (@$opt['count'] && !$opt['section']) { TESliceList($list, $opt); } return TextExtract($pagename, $list, $opt); }
function MarkupToHTML($pagename, $text) { # convert wiki markup text to HTML output global $MarkupRules, $MarkupFrame, $MarkupFrameBase, $K0, $K1, $RedoMarkupLine; StopWatch('MarkupToHTML begin'); array_unshift($MarkupFrame, $MarkupFrameBase); $markrules = BuildMarkupRules(); foreach ((array) $text as $l) { $lines[] = htmlspecialchars($l, ENT_NOQUOTES); } $out = array(); while (count($lines) > 0) { $x = array_shift($lines); $RedoMarkupLine = 0; foreach ($markrules as $p => $r) { if (substr($p, 0, 1) == '/') { $x = preg_replace($p, $r, $x); } elseif ($p == '' || strstr($x, $p) !== false) { $x = eval($r); } if (isset($php_errormsg)) { echo "pat={$p}"; unset($php_errormsg); } if ($RedoMarkupLine) { $lines = array_merge((array) $x, $lines); continue 2; } } if ($x > '') { $out[] = "{$x}\n"; } } foreach ((array) $MarkupFrame[0]['posteval'] as $v) { $x = eval($v); if ($x > '') { $out[] = "{$x}\n"; } } array_shift($MarkupFrame); StopWatch('MarkupToHTML end'); return implode('', (array) $out); }
function ls($pats = NULL) { global $GroupPattern, $NamePattern; StopWatch("PageStore::ls begin {$this->dirfmt}"); $pats = (array) $pats; array_push($pats, "/^{$GroupPattern}\\.{$NamePattern}\$/"); $dir = $this->pagefile('$Group.$Name'); $maxslash = substr_count($dir, '/'); $dirlist = array(preg_replace('!/*[^/]*\\$.*$!', '', $dir)); $out = array(); while (count($dirlist) > 0) { $dir = array_shift($dirlist); $dfp = @opendir($dir); if (!$dfp) { continue; } $dirslash = substr_count($dir, '/') + 1; $o = array(); while (($pagefile = readdir($dfp)) !== false) { if ($pagefile[0] == '.') { continue; } if ($dirslash < $maxslash && is_dir("{$dir}/{$pagefile}")) { array_push($dirlist, "{$dir}/{$pagefile}"); continue; } if ($dirslash == $maxslash) { $o[] = $pagefile; } } closedir($dfp); StopWatch("PageStore::ls merge {$this->dirfmt}"); $out = array_merge($out, MatchPageNames($o, $pats)); } StopWatch("PageStore::ls end {$this->dirfmt}"); return $out; }
function MakePageList($pagename, $opt) { global $MakePageListOpt, $SearchPatterns, $EnablePageListProtect, $PCache, $FmtV; StopWatch('MakePageList begin'); SDVA($MakePageListOpt, array('list' => 'default')); $opt = array_merge($MakePageListOpt, $opt); $readf = $opt['readf']; # we have to read the page if order= is anything but name $order = $opt['order']; $readf |= $order && $order != 'name' && $order != '-name'; $pats = (array) $SearchPatterns[$opt['list']]; if ($opt['group']) { array_unshift($pats, "/^({$opt['group']})\\./i"); } # inclp/exclp contain words to be included/excluded. $inclp = array(); $exclp = array(); foreach ((array) @$opt[''] as $i) { $inclp[] = '/' . preg_quote($i, '/') . '/i'; } foreach ((array) @$opt['+'] as $i) { $inclp[] = '/' . preg_quote($i, '/') . '/i'; } foreach ((array) @$opt['-'] as $i) { $exclp[] = '!' . preg_quote($i, '!') . '/i'; } $searchterms = count($inclp) + count($exclp); $readf += $searchterms; # forced read if incl/excl # link= (backlinks) if (@$opt['link']) { $linkpat = "/,{$opt['link']},/"; # find in target= attribute $readf = 1; # forced read } if (@$opt['trail']) { $trail = ReadTrail($pagename, $opt['trail']); foreach ($trail as $tstop) { $pn = $tstop['pagename']; $list[] = $pn; $tstop['parentnames'] = array(); PCache($pn, $tstop); } foreach ($trail as $tstop) { $PCache[$tstop['pagename']]['parentnames'][] = $trail[$tstop['parent']]['pagename']; } } else { $list = ListPages($pats); } if (IsEnabled($EnablePageListProtect, 0)) { $readf = 1000; } $matches = array(); $FmtV['$MatchSearched'] = count($list); foreach ((array) $list as $pn) { if ($readf) { $page = $readf == 1000 ? RetrieveAuthPage($pn, 'read', false, READPAGE_CURRENT) : ReadPage($pn, READPAGE_CURRENT); if (!$page) { continue; } if ($linkpat && !preg_match($linkpat, ",{$page['targets']},")) { continue; } if ($searchterms) { $text = $pn . "\n" . @$page['targets'] . "\n" . @$page['text']; foreach ($inclp as $i) { if (!preg_match($i, $text)) { continue 2; } } foreach ($exclp as $i) { if (preg_match($i, $text)) { continue 2; } } } $page['size'] = strlen(@$page['text']); } else { $page = array(); } $page['pagename'] = $page['name'] = $pn; PCache($pn, $page); $matches[] =& $PCache[$pn]; } SortPageList($matches, $order); StopWatch('MakePageList end'); return $matches; }
function PageIndexGrep($terms, $invert = false) { global $PageIndexFile; if (!$PageIndexFile) return array(); StopWatch('PageIndexGrep begin'); $pagelist = array(); $fp = @fopen($PageIndexFile, 'r'); if ($fp) { $terms = (array)$terms; while (!feof($fp)) { $line = fgets($fp, 4096); while (substr($line, -1, 1) != "\n" && !feof($fp)) $line .= fgets($fp, 4096); $i = strpos($line, ':'); if (!$i) continue; $add = true; foreach($terms as $t) if (strpos($line, $t) === false) { $add = false; break; } if ($add xor $invert) $pagelist[] = substr($line, 0, $i); } fclose($fp); } StopWatch('PageIndexGrep end'); return $pagelist; }
function LinkIndexUpdate($pagelist) { global $LinkIndexFile, $PCache, $LinkIndexTime; SDV($LinkIndexTime, 10); if (!$pagelist || !$LinkIndexFile) { return; } StopWatch('LinkIndexUpdate begin'); $pagelist = (array) $pagelist; Lock(2); $ofp = fopen("{$LinkIndexFile},new", 'w'); $timeout = time() + $LinkIndexTime; foreach ($pagelist as $n) { if (time() > $timeout) { break; } if (isset($PCache[$n]['targets'])) { $targets = $PCache[$n]['targets']; } else { $page = ReadPage($n, READPAGE_CURRENT); if (!$page) { continue; } $targets = @$page['targets']; } fputs($ofp, "{$n}={$targets}\n"); } $ifp = @fopen($LinkIndexFile, 'r'); if ($ifp) { while (!feof($ifp)) { $line = fgets($ifp, 4096); while (substr($line, -1, 1) != "\n" && !feof($ifp)) { $line .= fgets($ifp, 4096); } $i = strpos($line, '='); if ($i === false) { continue; } $n = substr($line, 0, $i); if (in_array($n, $pagelist)) { continue; } fputs($ofp, $line); } fclose($ifp); } fclose($ofp); if (file_exists($LinkIndexFile)) { unlink($LinkIndexFile); } rename("{$LinkIndexFile},new", $LinkIndexFile); fixperms($LinkIndexFile); StopWatch('LinkIndexUpdate end'); }
function HandleFastCacheBrowse($pagename, $auth = 'read') { # handle display of a page global $DefaultPageTextFmt, $PageNotFoundHeaderFmt, $HTTPHeaders, $EnableHTMLCache, $NoHTMLCache, $PageCacheFile, $LastModTime, $IsHTMLCached, $FmtV, $HandleBrowseFmt, $PageStartFmt, $PageEndFmt, $PageRedirectFmt; ## begin added global $FastCachePage, $FastCacheDir, $FastCacheValid, $FastCacheSuffix; if (!$FastCacheValid || !$FastCacheDir) { HandleBrowse($pagename, $auth); return; } SDV($FastCacheSuffix, '.html'); $fcfile = "{$FastCacheDir}/{$pagename}{$FastCacheSuffix}"; if (@filemtime($fcfile) > $LastModTime) { if ($FastCachePage = file_get_contents($fcfile)) { StopWatch("HandleFastCacheBrowse: using FastCached copy of {$pagename}"); echo $FastCachePage; } else { $FastCacheValid = FALSE; StopWatch("HandleFastCacheBrowse: read error on {$fcfile}"); HandleBrowse($pagename, $auth); } return; } ## end added $page = RetrieveAuthPage($pagename, $auth, true, READPAGE_CURRENT); if (!$page) { Abort("?cannot read {$pagename}"); } PCache($pagename, $page); if (PageExists($pagename)) { $text = @$page['text']; } else { $FastCacheValid = FALSE; ## added SDV($DefaultPageTextFmt, '(:include $[{$SiteGroup}.PageNotFound]:)'); $text = FmtPageName($DefaultPageTextFmt, $pagename); SDV($PageNotFoundHeaderFmt, 'HTTP/1.1 404 Not Found'); SDV($HTTPHeaders['status'], $PageNotFoundHeaderFmt); } $opt = array(); SDV($PageRedirectFmt, "<p><i>(\$[redirected from] <a rel='nofollow'\n href='{\$PageUrl}?action=edit'>{\$FullName}</a>)</i></p>\$HTMLVSpace\n"); if (@(!$_GET['from'])) { $opt['redirect'] = 1; $PageRedirectFmt = ''; } else { $PageRedirectFmt = FmtPageName($PageRedirectFmt, $_GET['from']); } if (@$EnableHTMLCache && !$NoHTMLCache && $PageCacheFile && @filemtime($PageCacheFile) > $LastModTime) { list($ctext) = unserialize(file_get_contents($PageCacheFile)); $FmtV['$PageText'] = "<!--cached-->{$ctext}"; $IsHTMLCached = 1; StopWatch("HandleFastCacheBrowse: using HTMLCached copy"); ## modified } else { $IsHTMLCached = 0; $text = '(:groupheader:)' . @$text . '(:groupfooter:)'; $t1 = time(); $FmtV['$PageText'] = MarkupToHTML($pagename, $text, $opt); if (@$EnableHTMLCache > 0 && !$NoHTMLCache && $PageCacheFile && time() - $t1 + 1 >= $EnableHTMLCache) { $fp = @fopen("{$PageCacheFile},new", "x"); if ($fp) { StopWatch("HandleFastCacheBrowse: HTMLCaching page"); ## modified fwrite($fp, serialize(array($FmtV['$PageText']))); fclose($fp); rename("{$PageCacheFile},new", $PageCacheFile); } } } SDV($HandleBrowseFmt, array(&$PageStartFmt, &$PageRedirectFmt, '$PageText', &$PageEndFmt)); ## begin modified if ($FastCacheValid) { ob_start(); PrintFmt($pagename, $HandleBrowseFmt); $FastCachePage = ob_get_contents(); ob_end_flush(); mkdirp(dirname($fcfile)); if ($FastCacheValid && ($fc = fopen("{$fcfile},new", 'x'))) { StopWatch("HandleFastCacheBrowse: FastCaching {$pagename}"); fwrite($fc, $FastCachePage); fclose($fc); rename("{$fcfile},new", $fcfile); } else { StopWatch("HandleFastCacheBrowse: error writing cache to {$fcfile},new"); } } else { PrintFmt($pagename, $HandleBrowseFmt); } ## end modified }