Rebase 783-contributing-file on branch 'dev'

This commit is contained in:
Marien Fressinaud
2015-02-17 14:55:25 +01:00
5 changed files with 496 additions and 18 deletions

View File

@@ -301,4 +301,5 @@ class FreshRSS_Context {
}
return false;
}
}

195
app/Models/Search.php Normal file
View File

@@ -0,0 +1,195 @@
<?php
/**
* Contains a search from the search form.
*
* It allows to extract meaningful bits of the search and store them in a
* convenient object
*/
class FreshRSS_Search {
// This contains the user input string
private $raw_input;
// The following properties are extracted from the raw input
private $intitle;
private $min_date;
private $max_date;
private $min_pubdate;
private $max_pubdate;
private $inurl;
private $author;
private $tags;
private $search;
public function __construct($input) {
if (strcmp($input, '') == 0) {
return;
}
$this->raw_input = $input;
$input = $this->parseIntitleSearch($input);
$input = $this->parseAuthorSearch($input);
$input = $this->parseInurlSearch($input);
$input = $this->parsePubdateSearch($input);
$input = $this->parseDateSearch($input);
$input = $this->parseTagsSeach($input);
$this->search = $this->cleanSearch($input);
}
public function getRawInput() {
return $this->raw_input;
}
public function getIntitle() {
return $this->intitle;
}
public function getMinDate() {
return $this->min_date;
}
public function getMaxDate() {
return $this->max_date;
}
public function getMinPubdate() {
return $this->min_pubdate;
}
public function getMaxPubdate() {
return $this->max_pubdate;
}
public function getInurl() {
return $this->inurl;
}
public function getAuthor() {
return $this->author;
}
public function getTags() {
return $this->tags;
}
public function getSearch() {
return $this->search;
}
/**
* Parse the search string to find intitle keyword and the search related
* to it.
* The search is the first word following the keyword.
*
* @param string $input
* @return string
*/
private function parseIntitleSearch($input) {
if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->intitle = $matches['search'];
return str_replace($matches[0], '', $input);
}
if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
$this->intitle = $matches['search'];
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find author keyword and the search related
* to it.
* The search is the first word following the keyword except when using
* a delimiter. Supported delimiters are single quote (') and double
* quotes (").
*
* @param string $input
* @return string
*/
private function parseAuthorSearch($input) {
if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
$this->author = $matches['search'];
return str_replace($matches[0], '', $input);
}
if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
$this->author = $matches['search'];
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find inurl keyword and the search related
* to it.
* The search is the first word following the keyword except.
*
* @param string $input
* @return string
*/
private function parseInurlSearch($input) {
if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
$this->inurl = $matches['search'];
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find date keyword and the search related
* to it.
* The search is the first word following the keyword.
*
* @param string $input
* @return string
*/
private function parseDateSearch($input) {
if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find pubdate keyword and the search related
* to it.
* The search is the first word following the keyword.
*
* @param string $input
* @return string
*/
private function parsePubdateSearch($input) {
if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Parse the search string to find tags keyword (# followed by a word)
* and the search related to it.
* The search is the first word following the #.
*
* @param string $input
* @return string
*/
private function parseTagsSeach($input) {
if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
$this->tags = $matches['search'];
return str_replace($matches[0], '', $input);
}
return $input;
}
/**
* Remove all unnecessary spaces in the search
*
* @param string $input
* @return string
*/
private function cleanSearch($input) {
$input = preg_replace('/\s+/', ' ', $input);
return trim($input);
}
}

View File

@@ -238,12 +238,7 @@ function toggleContent(new_active, old_active) {
old_active.removeClass("active current");
new_active.addClass("current");
if (context['auto_remove_article'] && !old_active.hasClass('not_read')) {
var p = old_active.prev();
var n = old_active.next();
if (p.hasClass('day') && n.hasClass('day')) {
p.remove();
}
old_active.remove();
auto_remove(old_active);
}
} else {
new_active.toggleClass('active');
@@ -258,7 +253,7 @@ function toggleContent(new_active, old_active) {
if (context['sticky_post']) {
var prev_article = new_active.prevAll('.flux'),
new_pos = new_active.position().top,
new_pos = new_active.position().top,
old_scroll = $(box_to_move).scrollTop();
if (prev_article.length > 0 && new_pos - prev_article.position().top <= 150) {
@@ -289,6 +284,16 @@ function toggleContent(new_active, old_active) {
}
}
function auto_remove(element) {
var p = element.prev();
var n = element.next();
if (p.hasClass('day') && n.hasClass('day')) {
p.remove();
}
element.remove();
$('#stream > .flux:not(.not_read):not(.active)').remove();
}
function prev_entry() {
var old_active = $(".flux.current"),
new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first");
@@ -683,7 +688,7 @@ function init_stream(divStream) {
}
var old_active = $(".flux.current"),
new_active = $(this).parent();
isCollapsed = true;
isCollapsed = true;
if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
if (context['auto_mark_article']) {
mark_read(new_active, true);
@@ -696,12 +701,7 @@ function init_stream(divStream) {
divStream.on('click', '.flux a.read', function () {
var active = $(this).parents(".flux");
if (context['auto_remove_article'] && active.hasClass('not_read')) {
var p = active.prev();
var n = active.next();
if (p.hasClass('day') && n.hasClass('day')) {
p.remove();
}
active.remove();
auto_remove(active);
}
mark_read(active, false);
return false;
@@ -882,8 +882,8 @@ function notifs_html5_show(nb) {
if (context['html5_notif_timeout'] !== 0){
setTimeout(function() {
notification.close();
}, context['html5_notif_timeout'] * 1000);
notification.close();
}, context['html5_notif_timeout'] * 1000);
}
}
@@ -899,7 +899,7 @@ function init_notifs_html5() {
function refreshUnreads() {
$.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
var isAll = $('.category.all.active').length > 0,
new_articles = false;
new_articles = false;
$.each(data, function(feed_id, nbUnreads) {
feed_id = 'f_' + feed_id;
@@ -1195,7 +1195,7 @@ function faviconNbUnread(n) {
function init_slider_observers() {
var slider = $('#slider'),
closer = $('#close-slider');
closer = $('#close-slider');
if (slider.length < 1) {
return;
}

View File

@@ -0,0 +1,5 @@
<?php
class ContextTest extends \PHPUnit_Framework_TestCase {
}

View File

@@ -0,0 +1,277 @@
<?php
require_once(LIB_PATH . '/lib_date.php');
class SearchTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider provideEmptyInput
* @param string|null $input
*/
public function test__construct_whenInputIsEmpty_getsOnlyNullValues($input) {
$search = new FreshRSS_Search($input);
$this->assertNull($search->getRawInput());
$this->assertNull($search->getIntitle());
$this->assertNull($search->getMinDate());
$this->assertNull($search->getMaxDate());
$this->assertNull($search->getMinPubdate());
$this->assertNull($search->getMaxPubdate());
$this->assertNull($search->getAuthor());
$this->assertNull($search->getTags());
$this->assertNull($search->getSearch());
}
/**
* Return an array of values for the search object.
* Here is the description of the values
* @return array
*/
public function provideEmptyInput() {
return array(
array(''),
array(null),
);
}
/**
* @dataProvider provideIntitleSearch
* @param string $input
* @param string $intitle_value
* @param string|null $search_value
*/
public function test__construct_whenInputContainsIntitle_setsIntitlePropery($input, $intitle_value, $search_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($intitle_value, $search->getIntitle());
$this->assertEquals($search_value, $search->getSearch());
}
/**
* @return array
*/
public function provideIntitleSearch() {
return array(
array('intitle:word1', 'word1', null),
array('intitle:word1 word2', 'word1', 'word2'),
array('intitle:"word1 word2"', 'word1 word2', null),
array("intitle:'word1 word2'", 'word1 word2', null),
array('word1 intitle:word2', 'word2', 'word1'),
array('word1 intitle:word2 word3', 'word2', 'word1 word3'),
array('word1 intitle:"word2 word3"', 'word2 word3', 'word1'),
array("word1 intitle:'word2 word3'", 'word2 word3', 'word1'),
array('intitle:word1 intitle:word2', 'word1', 'intitle:word2'),
array('intitle: word1 word2', null, 'word1 word2'),
array('intitle:123', '123', null),
array('intitle:"word1 word2" word3"', 'word1 word2', 'word3"'),
array("intitle:'word1 word2' word3'", 'word1 word2', "word3'"),
array('intitle:"word1 word2\' word3"', "word1 word2' word3", null),
array("intitle:'word1 word2\" word3'", 'word1 word2" word3', null),
);
}
/**
* @dataProvider provideAuthorSearch
* @param string $input
* @param string $author_value
* @param string|null $search_value
*/
public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($author_value, $search->getAuthor());
$this->assertEquals($search_value, $search->getSearch());
}
/**
* @return array
*/
public function provideAuthorSearch() {
return array(
array('author:word1', 'word1', null),
array('author:word1 word2', 'word1', 'word2'),
array('author:"word1 word2"', 'word1 word2', null),
array("author:'word1 word2'", 'word1 word2', null),
array('word1 author:word2', 'word2', 'word1'),
array('word1 author:word2 word3', 'word2', 'word1 word3'),
array('word1 author:"word2 word3"', 'word2 word3', 'word1'),
array("word1 author:'word2 word3'", 'word2 word3', 'word1'),
array('author:word1 author:word2', 'word1', 'author:word2'),
array('author: word1 word2', null, 'word1 word2'),
array('author:123', '123', null),
array('author:"word1 word2" word3"', 'word1 word2', 'word3"'),
array("author:'word1 word2' word3'", 'word1 word2', "word3'"),
array('author:"word1 word2\' word3"', "word1 word2' word3", null),
array("author:'word1 word2\" word3'", 'word1 word2" word3', null),
);
}
/**
* @dataProvider provideInurlSearch
* @param string $input
* @param string $inurl_value
* @param string|null $search_value
*/
public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($inurl_value, $search->getInurl());
$this->assertEquals($search_value, $search->getSearch());
}
/**
* @return array
*/
public function provideInurlSearch() {
return array(
array('inurl:word1', 'word1', null),
array('inurl: word1', null, 'word1'),
array('inurl:123', '123', null),
array('inurl:word1 word2', 'word1', 'word2'),
array('inurl:"word1 word2"', '"word1', 'word2"'),
);
}
/**
* @dataProvider provideDateSearch
* @param string $input
* @param string $min_date_value
* @param string $max_date_value
*/
public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($min_date_value, $search->getMinDate());
$this->assertEquals($max_date_value, $search->getMaxDate());
}
/**
* @return array
*/
public function provideDateSearch() {
return array(
array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'),
array('date:2007-03-01/', '1172725200', ''),
array('date:/2008-05-11', '', '1210564799'),
);
}
/**
* @dataProvider providePubdateSearch
* @param string $input
* @param string $min_pubdate_value
* @param string $max_pubdate_value
*/
public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($min_pubdate_value, $search->getMinPubdate());
$this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
}
/**
* @return array
*/
public function providePubdateSearch() {
return array(
array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'),
array('pubdate:2007-03-01/', '1172725200', ''),
array('pubdate:/2008-05-11', '', '1210564799'),
);
}
/**
* @dataProvider provideTagsSearch
* @param string $input
* @param string $tags_value
* @param string|null $search_value
*/
public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($tags_value, $search->getTags());
$this->assertEquals($search_value, $search->getSearch());
}
/**
* @return array
*/
public function provideTagsSearch() {
return array(
array('#word1', array('word1'), null),
array('# word1', null, '# word1'),
array('#123', array('123'), null),
array('#word1 word2', array('word1'), 'word2'),
array('#"word1 word2"', array('"word1'), 'word2"'),
array('#word1 #word2', array('word1', 'word2'), null),
);
}
/**
* @dataProvider provideMultipleSearch
* @param string $input
* @param string $author_value
* @param string $min_date_value
* @param string $max_date_value
* @param string $intitle_value
* @param string $inurl_value
* @param string $min_pubdate_value
* @param string $max_pubdate_value
* @param array $tags_value
* @param string|null $search_value
*/
public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value, $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) {
$search = new FreshRSS_Search($input);
$this->assertEquals($author_value, $search->getAuthor());
$this->assertEquals($min_date_value, $search->getMinDate());
$this->assertEquals($max_date_value, $search->getMaxDate());
$this->assertEquals($intitle_value, $search->getIntitle());
$this->assertEquals($inurl_value, $search->getInurl());
$this->assertEquals($min_pubdate_value, $search->getMinPubdate());
$this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
$this->assertEquals($tags_value, $search->getTags());
$this->assertEquals($search_value, $search->getSearch());
$this->assertEquals($input, $search->getRawInput());
}
public function provideMultipleSearch() {
return array(
array(
'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
'word1',
'1172725200',
'1210564799',
'word2',
'word3',
'1172725200',
'1210564799',
array('word4', 'word5'),
null,
),
array(
'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
'word1',
'1172725200',
'1210564799',
'word2',
'word3',
'1172725200',
'1210564799',
array('word4', 'word5'),
'word6',
),
array(
'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
'word1',
'1172725200',
'1210564799',
'word2',
'word3',
'1172725200',
'1210564799',
array('word4', 'word5'),
'word6 word7',
),
);
}
}