Skip to content

Commit

Permalink
Refs #4059 Converting dozens of plugins/* classes to use Namespaces, …
Browse files Browse the repository at this point in the history
…\Piwik\Plugins\*

Added namespaces to functions.php files in the five plugins that had one
Work in progress
  • Loading branch information
mattab committed Aug 2, 2013
1 parent 61997f4 commit a96ec40
Show file tree
Hide file tree
Showing 294 changed files with 3,837 additions and 3,263 deletions.
6 changes: 3 additions & 3 deletions core/API/DataTableManipulator.php
Expand Up @@ -14,7 +14,7 @@
use Piwik\DataTable\Row;
use Piwik\Period\Range;
use Piwik\DataTable;
use Piwik_API_API;
use Piwik\Plugins\API\API;
use Piwik\API\Proxy;
use Piwik\API\ResponseBuilder;

Expand Down Expand Up @@ -126,7 +126,7 @@ protected function loadSubtable($dataTable, $row)
}
}

$class = 'Piwik_' . $this->apiModule . '_API';
$class = Request::getClassNameAPI( $this->apiModule );
$method = $this->getApiMethodForSubtable();

$this->manipulateSubtableRequest($request);
Expand Down Expand Up @@ -166,7 +166,7 @@ protected abstract function manipulateSubtableRequest(&$request);
private function getApiMethodForSubtable()
{
if (!$this->apiMethodForSubtable) {
$meta = Piwik_API_API::getInstance()->getMetadata('all', $this->apiModule, $this->apiMethod);
$meta = API::getInstance()->getMetadata('all', $this->apiModule, $this->apiMethod);
if (isset($meta[0]['actionToLoadSubTables'])) {
$this->apiMethodForSubtable = $meta[0]['actionToLoadSubTables'];
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/API/DocumentationGenerator.php
Expand Up @@ -33,9 +33,9 @@ public function __construct()
{
$plugins = PluginsManager::getInstance()->getLoadedPluginsName();
foreach ($plugins as $plugin) {
$plugin = Common::unprefixClass($plugin);
try {
Proxy::getInstance()->registerClass('Piwik_' . $plugin . '_API');
$className = Request::getClassNameAPI($plugin);
Proxy::getInstance()->registerClass($className);
} catch (Exception $e) {
}
}
Expand Down
32 changes: 15 additions & 17 deletions core/API/Proxy.php
Expand Up @@ -88,7 +88,7 @@ public function getMetadata()
*
* The method will introspect the methods, their parameters, etc.
*
* @param string $className ModuleName eg. "Piwik_UserSettings_API"
* @param string $className ModuleName eg. "API"
*/
public function registerClass($className)
{
Expand Down Expand Up @@ -146,7 +146,7 @@ public function getCountRegisteredClasses()
* It also logs the API calls, with the parameters values, the returned value, the performance, etc.
* You can enable logging in config/global.ini.php (log_api_call)
*
* @param string $className The class name (eg. Piwik_Referers_API)
* @param string $className The class name (eg. API)
* @param string $methodName The method name
* @param array $parametersRequest The parameters pairs (name=>value)
*
Expand Down Expand Up @@ -186,16 +186,14 @@ public function call($className, $methodName, $parametersRequest)
$returnedValue = call_user_func_array(array($object, $methodName), $finalParameters);

// allow plugins to manipulate the value
if (substr($className, 0, 6) == 'Piwik_' && substr($className, -4) == '_API') {
$pluginName = substr($className, 6, -4);
Piwik_PostEvent('API.Proxy.processReturnValue', array(
&$returnedValue,
array('className' => $className,
'module' => $pluginName,
'action' => $methodName,
'parameters' => &$parametersRequest)
));
}
$pluginName = $this->getModuleNameFromClassName($className);
Piwik_PostEvent('API.Proxy.processReturnValue', array(
&$returnedValue,
array('className' => $className,
'module' => $pluginName,
'action' => $methodName,
'parameters' => &$parametersRequest)
));

// Restore the request
$_GET = $saveGET;
Expand Down Expand Up @@ -242,12 +240,12 @@ public function getParametersList($class, $name)
/**
* Returns the 'moduleName' part of 'Piwik_moduleName_API' classname
*
* @param string $className "Piwik_Referers_API"
* @param string $className "API"
* @return string "Referers"
*/
public function getModuleNameFromClassName($className)
{
return str_replace(array('Piwik_', '_API'), '', $className);
return str_replace(array('\\Piwik\\Plugins\\', '\\API'), '', $className);
}

/**
Expand Down Expand Up @@ -308,9 +306,9 @@ private function getRequestParametersArray($requiredParameters, $parametersReque
}

/**
* Includes the class Piwik_UserSettings_API by looking up plugins/UserSettings/API.php
* Includes the class API by looking up plugins/UserSettings/API.php
*
* @param string $fileName api class name eg. "Piwik_UserSettings_API"
* @param string $fileName api class name eg. "API"
* @throws Exception
*/
private function includeApiFile($fileName)
Expand Down Expand Up @@ -403,7 +401,7 @@ private function isMethodAvailable($className, $methodName)
private function checkClassIsSingleton($className)
{
if (!method_exists($className, "getInstance")) {
throw new Exception("Objects that provide an API must be Singleton and have a 'static public function getInstance()' method.");
throw new Exception("$className that provide an API must be Singleton and have a 'static public function getInstance()' method.");
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions core/API/Request.php
Expand Up @@ -149,12 +149,12 @@ public function process()
if (!PluginsManager::getInstance()->isPluginActivated($module)) {
throw new PluginDeactivatedException($module);
}
$moduleClass = "Piwik_" . $module . "_API";
$apiClassName = $this->getClassNameAPI($module);

self::reloadAuthUsingTokenAuth($this->request);

// call the method
$returnedValue = Proxy::getInstance()->call($moduleClass, $method, $this->request);
$returnedValue = Proxy::getInstance()->call($apiClassName, $method, $this->request);

$toReturn = $response->getResponse($returnedValue, $module, $method);
} catch (Exception $e) {
Expand All @@ -163,6 +163,11 @@ public function process()
return $toReturn;
}

static public function getClassNameAPI($module)
{
return "\\Piwik\\Plugins\\$module\\API";
}

/**
* If the token_auth is found in the $request parameter,
* the current session will be authenticated using this token_auth.
Expand Down
2 changes: 1 addition & 1 deletion core/Access.php
Expand Up @@ -212,7 +212,7 @@ protected function reloadAccessSuperUser()
$this->isSuperUser = true;

try {
$allSitesId = \Piwik_SitesManager_API::getInstance()->getAllSitesId();
$allSitesId = Plugins\SitesManager\API::getInstance()->getAllSitesId();
} catch(\Exception $e) {
$allSitesId = array();
}
Expand Down
2 changes: 1 addition & 1 deletion core/AssetManager.php
Expand Up @@ -133,7 +133,7 @@ private static function prepareMergedCssFile()

// Disable Merged Assets ==> Check on each request if file needs re-compiling
if ($mergedCssAlreadyGenerated
&& $isDevelopingPiwik
&& !$isDevelopingPiwik
) {
$pathMerged = self::getAbsoluteMergedFileLocation(self::MERGED_CSS_FILE);
$f = fopen($pathMerged, 'r');
Expand Down
22 changes: 2 additions & 20 deletions core/Common.php
Expand Up @@ -15,7 +15,7 @@
use Piwik\Tracker;
use Piwik\Tracker\Cache;
use Piwik\PluginsManager;
use Piwik_UserCountry_LocationProvider_Default;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;

/**
* Static class providing functions used by both the CORE of Piwik and the visitor Tracking engine.
Expand All @@ -28,8 +28,6 @@
*/
class Common
{
const CLASSES_PREFIX = 'Piwik_';

/**
* Const used to map the referer type to an integer in the log_visit table
*/
Expand Down Expand Up @@ -1554,26 +1552,10 @@ public static function getCurrentLocationProviderId()
{
$cache = Cache::getCacheGeneral();
return empty($cache['currentLocationProviderId'])
? Piwik_UserCountry_LocationProvider_Default::ID
? DefaultProvider::ID
: $cache['currentLocationProviderId'];
}

/**
* Unprefix class name (if needed)
*
* @param string $class
* @return string
*/
public static function unprefixClass($class)
{
$lenPrefix = strlen(self::CLASSES_PREFIX);
if (!strncmp($class, self::CLASSES_PREFIX, $lenPrefix)) {
return substr($class, $lenPrefix);
}
return $class;
}


/**
* Mark orphaned object for garbage collection
*
Expand Down
35 changes: 18 additions & 17 deletions core/Controller.php
Expand Up @@ -21,13 +21,13 @@
use Piwik\Access;
use Piwik\Date;
use Piwik\Site;
use Piwik_API_API;
use Piwik\Plugins\API\API;
use Piwik\API\Request;
use Piwik\FrontController;
use Piwik_LanguagesManager;
use Piwik_SitesManager_API;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\SitesManager\API as SitesManagerAPI;
use Piwik\Url;
use Piwik_UsersManager_API;
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;
use Piwik\View;
use Piwik\ViewDataTable;
use Piwik\ViewDataTable\GenerateGraphHTML\ChartEvolution;
Expand Down Expand Up @@ -81,8 +81,9 @@ public function __construct()

protected function init()
{
$aPluginName = explode('_', get_class($this));
$this->pluginName = $aPluginName[1];
$aPluginName = explode('\\', get_class($this));
$this->pluginName = $aPluginName[2];

$date = Common::getRequestVar('date', 'yesterday', 'string');
try {
$this->idSite = Common::getRequestVar('idSite', false, 'int');
Expand Down Expand Up @@ -202,7 +203,7 @@ protected function getLastUnitGraphAcrossPlugins($currentModuleName, $currentCon
$idSite = Common::getRequestVar('idSite');
$period = Common::getRequestVar('period');
$date = Common::getRequestVar('date');
$meta = Piwik_API_API::getInstance()->getReportMetadata($idSite, $period, $date);
$meta = API::getInstance()->getReportMetadata($idSite, $period, $date);

$columns = array_merge($columnsToDisplay, $selectableColumns);
$translations = array();
Expand Down Expand Up @@ -423,8 +424,8 @@ protected function setGeneralVariablesView($view)
$view->startDate = $dateStart;
$view->endDate = $dateEnd;

$language = Piwik_LanguagesManager::getLanguageForSession();
$view->language = !empty($language) ? $language : Piwik_LanguagesManager::getLanguageCodeForCurrentUser();
$language = LanguagesManager::getLanguageForSession();
$view->language = !empty($language) ? $language : LanguagesManager::getLanguageCodeForCurrentUser();

$view->config_action_url_category_delimiter = Config::getInstance()->General['action_url_category_delimiter'];

Expand All @@ -447,10 +448,10 @@ protected function setBasicVariablesView($view)
$view->isSuperUser = Access::getInstance()->isSuperUser();
$view->hasSomeAdminAccess = Piwik::isUserHasSomeAdminAccess();
$view->isCustomLogo = Config::getInstance()->branding['use_custom_logo'];
$view->logoHeader = Piwik_API_API::getInstance()->getHeaderLogoUrl();
$view->logoLarge = Piwik_API_API::getInstance()->getLogoUrl();
$view->logoSVG = Piwik_API_API::getInstance()->getSVGLogoUrl();
$view->hasSVGLogo = Piwik_API_API::getInstance()->hasSVGLogo();
$view->logoHeader = API::getInstance()->getHeaderLogoUrl();
$view->logoLarge = API::getInstance()->getLogoUrl();
$view->logoSVG = API::getInstance()->getSVGLogoUrl();
$view->hasSVGLogo = API::getInstance()->hasSVGLogo();

$view->enableFrames = Config::getInstance()->General['enable_framed_pages']
|| @Config::getInstance()->General['enable_framed_logins'];
Expand Down Expand Up @@ -669,7 +670,7 @@ protected function getDefaultWebsiteId()
$defaultWebsiteId = false;

// User preference: default website ID to load
$defaultReport = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT);
$defaultReport = UsersManagerAPI::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), UsersManagerAPI::PREFERENCE_DEFAULT_REPORT);
if (is_numeric($defaultReport)) {
$defaultWebsiteId = $defaultReport;
}
Expand All @@ -680,7 +681,7 @@ protected function getDefaultWebsiteId()
return $defaultWebsiteId;
}

$sitesId = Piwik_SitesManager_API::getInstance()->getSitesIdWithAtLeastViewAccess();
$sitesId = SitesManagerAPI::getInstance()->getSitesIdWithAtLeastViewAccess();
if (!empty($sitesId)) {
return $sitesId[0];
}
Expand All @@ -695,7 +696,7 @@ protected function getDefaultWebsiteId()
protected function getDefaultDate()
{
// NOTE: a change in this function might mean a change in plugins/UsersManager/javascripts/usersSettings.js as well
$userSettingsDate = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT_DATE);
$userSettingsDate = UsersManagerAPI::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), UsersManagerAPI::PREFERENCE_DEFAULT_REPORT_DATE);
if ($userSettingsDate == 'yesterday') {
return $userSettingsDate;
}
Expand All @@ -715,7 +716,7 @@ protected function getDefaultDate()
*/
protected function getDefaultPeriod()
{
$userSettingsDate = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT_DATE);
$userSettingsDate = UsersManagerAPI::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), UsersManagerAPI::PREFERENCE_DEFAULT_REPORT_DATE);
if ($userSettingsDate === false) {
return Config::getInstance()->General['default_period'];
}
Expand Down
Expand Up @@ -22,7 +22,7 @@
* You can also specify the precision of the percentage value to be displayed (defaults to 0, eg "11%")
*
* Usage:
* $nbVisits = Piwik_VisitsSummary_API::getInstance()->getVisits($idSite, $period, $date);
* $nbVisits = API::getInstance()->getVisits($idSite, $period, $date);
* $dataTable->queueFilter('ColumnCallbackAddColumnPercentage', array('nb_visits', 'nb_visits_percentage', $nbVisits, 1));
*
* @package Piwik
Expand Down
2 changes: 1 addition & 1 deletion core/DataTable/Renderer.php
Expand Up @@ -299,7 +299,7 @@ protected function getApiMetaData()
$this->apiMetaData = false;
}

$api = \Piwik_API_API::getInstance();
$api = \Piwik\Plugins\API\API::getInstance();
$meta = $api->getMetadata($this->idSite, $apiModule, $apiAction);
if (is_array($meta[0])) {
$meta = $meta[0];
Expand Down
2 changes: 1 addition & 1 deletion core/Db/Schema.php
Expand Up @@ -116,7 +116,7 @@ public static function getSchemas($adapterName)
$schemas = array();

foreach ($schemaNames as $schemaName) {
$className = 'Piwik_Db_Schema_' . $schemaName;
$className = __NAMESPACE__ . '\\Schema\\' . $schemaName;
if (call_user_func(array($className, 'isAvailable'))) {
$schemas[] = $schemaName;
}
Expand Down
11 changes: 9 additions & 2 deletions core/FrontController.php
Expand Up @@ -99,7 +99,7 @@ public function dispatch($module = null, $action = null, $parameters = null)
throw new PluginDeactivatedException($module);
}

$controllerClassName = 'Piwik_' . $module . '_Controller';
$controllerClassName = $this->getClassNameController( $module );

// FrontController's autoloader
if (!class_exists($controllerClassName, false)) {
Expand All @@ -110,7 +110,9 @@ public function dispatch($module = null, $action = null, $parameters = null)
require_once $moduleController; // prefixed by PIWIK_INCLUDE_PATH
}

$controller = new $controllerClassName();
$class = $this->getClassNameController($module);
/** @var $controller Controller */
$controller = new $class;
if ($action === false) {
$action = $controller->getDefaultAction();
}
Expand All @@ -135,6 +137,11 @@ public function dispatch($module = null, $action = null, $parameters = null)
}
}

protected function getClassNameController($module)
{
return "\\Piwik\\Plugins\\$module\\Controller";
}

/**
* Often plugins controller display stuff using echo/print.
* Using this function instead of dispatch() returns the output string form the actions calls.
Expand Down
8 changes: 7 additions & 1 deletion core/Loader.php
Expand Up @@ -46,7 +46,13 @@ protected static function getClassFileName($class)
return $class;
}

$vendorPrefixToRemove = 'Piwik/';
$class = self::removePrefix($class, 'Piwik/');
$class = self::removePrefix($class, 'Plugins/');
return $class;
}

protected static function removePrefix($class, $vendorPrefixToRemove)
{
if (strpos($class, $vendorPrefixToRemove) === 0) {
return substr($class, strlen($vendorPrefixToRemove));
}
Expand Down

0 comments on commit a96ec40

Please sign in to comment.