/**
  * @param string|null $typeBase
  * @param string|null $typeLocal
  */
 public function __construct($typeBase, $typeLocal)
 {
     Assert::parameterType('string|null', $typeBase, '$typeBase');
     Assert::parameterType('string|null', $typeLocal, '$typeLocal');
     $this->typeBase = $typeBase;
     $this->typeLocal = $typeLocal;
 }
 /**
  * @param string $format One of the SnakFormatter::FORMAT_... constants.
  * @param SnakFormatter $formatter A formatter returning plain text.
  * @param callable $escapeCallback A callable taking plain text and returning escaped text.
  *
  * @throws ParameterTypeException
  */
 public function __construct($format, SnakFormatter $formatter, $escapeCallback)
 {
     Assert::parameterType('string', $format, '$format');
     Assert::parameterType('callable', $escapeCallback, '$escapeCallback');
     $this->format = $format;
     $this->formatter = $formatter;
     $this->escapeCallback = $escapeCallback;
 }
 public function setServices(HttpClient $httpClient, $siteMatrix, $outputFile)
 {
     Assert::parameterType('string', $siteMatrix, '$siteMatrix');
     Assert::parameterType('string', $outputFile, '$outputFile');
     $this->httpClient = $httpClient;
     $this->siteMatrix = $siteMatrix;
     $this->outputFile = $outputFile;
 }
 /**
  * Overrides the default new for categorization callback
  * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
  *
  * @param callable $callback
  * @see RecentChange::newForCategorization for callback signiture
  *
  * @throws MWException
  */
 public function overrideNewForCategorizationCallback($callback)
 {
     if (!defined('MW_PHPUNIT_TEST')) {
         throw new MWException('Cannot override newForCategorization callback in operation.');
     }
     Assert::parameterType('callable', $callback, '$callback');
     $this->newForCategorizationCallback = $callback;
 }
Esempio n. 5
0
 public function merge(MergeableUpdate $update)
 {
     /** @var SiteStatsUpdate $update */
     Assert::parameterType(__CLASS__, $update, '$update');
     foreach (self::$counters as $field) {
         $this->{$field} += $update->{$field};
     }
 }
 public function __construct($sitesFile)
 {
     Assert::parameterType('string', $sitesFile, '$sitesFile');
     if (!is_readable($sitesFile)) {
         throw new \InvalidArgumentException('$sitesFile ' . $sitesFile . ' is not readable');
     }
     $this->sitesFile = $sitesFile;
 }
 /**
  * @param Term $matchedTerm
  * @param string $matchedTermType
  * @param EntityId $entityId
  * @param Term|null $displayLabel
  * @param Term|null $displayDescription
  */
 public function __construct(Term $matchedTerm, $matchedTermType, EntityId $entityId, Term $displayLabel = null, Term $displayDescription = null)
 {
     Assert::parameterType('string', $matchedTermType, '$matchedTermType');
     $this->matchedTerm = $matchedTerm;
     $this->matchedTermType = $matchedTermType;
     $this->entityId = $entityId;
     $this->displayLabel = $displayLabel;
     $this->displayDescription = $displayDescription;
 }
 public function __construct(HttpClient $httpClient, $siteMatrixUrl, $outputFile)
 {
     Assert::parameterType('string', $siteMatrixUrl, '$siteMatrixUrl');
     Assert::parameterType('string', $outputFile, '$outputFile');
     $this->dumper = new Dumper();
     $this->httpClient = $httpClient;
     $this->siteMatrixUrl = $siteMatrixUrl;
     $this->outputFile = $outputFile;
 }
 /**
  * Saves the change to a database table.
  *
  * @note Only supports Change objects that are derived from ChangeRow.
  *
  * @param Change $change
  *
  * @throws DBQueryError
  */
 public function saveChange(Change $change)
 {
     Assert::parameterType('Wikibase\\ChangeRow', $change, '$change');
     if ($change->getId() === null) {
         $this->insertChange($change);
     } else {
         $this->updateChange($change);
     }
 }
