Gallery: 如何编写脚本:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
第1行: 第1行:
You can extend Gallery's functionality by writing modules and themes, but you can also call Gallery from your own scripts.
你可以编写模块及主题来对Gallery的功能进行扩展,你也可以使用自己编写的脚本。.


Applications (examples):
应用程序(举例):
* Run tasks periodically based on cron as a scheduler calling your own PHP script to execute some operations in Gallery.
* 基于cron周期性地运行任务—相当于一个等程序(scheduler)呼叫PHP脚本来执行Gallery中的某些操作。
* Write custom scripts to batch-import thousands of users, comments, etc.
* 编写自定义脚本来批量导入大量用户信息及评论等。
* ...
* ...


Note that we don't want you to reinvent the wheel. If you're looking for a command line client to add items, take a look at [[Other_Clients|List of Other Clients]].
请注意我们并不是让你从零开始颇费周章地重新编写脚本。如果你在寻求一个命令行客户端程序,以用来添加项目的话,请参看一下:[[Other_Clients|其他客户端程序列表]]


== Basic Script Structure ==
== 脚本的基本结构 ==
The [http://gallery.menalto.com/apidoc/GalleryCore/Classes/GalleryEmbed.html GalleryEmbed API] is not only handy to [[Gallery:Embedding|integrate]] Gallery into other web applications, it's also useful to write command line scripts:
[http://gallery.menalto.com/apidoc/GalleryCore/Classes/GalleryEmbed.html GalleryEmbed API]不仅能够胜任Gallery[[Gallery:Embedding|整合]]如其他web应用程序的工作,而且在编写命令行脚本方面也有很大的作用:


<pre>
<pre>
<?php
<?php
/* Adjust the path to point to your gallery2 folder */
/* 调整路径使其指向你的gallery2文件夹 */
require_once('../gallery2/embed.php');
require_once('../gallery2/embed.php');


第23行: 第23行:
check($ret);
check($ret);


/* Here go your G2 API calls */
/* 这里是你的G2 API呼叫 */




/*
/*
  * At the end, commit the transaction by calling ::done()
  * 最后,呼叫::done()来提交数据库事务对象
  * or all changes made above will be rolled back automatically
  * 或者以上作出的所有修改都会自动回滚
  */
  */
$ret = GalleryEmbed::done();
$ret = GalleryEmbed::done();
第39行: 第39行:
</pre>
</pre>


== Available API ==
== 可用API ==
After the GalleryEmbed::init() call, you can use all of [[Gallery:API|Gallery's APIs]] (GalleryCoreApi, GalleryModuleApi, ...).
GalleryEmbed::init()呼叫完成后,你可以使用[[Gallery:API|Gallery的所有API]](GalleryCoreApi,GalleryModuleApi…)。


== Examples ==
== 实例 ==
* [http://gallery.menalto.com/node/42333#comment-160089 Reupload / Rescan all your images without losing captions, comments, ..]
* [http://gallery.menalto.com/node/42333#comment-160089 在不丢失字幕及评论等的前提下,重新上传/重复扫描所有图片]
* [http://gallery.menalto.com/node/60419#comment-220510 Batch Import Users From an External Data Source]
* [http://gallery.menalto.com/node/60419#comment-220510 由外部数据源批量导入用户信息]


[[Category:Gallery 2:Administration and Maintenance]]
[[Category:Gallery 2:Administration and Maintenance]]

2008年8月1日 (五) 11:24的版本

你可以编写模块及主题来对Gallery的功能进行扩展,你也可以使用自己编写的脚本。.

应用程序(举例):

  • 基于cron周期性地运行任务—相当于一个等程序(scheduler)呼叫PHP脚本来执行Gallery中的某些操作。
  • 编写自定义脚本来批量导入大量用户信息及评论等。
  • ...

请注意我们并不是让你从零开始颇费周章地重新编写脚本。如果你在寻求一个命令行客户端程序,以用来添加项目的话,请参看一下:其他客户端程序列表

脚本的基本结构

GalleryEmbed API不仅能够胜任Gallery整合如其他web应用程序的工作,而且在编写命令行脚本方面也有很大的作用:

<?php
/* 调整路径使其指向你的gallery2文件夹 */
require_once('../gallery2/embed.php');

/*
 * Initialize G2 (includes all necessary classes,
 * puts G2 into a state ready to process your calls)
 */
$ret = GalleryEmbed::init(array('fullInit' => true));
check($ret);

/* 这里是你的G2 API呼叫 */


/*
 * 最后,呼叫::done()来提交数据库事务对象
 * 或者以上作出的所有修改都会自动回滚
 */
$ret = GalleryEmbed::done();
check($ret);

function check($ret) {
    if ($ret) die($ret->getAsHtml());
}
?>

可用API

GalleryEmbed::init()呼叫完成后,你可以使用Gallery的所有API(GalleryCoreApi,GalleryModuleApi…)。

实例