protected static function callFontConfig($code) { if (ini_get('open_basedir')) { wfDebugLog('fcfont', 'Disabled because of open_basedir is active'); // Most likely we can't access any fonts we might find return false; } $cache = self::getCache(); $cachekey = wfMemckey('fcfont', $code); $timeout = 60 * 60 * 12; $cached = $cache->get($cachekey); if (is_array($cached)) { return $cached; } elseif ($cached === 'NEGATIVE') { return false; } $code = wfEscapeShellArg(":lang={$code}"); $ok = 0; $cmd = "fc-match {$code}"; $suggestion = wfShellExec($cmd, $ok); wfDebugLog('fcfont', "{$cmd} returned {$ok}"); if ($ok !== 0) { wfDebugLog('fcfont', "fc-match error output: {$suggestion}"); $cache->set($cachekey, 'NEGATIVE', $timeout); return false; } $pattern = '/^(.*?): "(.*)" "(.*)"$/'; $matches = array(); if (!preg_match($pattern, $suggestion, $matches)) { wfDebugLog('fcfont', "fc-match: return format not understood: {$suggestion}"); $cache->set($cachekey, 'NEGATIVE', $timeout); return false; } list(, $file, $family, $type) = $matches; wfDebugLog('fcfont', "fc-match: got {$file}: {$family} {$type}"); $file = wfEscapeShellArg($file); $family = wfEscapeShellArg($family); $type = wfEscapeShellArg($type); $cmd = "fc-list {$family} {$type} {$code} file | grep {$file}"; $candidates = trim(wfShellExec($cmd, $ok)); wfDebugLog('fcfont', "{$cmd} returned {$ok}"); if ($ok !== 0) { wfDebugLog('fcfont', "fc-list error output: {$candidates}"); $cache->set($cachekey, 'NEGATIVE', $timeout); return false; } # trim spaces $files = array_map('trim', explode("\n", $candidates)); $count = count($files); if (!$count) { wfDebugLog('fcfont', "fc-list got zero canditates: {$candidates}"); } # remove the trailing ":" $chosen = substr($files[0], 0, -1); wfDebugLog('fcfont', "fc-list got {$count} candidates; using {$chosen}"); $data = array('family' => $family, 'type' => $type, 'file' => $chosen); $cache->set($cachekey, $data, $timeout); return $data; }
protected function store(array $array) { $key = wfMemckey($this->key); $this->cache->set($key, $array); }
/** * Increases the failure count for a given service * @param $service */ public static function reportTranslationServiceFailure($service) { global $wgMemc; $key = wfMemckey("translate-service-{$service}"); $value = $wgMemc->get($key); if (!is_string($value)) { $count = 0; } else { list($count, ) = explode('|', $value, 2); } $count += 1; $failed = wfTimestamp(); $wgMemc->set($key, "{$count}|{$failed}", self::$serviceFailurePeriod * 5); if ($count == self::$serviceFailureCount) { error_log("Translation service {$service} suspended"); } elseif ($count > self::$serviceFailureCount) { error_log("Translation service {$service} still suspended"); } throw new TranslationHelperExpection("web service {$service} failed to provide valid response"); }
/** * This constructs the list of all groups from multiple different * sources. When possible, a cache dependency is created to automatically * recreate the cache when configuration changes. * @todo Reduce the ways of which messages can be added. Target is just * to have three ways: Yaml files, translatable pages and with the hook. * @todo In conjuction with the above, reduce the number of global * variables like wgTranslate#C and have the message groups specify * their own cache dependencies. */ protected static function loadGroupDefinitions() { global $wgTranslateAddMWExtensionGroups; global $wgEnablePageTranslation, $wgTranslateGroupFiles; global $wgTranslateAC, $wgTranslateEC, $wgTranslateCC; global $wgAutoloadClasses; global $wgTranslateWorkflowStates; $deps = array(); $deps[] = new GlobalDependency('wgTranslateAddMWExtensionGroups'); $deps[] = new GlobalDependency('wgEnablePageTranslation'); $deps[] = new GlobalDependency('wgTranslateGroupFiles'); $deps[] = new GlobalDependency('wgTranslateAC'); $deps[] = new GlobalDependency('wgTranslateEC'); $deps[] = new GlobalDependency('wgTranslateCC'); $deps[] = new GlobalDependency('wgTranslateExtensionDirectory'); $deps[] = new GlobalDependency('wgTranslateWorkflowStates'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/mediawiki-defines.txt'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/Wikia/extensions.txt'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/Toolserver/toolserver-textdomains.txt'); if ($wgTranslateAddMWExtensionGroups) { $a = new PremadeMediawikiExtensionGroups(); $a->addAll(); } if ($wgEnablePageTranslation) { $dbr = wfGetDB(DB_MASTER); $tables = array('page', 'revtag'); $vars = array('page_id', 'page_namespace', 'page_title'); $conds = array('page_id=rt_page', 'rt_type' => RevTag::getType('tp:mark')); $options = array('GROUP BY' => 'rt_page'); $res = $dbr->select($tables, $vars, $conds, __METHOD__, $options); foreach ($res as $r) { $title = Title::makeTitle($r->page_namespace, $r->page_title); $id = TranslatablePage::getMessageGroupIdFromTitle($title); $wgTranslateCC[$id] = new WikiPageMessageGroup($id, $title); $wgTranslateCC[$id]->setLabel($title->getPrefixedText()); } } if ($wgTranslateWorkflowStates) { $wgTranslateCC['translate-workflow-states'] = new WorkflowStatesMessageGroup(); } $autoload = array(); wfRunHooks('TranslatePostInitGroups', array(&$wgTranslateCC, &$deps, &$autoload)); foreach ($wgTranslateGroupFiles as $configFile) { wfDebug($configFile . "\n"); $deps[] = new FileDependency(realpath($configFile)); $fgroups = TranslateYaml::parseGroupFile($configFile); foreach ($fgroups as $id => $conf) { if (!empty($conf['AUTOLOAD']) && is_array($conf['AUTOLOAD'])) { $dir = dirname($configFile); foreach ($conf['AUTOLOAD'] as $class => $file) { // For this request and for caching. $wgAutoloadClasses[$class] = "{$dir}/{$file}"; $autoload[$class] = "{$dir}/{$file}"; } } $group = MessageGroupBase::factory($conf); $wgTranslateCC[$id] = $group; } } $key = wfMemckey('translate-groups'); $value = array('ac' => $wgTranslateAC, 'ec' => $wgTranslateEC, 'cc' => $wgTranslateCC, 'autoload' => $autoload); $wrapper = new DependencyWrapper($value, $deps); $wrapper->storeToCache(self::getCache(), $key, 60 * 60 * 2); wfDebug(__METHOD__ . "-end\n"); }
<?php global $IP; require_once 'commandLine.inc'; //require_once("$IP/extensions/wikihow/authors/ArticleAuthors.class.php"); global $wgMemc; $key = wfMemckey('pb-fa-list'); $wgMemc->delete($key); //$title = Title::newFromText('wikiHow:Autoconfirmed-users'); // if ($title) { // print $title->getText()."\n"; // $articleID = $title->getArticleID(); // $authors = ArticleAuthors::getAuthors($articleID); // //logged out // $showAllLink = false; // $link = false; // $authors_hash = md5( print_r($authors, true) . print_r($showAllLink,true) . print_r($link,true)); // $memkey = wfMemcKey('authors', $articleID, $authors_hash); // $wgMemc->delete($memkey); // //logged in // $showAllLink = true; // $link = true; // $authors_hash = md5( print_r($authors, true) . print_r($showAllLink,true) . print_r($link,true)); // $memkey = wfMemcKey('authors', $articleID, $authors_hash); // $wgMemc->delete($memkey); // //dump the full author list cache // $memkey = ArticleAuthors::getLoadAuthorsCachekey($articleID); // $wgMemc->delete($memkey); // } print "done\n";
public static function logMessages() { wfProfileIn(__METHOD__); global $wgAdaptiveMessageCache; if (!$wgAdaptiveMessageCache || !self::$instance instanceof MessageCache) { wfProfileOut(__METHOD__); return; } $cachekey = wfMemckey('message-profiling'); $cache = wfGetCache(CACHE_DB); $data = $cache->get($cachekey); if (!$data) { $data = array(); } $age = self::$mAdaptiveDataAge; $filterDate = substr(wfTimestamp(TS_MW, time() - $age), 0, 8); foreach (array_keys($data) as $key) { if ($key < $filterDate) { unset($data[$key]); } } $index = substr(wfTimestampNow(), 0, 8); if (!isset($data[$index])) { $data[$index] = array(); } foreach (self::$instance->mRequestedMessages as $message => $_) { if (!isset($data[$index][$message])) { $data[$index][$message] = 0; } $data[$index][$message]++; } $cache->set($cachekey, $data); wfProfileOut(__METHOD__); }
/** * Increases the failure count for this service */ protected function reportTranslationServiceFailure($msg) { $service = $this->service; wfDebugLog('translationservices', "Translation service {$service} problem: {$msg}"); $key = wfMemckey("translate-service-{$service}"); $value = wfGetCache(CACHE_ANYTHING)->get($key); if (!is_string($value)) { $count = 0; } else { list($count, ) = explode('|', $value, 2); } $count += 1; $failed = wfTimestamp(); wfGetCache(CACHE_ANYTHING)->set($key, "{$count}|{$failed}", $this->serviceFailurePeriod * 5); if ($count == $this->serviceFailureCount) { wfDebugLog('translationservices', "Translation service {$service} suspended"); } elseif ($count > $this->serviceFailureCount) { wfDebugLog('translationservices', "Translation service {$service} still suspended"); } }
/** * Searches for suitable font in the system. * @param $code \string Language code. * * @return bool|string */ public static function find( $code ) { if ( ini_get( 'open_basedir' ) ) { wfDebugLog( 'fcfont', 'Disabled because of open_basedir is active' ); // Most likely we can't access any fonts we might find return false; } $cache = self::getCache(); $cachekey = wfMemckey( 'fcfont', $code ); $timeout = 60 * 60 * 12; $cached = $cache->get( $cachekey ); if ( is_string( $cached ) ) { return $cached === 'NEGATIVE' ? false : $cached; } $code = wfEscapeShellArg( ":lang=$code" ); $ok = 0; $cmd = "fc-match $code"; $suggestion = wfShellExec( $cmd, $ok ); wfDebugLog( 'fcfont', "$cmd returned $ok" ); if ( $ok !== 0 ) { wfDebugLog( 'fcfont', "fc-match error output: $suggestion" ); $cache->set( $cachekey, 'NEGATIVE', $timeout ); return false; } $pattern = '/^(.*?): "(.*)" "(.*)"$/'; $matches = array(); if ( !preg_match( $pattern, $suggestion, $matches ) ) { wfDebugLog( 'fcfont', "fc-match: return format not understood: $suggestion" ); $cache->set( $cachekey, 'NEGATIVE', $timeout ); return false; } list( , $file, $family, $type ) = $matches; wfDebugLog( 'fcfont', "fc-match: got $file: $family $type" ); $file = wfEscapeShellArg( $file ); $family = wfEscapeShellArg( $family ); $type = wfEscapeShellArg( $type ); $cmd = "fc-list $family $type $code file | grep $file"; $candidates = trim( wfShellExec( $cmd, $ok ) ); wfDebugLog( 'fcfont', "$cmd returned $ok" ); if ( $ok !== 0 ) { wfDebugLog( 'fcfont', "fc-list error output: $candidates" ); $cache->set( $cachekey, 'NEGATIVE', $timeout ); return false; } # trim spaces $files = array_map( 'trim', explode( "\n", $candidates ) ); $count = count( $files ); if ( !$count ) { wfDebugLog( 'fcfont', "fc-list got zero canditates: $candidates" ); } # remove the trailing ":" $chosen = substr( $files[0], 0, -1 ); wfDebugLog( 'fcfont', "fc-list got $count candidates; using $chosen" ); $cache->set( $cachekey, $chosen, $timeout ); return $chosen; }
/** * Gets the description of a group. This is a bit slow thing to do for * thousand+ groups, so some caching is involved. * @param $group MessageGroup * @return string Plain text */ public function getGroupDescription( MessageGroup $group ) { $code = $this->lang->getCode(); $cache = wfGetCache( CACHE_ANYTHING ); $key = wfMemckey( "translate-groupdesc-$code-" . $group->getId() ); $desc = $cache->get( $key ); if ( is_string( $desc ) ) { return $desc; } $realFunction = array( 'MessageCache', 'singleton' ); if ( is_callable( $realFunction ) ) { $mc = MessageCache::singleton(); } else { global $wgMessageCache; $mc = $wgMessageCache; } $desc = $mc->transform( $group->getDescription(), true, $this->lang ); $cache->set( $key, $desc ); return $desc; }
function getFeaturedArticles() { global $wgMemc; $cachekey = wfMemckey('pb-fa-list'); $fas = $wgMemc->get($cachekey); if (is_array($fas)) { self::$featuredArticles = $fas; } if (!self::$featuredArticles) { // LIST ALL FEATURED ARTICLES $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select(array('templatelinks', 'page'), array('page_title'), array('tl_from = page_id', 'tl_title' => 'Fa'), __METHOD__); foreach ($res as $row) { self::$featuredArticles[$row->page_title] = 1; } $wgMemc->set($cachekey, self::$featuredArticles); } return self::$featuredArticles; }
protected function cacheSourceText($code, $ids) { $cache = wfGetCache(CACHE_DB); $key = wfMemckey(__CLASS__, 'cc', $code); $text = $cache->get($key); if (!is_string($text)) { $snippets = array(); $ids = explode('|', $ids); $len = count($ids); if ($len < 1000) { $this->output("{$code}: {$len} SKIPPED\n"); return ''; } else { $this->output("{$code} PROCESSING\n"); } $time = microtime(true); foreach ($ids as $id) { $params = new FauxRequest(array('pageid' => $id, 'action' => 'parse', 'prop' => 'text', 'disablepp' => 'true')); $api = new ApiMain($params); $api->execute(); if (defined('ApiResult::META_CONTENT')) { $result = $api->getResult()->getResultData(null, array('BC' => array())); } else { $result = $api->getResultData(); } $text = $result['parse']['text']['*']; $text = strip_tags($text); $text = str_replace('!!FUZZY!!', '', $text); $text = preg_replace('/\\$[0-9]/', '', $text); $text = trim($text); $snippets[] = $text; } $text = implode(" ", $snippets); $cache->set($key, $text, 3600 * 24); $delta = microtime(true) - $time; $this->output("{$code} TOOK {$delta}\n"); } else { $this->output("{$code} FROM CACHE\n"); } return $text; }
/** * This constructs the list of all groups from multiple different * sources. When possible, a cache dependency is created to automatically * recreate the cache when configuration changes. */ protected function loadGroupDefinitions() { global $wgAutoloadClasses; $groups = $deps = $autoload = array(); Hooks::run('TranslatePostInitGroups', array(&$groups, &$deps, &$autoload)); // Register autoloaders for this request, both values modified by reference self::appendAutoloader($autoload, $wgAutoloadClasses); $key = wfMemckey('translate-groups'); $value = array('cc' => $groups, 'autoload' => $autoload); $wrapper = new DependencyWrapper($value, $deps); $wrapper->storeToCache($this->getCache(), $key, 60 * 60 * 2); return $groups; }