Piwik\API\DocumentationGenerator::getExampleUrl PHP Method

getExampleUrl() public method

It will not export links for methods such as deleteSite or deleteUser
public getExampleUrl ( string $class, string $methodName, array $parametersToSet = [] ) : string | boolean
$class string the class
$methodName string the method
$parametersToSet array parameters to set
return string | boolean when not possible
    public function getExampleUrl($class, $methodName, $parametersToSet = array())
    {
        $knowExampleDefaultParametersValues = array('access' => 'view', 'userLogin' => 'test', 'passwordMd5ied' => 'passwordExample', 'email' => '[email protected]', 'languageCode' => 'fr', 'url' => 'http://forum.piwik.org/', 'pageUrl' => 'http://forum.piwik.org/', 'apiModule' => 'UserCountry', 'apiAction' => 'getCountry', 'lastMinutes' => '30', 'abandonedCarts' => '0', 'segmentName' => 'pageTitle', 'ip' => '194.57.91.215', 'idSites' => '1,2', 'idAlert' => '1', 'seconds' => '3600');
        foreach ($parametersToSet as $name => $value) {
            $knowExampleDefaultParametersValues[$name] = $value;
        }
        // no links for these method names
        $doNotPrintExampleForTheseMethods = array('deleteSite', 'addSite', 'updateSite', 'addSiteAliasUrls', 'deleteUser', 'addUser', 'updateUser', 'setUserAccess', 'addGoal', 'updateGoal', 'deleteGoal', 'deleteLicenseKey');
        if (in_array($methodName, $doNotPrintExampleForTheseMethods)) {
            return false;
        }
        // we try to give an URL example to call the API
        $aParameters = Proxy::getInstance()->getParametersList($class, $methodName);
        // Kindly force some known generic parameters to appear in the final list
        // the parameter 'format' can be set to all API methods (used in tests)
        // the parameter 'hideIdSubDatable' is used for system tests only
        // the parameter 'serialize' sets php outputs human readable, used in system tests and debug
        // the parameter 'language' sets the language for the response (eg. country names)
        // the parameter 'flat' reduces a hierarchical table to a single level by concatenating labels
        // the parameter 'include_aggregate_rows' can be set to include inner nodes in flat reports
        // the parameter 'translateColumnNames' can be set to translate metric names in csv/tsv exports
        $aParameters['format'] = false;
        $aParameters['hideIdSubDatable'] = false;
        $aParameters['serialize'] = false;
        $aParameters['language'] = false;
        $aParameters['translateColumnNames'] = false;
        $aParameters['label'] = false;
        $aParameters['flat'] = false;
        $aParameters['include_aggregate_rows'] = false;
        $aParameters['filter_offset'] = false;
        //@review without adding this, I can not set filter_offset in $otherRequestParameters system tests
        $aParameters['filter_limit'] = false;
        //@review without adding this, I can not set filter_limit in $otherRequestParameters system tests
        $aParameters['filter_sort_column'] = false;
        //@review without adding this, I can not set filter_sort_column in $otherRequestParameters system tests
        $aParameters['filter_sort_order'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_excludelowpop'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_excludelowpop_value'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_column_recursive'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_pattern'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_pattern_recursive'] = false;
        //@review without adding this, I can not set filter_sort_order in $otherRequestParameters system tests
        $aParameters['filter_truncate'] = false;
        $aParameters['hideColumns'] = false;
        $aParameters['showColumns'] = false;
        $aParameters['filter_pattern_recursive'] = false;
        $aParameters['pivotBy'] = false;
        $aParameters['pivotByColumn'] = false;
        $aParameters['pivotByColumnLimit'] = false;
        $aParameters['disable_queued_filters'] = false;
        $aParameters['disable_generic_filters'] = false;
        $aParameters['expanded'] = false;
        $aParameters['idDimenson'] = false;
        $moduleName = Proxy::getInstance()->getModuleNameFromClassName($class);
        $aParameters = array_merge(array('module' => 'API', 'method' => $moduleName . '.' . $methodName), $aParameters);
        foreach ($aParameters as $nameVariable => &$defaultValue) {
            if (isset($knowExampleDefaultParametersValues[$nameVariable])) {
                $defaultValue = $knowExampleDefaultParametersValues[$nameVariable];
            } elseif ($defaultValue instanceof NoDefaultValue) {
                return false;
            }
        }
        return '?' . Url::getQueryStringFromParameters($aParameters);
    }

Usage Example

Esempio n. 1
0
 /**
  * Given a list of default parameters to set, returns the URLs of APIs to call
  * If any API was specified in $this->apiNotToCall we ensure only these are tested.
  * If any API is set as excluded (see list below) then it will be ignored.
  *
  * @param array $parametersToSet Parameters to set in api call
  * @param array $formats         Array of 'format' to fetch from API
  * @param array $periods         Array of 'period' to query API
  * @param bool  $supertableApi
  * @param bool  $setDateLastN    If set to true, the 'date' parameter will be rewritten to query instead a range of dates, rather than one period only.
  * @param bool|string $language        2 letter language code, defaults to default piwik language
  * @param bool|string $fileExtension
  *
  * @throws Exception
  *
  * @return array of API URLs query strings
  */
 protected function generateApiUrlPermutations($parametersToSet)
 {
     $formats = array($this->testConfig->format);
     $originalDate = $parametersToSet['date'];
     $requestUrls = array();
     $apiMetadata = new DocumentationGenerator();
     // Get the URLs to query against the API for all functions starting with get*
     foreach ($this->getAllApiMethods() as $apiMethodInfo) {
         list($class, $moduleName, $methodName) = $apiMethodInfo;
         $apiId = $moduleName . '.' . $methodName;
         foreach ($this->testConfig->periods as $period) {
             $parametersToSet['period'] = $period;
             // If date must be a date range, we process this date range by adding 6 periods to it
             if ($this->testConfig->setDateLastN) {
                 if (!isset($parametersToSet['dateRewriteBackup'])) {
                     $parametersToSet['dateRewriteBackup'] = $parametersToSet['date'];
                 }
                 $lastCount = $this->testConfig->setDateLastN;
                 $secondDate = date('Y-m-d', strtotime("+{$lastCount} " . $period . "s", strtotime($originalDate)));
                 $parametersToSet['date'] = $originalDate . ',' . $secondDate;
             }
             // Set response language
             if ($this->testConfig->language !== false) {
                 $parametersToSet['language'] = $this->testConfig->language;
             }
             // set idSubtable if subtable API is set
             if ($this->testConfig->supertableApi !== false) {
                 $request = new Request(array('module' => 'API', 'method' => $this->testConfig->supertableApi, 'idSite' => $parametersToSet['idSite'], 'period' => $parametersToSet['period'], 'date' => $parametersToSet['date'], 'format' => 'php', 'serialize' => 0));
                 $content = $request->process();
                 SystemTestCase::assertApiResponseHasNoError($content);
                 // find first row w/ subtable
                 foreach ($content as $row) {
                     if (isset($row['idsubdatatable'])) {
                         $parametersToSet['idSubtable'] = $row['idsubdatatable'];
                         break;
                     }
                 }
                 // if no subtable found, throw
                 if (!isset($parametersToSet['idSubtable'])) {
                     throw new Exception("Cannot find subtable to load for {$apiId} in {$this->testConfig->supertableApi}.");
                 }
             }
             // Generate for each specified format
             foreach ($formats as $format) {
                 $parametersToSet['format'] = $format;
                 $parametersToSet['hideIdSubDatable'] = 1;
                 $parametersToSet['serialize'] = 1;
                 $exampleUrl = $apiMetadata->getExampleUrl($class, $methodName, $parametersToSet);
                 if ($exampleUrl === false) {
                     continue;
                 }
                 // Remove the first ? in the query string
                 $exampleUrl = substr($exampleUrl, 1);
                 $apiRequestId = $apiId;
                 if (strpos($exampleUrl, 'period=') !== false) {
                     $apiRequestId .= '_' . $period;
                 }
                 $apiRequestId .= '.' . $format;
                 if ($this->testConfig->fileExtension) {
                     $apiRequestId .= '.' . $this->testConfig->fileExtension;
                 }
                 $requestUrls[$apiRequestId] = UrlHelper::getArrayFromQueryString($exampleUrl);
             }
         }
     }
     return $requestUrls;
 }
All Usage Examples Of Piwik\API\DocumentationGenerator::getExampleUrl