Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of InDefero, an open source project management application. # Copyright (C) 2008 Céondo Ltd and contributors. # # InDefero is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # InDefero is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * This class implements the cache storage for the Git commits. * * The storage is simple. Each commit is linked to a project to drop * the cache when the project is dropped. The key is the commit hash * and the data is the date, author and one line title of information. * * A clean interface is available to bulk set/get a series of commit * info with the minimum of SQL queries. The goal is to be fast. */ class IDF_Scm_Cache_Git extends Pluf_Model { public $_model = __CLASS__ ; /** * The current project to limit the search to. */ public $_project = null; /** * Store in the cache blob infos. * * The info is an array of stdClasses, with hash, date, title and * author properties. * * @param array Blob infos */ public function store( $infos ) { foreach ( $infos as $blob ) { $cache = new IDF_Scm_Cache_Git(); $cache ->project = $this ->_project; $cache ->githash = $blob ->hash; $blob ->title = IDF_Commit::toUTF8( $blob ->title); $cache ->content = IDF_Commit::toUTF8( $blob -> date ) . chr (31) . IDF_Commit::toUTF8( $blob ->author) . chr (31) . IDF_Commit::toUTF8( $blob ->title); $sql = new Pluf_SQL( 'project=%s AND githash=%s' , array ( $this ->_project->id, $blob ->hash)); if (0 == Pluf::factory( __CLASS__ )->getCount( array ( 'filter' => $sql ->gen()))) { $cache ->create(); } } } /** * 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); // sometimes the title might be empty if (!isset( $tmp [2])) $tmp [2] = '' ; $res [ $blob ->githash] = (object) array ( 'hash' => $blob ->githash, 'date' => $tmp [0], 'title' => $tmp [2], 'author' => $tmp [1], ); } return $res ; } /** * The storage is composed of 4 columns, id, project, hash and the * raw data. */ function init() { $this ->_a[ 'table' ] = 'idf_scm_cache_git' ; $this ->_a[ 'model' ] = __CLASS__ ; $this ->_a[ 'cols' ] = array ( // It is mandatory to have an "id" column. 'id' => array ( 'type' => 'Pluf_DB_Field_Sequence' , 'blank' => true, ), 'project' => array ( 'type' => 'Pluf_DB_Field_Foreignkey' , 'model' => 'IDF_Project' , 'blank' => false, ), 'githash' => array ( 'type' => 'Pluf_DB_Field_Varchar' , 'blank' => false, 'size' => 40, 'index' => true, ), 'content' => array ( 'type' => 'Pluf_DB_Field_Text' , 'blank' => false, ), ); } } |