X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=classes%2Fplatal.php;h=d15c99f9e5619cc6136d47d7afa77b1ba81d3a5c;hb=f3f26e2b84a4ddd6c1daf3013bfa8adc252b727a;hp=520b1962a6e7386cb66c763e8c79810b451dd59b;hpb=e92ecb8c24421ca1dd4f87ad7478d0d8277e1f60;p=platal.git diff --git a/classes/platal.php b/classes/platal.php index 520b196..d15c99f 100644 --- a/classes/platal.php +++ b/classes/platal.php @@ -54,9 +54,9 @@ abstract class PlHook return ($this->type & $type) == $type; } - abstract protected function run(PlPage &$page, array $args); + abstract protected function run(PlPage $page, array $args); - public function call(PlPage &$page, array $args) + public function call(PlPage $page, array $args) { global $globals, $session, $platal; if (!$session->checkAuth($this->auth)) { @@ -92,7 +92,7 @@ class PlStdHook extends PlHook $this->callback = $callback; } - protected function run(PlPage &$page, array $args) + protected function run(PlPage $page, array $args) { global $session, $platal; @@ -152,7 +152,7 @@ class PlApiHook extends PlHook return empty($encodedPayload) ? array() : json_decode($encodedPayload, true); } - protected function run(PlPage &$page, array $args) + protected function run(PlPage $page, array $args) { $method = $_SERVER['REQUEST_METHOD']; $encodedPayload = $this->getEncodedPayload($method); @@ -215,7 +215,7 @@ class PlTokenHook extends PlHook $this->callback = $callback; } - protected function run(PlPage &$page, array $args) + protected function run(PlPage $page, array $args) { // Retrieve the user, either from the session (less expensive, as it is // already there), or from the in-path (user, token) pair. @@ -250,7 +250,7 @@ class PlWikiHook extends PlHook parent::__construct($auth, $perms, $type); } - protected function run(PlPage &$page, array $args) + protected function run(PlPage $page, array $args) { return PL_WIKI; } @@ -262,51 +262,51 @@ class PlHookTree public $aliased = null; public $children = array(); - public function addChild(array $path, PlHook $hook) + public function addChildren(array $hooks) { global $platal; - $next = array_shift($path); - $alias = null; - if ($next && $next{0} == '%') { - $alias = $next; - $next = $platal->hook_map(substr($next, 1)); - } - if (!$next) { - return; - } - @$child =& $this->children[$next]; - if (!$child) { - $child = new PlHookTree(); - $this->children[$next] =& $child; - $child->aliased = $alias; - } - if (empty($path)) { - $child->hook = $hook; - } else { - $child->addChild($path, $hook); - } - } - - private function findChildAux(array $remain, array $matched, array $aliased) - { - $next = @$remain[0]; - if ($this->aliased) { - $aliased = $matched; - } - if (!empty($next)) { - $child = @$this->children[$next]; - if ($child) { - array_shift($remain); - $matched[] = $next; - return $child->findChildAux($remain, $matched, $aliased); + foreach ($hooks as $path=>$hook) { + $path = explode('/', $path); + $element = $this; + foreach ($path as $next) { + $alias = null; + if ($next{0} == '%') { + $alias = $next; + $next = $platal->hook_map(substr($next, 1)); + } + if (!isset($element->children[$next])) { + $child = new PlHookTree(); + $child->aliased = $alias; + $element->children[$next] = $child; + } else { + $child = $element->children[$next]; + } + $element = $child; } + $element->hook = $hook; } - return array($this->hook, $matched, $remain, $aliased); } public function findChild(array $path) { - return $this->findChildAux($path, array(), array()); + $remain = $path; + $matched = array(); + $aliased = array(); + $element = $this; + while (true) + { + $next = @$remain[0]; + if ($element->aliased) { + $aliased = $matched; + } + if (empty($next) || !isset($element->children[$next])) { + break; + } + $element = $element->children[$next]; + array_shift($remain); + $matched[] = $next; + } + return array($element->hook, $matched, $remain, $aliased); } private function findNearestChildAux(array $remain, array $matched, array $aliased) @@ -322,16 +322,18 @@ class PlHookTree $nearest_sdx = 50; $match = null; foreach ($this->children as $path=>$hook) { - $lev = levenshtein($next, $path); - if ($lev <= $nearest_lev - && ($lev < strlen($next) / 2 || strpos($next, $path) !== false - || strpos($path, $next) !== false)) { - $sdx = levenshtein(soundex($next), soundex($path)); - if ($lev == $nearest_lev || $sdx < $nearest_sdx) { - $child = $hook; - $nearest_lev = $lev; - $nearest_sdx = $sdx; - $match = $path; + if ($path) { + $lev = levenshtein($next, $path); + if ($lev <= $nearest_lev + && ($lev < strlen($next) / 2 || strpos($next, $path) !== false + || strpos($path, $next) !== false)) { + $sdx = levenshtein(soundex($next), soundex($path)); + if ($lev == $nearest_lev || $sdx < $nearest_sdx) { + $child = $hook; + $nearest_lev = $lev; + $nearest_sdx = $sdx; + $match = $path; + } } } } @@ -401,10 +403,7 @@ abstract class Platal foreach ($modules as $module) { $module = strtolower($module); $this->mods[$module] = $m = PLModule::factory($module); - $hooks = $m->handlers(); - foreach ($hooks as $path=>$hook) { - $this->hooks->addChild(explode('/', $path), $hook); - } + $this->hooks->addChildren($m->handlers()); } if ($globals->mode == '') { @@ -470,7 +469,7 @@ abstract class Platal return $url; } - private function call_hook(PlPage &$page) + private function call_hook(PlPage $page) { $hook = $this->find_hook(); if (empty($hook)) { @@ -486,7 +485,12 @@ abstract class Platal /** Show the authentication form. */ - abstract public function force_login(PlPage& $page); + abstract public function force_login(PlPage $page); + + protected function report_error($error) + { + PlErrorReport::report($error); + } public function run() { @@ -517,7 +521,7 @@ abstract class Platal } } catch (Exception $e) { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); - PlErrorReport::report($e); + $this->report_error($e); if (self::globals()->debug) { $page->kill(pl_entities($e->getMessage()) . '
' . pl_entities("" . $e) . '
');