public function testFullRender() { $renderer = Pluf::factory('Pluf_Text_Wiki_Renderer'); $string = file_get_contents(dirname(__FILE__) . '/wikisample.txt'); $render = file_get_contents(dirname(__FILE__) . '/wikisample.render.txt'); $this->assertEquals($render, $renderer->render($string)); }
/** * Remove orphan repositories. */ public static function removeOrphanRepositories() { $path = Pluf::f('idf_plugin_syncgit_base_repositories', '/home/git/repositories'); if (!is_dir($path) || is_link($path)) { throw new Pluf_Exception_SettingError(sprintf('Directory %s does not exist! Setting "idf_plugin_syncgit_base_repositories not set.', $path)); } if (!is_writable($path)) { throw new Exception(sprintf('Repository %s is not writable.', $path)); } $projects = array(); foreach (Pluf::factory('IDF_Project')->getList() as $project) { $projects[] = $project->shortname; } unset($project); $it = new DirectoryIterator($path); $orphans = array(); while ($it->valid()) { if (!$it->isDot() && $it->isDir() && !in_array(basename($it->getFileName(), '.git'), $projects)) { $orphans[] = $it->getPathName(); } $it->next(); } if (count($orphans)) { $cmd = Pluf::f('idf_exec_cmd_prefix', '') . 'rm -rf ' . implode(' ', $orphans); exec($cmd); clearstatcache(); while (list(, $project) = each($orphans)) { if (is_dir($project)) { throw new Exception(sprintf('Cannot remove %s directory.', $project)); } } } }
/** * Get the current item. */ public function current() { $i = current($this->results); $doc = Pluf::factory($i['model_class'], $i['model_id']); $doc->_searchScore = $i['score']; return $doc; }
/** * Send the reminder email. * */ function save($commit = true) { if (!$this->isValid()) { throw new Exception(__('Cannot save the model from an invalid form.')); } $account = $this->cleaned_data['account']; $sql = new Pluf_SQL('email=%s OR login=%s', array($account, $account)); $users = Pluf::factory('Pluf_User')->getList(array('filter' => $sql->gen())); $return_url = ''; foreach ($users as $user) { if ($user->active) { $return_url = Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryInputCode'); $tmpl = new Pluf_Template('idf/user/passrecovery-email.txt'); $cr = new Pluf_Crypt(md5(Pluf::f('secret_key'))); $code = trim($cr->encrypt($user->email . ':' . $user->id . ':' . time()), '~'); $code = substr(md5(Pluf::f('secret_key') . $code), 0, 2) . $code; $url = Pluf::f('url_base') . Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecovery', array($code), array(), false); $urlic = Pluf::f('url_base') . Pluf_HTTP_URL_urlForView('IDF_Views::passwordRecoveryInputCode', array(), array(), false); $context = new Pluf_Template_Context(array('url' => Pluf_Template::markSafe($url), 'urlik' => Pluf_Template::markSafe($urlic), 'user' => Pluf_Template::markSafe($user), 'key' => Pluf_Template::markSafe($code))); $email = new Pluf_Mail(Pluf::f('from_email'), $user->email, __('Password Recovery - InDefero')); $email->setReturnPath(Pluf::f('bounce_email', Pluf::f('from_email'))); $email->addTextMessage($tmpl->render($context)); $email->sendMail(); } if (!$user->active and $user->first_name == '---') { $return_url = Pluf_HTTP_URL_urlForView('IDF_Views::registerInputKey'); IDF_Form_Register::sendVerificationEmail($user); } } return $return_url; }
/** * Get an item to process. * * @return mixed False if no item to proceed. */ public static function getItem() { $item = false; $db = Pluf::db(); $db->begin(); // In a transaction to not process the same item at // the same time from to processes. $gqueue = new Pluf_Queue(); $items = $gqueue->getList(array('filter' => $db->qn('lock') . '=0', 'order' => 'creation_dtime ASC')); if ($items->count() > 0) { $item = $items[0]; $item->lock = 1; $item->update(); } $db->commit(); if ($item === false) { return false; } // try to get the corresponding object $obj = Pluf::factory($item->model_class, $item->model_id); if ($obj->id != $item->model_id) { $obj = null; } return array('queue' => $item, 'item' => $obj); }
function postSave($create = false) { if ($create) { // Check if more than one revision for this page. We do // not want to insert the first revision in the timeline // as the page itself is inserted. We do not insert on // update as update is performed to change the is_head // flag. $sql = new Pluf_SQL('wikipage=%s', array($this->wikipage)); $rev = Pluf::factory('IDF_WikiRevision')->getList(array('filter' => $sql->gen())); if ($rev->count() > 1) { IDF_Timeline::insert($this, $this->get_wikipage()->get_project(), $this->get_submitter()); foreach ($rev as $r) { if ($r->id != $this->id and $r->is_head) { $r->is_head = false; $r->update(); } } } $page = $this->get_wikipage(); $page->update(); // Will update the modification timestamp. IDF_Search::index($page); } }
public function initFields($extra = array()) { $this->user = $extra['user']; $this->project = $extra['project']; if ($this->user->hasPerm('IDF.project-owner', $this->project) or $this->user->hasPerm('IDF.project-member', $this->project)) { $this->show_full = true; } $this->fields['summary'] = new Pluf_Form_Field_Varchar(array('required' => true, 'label' => __('Summary'), 'initial' => '', 'widget_attrs' => array('maxlength' => 200, 'size' => 67))); $this->fields['description'] = new Pluf_Form_Field_Varchar(array('required' => true, 'label' => __('Description'), 'initial' => '', 'widget' => 'Pluf_Form_Widget_TextareaInput', 'widget_attrs' => array('cols' => 58, 'rows' => 7))); $sql = new Pluf_SQL('project=%s', array($this->project->id)); $commits = Pluf::factory('IDF_Commit')->getList(array('order' => 'creation_dtime DESC', 'nb' => 10, 'filter' => $sql->gen())); $choices = array(); foreach ($commits as $c) { $id = strlen($c->scm_id) > 10 ? substr($c->scm_id, 0, 10) : $c->scm_id; $ext = mb_strlen($c->summary) > 50 ? mb_substr($c->summary, 0, 47) . '...' : $c->summary; $choices[$id . ' - ' . $ext] = $c->scm_id; } $this->fields['commit'] = new Pluf_Form_Field_Varchar(array('required' => true, 'label' => __('Commit'), 'initial' => '', 'widget' => 'Pluf_Form_Widget_SelectInput', 'widget_attrs' => array('choices' => $choices))); $upload_path = Pluf::f('upload_issue_path', false); if (false === $upload_path) { throw new Pluf_Exception_SettingError(__('The "upload_issue_path" configuration variable was not set.')); } $md5 = md5(rand() . microtime() . Pluf_Utils::getRandomString()); // We add .dummy to try to mitigate security issues in the // case of someone allowing the upload path to be accessible // to everybody. $filename = substr($md5, 0, 2) . '/' . substr($md5, 2, 2) . '/' . substr($md5, 4) . '/%s.dummy'; $this->fields['patch'] = new Pluf_Form_Field_File(array('required' => true, 'label' => __('Patch'), 'move_function_params' => array('upload_path' => $upload_path, 'upload_path_create' => true, 'file_name' => $filename))); if ($this->show_full) { $this->fields['status'] = new Pluf_Form_Field_Varchar(array('required' => true, 'label' => __('Status'), 'initial' => 'New', 'widget_attrs' => array('maxlength' => 20, 'size' => 15))); } }
/** * Create a commit from a simple class commit info of a changelog. * * @param stdClass Commit info * @param IDF_Project Current project * @return IDF_Commit */ public static function getOrAdd($change, $project) { $sql = new Pluf_SQL('project=%s AND scm_id=%s', array($project->id, $change->commit)); $r = Pluf::factory('IDF_Commit')->getList(array('filter' => $sql->gen())); if ($r->count() > 0) { $r[0]->extra = new IDF_Gconf(); $r[0]->extra->serialize = true; $r[0]->extra->setModel($r[0]); $r[0]->extra->initCache(); return $r[0]; } if (!isset($change->full_message)) { $change->full_message = ''; } $scm = IDF_Scm::get($project); $commit = new IDF_Commit(); $commit->project = $project; $commit->scm_id = $change->commit; $commit->summary = self::toUTF8($change->title); $commit->fullmessage = self::toUTF8($change->full_message); $commit->author = $scm->findAuthor($change->author); $commit->origauthor = self::toUTF8($change->author); $commit->creation_dtime = $change->date; $commit->create(); $extra = $scm->getExtraProperties($change); $commit->extra = new IDF_Gconf(); $commit->extra->serialize = true; // As we can store arrays $commit->extra->setModel($commit); foreach ($extra as $key => $val) { $commit->extra->setVal($key, $val); } $commit->notify($project->getConf()); return $commit; }
function tearDown() { $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $m = new Pluf_Permission(); $schema->model = $m; $schema->dropTables(); }
protected function tearDown() { $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $m1 = new TestFormModel(); $schema->model = $m1; $schema->dropTables(); }
/** * Predelete to drop the row level permissions. */ function preDelete() { if (Pluf::f('pluf_use_rowpermission', false)) { $_rpt = Pluf::factory('Pluf_RowPermission')->getSqlTable(); $sql = new Pluf_SQL('owner_class=%s AND owner_id=%s', array($this->_a['model'], $this->_data['id'])); $this->_con->execute('DELETE FROM ' . $_rpt . ' WHERE ' . $sql->gen()); } }
/** * Get an object by SQL or raise a 404 error. * * Usage: * <pre> * $obj = Pluf_Shortcuts_GetOneOr404('MyApp_Model', * 'path=%s AND status=%s', * array('welcome', 1)); * </pre> * * @param string Model * @param string Base SQL request * @param string Parameters for the base SQL * @return Object The found object */ function Pluf_Shortcuts_GetOneOr404($object, $bsql, $psql) { $sql = new Pluf_SQL($bsql, $psql); $item = Pluf::factory($object)->getOne(array('filter' => $sql->gen())); if ($item != null) { return $item; } throw new Pluf_HTTP_Error404(); }
public function testSimplePaginate() { $model = new TestFormModel(); $pag = Pluf::factory('Pluf_Paginator', $model); $pag->action = '/permission/'; $pag->items_per_page = 5; $render = file_get_contents(dirname(__FILE__) . '/rendersimplepaginate.html'); $this->assertEquals($render, $pag->render()); $this->assertEquals($pag->page_number, 2); }
/** * Get the matching permission object from the permission string. * * @param string Permission string, for example 'Pluf_User.create'. * @return false|Pluf_Permission The matching permission or false. */ public static function getFromString($perm) { list($app, $code) = explode('.', trim($perm)); $sql = new Pluf_SQL('code_name=%s AND application=%s', array($code, $app)); $perms = Pluf::factory('Pluf_Permission')->getList(array('filter' => $sql->gen())); if ($perms->count() != 1) { return false; } return $perms[0]; }
/** * Given a model or model name, dump the content. * * If the object is given, only this single object is dumped else * the complete table. * * @param mixed Model object or model name * @param bool Serialize as JSON (true) * @return mixed Array or JSON string */ public static function dump($model, $serialize = true) { if (is_object($model)) { return $serialize ? json_encode(array(self::prepare($model))) : array(self::prepare($model)); } $out = array(); foreach (Pluf::factory($model)->getList(array('order' => 'id ASC')) as $item) { $out[] = self::prepare($item); } return $serialize ? json_encode($out) : $out; }
public function findAuthor($author) { // We extract the email. $match = array(); if (!preg_match('/<(.*)>/', $author, $match)) { return null; } $sql = new Pluf_SQL('email=%s', array($match[1])); $users = Pluf::factory('Pluf_User')->getList(array('filter' => $sql->gen())); return $users->count() > 0 ? $users[0] : null; }
/** * Get for the given hashes the corresponding date, title and * author. * * It returns an hash indexed array with the info. If an hash is * not in the db, the key is not set. * * Note that the hashes must always come from internal tools. * * @param array Hashes to get info * @return array Blob infos */ public function retrieve($hashes) { $res = array(); $db = $this->getDbConnection(); $hashes = array_map(array($db, 'esc'), $hashes); $sql = new Pluf_SQL('project=%s AND githash IN (' . implode(', ', $hashes) . ')', array($this->_project->id)); foreach (Pluf::factory(__CLASS__)->getList(array('filter' => $sql->gen())) as $blob) { $tmp = explode(chr(31), $blob->content, 3); $res[$blob->githash] = (object) array('hash' => $blob->githash, 'date' => $tmp[0], 'title' => $tmp[2], 'author' => $tmp[1]); } return $res; }
function callbackWikiPage($m) { $sql = new Pluf_SQL('project=%s AND title=%s', array($this->project->id, $m[2])); $pages = Pluf::factory('IDF_WikiPage')->getList(array('filter' => $sql->gen())); if ($pages->count() != 1 and $this->request->rights['hasWikiAccess'] and !$this->request->user->isAnonymous()) { return '<img style="vertical-align: text-bottom;" alt=" " src="' . Pluf::f('url_media') . '/idf/img/add.png" /><a href="' . Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::create', array($this->project->shortname), array('name' => $m[2])) . '" title="' . __('Create this documentation page') . '">' . $m[1] . '</a>'; } if (!$this->request->rights['hasWikiAccess'] or $pages->count() == 0) { return $m[1]; } return '<a href="' . Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', array($this->project->shortname, $pages[0]->title)) . '" title="' . Pluf_esc($pages[0]->summary) . '">' . $m[1] . '</a>'; }
public function clean_title() { $title = $this->cleaned_data['title']; if (preg_match('/[^a-zA-Z0-9\\-]/', $title)) { throw new Pluf_Form_Invalid(__('The title contains invalid characters.')); } $sql = new Pluf_SQL('project=%s AND title=%s', array($this->project->id, $title)); $pages = Pluf::factory('IDF_WikiPage')->getList(array('filter' => $sql->gen())); if ($pages->count() > 0) { throw new Pluf_Form_Invalid(__('A page with this title already exists.')); } return $title; }
function IDF_Migrations_12DownloadDesc_down($params = null) { $table = Pluf::factory('IDF_Upload')->getSqlTable(); $sql = array(); $sql['PostgreSQL'] = 'ALTER TABLE ' . $table . ' DROP COLUMN "changelog"'; $sql['MySQL'] = 'ALTER TABLE ' . $table . ' DROP COLUMN `changelog`'; $db = Pluf::db(); $engine = Pluf::f('db_engine'); if (!isset($sql[$engine])) { throw new Exception('SQLite complex migration not supported.'); } $db->execute($sql[$engine]); }
/** * Add the download of files. */ function IDF_Migrations_2Search_up($params = null) { $models = array('IDF_Search_Occ'); $db = Pluf::db(); $schema = new Pluf_DB_Schema($db); foreach ($models as $model) { $schema->model = new $model(); $schema->createTables(); } foreach (Pluf::factory('IDF_Issue')->getList() as $i) { IDF_Search::index($i); } }
function IDF_Migrations_9ShortDescription_down($params = null) { $table = Pluf::factory('IDF_Project')->getSqlTable(); $sql = array(); $sql['PostgreSQL'] = 'ALTER TABLE ' . $table . ' DROP COLUMN "shortdesc"'; $sql['MySQL'] = 'ALTER TABLE ' . $table . ' DROP COLUMN `shortdesc`'; $db = Pluf::db(); $engine = Pluf::f('db_engine'); if (!isset($sql[$engine])) { throw new Exception('SQLite complex migration not supported.'); } $db->execute($sql[$engine]); }
public function clean_mtn_master_branch() { $mtn_master_branch = mb_strtolower($this->cleaned_data['mtn_master_branch']); if (!preg_match('/^([\\w\\d]+([-][\\w\\d]+)*)(\\.[\\w\\d]+([-][\\w\\d]+)*)*$/', $mtn_master_branch)) { throw new Pluf_Form_Invalid(__('The master branch is empty or contains illegal characters, ' . 'please use only letters, digits, dashs and dots as separators.')); } $sql = new Pluf_SQL('vkey=%s AND vdesc=%s AND project!=%s', array('mtn_master_branch', $mtn_master_branch, (string) $this->project->id)); $l = Pluf::factory('IDF_Conf')->getList(array('filter' => $sql->gen())); if ($l->count() > 0) { throw new Pluf_Form_Invalid(__('This master branch is already used. Please select another one.')); } return $mtn_master_branch; }
public function testCreate() { $gproj = Pluf::factory('IDF_Project')->getList(); $this->assertEqual(0, $gproj->count()); $project = new IDF_Project(); $project->name = 'Test project'; $project->shortname = 'test'; $project->description = 'This is a test project.'; $project->create(); $id = $project->id; $p2 = new IDF_Project($id); $this->assertEqual($p2->shortname, $project->shortname); }
/** * Display the main page of the application. * * The main page is only displaying data. So we load the categories * and for each category we display the corresponding items. * * @param Pluf_HTTP_Request Request object * @param array Matches against the regex of the dispatcher * @return Pluf_HTTP_Response or can throw Exception */ public function main($request, $match) { // In the main page we want a list of all the Todo lists with // a link to edit each of them, a link to see the content and // a link to create a new list. // Get the complete list of Todo_List object $lists = Pluf::factory('Todo_List')->getList(); // Create a context for the template $context = new Pluf_Template_Context(array('page_title' => 'Home', 'lists' => $lists)); // Load a template $tmpl = new Pluf_Template('todo/index.html'); // Render the template and send the response to the user return new Pluf_HTTP_Response($tmpl->render($context)); }
function postSave($create = false) { if ($create) { // Check if more than one comment for this issue. We do // not want to insert the first comment in the timeline as // the issue itself is inserted. $sql = new Pluf_SQL('issue=%s', array($this->issue)); $co = Pluf::factory('IDF_IssueComment')->getList(array('filter' => $sql->gen())); if ($co->count() > 1) { IDF_Timeline::insert($this, $this->get_issue()->get_project(), $this->get_submitter()); } } IDF_Search::index($this->get_issue()); }
/** * Add the download of files. */ function IDF_Migrations_5DescToText_up($params = null) { $table = Pluf::factory('IDF_Conf')->getSqlTable(); $sql = array(); $sql['PostgreSQL'] = 'ALTER TABLE ' . $table . ' ALTER vdesc TYPE text'; $sql['MySQL'] = 'ALTER TABLE ' . $table . ' CHANGE vdesc TYPE text'; $db = Pluf::db(); $engine = Pluf::f('db_engine'); if (!isset($sql[$engine])) { echo 'Skip SQLite upgrade as not needed.' . "\n"; return; } $db->execute($sql[$engine]); }
/** * Generate a standard "line" of the body. * * It is important to note that the table has only 2 columns, so * the timelineFragment() method of each item must take that into * account. */ function bodyLine($item) { $doc = Pluf::factory($item->model_class, $item->model_id); $doc->public_dtime = $item->public_dtime; $item_day = Pluf_Template_dateFormat($item->creation_dtime, '%y-%m-%d'); $out = ''; if ($this->current_day == null or Pluf_Date::dayCompare($this->current_day, $item_day) != 0) { $day = Pluf_Template_dateFormat($item->creation_dtime); if ($item_day == Pluf_Template_timeFormat(time(), 'y-m-d')) { $day = __('Today'); } $out = '<tr><th colspan="2">' . $day . '</th></tr>' . "\n"; $this->current_day = $item_day; } return $out . $doc->timelineFragment($item->request); }
function Todo_Migrations_Install_teardown($params = '') { // The uninstallation is the reverse of the installation. // We create the data models the same way, but instead of calling // createTables() we call dropTables() // You can see that without all the comments, you do not have a // lot of lines of code. $list = new Todo_List(); $item = new Todo_Item(); $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $schema->model = $list; $schema->dropTables(); $schema->model = $item; $schema->dropTables(); }
function formField($def, $form_field = 'Pluf_Form_Field_Integer') { $method = 'get_' . $def['name'] . '_list'; $def['multiple'] = true; $def['initial'] = array(); foreach ($def['model_instance']->{$method}() as $item) { $def['initial'][(string) $item] = $item->id; } $def['choices'] = array(); foreach (Pluf::factory($def['model'])->getList() as $item) { $def['choices'][(string) $item] = $item->id; } if (!isset($def['widget'])) { $def['widget'] = 'Pluf_Form_Widget_SelectMultipleInput'; } return parent::formField($def, $form_field); }