source: applications/nimpad/trunk/lib/NimpadAdmin.php @ 1490

SVN
Revision 1490, 2.6 KB checked in by anatoo, 10 months ago (diff)

0.2ブランチのリビジョン1489,1488の変更をtrunkに取り込んだ

Line 
1<?php
2
3class NimpadAdmin
4{
5    /**
6     * @var dao_Wiki
7     */
8    public $daoWiki;
9   
10    /**
11     * @var dao_Page
12     */
13    public $daoPage;
14   
15    /**
16     * @var PDO
17     */
18    public $pdo;
19   
20    /**
21     * @var text_Processor
22     */
23    public $textProcessor;
24   
25    /**
26     * @var string
27     */
28    public $baseurl;
29   
30    /**
31     * ランダムな名前を持つwikiの作成
32     *
33     * @param string
34     * @param string $baseurl ex. http://hoge.com/
35     * @return string ランダムな名前
36     */
37    function createNewWiki($text, $baseurl)
38    {
39        $this->pdo->beginTransaction();
40        try {
41            $name = $this->findNewRandomName();
42            $wiki_id = $this->daoWiki->create($name, array('home' => 'README'));
43            $this->daoPage->create($wiki_id, 'README', 
44                array('html' => $this->textProcessor->render('hatena', $text, 'README', $baseurl . $name . '/'), 
45                      'text' => $text));
46            $this->pdo->commit();
47        } catch (PDOException $e) {
48            $this->pdo->rollBack();
49            return false;
50        }
51        return $name;
52    }
53   
54    protected function findNewRandomName()
55    {
56        for (;;) {
57            $name = $this->buildRandomName(); 
58            if ($name === 'site') continue; 
59            if (!$this->existsName($name)) break;
60        }
61       
62        return $name;
63    }
64   
65    function existsName($name)
66    {
67        return $this->daoWiki->existsName($name);
68    }
69   
70    protected function buildRandomName()
71    {
72        static $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
73
74        for ($name = '', $i = 7, $len = strlen($chars); $i-- > 0;) 
75            $name .= $chars[rand(0, $len - 1)];
76        return $name;
77    }
78   
79    function fetchByEmail($email)
80    {
81        return $this->daoWiki->fetchByEmail($email);
82    }
83}
84
85return;
86<<<PHPOD
87
88* NimpadAdminクラス
89このクラスは、モデルの最上層のひとつ。
90Nimpadクラスがすでに存在するwikiをユーザーが操作するのに使われるのに対して、
91このクラスはまだ操作すべきwikiがない場合に使う。
92具体的には以下の通り。
93- ランダムな名前をもつwikiの作成
94- ある特定の名前を持つwikiが存在するか
95
96* ランダムな名前を持つwikiの作成
97- nim_wikiにエントリ挿入
98-- defaultというページをすでに持つ
99-- まだ存在していないランダムな名前
100- nim_pageにエントリ挿入
101-- defaultというページ
102- 新しく作成した名前を返す
103PHPOD;
Note: See TracBrowser for help on using the repository browser.