Esempio n. 10
0
 public function testParameterType_catch()
 {
     try {
         Assert::parameterType('string', 17, 'test');
         $this->fail('Expected exception');
     } catch (AssertionException $ex) {
         // ok
     }
 }
 /**
  * @param string $format The output format generated by this formatter. All SnakFormatters
  *  provided via $formattersBySnakType and $formattersByDataType must be safe for this
  *  output format. This is checked by comparing the $format with what each SnakFormatter
  *  returns from getFormat(). MIME parameters are ignored for this check, so FORMAT_HTML
  *  is considered compatible with FORMAT_HTML_DIFF, etc.
  * @param PropertyDataTypeLookup $dataTypeLookup
  * @param SnakFormatter[] $formattersBySnakType An associative array mapping snak types
  *  to SnakFormatter objects. If no formatter is defined for the a given snak type,
  *  $formattersByDataType will be checked for a SnakFormatter for the snak's data type.
  * @param SnakFormatter[] $formattersByDataType An associative array mapping data types
  *  to SnakFormatter objects. If no formatter is defined for the a given data type,
  *  the "*" key in this array is checked for a default formatter.
  *
  * @throws InvalidArgumentException If any of the given formatters is incompatible
  *         with $format. Formats are assumed to be represented by MIME types,
  *         MIME parameters are ignored.
  */
 public function __construct($format, PropertyDataTypeLookup $dataTypeLookup, array $formattersBySnakType, array $formattersByDataType)
 {
     Assert::parameterType('string', $format, '$format');
     $this->assertFormatterArray($format, $formattersBySnakType);
     $this->assertFormatterArray($format, $formattersByDataType);
     $this->format = $format;
     $this->dataTypeLookup = $dataTypeLookup;
     $this->formattersBySnakType = $formattersBySnakType;
     $this->formattersByDataType = $formattersByDataType;
 }
 /**
  * @param string $type The data type or value type to register the formatter factory for.
  *        Use the "PT:" prefix for data types and "VT:" for value types.
  * @param callable|null $factoryFunction The factory method. Will be called with two parameters,
  *        a string indicating the desired target format, and a FormatterOptions object. The
  *        callback must return a ValueFormatter suitable for emitting the given output format,
  *        or null.
  */
 public function setFormatterFactoryCallback($type, $factoryFunction)
 {
     Assert::parameterType('string', $type, '$type');
     Assert::parameterType('callable|null', $factoryFunction, '$factoryFunction');
     if ($factoryFunction === null) {
         unset($this->factoryFunctions[$type]);
     } else {
         $this->factoryFunctions[$type] = $factoryFunction;
     }
 }
 /**
  * @param int $revisionId
  * @param string $mode One of the self::FROM_... constants.
  *
  * @return Change|null
  */
 public function loadByRevisionId($revisionId, $mode = self::FROM_SLAVE)
 {
     Assert::parameterType('integer', $revisionId, '$revisionId');
     $change = $this->loadChanges(array('change_revision_id' => $revisionId), array('LIMIT' => 1), __METHOD__, $mode === self::FROM_MASTER ? DB_MASTER : DB_SLAVE);
     if (isset($change[0])) {
         return $change[0];
     } else {
         return null;
     }
 }
 /**
  * @param string $fileName File name, without the File: prefix.
  *
  * @return string|null The normalized file name or null if the page does not exist
  * @throws InvalidArgumentException
  */
 public function lookupFileName($fileName)
 {
     Assert::parameterType('string', $fileName, '$pageName');
     $cachedValue = $this->cache->get($this->getCacheKey($fileName));
     if ($cachedValue !== false) {
         return $cachedValue;
     }
     $actualFileName = $this->doLookup($fileName);
     $this->cacheResult($fileName, $actualFileName);
     return $actualFileName;
 }
 /**
  * @see SnakFormatter::format
  *
  * Formats the given Snak as an wikitext link to an authoritative resource.
  * The URL of that link is determined using a SnakUrlExpander.
  * If the snak could not be expanded into a URL, the identifier is returned as simple text.
  *
  * @param Snak $snak
  *
  * @throws ParameterTypeException if $snak is not a PropertyValueSnak, or if $snak->getDataValue()
  * does not return a StringValue.
  * @return string Wikitext
  */
 public function formatSnak(Snak $snak)
 {
     Assert::parameterType('Wikibase\\DataModel\\Snak\\PropertyValueSnak', $snak, '$snak');
     /** @var PropertyValueSnak $snak */
     $id = $snak->getDataValue()->getValue();
     $url = $this->urlExpander->expandUrl($snak);
     if ($url === null) {
         return wfEscapeWikiText($id);
     } else {
         return '[' . $this->escapeWikitextInUrl($url) . ' ' . wfEscapeWikiText($id) . ']';
     }
 }
