diff options
author | Romain d'Alverny <rda@mageia.org> | 2012-11-12 11:36:20 +0000 |
---|---|---|
committer | Romain d'Alverny <rda@mageia.org> | 2012-11-12 11:36:20 +0000 |
commit | 6e42f3702b38f97a1a436d30310a0e6c4faa5145 (patch) | |
tree | ed04426dcca7331b912904d636fb97abbae46fdb | |
parent | c36f156acaff30b9c512a022484f80fbfb237c95 (diff) | |
download | www-6e42f3702b38f97a1a436d30310a0e6c4faa5145.tar www-6e42f3702b38f97a1a436d30310a0e6c4faa5145.tar.gz www-6e42f3702b38f97a1a436d30310a0e6c4faa5145.tar.bz2 www-6e42f3702b38f97a1a436d30310a0e6c4faa5145.tar.xz www-6e42f3702b38f97a1a436d30310a0e6c4faa5145.zip |
pinq is a new controller-based framework for www.m.o
-rw-r--r-- | app.php | 32 | ||||
-rw-r--r-- | apps/MGA_Downloads/MGA_Downloads_app.php | 27 | ||||
-rw-r--r-- | apps/error/error_app.php | 40 | ||||
-rw-r--r-- | apps/error/templates/404.tpl | 28 | ||||
-rw-r--r-- | lib/pinq/App.php | 25 | ||||
-rw-r--r-- | lib/pinq/Controller.php | 185 | ||||
-rw-r--r-- | lib/pinq/templates/analytics.tpl | 13 | ||||
-rw-r--r-- | lib/pinq/templates/layout.tpl | 22 |
8 files changed, 372 insertions, 0 deletions
diff --git a/app.php b/app.php new file mode 100644 index 000000000..43323dbc6 --- /dev/null +++ b/app.php @@ -0,0 +1,32 @@ +<?php +/** + * Transition controller for www.mageia.org: + * we are moving from a simple site where each page is called by its own script file + * to a site where requests are managed and dispatched by a single controller. + * + * But, we do it step by step, without migrating/breaking everything at once. + * See comments below. + * + * PHP version 5.4 + * + * @category Mageia + * @package Mageia\Web\www + * @author rda <rda@mageia.org> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ + * @link http://www.mageia.org/ + * +*/ + +require 'lib/Pinq/Controller.php'; +require 'lib/Pinq/App.php'; + +$pc = new Pinq_Controller(__DIR__, $_SERVER); +$pc->init(); + +$routes = array( +// 'downloads' => 'MGA_Downloads' +); + +$pc->run($routes); + + diff --git a/apps/MGA_Downloads/MGA_Downloads_app.php b/apps/MGA_Downloads/MGA_Downloads_app.php new file mode 100644 index 000000000..74b01c9f2 --- /dev/null +++ b/apps/MGA_Downloads/MGA_Downloads_app.php @@ -0,0 +1,27 @@ +<?php +/** + * + * PHP version 5.4 + * + * @category Mageia + * @package Mageia\Web\www + * @author rda <rda@mageia.org> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ + * @link http://www.mageia.org/ + * +*/ + +class MGA_Downloads extends Pinq_App +{ + function __construct($controller, $options) + { + $this->_control = $controller; + $this->_options = $options; + echo "c"; + } + + function run() + { + echo "run"; + } +}
\ No newline at end of file diff --git a/apps/error/error_app.php b/apps/error/error_app.php new file mode 100644 index 000000000..17210c812 --- /dev/null +++ b/apps/error/error_app.php @@ -0,0 +1,40 @@ +<?php +/** + * + * PHP version 5.4 + * + * @category Mageia + * @package Mageia\Web\www + * @author rda <rda@mageia.org> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ + * @link http://www.mageia.org/ + * +*/ + +class Error extends Pinq_App +{ + public function __construct($controller, $options) + { + $this->_control = $controller; + $this->_options = $options; + } + + public function run() + { + if ($this->_options['code'] == 404) { + return $this->_error_404(); + } + } + + private function _error_404() + { + define('HLANG', true); + require 'langs.php'; // needs absolute path in some form to be link depth independent + _lang_load($locale, '404'); + + header('Mageia-Test: 1'); + header('HTTP/1.0 404 Not Found'); + header('Status: 404 Not Found'); + include 'templates/404.tpl'; + } +}
\ No newline at end of file diff --git a/apps/error/templates/404.tpl b/apps/error/templates/404.tpl new file mode 100644 index 000000000..bec36f06a --- /dev/null +++ b/apps/error/templates/404.tpl @@ -0,0 +1,28 @@ +<!DOCTYPE html> +<html dir="ltr" lang="<?php echo $locale; ?>"> +<head> + <meta charset="utf-8"> + <title><?php _e('Sorry, we could not find this page.')?></title> + <meta http-equiv="expires" content="0"> + <meta name="robots" content="noindex, nofollow"> + <meta name="author" content="Mageia"> + <link rel="stylesheet" type="text/css" href="/g/style/all.css"> + <?php include 'analytics.php'; ?> +</head> +<body> + <?php echo $hsnav; ?> + <h1 id="mgnavt"><?php _e('Sorry, we could not find this page.')?></h1> + <div id="doc" class="yui-t7"> + <div id="bd" role="main"> + <div class="yui-g"> + <div class="para values"> + <ul class="hl"><?php + _h('Try searching it on <a href="%s">mageia.org site map</a>,', array('/map/'), 'li'); + _h('or try to <a href="%s">search with Google</a>.', array('https://www.google.com/?q=site:mageia.org'), 'li'); + ?></ul> + </div> + </div> + </div> + </div> +</body> +</html> diff --git a/lib/pinq/App.php b/lib/pinq/App.php new file mode 100644 index 000000000..2f2ebd88a --- /dev/null +++ b/lib/pinq/App.php @@ -0,0 +1,25 @@ +<?php +/** + * + * PHP version 5.4 + * + * @category Mageia + * @package Mageia\Web\www\Pinq + * @author rda <rda@mageia.org> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ + * @link http://www.mageia.org/ + * +*/ + +/** +*/ +abstract class Pinq_App +{ + function __construct($controller, $options) + { + $this->_control = $controller; + $this->_options = $options; + } + + abstract function run(); +}
\ No newline at end of file diff --git a/lib/pinq/Controller.php b/lib/pinq/Controller.php new file mode 100644 index 000000000..8a204357c --- /dev/null +++ b/lib/pinq/Controller.php @@ -0,0 +1,185 @@ +<?php +/** + * + * PHP version 5.4 + * + * @category Mageia + * @package Mageia\Web\www\Pinq + * @author rda <rda@mageia.org> + * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ + * @link http://www.mageia.org/ + * +*/ + +/** +*/ +class Pinq_Controller +{ + /** + * @param string $dir root application directory + * @param array $server $_SERVER array + * + */ + public function __construct($dir, $server) + { + $this->_app_root = $dir; + $this->_server = $server; + } + + /** + */ + public function init() + { + $this->uri = $this->_server['REQUEST_URI']; + + if (strpos('?', $this->uri) !== false) { + $uri = explode('?', $this->uri); + $this->query = $uri[1]; + $this->uri = $uri[0]; + } + } + + /** + * @param array $routes + * + * @return boolean + */ + public function run($routes = null) + { + // static, image files are expected to be served directly by the server. + + // detect path language; if not set, redirect to best fallback language (English for now), end + $this->lang = $this->get_request_language($this->uri); + if (!$this->lang_is_managed($this->lang)) { + // TODO + } + + // delegate to declared routes/apps + $re = ' + / + \/(([a-zA-Z\-\_]{2,5})\/)? + (.*) + /x'; + + if (preg_match_all($re, $this->uri, $matches)) { + $this->_lang = $matches[2][0]; + $this->_path = $matches[3][0]; + } + + if (is_array($routes) && count($routes) > 0) { + foreach ($routes as $r => $app) { + $re = '/' . $r . '/'; + if (preg_match_all($re, $this->uri, $m)) { + return $this->delegate_to($app); + } + } + } + + // delegate to local script + // TODO look at local code at $uri, and decide if we can load it and decorate it. + + // finally, act as we used to before + if ($this->fallback_to_previous_mode($this->uri, $this->lang)) { + return true; + } + + // if nothing matched, well... + return $this->delegate_to('error', array('code' => '404')); + } + + /** + * @param string $uri + * + * @return string + */ + function get_request_language($uri) + { + $tu = explode('/', $uri); + $lang = $tu[1]; + $sl = strlen($lang); + if ($this->lang_exists($lang)) { + return $lang; + } + + return null; + } + + /** + */ + function lang_exists($lang) + { + // is it a valid language code? ll[-cc] or ll[_CC]? + return true; + } + + /** + */ + function lang_is_managed($lang) + { + // in managed languages array? + return true; + } + + /** + * @param string $app_name + * + * @return boolean + */ + private function delegate_to($app_name, $options = null) + { + $app_cont = realpath(sprintf('%s/apps/%s/%s_app.php', $this->_app_root, $app_name, $app_name)); + if (!file_exists($app_cont)) { + throw new Exception(sprintf('Expected "%s" app, found no definition file.', $app_name)); + } + + include $app_cont; + $a = new $app_name($this, $options); + + return $a->run(); + } + + /** + * @return boolean true if fallback happened + */ + function fallback_to_previous_mode($uri, $lang) + { + $alt_uri = sprintf( + '/%s/%s', + 'en', + substr($uri, strlen($lang) + 2) + ); + + $test_uris = array( +/* + $uri, + $uri . 'index.php', + $uri . 'index.html', +/**/ + $alt_uri, + $alt_uri . 'index.php', + $alt_uri . 'index.html' + ); + + $found = false; + + foreach ($test_uris as $inc_uri) { + + $real_file = realpath($this->_app_root . $inc_uri); + + if (file_exists($real_file) && !is_dir($real_file)) { + + $found = true; + break; + } + } + + if ($found) { + chdir(dirname($real_file)); + require $real_file; + return true; + } + + return false; + } + +} diff --git a/lib/pinq/templates/analytics.tpl b/lib/pinq/templates/analytics.tpl new file mode 100644 index 000000000..81ee4ce38 --- /dev/null +++ b/lib/pinq/templates/analytics.tpl @@ -0,0 +1,13 @@ +<link rel="icon" type="image/png" href="/g/favicon.png" /> +<script type="text/javascript"> + var _gaq = _gaq || []; + _gaq.push(['_setAccount', 'UA-18603191-1']); + _gaq.push(['_setDomainName', '.mageia.org']); + _gaq.push(['_trackPageview']); + + (function() { + var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; + ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); + })(); +</script>
\ No newline at end of file diff --git a/lib/pinq/templates/layout.tpl b/lib/pinq/templates/layout.tpl new file mode 100644 index 000000000..9e0c2abc5 --- /dev/null +++ b/lib/pinq/templates/layout.tpl @@ -0,0 +1,22 @@ +<!DOCTYPE html> +<html lang="{block name=lang}en{/block}" dir="{block name=ltr}ltr{/block}"> +<head> + <meta charset="utf-8"> + <title>{block name=page_title}{/block}</title> + <meta name="description" content="{block name=page_desc}{/block}"> + <meta name="keywords" content="{block name=page_kw}{/block}"> + <meta name="author" content=""> + <link rel="canonical" href="{block name=canonical_link}/{/block}"> + <link rel="home" href="http://www.mageia.org/"> + <link rel="alternate" type="application/rss+xml" title="Les nouveautés de Gasparine - RSS" href="/rss/"> + {block name=more_meta}{/block} +</head> +<body class="{$page_class}"> + + {include file="nav.tpl"} + + {block name=body}{/block} + + {include file='analytics.tpl'} +</body> +</html>
\ No newline at end of file |