/** * Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to * load CategoryTreeFunctions.php on demand. */ function ajax($category, $mode) { global $wgDBname; $title = self::makeTitle($category); if (!$title) { return false; } #TODO: error message? $this->mIsAjaxRequest = true; # Retrieve page_touched for the category $dbkey = $title->getDBkey(); $dbr =& wfGetDB(DB_SLAVE); $touched = $dbr->selectField('page', 'page_touched', array('page_namespace' => NS_CATEGORY, 'page_title' => $dbkey), __METHOD__); $mckey = "{$wgDBname}:categorytree({$mode}):{$dbkey}"; //FIXME: would need to add depth parameter. $response = new AjaxResponse(); if ($response->checkLastModified($touched)) { return $response; } if ($response->loadFromMemcached($mckey, $touched)) { return $response; } $html = $this->renderChildren($title, $mode); //FIXME: would need to pass depth parameter. if ($html == '') { $html = ' '; } #HACK: Safari doesn't like empty responses. #see Bug 7219 and http://bugzilla.opendarwin.org/show_bug.cgi?id=10716 $response->addText($html); $response->storeInMemcached($mckey, 86400); return $response; }
/** * Entry point for Ajax, registered in $wgAjaxExportList. * This loads CategoryTreeFunctions.php and calls CategoryTree::ajax() */ function efAjaxTest($text, $usestring, $httpcache, $lastmod, $error) { $text = htmlspecialchars($text) . "(" . wfTimestampNow() . ")"; if ($usestring) { return $text; } else { $response = new AjaxResponse($text); if ($error) { throw new Exception($text); } if ($httpcache) { $response->setCacheDuration(24 * 60 * 60); } # cache for a day if ($lastmod) { $response->checkLastModified('19700101000001'); # never modified } return $response; } }
/** * Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to * load CategoryTreeFunctions.php on demand. * @param $category * @param $depth int * @return AjaxResponse|bool */ function ajax($category, $depth = 1) { global $wgLang, $wgContLang, $wgRenderHashAppend; $title = self::makeTitle($category); if (!$title) { return false; # TODO: error message? } # Retrieve page_touched for the category $dbkey = $title->getDBkey(); $dbr = wfGetDB(DB_SLAVE); $touched = $dbr->selectField('page', 'page_touched', array('page_namespace' => NS_CATEGORY, 'page_title' => $dbkey), __METHOD__); $mckey = wfMemcKey("categorytree(" . $this->getOptionsAsCacheKey($depth) . ")", $dbkey, $wgLang->getCode(), $wgContLang->getExtraHashOptions(), $wgRenderHashAppend); $response = new AjaxResponse(); if ($response->checkLastModified($touched)) { return $response; } if ($response->loadFromMemcached($mckey, $touched)) { return $response; } $html = $this->renderChildren($title, $depth); if ($html == '') { # HACK: Safari doesn't like empty responses. # see Bug 7219 and http://bugzilla.opendarwin.org/show_bug.cgi?id=10716 $html = ' '; } $response->addText($html); $response->storeInMemcached($mckey, 86400); return $response; }
/** 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; }
/** * @author Maciej Błaszkowski <marooned at wikia-inc.com> */ function wfAnswersGetEditPointsAjax() { global $wgRequest, $wgSquidMaxage; $userId = intval($wgRequest->getVal('userId')); $points = AttributionCache::getInstance()->getUserEditPoints($userId); $timestamp = AttributionCache::getInstance()->getUserLastModifiedFromCache($userId); $timestamp = !empty($timestamp) ? $timestamp : wfTimestampNow(); $data = array('points' => $points, 'timestamp' => wfTimestampNow()); $json = json_encode($data); $response = new AjaxResponse($json); $response->setContentType('application/json; charset=utf-8'); $response->checkLastModified(strtotime($timestamp)); $response->setCacheDuration($wgSquidMaxage); return $response; }