Esempio n. 16
0
 /**
  * Constructs a TitleValue.
  *
  * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
  * from a database entry, or by a TitleParser. We could apply "some" normalization here,
  * such as substituting spaces by underscores, but that would encourage the use of
  * un-normalized text when constructing TitleValues. For constructing a TitleValue from
  * user input or external sources, use a TitleParser.
  *
  * @param int $namespace The namespace ID. This is not validated.
  * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
  * @param string $fragment The fragment title. Use '' to represent the whole page.
  *   No validation or normalization is applied.
  *
  * @throws InvalidArgumentException
  */
 public function __construct($namespace, $dbkey, $fragment = '')
 {
     Assert::parameterType('integer', $namespace, '$namespace');
     Assert::parameterType('string', $dbkey, '$dbkey');
     Assert::parameterType('string', $fragment, '$fragment');
     // Sanity check, no full validation or normalization applied here!
     Assert::parameter(!preg_match('/^_|[ \\r\\n\\t]|_$/', $dbkey), '$dbkey', 'invalid DB key');
     Assert::parameter($dbkey !== '', '$dbkey', 'should not be empty');
     $this->namespace = $namespace;
     $this->dbkey = $dbkey;
     $this->fragment = $fragment;
 }
 /**
  * @see SnakUrlExpander::expandUrl
  *
  * @param PropertyValueSnak $snak
  *
  * @return string|null A URL or URI derived from the Snak, or null if no such URL
  *         could be determined.
  */
 public function expandUrl(PropertyValueSnak $snak)
 {
     $propertyId = $snak->getPropertyId();
     $value = $snak->getDataValue();
     Assert::parameterType('DataValues\\StringValue', $value, '$snak->getDataValue()');
     $pattern = $this->infoProvider->getPropertyInfo($propertyId);
     if ($pattern === null) {
         return null;
     }
     $id = urlencode($value->getValue());
     $url = str_replace('$1', $id, $pattern);
     return $url;
 }
 /**
  * @see SnakFormatter::format
  *
  * Formats the given Snak's value as a HTML link.
  * The URL of that link is determined using a SnakUrlExpander.
  * If the snak could not be expanded into a URL, the identifier is returned as simple text.
  *
  * @param Snak $snak
  *
  * @throws ParameterTypeException if $snak is not a PropertyValueSnak, or if $snak->getDataValue()
  * does not return a StringValue.
  * @return string HTML
  */
 public function formatSnak(Snak $snak)
 {
     Assert::parameterType('Wikibase\\DataModel\\Snak\\PropertyValueSnak', $snak, '$snak');
     /** @var PropertyValueSnak $snak */
     $id = $snak->getDataValue()->getValue();
     $url = $this->urlExpander->expandUrl($snak);
     $attr = array('class' => 'wb-external-id');
     if ($url === null) {
         return Html::element('span', $attr, $id);
     } else {
         $attr['href'] = $url;
         return Html::element('a', $attr, $id);
     }
 }
Esempio n. 19
0
 /**
  * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
  * registered factory function for both is the same. Cache config is not copied,
  * and only instances of caches defined on this instance with the same config
  * are copied.
  *
  * @see SalvageableService::salvage()
  *
  * @param SalvageableService $other The object to salvage state from. $other must have the
  * exact same type as $this.
  */
 public function salvage(SalvageableService $other)
 {
     Assert::parameterType(self::class, $other, '$other');
     /** @var ConfigFactory $other */
     foreach ($other->factoryFunctions as $name => $otherFunc) {
         if (!isset($this->factoryFunctions[$name])) {
             continue;
         }
         // if the callback function is the same, salvage the Cache object
         // XXX: Closures are never equal!
         if (isset($other->configs[$name]) && $this->factoryFunctions[$name] == $otherFunc) {
             $this->configs[$name] = $other->configs[$name];
             unset($other->configs[$name]);
         }
     }
     // disable $other
     $other->factoryFunctions = [];
     $other->configs = [];
 }
Esempio n. 20
0
 /**
  * @param int $maxKeys Maximum number of entries allowed (min 1).
  * @throws Exception When $maxCacheKeys is not an int or not above zero.
  */
 public function __construct($maxKeys)
 {
     Assert::parameterType('integer', $maxKeys, '$maxKeys');
     Assert::parameter($maxKeys > 0, '$maxKeys', 'must be above zero');
     $this->maxCacheKeys = $maxKeys;
 }
 /**
  * @param string $hookName
  */
 public function __construct($hookName)
 {
     Assert::parameterType('string', $hookName, '$hookName');
     $this->hookName = $hookName;
 }
Esempio n. 22
0
 /**
  * Get a statistically unique 128-bit unsigned integer ID string.
  * The bits of the UID are prefixed with the time (down to the millisecond).
  *
  * These IDs are suitable as globally unique IDs, without any enforced uniqueness.
  * New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast.
  * They can also be stored as "DECIMAL(39) UNSIGNED" or BINARY(16) in MySQL.
  *
  * UID generation is serialized on each server (as the node ID is for the whole machine).
  *
  * @param int $base Specifies a base other than 10
  * @return string Number
  * @throws MWException
  */
 public static function newTimestampedUID128($base = 10)
 {
     Assert::parameterType('integer', $base, '$base');
     Assert::parameter($base <= 36, '$base', 'must be <= 36');
     Assert::parameter($base >= 2, '$base', 'must be >= 2');
     $gen = self::singleton();
     $time = $gen->getTimestampAndDelay('lockFile128', 16384, 1048576);
     return wfBaseConvert($gen->getTimestampedID128($time), 2, $base);
 }
 /**
  * @param bool $useLanguageFallback
  */
 public function setUseLanguageFallback($useLanguageFallback)
 {
     Assert::parameterType('boolean', $useLanguageFallback, '$useLanguageFallback');
     $this->useLanguageFallback = $useLanguageFallback;
 }
 /**
  * @param IContextSource $context
  * @param string $specialPageName
  *
  * @return ChangesListSpecialPageHooksHandler
  */
 private static function newFromGlobalState(IContextSource $context, $specialPageName)
 {
     Assert::parameterType('string', $specialPageName, '$specialPageName');
     $settings = WikibaseClient::getDefaultInstance()->getSettings();
     return new self($context->getRequest(), $context->getUser(), LBFactory::singleton()->getMainLB(), $specialPageName, $settings->getSetting('showExternalRecentChanges'));
 }
