Permalink
Browse files

feature(assets): Move all core static assets to views

Fixes #5105
  • Loading branch information...
ewinslow committed Jun 8, 2015
1 parent 4e72de0 commit c44740d59be73bf531cd1d311b85c00278fd0c3c
Showing with 269 additions and 176 deletions.
  1. +1 −1 docs/conf.py
  2. +2 −2 docs/guides/javascript.rst
  3. +1 −1 docs/guides/services.rst
  4. +9 −9 docs/guides/views/simplecache.rst
  5. +63 −11 engine/classes/Elgg/Application/CacheHandler.php
  6. +35 −15 engine/classes/Elgg/Cache/SimpleCache.php
  7. +0 −2 engine/classes/Elgg/ViewsService.php
  8. +1 −1 engine/classes/ElggEntity.php
  9. +4 −4 engine/lib/admin.php
  10. +20 −43 engine/lib/cache.php
  11. +1 −1 engine/lib/deprecated-1.8.php
  12. +8 −8 engine/lib/elgglib.php
  13. +1 −1 engine/lib/users.php
  14. +15 −19 engine/lib/views.php
  15. +16 −0 engine/tests/phpunit/Elgg/Application/CacheHandlerTest.php
  16. +34 −0 engine/tests/phpunit/Elgg/Cache/SimpleCacheTest.php
  17. +2 −0 install/config/htaccess.dist
  18. +4 −4 mod/aalborg_theme/views/default/css/elements/forms.php
  19. +2 −2 mod/aalborg_theme/views/default/css/elements/icons.php
  20. +5 −1 mod/developers/start.php
  21. +1 −1 mod/embed/start.php
  22. +1 −1 mod/profile/icondirect.php
  23. +2 −2 mod/profile/start.php
  24. +1 −1 mod/search/views/default/search/css.php
  25. +1 −1 mod/site_notifications/start.php
  26. +1 −1 mod/thewire/start.php
  27. 0 {_graphics → views/default}/SPRITES_LICENSE.TXT
  28. BIN {_graphics → views/default}/admin_sprites.png
  29. BIN {_graphics → views/default}/ajax_loader.gif
  30. BIN {_graphics → views/default}/ajax_loader_bw.gif
  31. BIN {_graphics → views/default}/button_background.gif
  32. BIN {_graphics → views/default}/button_graduation.png
  33. +6 −6 views/default/css/admin.php
  34. +6 −6 views/default/css/elements/buttons.php
  35. +4 −4 views/default/css/elements/forms.php
  36. +2 −2 views/default/css/elements/icons.php
  37. +4 −4 views/default/css/elements/layout.php
  38. +3 −3 views/default/css/maintenance.php
  39. +6 −6 views/default/css/walled_garden.php
  40. BIN {_graphics → views/default}/elgg_logo.png
  41. BIN {_graphics → views/default}/elgg_sprites.png
  42. BIN {_graphics → views/default}/elgg_toolbar_logo.gif
  43. BIN {_graphics → views/default}/favicon-128.png
  44. BIN {_graphics → views/default}/favicon-16.png
  45. BIN {_graphics → views/default}/favicon-32.png
  46. BIN {_graphics → views/default}/favicon-64.png
  47. BIN {_graphics → views/default}/favicon.ico
  48. 0 {_graphics → views/default}/favicon.svg
  49. BIN {_graphics → views/default}/friendspicker.png
  50. BIN {_graphics → views/default}/header_shadow.png
  51. BIN {_graphics → views/default}/icons/default/large.png
  52. BIN {_graphics → views/default}/icons/default/medium.png
  53. BIN {_graphics → views/default}/icons/default/small.png
  54. BIN {_graphics → views/default}/icons/default/tiny.png
  55. BIN {_graphics → views/default}/icons/default/topbar.png
  56. BIN {_graphics → views/default}/icons/user/defaultlarge.gif
  57. BIN {_graphics → views/default}/icons/user/defaultmaster.gif
  58. BIN {_graphics → views/default}/icons/user/defaultmedium.gif
  59. BIN {_graphics → views/default}/icons/user/defaultsmall.gif
  60. BIN {_graphics → views/default}/icons/user/defaulttiny.gif
  61. BIN {_graphics → views/default}/icons/user/defaulttopbar.gif
  62. +1 −1 views/default/page/maintenance.php
  63. BIN {_graphics → views/default}/powered_by_elgg_badge_drk_bckgnd.gif
  64. BIN {_graphics → views/default}/powered_by_elgg_badge_light_bckgnd.gif
  65. +2 −2 views/default/resources/admin/plugin_screenshot.img.php
  66. +2 −6 views/default/resources/avatar/view.php
  67. BIN {_graphics → views/default}/sidebar_background.gif
  68. BIN {_graphics → views/default}/spacer.gif
  69. BIN {_graphics → views/default}/toptoolbar_background.gif
  70. BIN {_graphics → views/default}/two_sidebar_background.gif
  71. BIN {_graphics → views/default}/walled_garden/one_column_bottom.png
  72. BIN {_graphics → views/default}/walled_garden/one_column_middle.png
  73. BIN {_graphics → views/default}/walled_garden/one_column_top.png
  74. BIN {_graphics → views/default}/walled_garden/two_column_bottom.png
  75. BIN {_graphics → views/default}/walled_garden/two_column_middle.png
  76. BIN {_graphics → views/default}/walled_garden/two_column_top.png
  77. +1 −1 views/installation/page/default.php
  78. +1 −3 views/installation/page/elements/header.php
View
@@ -125,7 +125,7 @@ def setup(app):
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
-html_favicon = '../_graphics/favicon.ico'
+html_favicon = '../views/default/favicon.ico'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
@@ -160,7 +160,7 @@ In your PHP ``init, system`` event handler, you can use ``elgg_define_js()`` to
<?php
elgg_define_js('underscore', [
- 'src' => elgg_get_simplecache_url('js', 'underscore.js'),
+ 'src' => elgg_get_simplecache_url('js/underscore.js'),
]);
.. note::
@@ -187,7 +187,7 @@ setting ``exports`` and optionally ``deps``:
// set the path, define its dependencies, and what value it returns
elgg_define_js('jquery.form', [
- 'src' => elgg_get_simplecache_url('js', 'jquery.form.js'),
+ 'src' => elgg_get_simplecache_url('js/jquery.form.js'),
'deps' => array('jquery'),
'exports' => 'jQuery.fn.ajaxForm',
]);
View
@@ -23,7 +23,7 @@ burden of supporting older Elgg versions, we encourage you to continue using the
Service: config
---------------
-An instance of ``Elgg\Services\ConfigInterface``, this is for getting and setting various system
+An instance of ``Elgg\Services\Config``, this is for getting and setting various system
configuration values.
.. note::
@@ -6,9 +6,11 @@ Simplecache
- :doc:`/admin/performance`
- :doc:`/guides/views`
-The Simplecache is a mechanism designed to alleviate the need for certain views to be regenerated dynamically. Instead, they are generated once, saved as a static file, and served in a way that entirely bypasses the Elgg engine.
+The Simplecache is a mechanism designed to alleviate the need for certain views to be regenerated dynamically.
+Instead, they are generated once, saved as a static file, and served in a way that entirely bypasses the Elgg engine.
-If Simplecache is turned off (which can be done from the administration panel), these views will be served as normal, with the exception of site CSS.
+If Simplecache is turned off (which can be done from the administration panel),
+these views will be served as normal, with the exception of site CSS.
The criteria for whether a view is suitable for the Simplecache is as follows:
@@ -38,13 +40,11 @@ You can register a view with the Simplecache with the following function at init
**Accessing the cached view**
-If you registered a JavaScript or CSS file with Simplecache and put in in the view folder ``js/your_view`` or ``css/your_view`` you can very easily the the url to this cached view by calling
+If you registered a JavaScript or CSS file with Simplecache and put in in the view folder
+``js/your_view.js`` or ``css/your_view.css`` you can very easily get the url to this cached view by calling
+``elgg_get_simplecache_url($view)``. For example:
.. code:: php
- $url = elgg_get_simplecache_url($type, $view)
-
-Where:
-
-- ``$type`` is js or css
-- ``$view`` the view name after css/ or js/
+ $js = elgg_get_simplecache_url('js/your_view.js');
+ $css = elgg_get_simplecache_url('css/your_view.css');
@@ -46,11 +46,15 @@ public function handleRequest($path) {
if (!$request) {
$this->send403();
}
+
$ts = $request['ts'];
$view = $request['view'];
$viewtype = $request['viewtype'];
- $this->sendContentType($view);
+ $contentType = $this->getContentType($view);
+ if (!empty($contentType)) {
+ header("Content-Type: $contentType", true);
+ }
// this may/may not have to connect to the DB
$this->setupSimplecache();
@@ -200,22 +204,70 @@ protected function sendCacheHeaders($etag) {
}
/**
- * Send content type
+ * Get the content type
*
* @param string $view The view name
- * @return void
+ *
+ * @return string?
*/
- protected function sendContentType($view) {
- $segments = explode('/', $view, 2);
- switch ($segments[0]) {
- case 'css':
- header("Content-Type: text/css", true);
- break;
+ protected function getContentType($view) {
+
+ $extension = $this->getViewFileType($view);
+
+ switch ($extension) {
+ case 'gif':
+ case 'png':
+ case 'webp':
+ case 'bmp':
+ case 'tiff':
+ case 'jpeg':
+ return "image/$extension";
+
+ case 'jpg':
+ return "image/jpeg";
+
+ case 'ico':
+ return 'image/x-icon';
+
+ case 'svg':
+ return 'image/svg+xml';
+
case 'js':
- header('Content-Type: text/javascript', true);
+ return 'application/javascript';
+
+ case 'css':
+ case 'html':
+ case 'xml':
+ return "text/$extension";
+
+ default:
break;
}
}
+
+ /**
+ * Returns the type of output expected from the view.
+ *
+ * - view/name.extension returns "extension"
+ * - css/view views return "css"
+ * - js/view views return "js"
+ * - Otherwise, returns "unknown"
+ *
+ * @param string $view The view name
+ * @return string
+ */
+ private function getViewFileType($view) {
+ $extension = (new \SplFileInfo($view))->getExtension();
+ if ($extension) {
+ return $extension;
+ }
+
+ if (preg_match('~(?:^|/)(css|js)(?:$|/)~', $view, $m)) {
+ return $m[1];
+ } else {
+ return 'unknown';
+ }
+ }
/**
* Get the contents of a view for caching
@@ -228,7 +280,7 @@ protected function sendContentType($view) {
protected function getProcessedView($view, $viewtype) {
$content = $this->renderView($view, $viewtype);
- $hook_type = _elgg_get_view_filetype($view);
+ $hook_type = $this->getViewFileType($view);
$hook_params = array(
'view' => $view,
'viewtype' => $viewtype,
@@ -49,30 +49,50 @@ function registerView($view_name) {
}
/**
- * Get the URL for the cached file
+ * Get the URL for the cached view.
*
- * This automatically registers the view with Elgg's simplecache.
+ * Recommended usage is to just pass the entire view name as the first and only arg:
+ *
+ * ```
+ * $blog_js = $simpleCache->getUrl('js/blog/save_draft.js');
+ * $favicon = $simpleCache->getUrl('favicon.ico');
+ * ```
*
- * @example
- * $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
- * elgg_register_js('elgg.blog', $blog_js);
- * elgg_load_js('elgg.blog');
+ * For backwards compatibility with older versions of Elgg, you can also pass
+ * "js" or "css" as the first arg, with the rest of the view name as the second arg:
*
- * @param string $type The file type: css or js
- * @param string $view The view name after css/ or js/
+ * ```
+ * $blog_js = $simpleCache->getUrl('js', 'blog/save_draft.js');
+ * ```
+ *
+ * Note that with this older approach, there is no way to access cached views
+ * outside of the js/ or css/ folders.
+ *
+ * This automatically registers the view with Elgg's simplecache.
+ *
+ * @param string $view The full view name
+ * @param string $subview If the first arg is "css" or "js", the rest of the view name
+ *
* @return string
*/
- function getUrl($type, $view) {
- // handle file type passed with view name
- if (($type === 'js' || $type === 'css') && 0 === strpos($view, $type . '/')) {
- $view = substr($view, strlen($type) + 1);
+ function getUrl($view, $subview = '') {
+ // handle `getUrl('js', 'js/blog/save_draft.js')`
+ if (($view === 'js' || $view === 'css') && 0 === strpos($subview, $view . '/')) {
+ $view = $subview;
+ $subview = '';
+ }
+
+ // handle `getUrl('js', 'blog/save_draft.js')`
+ if (!empty($subview)) {
+ $view = "$view/$subview";
+ $subview = '';
}
- elgg_register_simplecache_view("$type/$view");
- return _elgg_get_simplecache_root() . "$type/$view";
+ // should be normalized to canonical form by now: `getUrl('js/blog/save_draft.js')`
+ elgg_register_simplecache_view($view);
+ return $this->getRoot() . $view;
}
-
/**
* Get the base url for simple cache requests
*
@@ -432,8 +432,6 @@ public function registerCacheableView($view) {
* @access private
*/
public function isCacheableView($view) {
-
-
if (!isset($this->CONFIG->views)) {
$this->CONFIG->views = new \stdClass;
}
@@ -1453,7 +1453,7 @@ public function getIconURL($params = array()) {
$url = _elgg_services()->hooks->trigger('entity:icon:url', $type, $params, null);
if ($url == null) {
- $url = "_graphics/icons/default/$size.png";
+ $url = elgg_get_simplecache_url("icons/default/$size.png");
}
return elgg_normalize_url($url);
View
@@ -204,7 +204,7 @@ function _elgg_admin_init() {
if (elgg_get_config('elgg_maintenance_mode', null)) {
elgg_register_plugin_hook_handler('route', 'all', '_elgg_admin_maintenance_handler', 600);
elgg_register_plugin_hook_handler('action', 'all', '_elgg_admin_maintenance_action_check', 600);
- elgg_register_css('maintenance', elgg_get_simplecache_url('css', 'maintenance'));
+ elgg_register_css('maintenance', elgg_get_simplecache_url('css/maintenance'));
elgg_register_menu_item('topbar', array(
'name' => 'maintenance_mode',
@@ -245,10 +245,10 @@ function _elgg_admin_init() {
elgg_register_action('profile/fields/reorder', '', 'admin');
elgg_register_simplecache_view('css/admin');
- $url = elgg_get_simplecache_url('js', 'admin');
+ $url = elgg_get_simplecache_url('js/admin');
elgg_register_js('elgg.admin', $url);
elgg_register_js('elgg.upgrades', 'js/lib/upgrades.js');
- elgg_register_js('jquery.jeditable', elgg_get_simplecache_url('js', 'jquery.jeditable.js'));
+ elgg_register_js('jquery.jeditable', elgg_get_simplecache_url('js/jquery.jeditable.js'));
// administer
// dashboard
@@ -358,7 +358,7 @@ function _elgg_admin_init() {
*/
function _elgg_admin_pagesetup() {
if (elgg_in_context('admin')) {
- $url = elgg_get_simplecache_url('css', 'admin');
+ $url = elgg_get_simplecache_url('css/admin');
elgg_register_css('elgg.admin', $url);
elgg_load_css('elgg.admin');
elgg_unregister_css('elgg');
View
@@ -100,57 +100,34 @@ function elgg_register_simplecache_view($view_name) {
}
/**
- * Get the URL for the cached file
+ * Get the URL for the cached view.
*
- * This automatically registers the view with Elgg's simplecache.
- *
- * @example
- * $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
- * elgg_register_js('elgg.blog', $blog_js);
- * elgg_load_js('elgg.blog');
+ * Recommended usage is to just pass the entire view name as the first and only arg:
*
- * @param string $type The file type: css or js
- * @param string $view The view name after css/ or js/
- * @return string
- * @since 1.8.0
- */
-function elgg_get_simplecache_url($type, $view) {
- return _elgg_services()->simpleCache->getUrl($type, $view);
-}
-
-
-/**
- * Get the base url for simple cache requests
+ * ```
+ * $blog_js = elgg_get_simplecache_url('js/blog/save_draft.js');
+ * $favicon = elgg_get_simplecache_url('favicon.ico');
+ * ```
*
- * @return string The simplecache root url for the current viewtype.
- * @access private
- */
-function _elgg_get_simplecache_root() {
- return _elgg_services()->simpleCache->getRoot();
-}
-
-/**
- * Returns the type of output expected from the view.
- *
- * css/* views always return "css"
- * js/* views always return "js"
+ * For backwards compatibility with older versions of Elgg, this function supports
+ * "js" or "css" as the first arg, with the rest of the view name as the second arg:
*
- * @todo why isn't this in the CacheHandler class? It is not used anywhere else.
- *
- * @todo view/name.suffix returns "suffix"
+ * ```
+ * $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft.js');
+ * ```
*
- * Otherwise, returns "unknown"
+ * Note that with this older approach, there is no way to access cached views
+ * outside of the js/ or css/ folders.
*
- * @param string $view The view name
+ * This automatically registers the view with Elgg's simplecache.
+ *
+ * @param string $view The full view name
+ * @param string $subview If the first arg is "css" or "js", the rest of the view name
* @return string
- * @access private
+ * @since 1.8.0
*/
-function _elgg_get_view_filetype($view) {
- if (preg_match('~(?:^|/)(css|js)(?:$|/)~', $view, $m)) {
- return $m[1];
- } else {
- return 'unknown';
- }
+function elgg_get_simplecache_url($view, $subview = '') {
+ return _elgg_services()->simpleCache->getUrl($view, $subview);
}
/**
@@ -3016,7 +3016,7 @@ function get_entity_icon_url(\ElggEntity $entity, $size = 'medium') {
return $overrideurl;
}
- $url = "_graphics/icons/default/$size.png";
+ $url = elgg_get_simplecache_url("icons/default/$size.png");
}
return elgg_normalize_url($url);
Oops, something went wrong.

0 comments on commit c44740d

Please sign in to comment.