static function color_getColorData($page_title, $page_id = 0, $rev_id = 0) { $ctx = stream_context_create( array('http' => array( 'timeout' => self::TRUST_TIMEOUT, )) ); $MAX_TIMES_THROUGH = 2; $times_though=0; global $wgUser, $wgWikiTrustContentServerURL; $username = $wgUser->getName(); $url = $wgWikiTrustContentServerURL . "?method=wikiorhtml" . "&revid=" . urlencode($rev_id) . "&pageid=" . urlencode($page_id) . "&title=" . urlencode($page_title) . "&time=" . urlencode(wfTimestampNow()) . "&username="******":".__LINE__.": $url"); $colored_raw = (file_get_contents($url, 0, $ctx)); if (!$colored_raw) return ''; $mode = substr($colored_raw, 0, 1); $colored_text = substr($colored_raw, 1); if (!$colored_text || $colored_raw == self::NOT_FOUND_TEXT_TOKEN || $colored_raw == "bad") { return ''; } // Are we using HTML or WIKI if ($mode == WIKITRUST_HTML){ // The HTML is all rendered -- we just display this. self::$html_rendered = true; return $colored_text; } // Pick off the median value first. $colored_data = explode(",", $colored_text, 2); $colored_new = $colored_data[1]; self::$median = $colored_data[0] + 0; if (self::$median == 0) self::$median = self::TRUST_DEFAULT_MEDIAN; return $colored_new; }
/** Returns colored markup. @return colored markup. */ static function ajax_getColoredText($page_title_raw, $page_id_raw = NULL, $rev_id_raw = NULL) { global $wgParser, $wgWikiTrustContentServerURL, $wgWikiTrustApiURL, $wgUser; global $wgMemc; $response = new AjaxResponse(""); $request_headers = apache_request_headers(); // Try to use gzip for the content, if possible. // Ian - This isn't working with redherring, for some reason. if (strstr($request_headers["Accept-Encoding"], "gzip")) { // $response->setContentType("gzip"); } // Can set this to use client side caching, but this can also cause // problems. // Mark that the content can be cached // $response->setCacheDuration(self::TRUST_CACHE_VALID); if (!$page_id_raw || !$rev_id_raw) { $data = array('action' => 'query', 'prop' => 'revisions', 'titles' => $page_title_raw, 'rvlimit' => '1', 'rvprop' => 'ids', 'format' => 'json'); $page_info_raw = file_get_contents($wgWikiTrustApiURL . http_build_query($data)); $page_json = json_decode($page_info_raw, true); $pages_arr = array_keys($page_json["query"]["pages"]); // Now, parse out only what we need if (!$page_id_raw) { $page_id_raw = $pages_arr[0]; } if (!$rev_id_raw) { $rev_id_raw = $page_json["query"]["pages"][$page_id_raw]["revisions"][0]["revid"]; } } $dbr =& wfGetDB(DB_SLAVE); $page_id = $dbr->strencode($page_id_raw, $dbr); $rev_id = $dbr->strencode($rev_id_raw, $dbr); $page_title = $dbr->strencode($page_title_raw, $dbr); // Check the If-Modified-Since header. // If the timestamp of the requested revision is earlier than the IMS // header, return 304 and do nothing further. $rev_ts = '19700101000000'; $res = $dbr->select(self::util_getDbTable('wikitrust_colored_markup'), array('revision_createdon'), array('revision_id' => $rev_id), array()); if ($res && $dbr->numRows($res) > 0) { $row = $dbr->fetchRow($res); $rev_ts = $row['revision_createdon']; if (!$rev_ts) { $rev_ts = '19700101000000'; } } $dbr->freeResult($res); if ($response->checkLastModified($rev_ts)) { return $response; } // See if we have a cached version of the colored text, or if // we need to generate new text. $memcKey = wfMemcKey('revisiontext', 'revid', $rev_id); $cached_text = $wgMemc->get($memcKey); if ($cached_text) { $response->addText($cached_text); return $response; } // Since we are here, we need to get the colored HTML the hard way. $ctx = stream_context_create(array('http' => array('timeout' => self::TRUST_TIMEOUT))); // TODO: Should we do doing this via HTTPS? Or POST? // TODO: in RemoteMode, shouldn't we use local database? $colored_raw = file_get_contents($wgWikiTrustContentServerURL . "rev=" . urlencode($rev_id) . "&page=" . urlencode($page_id) . "&page_title=" . urlencode($page_title) . "&time=" . urlencode(wfTimestampNow()) . "&user=0", 0, $ctx); if ($colored_raw && $colored_raw != self::NOT_FOUND_TEXT_TOKEN) { // Inflate. Pick off the first 10 bytes for python-php conversion. $colored_raw = gzinflate(substr($colored_raw, 10)); // Pick off the median value first. $colored_data = explode(",", $colored_raw, 2); $colored_text = $colored_data[1]; if (preg_match("/^[+-]?(([0-9]+)|([0-9]*\\.[0-9]+|[0-9]+\\.[0-9]*)|\n\t\t\t (([0-9]+|([0-9]*\\.[0-9]+|[0-9]+\\.[0-9]*))[eE][+-]?[0-9]+))\$/", $colored_data[0])) { self::$median = $colored_data[0]; if ($colored_data[0] == 0) { self::$median = self::TRUST_DEFAULT_MEDIAN; } } // First, make sure that there are not any instances of our tokens in the colored_text $colored_text = str_replace(self::TRUST_OPEN_TOKEN, "", $colored_text); $colored_text = str_replace(self::TRUST_CLOSE_TOKEN, "", $colored_text); $colored_text = preg_replace("/'/", "'", $colored_text, -1); $colored_text = preg_replace("/&/", "&", $colored_text, -1); $colored_text = preg_replace("/</", self::TRUST_OPEN_TOKEN, $colored_text, -1); $colored_text = preg_replace("/>/", self::TRUST_CLOSE_TOKEN, $colored_text, -1); $title = Title::newFromText($page_title); $options = ParserOptions::newFromUser($wgUser); $parsed = $wgParser->parse($colored_text, $title, $options); $text = $parsed->getText(); $count = 0; // Update the trust tags $text = preg_replace_callback("/\\{\\{#t:(\\d+),(\\d+),(.*?)\\}\\}/", "WikiTrust::color_handleParserRe", $text, -1, $count); // Update open, close, images, and links. $text = preg_replace('/' . self::TRUST_OPEN_TOKEN . '/', "<", $text, -1, $count); // Regex broken for some pages. // Removing for now. /** $text = preg_replace('/<a href="(.*?)(File):(.*?)" (.*?)>/' , self::TRUST_OPEN_TOKEN, $text, -1, $count); $text = preg_replace('/<a href="(.*?)(Image):(.*?)" (.*?)>/' , self::TRUST_OPEN_TOKEN, $text, -1, $count); */ $text = preg_replace('/<a href="(.*?)title=(.*?)&action=edit&redlink=1" class="new" title="(.*?) \\(not yet written\\)">/', '<a href="/wiki/$2" title="$3">', $text, -1, $count); /* $text = preg_replace_callback( '/'.self::TRUST_OPEN_TOKEN .'(Image|File):(.*?)<\/a>/' ,"WikiTrust::color_getImageInfo" ,$text, -1, $count); */ $text = preg_replace('/' . self::TRUST_CLOSE_TOKEN . '/', ">", $text, -1, $count); $text = preg_replace('/<\\/p>/', "</span></p>", $text, -1, $count); $text = preg_replace('/<p><\\/span>/', "<p>", $text, -1, $count); $text = preg_replace('/<li><\\/span>/', "<li>", $text, -1, $count); // Save the finished text in the cache. $wgMemc->set($memcKey, $text, self::TRUST_CACHE_VALID); // And finally return the colored HTML. $response->addText($text); // And mark that we have the colored version, for cache control. $dbw = wfGetDB(DB_MASTER); $dbw->begin(); $dbw->insert('wikitrust_colored_markup', array('revision_id' => $rev_id, 'revision_text' => "memcached", 'revision_createdon' => wfTimestampNow()), 'Database::insert', array('IGNORE')); $dbw->commit(); } else { // text not found. $response = new AjaxResponse(self::NOT_FOUND_TEXT_TOKEN); } return $response; }