Esempio n. 25
0
 /**
  * Get a statistically unique 128-bit unsigned integer ID string.
  * The bits of the UID are prefixed with the time (down to the millisecond).
  *
  * These IDs are suitable as globally unique IDs, without any enforced uniqueness.
  * New rows almost always have higher UIDs, which makes B-TREE updates on INSERT fast.
  * They can also be stored as "DECIMAL(39) UNSIGNED" or BINARY(16) in MySQL.
  *
  * UID generation is serialized on each server (as the node ID is for the whole machine).
  *
  * @param int $base Specifies a base other than 10
  * @return string Number
  * @throws RuntimeException
  */
 public static function newTimestampedUID128($base = 10)
 {
     Assert::parameterType('integer', $base, '$base');
     Assert::parameter($base <= 36, '$base', 'must be <= 36');
     Assert::parameter($base >= 2, '$base', 'must be >= 2');
     $gen = self::singleton();
     $info = $gen->getTimeAndDelay('lockFile128', 16384, 1048576, 1048576);
     $info['offsetCounter'] = $info['offsetCounter'] % 1048576;
     return Wikimedia\base_convert($gen->getTimestampedID128($info), 2, $base);
 }
Esempio n. 26
0
 public function merge(MergeableUpdate $update)
 {
     /** @var CdnCacheUpdate $update */
     Assert::parameterType(__CLASS__, $update, '$update');
     $this->urls = array_merge($this->urls, $update->urls);
 }
Esempio n. 27
0
 /**
  * Resize the maximum number of cache entries, removing older entries as needed
  *
  * @param int $maxKeys
  * @return void
  * @throws UnexpectedValueException
  */
 public function resize($maxKeys)
 {
     Assert::parameterType('integer', $maxKeys, '$maxKeys');
     Assert::parameter($maxKeys > 0, '$maxKeys', 'must be above zero');
     $this->maxCacheKeys = $maxKeys;
     while (count($this->cache) > $this->maxCacheKeys) {
         reset($this->cache);
         $evictKey = key($this->cache);
         unset($this->cache[$evictKey]);
         unset($this->cacheTimes[$evictKey]);
     }
 }
Esempio n. 28
0
 /**
  * Resets a service by dropping the service instance.
  * If the service instances implements DestructibleService, destroy()
  * is called on the service instance.
  *
  * @warning This is generally unsafe! Other services may still retain references
  * to the stale service instance, leading to failures and inconsistencies. Subclasses
  * may use this method to reset specific services under specific instances, but
  * it should not be exposed to application logic.
  *
  * @note This is declared final so subclasses can not interfere with the expectations
  * disableService() has when calling resetService().
  *
  * @see redefineService()
  * @see disableService().
  *
  * @param string $name The name of the service to reset.
  * @param bool $destroy Whether the service instance should be destroyed if it exists.
  *        When set to false, any existing service instance will effectively be detached
  *        from the container.
  *
  * @throws RuntimeException if $name is not a known service.
  */
 protected final function resetService($name, $destroy = true)
 {
     Assert::parameterType('string', $name, '$name');
     $instance = $this->peekService($name);
     if ($destroy && $instance instanceof DestructibleService) {
         $instance->destroy();
     }
     unset($this->services[$name]);
     unset($this->disabled[$name]);
 }
 /**
  * @param PropertyInfoStore $infoStore
  * @param string $propertyInfoKey Name of the desired field in the PropertyInfo array.
  *        Use one of the PropertyInfoStore::KEY_XXX constants.
  */
 public function __construct(PropertyInfoStore $infoStore, $propertyInfoKey)
 {
     Assert::parameterType('string', $propertyInfoKey, '$propertyInfoKey');
     $this->infoStore = $infoStore;
     $this->propertyInfoKey = $propertyInfoKey;
 }
Esempio n. 30
0
 public function testParameterType_catch()
 {
     $this->setExpectedException('Wikimedia\\Assert\\AssertionException');
     Assert::parameterType('string', 17, 'test');
 }