站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
Gallery:编码指引
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
Firebrance
(
讨论
|
贡献
)
2008年11月11日 (二) 10:40的版本
(
Gallery:Coding Guidelines
移动到
Gallery:编码指引
)
(
差异
)
←上一版本
|
最后版本
(
差异
) |
下一版本→
(
差异
)
跳转至:
导航
、
搜索
警告:您正在编辑的是本页面的旧版本。
如果您发布该更改,该版本后的所有更改都会丢失。
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
This guide describes how to code for the Gallery 2 framework to make sure that it works on all platforms and as reliable as possible as well as that it is compatible with Gallery 2's requirements. This document does not cover the the [[Gallery2:Coding_Standards|Gallery 2 Coding Standards]], that is, the coding style is described elsewhere. __TOC__ == Database == * Do not use DBMS dependent SQL fragments. Some DBMS dependent SQL is abstracted in <tt>$storage->getFunctionSql()</tt>, e.g. <tt>BIT_OR, BIT_AND</tt>, etc. * Do not call database related methods directly (e.g. <tt>mysql_connect</tt>). Instead use the Gallery 2 methods. <pre><nowiki> global $gallery; list ($ret, $results) = $gallery->search('SELECT * FROM [GalleryPhotoItem]'); </nowiki></pre> * Do not modify Map tables directly. Use <tt>GalleryCoreApi::addMapEntry($mapName, ..), ::removeMapEntry, ..</tt> instead. :: '''Note:''' In G2.0, this was <tt>MapNameMap::addMapEntry()</tt> etc. * Do not create your own database queries unless it's really necessary. Instead use Gallery 2's API methods. * Do not use {{H:title|Data Definition Language|DDL}} (the <tt>CREATE TABLE, ALTER TABLE, ...</tt> subset of the SQL language). Instead, use the Entity classes and Maps.xml including the XML descriptions to create new or modify existing tables if necessary. Gallery 2 will then generate SQL code for all supported DBMS once you run <tt>make</tt> in the <tt>classes/</tt> directory. * '''Locking:''' Use Read and write locks when you need to update persistent data (data that is stored in database). Do not forget to release the locks once you're finished. :: '''Hint:''' lockIds are not the same as itemIds. === Large Datasets === * When dealing with large datasets, think about using a progress bar and <tt>$storage->checkPoint</tt> to minimize the effect of a possible fatal timeout / error. * Do not query with <tt>WHERE / UPDATE</tt> clauses that scale in query string size by the number of arguments since the query size is limited. Instead, do it in smaller batches.<nowiki> </nowiki>As an example of a bad <tt>WHERE / UPDATE</tt> clause, assume $allItemIds is a list of 1000+ ids: <pre><nowiki> $gallery->search("SELECT * FROM [GalleryItem] WHERE [GalleryItem::id] IN ($markers)", $allItemIds); </nowiki></pre> :: '''Note:''' GalleryCoreApi::loadEntitiesById() and GalleryCoreApi::acquire*Lock() are both not yet fixed. This means that you should call these methods with smaller batches too. Even once they are fixed to do the work in batches, you will still have the problem of webserver (not php) timeouts. We can't use a progress bar deep down in the API since we don't know if the current view supports a progress bar. == PHP == * Do not call <tt>die / exit / header('Location: ...')</tt> in your code. Instead, return a proper status / redirect url in your views/controllers. This way Gallery 2 has the chance to commit/rollback open transactions, do some cleanup, and apply patches to bugs (e.g. <tt>header('Location:')</tt> is buggy on some platforms and we fix it at the end of the request). * For PHP functions that are listed in <tt>modules/core/classes/GalleryPhpVm.class</tt>, use <pre><nowiki> global $gallery; $phpVm =& $gallery->getPhpVm(); $phpVm->functionName(); </nowiki></pre> * '''References:''' See [http://gallery.menalto.com/node/33076 how to handle references in PHP 4.4.0]. As a general rule, don't use PHP references unless they really make sense. Also, the performance when using references is not necessarily faster! See: [http://www.blueshoes.org/phpBench.php PHP Benchmark tests] * '''Compatibility:''' G2 code must run with PHP 4 and 5, that is PHP 4.1.0+ (as of G2.2 it'll be 4.3.0+) and PHP 5.0.4+. Do not use any "PHP 5"-only OO code and do not use any methods that are not available in PHP 4.1.0 unless you provide a fallback solution. <pre><nowiki> if (function_exists('foo_bar')) { foo_bar($data); } else { myFooBar($data); } </nowiki></pre> == Filesystem/Platform == * Do not call any filesystem / platform functions directly (e.g. <tt>fopen, fsockopen, exec, chmod, ...</tt>). Use the Gallery 2 methods instead. <pre><nowiki> global $gallery; $platform =& $gallery->getPlatform(); $platform->fopen($path); </nowiki></pre> == Strings/Texts and Translation == * For all strings / texts that the end user gets to see, use <pre><nowiki> $plugin->translate('Some text') </nowiki></pre> <nowiki> </nowiki>If the text is going into the database and <tt>translate()</tt> is called when retrieving it from the database, use <pre><nowiki> $gallery->i18n('Some text') </nowiki></pre> <nowiki> </nowiki>In smarty templates: <pre><nowiki> {g->text text="Some text"} </nowiki></pre> :: '''Note:''' Exceptions are the optional messages in GalleryStatus objects, and unit test failure messages which do not use <tt>translate()</tt>. * If you're using HTML in your string write it using the translation method. <pre><nowiki> /* Bad */ $plugin->translate('Do <b>NOT</b> do this!'); /* Good */ $plugin->translate(array('text' => 'Do %sNOT%s do this!', 'arg1' => '<b>', 'arg2' => '</b>')); </nowiki></pre> * Also use the translate arguments when the string includes variables. <pre><nowiki> /* Bad */ $plugin->translate("Next, please read chapter '$chapter'"); /* Good */ $plugin->translate(array('text' => "Next, please read '%s'", 'arg1' => $chapter)); </nowiki></pre> <nowiki> </nowiki>In smarty templates: <pre><nowiki> {g->text text="Next, please read '%s'" arg1=$chapter} </nowiki></pre> == Misc == * '''URL Generation:''' Generate all URLs using methods provided by Gallery.<br /><nowiki> </nowiki>In PHP: <pre><nowiki> global $gallery; $urlGenerator =& $gallery->getUrlGenerator(); $url = $urlGenerator->generateUrl($params, $options); </nowiki></pre> <nowiki> </nowiki>In smarty templates: <pre><nowiki> {g->url arg1= ....} </nowiki></pre> * '''Modifying code:''' Do not modify the code of existing modules. Instead, extend the feature set of Gallery 2 with additional modules. Reason: Users should be able to administer Gallery 2 with a simple web-interface. Adding a new feature should be as easy as installing a new module with a few mouse clicks. We don't want hacky installation instructions like "open file X, find line Y, replace A with B, ...". If you need a change in an existing module, please talk to the module author if this change could be incorporated in the existing module, preferably in a modular way (Factory Registrations), e.g. a module could check in the G2 factory if there are plugins for this module and your new module could register a plugin for the other module, thus extending the functionality in a modular way. [[Category:Gallery 2:Development|Coding Guidelines]]
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
模板:H:title
(
编辑
)