4 * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * FuzzyPages is plugin which searches for similar page titles.
27 * Pages are considered similar by averaging the similarity scores of
28 * the spelling comparison and the metaphone comparison for each page
29 * title in the database (php's metaphone() is an improved soundex
32 * http://www.php.net/manual/en/function.similar-text.php
33 * http://www.php.net/manual/en/function.metaphone.php
35 class WikiPlugin_FuzzyPages
38 function getDescription()
40 return sprintf(_("Search for page titles similar to %s."),
44 function getDefaultArguments()
46 return array('s' => false,
50 function spelling_similarity($subject)
52 $spelling_similarity_score = 0;
53 similar_text($subject, $this->_searchterm,
54 $spelling_similarity_score);
55 return $spelling_similarity_score;
58 function sound_similarity($subject)
60 $sound_similarity_score = 0;
61 similar_text(metaphone($subject), $this->_searchterm_metaphone,
62 $sound_similarity_score);
63 return $sound_similarity_score;
66 function averageSimilarities($subject)
68 return ($this->spelling_similarity($subject)
69 + $this->sound_similarity($subject)) / 2;
72 function collectSimilarPages(&$list, &$dbi)
74 if (!defined('MIN_SCORE_CUTOFF'))
75 define('MIN_SCORE_CUTOFF', 33);
77 $this->_searchterm_metaphone = metaphone($this->_searchterm);
79 $allPages = $dbi->getAllPages();
81 while ($pagehandle = $allPages->next()) {
82 $pagename = $pagehandle->getName();
83 $similarity_score = $this->averageSimilarities($pagename);
84 if ($similarity_score > MIN_SCORE_CUTOFF)
85 $list[$pagename] = $similarity_score;
89 function sortCollectedPages(&$list)
91 arsort($list, SORT_NUMERIC);
94 function addTableCaption(&$table, &$dbi)
96 if ($dbi->isWikiPage($this->_searchterm))
97 $link = WikiLink($this->_searchterm, 'auto');
99 $link = $this->_searchterm;
100 $caption = fmt("These page titles match fuzzy with ā%sā", $link);
101 $table->pushContent(HTML::caption($caption));
104 function addTableHead(&$table)
106 $row = HTML::tr(HTML::th(_("Name")), HTML::th(_("Score")));
108 if (defined('DEBUG') && DEBUG && $this->debug) {
109 $this->pushDebugHeadingTDinto($row);
112 $table->pushContent(HTML::thead($row));
115 function addTableBody(&$list, &$table)
117 if (!defined('HIGHLIGHT_ROWS_CUTOFF_SCORE'))
118 define('HIGHLIGHT_ROWS_CUTOFF_SCORE', 60);
120 $tbody = HTML::tbody();
121 foreach ($list as $found_pagename => $score) {
122 $row = HTML::tr(array('class' =>
123 $score > HIGHLIGHT_ROWS_CUTOFF_SCORE
124 ? 'evenrow' : 'oddrow'),
125 HTML::td(WikiLink($found_pagename)),
126 HTML::td(array('class' => 'align-right'),
129 if (defined('DEBUG') && DEBUG && $this->debug) {
130 $this->pushDebugTDinto($row, $found_pagename);
133 $tbody->pushContent($row);
135 $table->pushContent($tbody);
138 function formatTable(&$list, &$dbi)
142 return HTML::p(fmt("No fuzzy matches with ā%sā", $this->_searchterm));
144 $table = HTML::table(array('class' => 'pagelist'));
145 $this->addTableCaption($table, $dbi);
146 $this->addTableHead($table);
147 $this->addTableBody($list, $table);
151 function run($dbi, $argstr, &$request, $basepage)
153 $args = $this->getArgs($argstr, $request);
159 if (defined('DEBUG') && DEBUG) {
160 $this->debug = $debug;
163 $this->_searchterm = $s;
164 $this->_list = array();
166 $this->collectSimilarPages($this->_list, $dbi);
167 $this->sortCollectedPages($this->_list);
168 return $this->formatTable($this->_list, $dbi);
171 private function pushDebugHeadingTDinto(&$row)
173 $row->pushContent(HTML::td(_("Spelling Score")),
174 HTML::td(_("Sound Score")),
175 HTML::td('Metaphones'));
178 private function pushDebugTDinto(&$row, $pagename)
180 // This actually calculates everything a second time for each pagename
181 // so the individual scores can be displayed separately for debugging.
182 $debug_spelling = round($this->spelling_similarity($pagename), 1);
183 $debug_sound = round($this->sound_similarity($pagename), 1);
184 $debug_metaphone = sprintf("(%s, %s)", metaphone($pagename),
185 $this->_searchterm_metaphone);
187 $row->pushcontent(HTML::td(array('class' => 'align-center'), $debug_spelling),
188 HTML::td(array('class' => 'align-center'), $debug_sound),
189 HTML::td($debug_metaphone));
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil