Gallery:代码概览

来自站长百科
Firebrance讨论 | 贡献2008年11月25日 (二) 16:57的版本
跳转至: 导航、​ 搜索

代码回顾

该页面描述Gallery2应用程序的基本结构以及一些高级设计概念。它能帮你在开发新模块,外观主题或修改现有代码之前找到门路。它能帮助读者了解面向软件编程以及基于web的应用程序。

Gallery2是一个PHP应用程序,它使用单个入口点来处理所有的HTTP请求:main.php。此脚本会读取你的配置文件(config.php)然后检查某些基础参量,这样它就能将请求处理转为正确的代码了。Gallery使用 [1]的设计模式(尽管使用的是松散定义的模型)。简言之就是,视图用于显示而控制器用于发出动作。所以main.php决定处理请求由哪个视图或控制器进行控制,并会将控制权移交给选择。视图是被包裹在一个外观主题系统中的,这样就能使得网站建立者可以对网站布局和外观进行控制,而只需要牵涉到最少的PHP代码就可以达到目的。

img:146290

视图

模块在模块目录的*.inc文件中对视图(类别扩展Gallery视图)进行定义。有两类视图:

标准视图(Standard Views): 这些视图在应用程序中创建一个HTML页面。要将代码从用户界面中分离出来的话,所有页面创建所需的数据都会先被载入。数据容器为Gallery模板。视图会读取其所需的任何数据,然后外观主题(外观主题也会载入某些数据)则接手并对页面进行渲染。

即时视图(Immediate Views): 这些应特殊目的而存在的视图会绕过外观主题系统并直接发送输出。这可发送非HTML的输出,如binaryXML数据。即时视图也可用于AJAX请求,其中javascript代码能与Gallery服务器后台进行交互,并在结果返回时动态更新页面。某些AJAX视图可能会将更改保存到数据库(也有争议说这些应是控制器)。

  • 技术注解:一个即时视图包括一个返回true值的isImmediate函数;一个保存更改的AJAX视图还应包括一个返回true值的isControllerLike函数。


外观主题

Themes have total control of Gallery's page layout and appearance. The Gallery framework determines the type of page being accessed and calls one of six functions in the active theme's theme.inc file (a class extending GalleryTheme).

  • showAlbumPage or showPhotoPage: Displays individual or grouped Gallery items
  • showModulePage: Other Gallery pages added by modules
  • showAdminPage: Gallery administration pages; they function just like module pages, but call a different theme function in case the theme wants to customize the display of browsing vs admin pages
  • showErrorPage: main.php routes errors to the core.ErrorPage view
  • showProgressBar: Controllers may delegate control to the core.ProgressBar view for long running tasks

Each of these functions loads any additional required data into the template and then specifies a tpl file for the page display. In most cases this is theme.tpl. Calling theme.tpl first creates a wrapper for other page types and maintains a basic look and feel (headers, banners, etc) across all Gallery pages. The theme.tpl then rechecks the page type and includes the associated tpl for the Gallery content requested. More about themes and the template system

控制器

Controllers perform actions that will save changes on the server. Adding new photos or comments, changing settings or even logging in are actions handled by controllers. Controllers extend the GalleryController class and their code resides in the *.inc files in a module's directory along with the views.

Generally the first task done by controllers is to verify proper permissions for the requested action. Always remember that even if your view omits a button or link based on permissions, the controller still must check permissions as HTTP requests can be manually constructed.

The second task is to validate the input data. If any user entered data is missing or invalid, the controller adds a token to the $error array that will be returned. Only if the inputs are valid does the controller proceed with the operation and set a key in the $success array. Controllers then instruct main.php what to do next. Upon success a controller will send a redirect, usually back to the same view. The $success data is stored in the session and made available to the view in the next request so it can show a status message. If $error is nonempty usually a controller will delegate back to the same view. This means the view is rendered in the same HTTP request, so the given $form data is still available. The view can then fill in the user entered data (so they don't have to enter it again) and check the $error tokens to show the appropriate error message next to the invalid data. These checks and messages are in the tpl file for the view.


子视图,*插件和*选项

Some views make it possible for other modules to add components, bringing different features together in a common user interface. All three of the admin type views (core.SiteAdmin, core.ItemAdmin and core.UserAdmin) use "subviews" to group those functions together. A subview is defined in a *.inc file and is written like a normal view. The difference is the module.inc file will list the view in its getSiteAdminViews, getItemAdminViews or getUserAdminViews function so a link for the view will appear in the sidebar of the appropriate admin area. Note that subviews use their own controllers (there is no "subcontroller").

Two subviews have additional levels of depth for adding user interface components. core.ItemAdd and core.ItemEdit are both ItemAdmin subviews, but each includes too many options to show on a single page. So each offers a plugin interface (not to be confused with the usual meaning of a Gallery plugin, which is a module or theme) to add components, which are shown as tabs. Modules can register implementations of the ItemAddPlugin or ItemEditPlugin interface in the performFactoryRegistrations function to make these components available when the module is activated. While registered in a different manner, these are still similar to subviews in that they control the main content of the page. The final level of depth is ItemAddOption and ItemEditOption. These are also factory registrations, but instead of defining an entire form they add just a section to a form and some processing upon form submission. ItemAddOptions may add some form controls (such as the checkbox for whether to build thumbnails), or they may add no UI component and just perform post-processing (such as EXIF autorotate). ItemEditOptions usually have both UI and post-processing, and also are registered to appear with a particular ItemEditPlugin. For example, the thumbnail module adds its custom thumbnail controls to the General tab, as any item type can use a custom thumbnail. The size limit module adds controls to the Album tab, as its settings are defined for an entire album.

厂(Factory)

The *Option classes discussed above are two examples of factory interfaces. Gallery uses this mechanism for all communication between modules. A module defines an interface class that describes the functions of the interface. This can be used in two ways: other modules can define implementations of the interface, allowing those implementations to be found and used. This is how the *Option classes are discovered. It is also how the search module allows other modules to make their data searchable. Alternatively, the module itself can implement the interface which allows other modules to use it. This is how the EXIF module allows other modules to read EXIF data from image files. Implementations are always registered in the performFactoryRegistrations function of module.inc. The interfaces themselves don't need to be registered.


事件侦听器

Gallery includes an event system so that modules may take action when certain events take place. For example, the ratings module listens for GalleryEntity::delete events and remove all ratings for an item when the item is deleted. No registration is needed for event types; other modules just need to listen for the right event name. Read more about events.


安全性

Security is a shared responsibility of modules and the Gallery framework. The framework provides tools such as GalleryUtilities::getRequestVariables and GalleryCoreApi::assertHasItemPermission, but modules must also use these properly. Other aspects of security are transparent to modules, and handled automatically by the framework, such as preventing session hijacking or cross site request forgery. Read more about Gallery 2 Security.


会话管理

Gallery manages user sessions automatically. When a user logs in or a guest does something that requires saving state between requests (such as adding items to a cart), a persistent session is created. Modules can store short-lived information in the session too. This information is discarded if the user logs out or the session expires. Gallery takes care of creating a cookie to track the session, or adding the session ID to all URLs in the page if the browser does not accept cookies. Read more about session management.


国际化

Gallery is an internationalized application. This means all user-visible text in the application can be translated for display in any language. Modules make text available for translation by using the $module->translate() or $gallery->i18n() functions in PHP code, and the {g->text} tag in tpl files. Note that these translations are for text included with the application. User entered text (such as image titles and captions) are not translated by this system. The multilanguage module does make it possible to enter a few basic fields in more than one language. Read more about internationalization and translations.


接下来的步骤

Check the available documents in the Gallery 2 Development category to find additional detail on these and other topics. Particularly look for the Module development tutorial and information about the Gallery 2 APIs. Also, once you understand the basics, looking at existing code is a great way to learn how to accomplish various tasks in Gallery code. You can also visit the #gallery channel on irc://chat.freenode.net to ask development questions. Good luck!