Indefero

Indefero Commit Details


Date:2008-08-13 11:26:36 (16 years 4 months ago)
Author:Loic d'Anterroches
Branch:dev, develop, feature-issue_links, feature.better-home, feature.content-md5, feature.diff-whitespace, feature.download-md5, feature.issue-links, feature.issue-of-others, feature.issue-summary, feature.search-filter, feature.webrepos, feature.wiki-default-page, master, release-1.1, release-1.2, release-1.3, svn
Commit:a1e10bd169c83ec0e8d4616027ced350a73963d9
Parents: 5275a1a9d6b8c4a8796d6b5b50a44bcb7348aae1
Message:Added a search engine in the issues.

Changes:

File differences

src/IDF/Issue.php
132132
133133
134134
135
135
136
137
138
139
140
136141
137142
138143
......
146151
147152
148153
149
150
151
152
153
154
155
156
154
157155
158156
function _toIndex()
{
return '';
$r = array();
foreach ($this->get_comments_list() as $c) {
$r[] = $c->_toIndex();
}
$str = str_repeat($this->summary.' ', 4).' '.implode(' ', $r);
return Pluf_Text::cleanString(html_entity_decode($str, ENT_QUOTES, 'UTF-8'));
}
function postSave($create=false)
{
// This will be used to fire the indexing or send a
// notification email to the interested people, etc.
$q = new Pluf_Queue();
$q->model_class = __CLASS__;
$q->model_id = $this->id;
$q->action = 'updated';
$q->lock = 0;
$q->create();
print IDF_Search::index($this);
}
}
src/IDF/IssueComment.php
107107
108108
109109
110
111
112
113
114
115
116
117
118110
119111
function postSave($create=false)
{
// This will be used to fire the indexing or send a
// notification email to the interested people, etc.
$q = new Pluf_Queue();
$q->model_class = __CLASS__;
$q->model_id = $this->id;
$q->action = 'updated';
$q->lock = 0;
$q->create();
}
}
src/IDF/Migrations/2Search.php
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
<?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 ***** */
/**
* 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();
}
}
function IDF_Migrations_2Search_down($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->dropTables();
}
}
src/IDF/Migrations/Install.php
3636
3737
3838
39
3940
4041
4142
......
6566
6667
6768
69
6870
6971
7072
'IDF_IssueComment',
'IDF_Conf',
'IDF_Upload',
'IDF_Search_Occ',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
$perm = Pluf_Permission::getFromString('IDF.project-owner');
if ($perm) $perm->delete();
$models = array(
'IDF_Search_Occ',
'IDF_Upload',
'IDF_Conf',
'IDF_IssueComment',
src/IDF/Search.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?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
#
# Based on work under GNU LGPL copyright, from the Pluf Framework
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# ***** END LICENSE BLOCK ***** */
/**
* Class implementing the search engine
*
* It is a modified version of the Pluf_Search class to be able to
* cluster the results by project.
*/
class IDF_Search extends Pluf_Search
{
/**
* Search.
*
* Returns an array of array with model_class, model_id and
* score. The list is already sorted by score descending.
*
* You can then filter the list as you wish with another set of
* weights.
*
* @param string Query string.
* @param int Project id to limit the results (null)
* @param string Stemmer class.
* @return array Results.
*/
public static function mySearch($query, $project=null, $stemmer='Pluf_Text_Stemmer_Porter')
{
$query = Pluf_Text::cleanString(html_entity_decode($query, ENT_QUOTES, 'UTF-8'));
$words = Pluf_Text::tokenize($query);
if ($stemmer != null) {
$words = self::stem($words, $stemmer);
}
$words_flat = array();
foreach ($words as $word=>$c) {
$words_flat[] = $word;
}
$word_ids = self::getWordIds($words_flat);
if (in_array(null, $word_ids)) {
return array();
}
return self::mySearchDocuments($word_ids, $project);
}
/**
* Search documents.
*
* Only the total of the ponderated occurences is used to sort the
* results.
*
* @param array Ids.
* @param IDF_Project Project to limit the search.
* @return array Sorted by score, returns model_class, model_id and score.
*/
public static function mySearchDocuments($wids, $project)
{
$db =& Pluf::db();
$gocc = new IDF_Search_Occ();
$where = array();
foreach ($wids as $id) {
$where[] = $db->qn('word').'='.(int)$id;
}
$prj = (is_null($project)) ? '' : ' AND project='.(int)$project->id;
$select = 'SELECT model_class, model_id, SUM(pondocc) AS score FROM '.$gocc->getSqlTable().' WHERE '.implode(' OR ', $where).$prj.' GROUP BY model_class, model_id HAVING COUNT(*)='.count($wids).' ORDER BY score DESC';
return $db->select($select);
}
/**
* Index a document.
*
* See Pluf_Search for the disclaimer and informations.
*
* @param Pluf_Model Document to index.
* @param Stemmer used. ('Pluf_Text_Stemmer_Porter')
* @return array Statistics.
*/
public static function index($doc, $stemmer='Pluf_Text_Stemmer_Porter')
{
$words = Pluf_Text::tokenize($doc->_toIndex());
if ($stemmer != null) {
$words = self::stem($words, $stemmer);
}
// Get the total number of words.
$total = 0.0;
$words_flat = array();
foreach ($words as $word => $occ) {
$total += (float) $occ;
$words_flat[] = $word;
}
// Drop the last indexation.
$gocc = new IDF_Search_Occ();
$sql = new Pluf_SQL('DELETE FROM '.$gocc->getSqlTable().' WHERE model_class=%s AND model_id=%s', array($doc->_model, $doc->id));
$db =& Pluf::db();
$db->execute($sql->gen());
// Get the ids for each word.
$ids = self::getWordIds($words_flat);
// Insert a new word for the missing words and add the occ.
$n = count($ids);
$new_words = 0;
$done = array();
for ($i=0;$i<$n;$i++) {
if ($ids[$i] === null) {
$word = new Pluf_Search_Word();
$word->word = $words_flat[$i];
$word->create();
$ids[$i] = $word->id;
$new_words++;
}
if (isset($done[$ids[$i]])) {
continue;
}
$done[$ids[$i]] = true;
$occ = new IDF_Search_Occ();
$occ->word = new Pluf_Search_Word($ids[$i]);
$occ->model_class = $doc->_model;
$occ->model_id = $doc->id;
$occ->project = $doc->get_project();
$occ->occ = $words[$words_flat[$i]];
$occ->pondocc = $words[$words_flat[$i]]/$total;
$occ->create();
}
// update the stats
$sql = new Pluf_SQL('model_class=%s AND model_id=%s',
array($doc->_model, $doc->id));
$last_index = Pluf::factory('Pluf_Search_Stats')->getList(array('filter' => $sql->gen()));
if ($last_index->count() == 0) {
$stats = new Pluf_Search_Stats();
$stats->model_class = $doc->_model;
$stats->model_id = $doc->id;
$stats->indexations = 1;
$stats->create();
} else {
$last_index[0]->indexations += 1;
$last_index[0]->update();
}
return array('total' => $total, 'new' => $new_words, 'unique'=>$n);
}
}
src/IDF/Search/Occ.php
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
<?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 ***** */
/**
* Storage of the occurence of the words.
*/
class IDF_Search_Occ extends Pluf_Model
{
public $_model = 'IDF_Search_Occ';
function init()
{
$this->_a['verbose'] = __('occurence');
$this->_a['table'] = 'idf_search_occs';
$this->_a['model'] = 'IDF_Search_Occ';
$this->_a['cols'] = array(
// It is mandatory to have an "id" column.
'id' =>
array(
'type' => 'Pluf_DB_Field_Sequence',
//It is automatically added.
'blank' => true,
),
'word' =>
array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'Pluf_Search_Word',
'blank' => false,
'verbose' => __('word'),
),
'model_class' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => false,
'size' => 150,
'verbose' => __('model class'),
),
'model_id' =>
array(
'type' => 'Pluf_DB_Field_Integer',
'blank' => false,
'verbose' => __('model id'),
),
'project' =>
array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'IDF_Project',
'blank' => false,
'verbose' => __('project'),
),
'occ' =>
array(
'type' => 'Pluf_DB_Field_Integer',
'blank' => false,
'verbose' => __('occurences'),
),
'pondocc' =>
array(
'type' => 'Pluf_DB_Field_Float',
'blank' => false,
'verbose' => __('ponderated occurence'),
),
);
$this->_a['idx'] = array(
'model_class_id_combo_word_idx' =>
array(
'type' => 'unique',
'col' => 'model_class, model_id, word',
),
);
}
function __toString()
{
return $this->word;
}
}
src/IDF/Views/Issue.php
183183
184184
185185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
186227
187228
188229
$request);
}
public $search_precond = array('IDF_Precondition::accessIssues');
public function search($request, $match)
{
if (!isset($request->REQUEST['q']) or trim($request->REQUEST['q']) == '') {
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Issue::index');
return new Pluf_HTTP_Response_Redirect($url);
}
$prj = $request->project;
$q = $request->REQUEST['q'];
$title = sprintf(__('Search Issues - %s'), Pluf_esc($q));
$issues = new Pluf_Search_ResultSet(IDF_Search::mySearch($q, $prj));
if (count($issues) > 100) {
// no more than 100 results as we do not care
$issues->results = array_slice($issues->results, 0, 100);
}
$pag = new Pluf_Paginator();
$pag->items = $issues;
$pag->class = 'recent-issues';
$pag->item_extra_props = array('project_m' => $prj,
'shortname' => $prj->shortname,
'current_user' => $request->user);
$pag->summary = __('This table shows the found issues.');
$pag->action = array('IDF_Views_Issue::search', array($prj->shortname), array('q'=> $q));
$list_display = array(
'id' => __('Id'),
array('summary', 'IDF_Views_Issue_SummaryAndLabels', __('Summary')),
array('status', 'IDF_Views_Issue_ShowStatus', __('Status')),
array('modif_dtime', 'Pluf_Paginator_DateAgo', __('Last Updated')),
);
$pag->configure($list_display);
$pag->items_per_page = 100;
$pag->no_results_text = __('No issues were found.');
$pag->setFromRequest($request);
$params = array('page_title' => $title,
'issues' => $pag,
'q' => $q,
);
return Pluf_Shortcuts_RenderToResponse('issues/search.html', $params, $request);
}
public $view_precond = array('IDF_Precondition::accessIssues');
public function view($request, $match)
{
src/IDF/conf/views.php
9191
9292
9393
94
95
96
97
98
99
94100
95101
96102
'model' => 'IDF_Views_Issue',
'method' => 'index');
$ctl[] = array('regex' => '#^/p/(\w+)/issues/search/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views_Issue',
'method' => 'search');
$ctl[] = array('regex' => '#^/p/(\w+)/issues/(\d+)/$#',
'base' => $base,
'priority' => 4,
src/IDF/relations.php
2828
2929
3030
31
3132
3233
$m['IDF_IssueComment'] = array('relate_to' => array('IDF_Issue', 'Pluf_User'));
$m['IDF_Upload'] = array('relate_to' => array('IDF_Project', 'Pluf_User'),
'relate_to_many' => array('IDF_Tag'));
$m['IDF_Search_Occ'] = array('relate_to' => array('IDF_Project'),);
return $m;
src/IDF/templates/issues/base.html
33
44
55
6
6
7
8
9
10
711
812
913
{block subtabs}
<div id="sub-tabs">
<a href="{url 'IDF_Views_Issue::index', array($project.shortname)}">{trans 'Open issues'}</a>
{if !$user.isAnonymous()} | <a href="{url 'IDF_Views_Issue::create', array($project.shortname)}">{trans 'New Issue'}</a> | <a href="{url 'IDF_Views_Issue::myIssues', array($project.shortname, 'submit')}">{trans 'My Issues'}</a>{/if}
{if !$user.isAnonymous()} | <a href="{url 'IDF_Views_Issue::create', array($project.shortname)}">{trans 'New Issue'}</a> | <a href="{url 'IDF_Views_Issue::myIssues', array($project.shortname, 'submit')}">{trans 'My Issues'}</a>{/if} |
<form class="star" action="{url 'IDF_Views_Issue::search', array($project.shortname)}" method="get">
<input type="text" value="{$q}" name="q" size="20" />
<input type="submit" name="s" value="{trans 'Search'}" />
</form>
{superblock}
</div>
{/block}
src/IDF/templates/issues/search.html
1
2
3
4
5
6
7
8
9
10
11
12
{extends "issues/base.html"}
{block docclass}yui-t2{/block}
{block body}
{$issues.render}
{if !$user.isAnonymous()}
{aurl 'url', 'IDF_Views_Issue::create', array($project.shortname)}
<p><a href="{$url}"><img style="vertical-align: text-bottom;" src="{media '/idf/img/add.png'}" alt="+" align="bottom" /></a> <a href="{$url}">{trans 'New Issue'}</a></p>{/if}
{/block}
{block context}
<p><strong>{trans 'Found issues:'}</strong> {$issues.nb_items}</p>
{/block}

Archive Download the corresponding diff file

Page rendered in 0.11293s using 13 queries.