Example #1
0
 public function get($code, $key)
 {
     if (!isset($this->readers[$code])) {
         $fileName = $this->getFileName($code);
         $this->readers[$code] = false;
         if (file_exists($fileName)) {
             try {
                 $this->readers[$code] = Reader::open($fileName);
             } catch (Exception $e) {
                 wfDebug(__METHOD__ . ": unable to open cdb file for reading\n");
             }
         }
     }
     if (!$this->readers[$code]) {
         return null;
     } else {
         $value = false;
         try {
             $value = $this->readers[$code]->get($key);
         } catch (Exception $e) {
             wfDebug(__METHOD__ . ": \\Cdb\\Exception caught, error message was " . $e->getMessage() . "\n");
         }
         if ($value === false) {
             return null;
         }
         return unserialize($value);
     }
 }
Example #2
0
 /**
  * @since 0.1
  *
  * @param string $target
  */
 public function __construct($target)
 {
     try {
         $this->cdb = Reader::open($target);
     } catch (Exception $e) {
         // Do nothing
     }
 }
Example #3
0
 /**
  * Ensure that password isn't in top X most popular passwords
  *
  * @param $policyVal int Cut off to use. Will automatically shrink to the max
  *   supported for error messages if set to more than max number of passwords on file,
  *   so you can use the PHP_INT_MAX constant here safely.
  * @param $user User
  * @param $password String
  * @since 1.27
  * @return Status
  */
 public static function checkPopularPasswordBlacklist($policyVal, User $user, $password)
 {
     global $wgPopularPasswordFile, $wgSitename;
     $status = Status::newGood();
     if ($policyVal > 0) {
         $langEn = Language::factory('en');
         $passwordKey = $langEn->lc(trim($password));
         // People often use the name of the current site, which won't be
         // in the common password file. Also check '' for people who use
         // just whitespace.
         $sitename = $langEn->lc(trim($wgSitename));
         $hardcodedCommonPasswords = array('', 'wiki', 'mediawiki', $sitename);
         if (in_array($passwordKey, $hardcodedCommonPasswords)) {
             $status->error('passwordtoopopular');
             return $status;
         }
         // This could throw an exception, but there's not a good way
         // of failing gracefully, if say the file is missing, so just
         // let the exception fall through.
         // Format of cdb file is mapping password => popularity rank.
         // See maintenance/createCommonPasswordCdb.php
         $db = CdbReader::open($wgPopularPasswordFile);
         $res = $db->get($passwordKey);
         if ($res && (int) $res <= $policyVal) {
             // Note: If you want to find the true number of common
             // passwords stored (for reporting the error), you have to take
             // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
             $status->error('passwordtoopopular');
         }
     }
     return $status;
 }
Example #4
0
 /**
  * Fetch all interwiki prefixes from interwiki cache
  *
  * @param null|string $local If not null, limits output to local/non-local interwikis
  * @return array List of prefixes
  * @since 1.19
  */
 protected static function getAllPrefixesCached($local)
 {
     global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
     static $db, $site;
     wfDebug(__METHOD__ . "()\n");
     $data = array();
     try {
         if (!$db) {
             $db = CdbReader::open($wgInterwikiCache);
         }
         /* Resolve site name */
         if ($wgInterwikiScopes >= 3 && !$site) {
             $site = $db->get('__sites:' . wfWikiID());
             if ($site == '') {
                 $site = $wgInterwikiFallbackSite;
             }
         }
         // List of interwiki sources
         $sources = array();
         // Global Level
         if ($wgInterwikiScopes >= 2) {
             $sources[] = '__global';
         }
         // Site level
         if ($wgInterwikiScopes >= 3) {
             $sources[] = '_' . $site;
         }
         $sources[] = wfWikiID();
         foreach ($sources as $source) {
             $list = $db->get("__list:{$source}");
             foreach (explode(' ', $list) as $iw_prefix) {
                 $row = $db->get("{$source}:{$iw_prefix}");
                 if (!$row) {
                     continue;
                 }
                 list($iw_local, $iw_url) = explode(' ', $row);
                 if ($local !== null && $local != $iw_local) {
                     continue;
                 }
                 $data[$iw_prefix] = array('iw_prefix' => $iw_prefix, 'iw_url' => $iw_url, 'iw_local' => $iw_local);
             }
         }
     } catch (CdbException $e) {
         wfDebug(__METHOD__ . ": CdbException caught, error message was " . $e->getMessage());
     }
     ksort($data);
     return array_values($data);
 }
 private function getCacheValue($key)
 {
     if ($this->cdbReader === null) {
         if (is_string($this->cdbData)) {
             $this->cdbReader = \Cdb\Reader::open($this->cdbData);
         } elseif (is_array($this->cdbData)) {
             $this->cdbReader = new \Cdb\Reader\Hash($this->cdbData);
         } else {
             $this->cdbReader = false;
         }
     }
     if ($this->cdbReader) {
         return $this->cdbReader->get($key);
     } else {
         return false;
     }
 }
Example #6
0
 $command = array_shift($args);
 // process command
 switch ($command) {
     case 'help':
         // show an help message
         cdbShowHelp(array_shift($args));
         break;
     case 'load':
         if (!isset($args[0])) {
             print "Need a filename there buddy\n";
             break;
         }
         $file = $args[0];
         print "Loading cdb file {$file}...";
         try {
             $fileHandle = CdbReader::open($file);
         } catch (CdbException $e) {
         }
         if (!$fileHandle) {
             print "not a cdb file or unable to read it\n";
         } else {
             print "ok\n";
         }
         break;
     case 'get':
         if (!$fileHandle) {
             print "Need to load a cdb file first\n";
             break;
         }
         if (!isset($args[0])) {
             print "Need to specify a key, Luke\n";
/**
 * The main function
 */
function handleMissingWiki()
{
    $projects = array('wikibooks' => 'b', 'wikinews' => 'n', 'wikipedia' => 'p', 'wikiquote' => 'q', 'wikisource' => 's', 'wikiversity' => 'v', 'wikivoyage' => 'y', 'wiktionary' => 't');
    list($protocol, $host) = getProtocolAndHost();
    if (strpos($host, '.m.') !== false) {
        // Invalid request to mobile site, not rewritten by Varnish
        showMobileError();
        return;
    }
    # $language.$project.org
    $tmp = explode('.', $host);
    $project = $incubatorCode = false;
    if (count($tmp) == 3) {
        list($language, $project, $tld) = $tmp;
        if (isset($_SERVER['PATH_INFO']) && preg_match('!^/(.*)$!', $_SERVER['PATH_INFO'], $m)) {
            $page = $m[1];
        } elseif (isset($_GET['title'])) {
            $page = $_GET['title'];
            # index.php?title=Page
        } else {
            $page = '';
            # Main page
        }
        $incubatorCode = isset($projects[$project]) ? $projects[$project] : null;
    }
    if (!$incubatorCode) {
        showGenericError();
        return;
    }
    if (strpos($page, ':') !== false) {
        # Open the interwiki file to see if we have an interwiki prefix
        $db = null;
        try {
            $db = CdbReader::open(__DIR__ . '/interwiki.cdb');
        } catch (CdbException $e) {
        }
        if ($db) {
            $prefix = strtok($page, ':');
            # Try looking for lateral links (w: q: voy: ...)
            $row = $db->get("{$language}wiki:{$prefix}");
            if (!$row) {
                # Also try interlanguage links
                $projectForCdb = $project === 'wikipedia' ? 'wiki' : $project;
                $row = $db->get("_{$projectForCdb}:{$prefix}");
            }
            if ($row) {
                list($iw_local, $iw_url) = explode(' ', $row);
                if ($iw_local) {
                    # Redirect to the appropriate WMF wiki
                    # strtok gives us the remainder of the page title after the interwiki prefix
                    showRedirect($protocol . ':' . str_replace('$1', strtok(''), $iw_url));
                    return;
                }
            }
            # We don't have an interwiki link, keep going and see what else we could have
        }
    }
    if ($project === 'wikisource') {
        # Wikisource should redirect to the multilingual wikisource
        showRedirect($protocol . '://wikisource.org/wiki/' . $page);
    } elseif ($project === 'wikiversity') {
        # Wikiversity gives an error page
        showMissingSubdomainError($project, $language);
    } else {
        # Redirect to incubator
        $incubatorBase = 'incubator.wikimedia.org/wiki/';
        $location = $protocol . '://' . $incubatorBase . 'W' . $incubatorCode . '/' . urlencode($language);
        # Go to the page if specified (look out for slashes), otherwise go to
        # the main page Wx/xyz?goto=mainpage (WikimediaIncubator extension takes care of that)
        $location .= $page && $page !== '/' ? '/' . $page : '?goto=mainpage' . (isset($_GET['uselang']) ? '&uselang=' . urlencode($_GET['uselang']) : '');
        showRedirect($location);
    }
}
Example #8
0
 private static function getCacheValue($key)
 {
     global $wgInterwikiCache;
     static $reader;
     if ($reader === null) {
         $reader = is_array($wgInterwikiCache) ? false : CdbReader::open($wgInterwikiCache);
     }
     if ($reader) {
         return $reader->get($key);
     } else {
         return isset($wgInterwikiCache[$key]) ? $wgInterwikiCache[$key] : false;
     }
 }