Merge branch 'master' into filterrefactor

* master:
  reintroduce init method
  Version upped
  streamline database loading
  Version upped
  Provide mechanism for disabling default select filter 'latest = 1'
  Version upped
  fix accidentally broken test
  add failing test for #512
This commit is contained in:
Andreas Gohr
2023-07-19 11:04:11 +02:00
4 changed files with 69 additions and 29 deletions

View File

@ -32,6 +32,13 @@ class AggregationResultsTest extends StructTest
$assignments = mock\Assignments::getInstance();
$assignments->clear(true);
// different values for each entry
$second = [
['green', 'red'],
['green', 'blue'],
['blue', 'yellow']
];
for ($i = 0; $i < 3; $i++) {
// assign a schema
$assignments->assignPageSchema("test$i", 'schema1');
@ -42,7 +49,7 @@ class AggregationResultsTest extends StructTest
// save serial data
$data = [
'first' => "foo$i",
'second' => ["bar$i", "baz$i"],
'second' => $second[$i],
'third' => "foobar$i",
'fourth' => "barfoo$i",
];
@ -65,7 +72,7 @@ class AggregationResultsTest extends StructTest
$this->assertEquals('test1', $result[0][0]->getValue());
// skip %rowid% column and test saved values
$this->assertEquals('foo1', $result[0][2]->getValue());
$this->assertEquals(['bar1', 'baz1'], $result[0][3]->getValue());
$this->assertEquals(['green', 'blue'], $result[0][3]->getValue());
$this->assertEquals('foobar1', $result[0][4]->getValue());
$this->assertEquals('barfoo1', $result[0][5]->getValue());
}
@ -86,6 +93,22 @@ class AggregationResultsTest extends StructTest
$this->assertCount(0, $result);
}
/** @noinspection PhpUnreachableStatementInspection */
public function test_filter_multi()
{
$schema = 'schema1';
$result = $this->fetchPagesResult($schema, '');
$this->assertCount(3, $result);
$result = $this->fetchPagesResult($schema, '', ['second', '=', 'green', 'AND']);
$this->assertCount(2, $result);
$this->markTestIncomplete('negative filters currently do not work on multi fields. See #512');
$result = $this->fetchPagesResult($schema, '', ['second', '!~', 'green', 'AND']);
$this->assertCount(1, $result);
}
/**
* Test filtering on a page field, with 'usetitles' set to true and false
*/

View File

@ -7,22 +7,15 @@
* @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
*/
use dokuwiki\Extension\Event;
use dokuwiki\ErrorHandler;
use dokuwiki\plugin\sqlite\SQLiteDB;
use dokuwiki\plugin\struct\meta\StructException;
class helper_plugin_struct_db extends DokuWiki_Plugin
{
/** @var \dokuwiki\plugin\sqlite\SQLiteDB */
/** @var SQLiteDB */
protected $sqlite;
/**
* helper_plugin_struct_db constructor.
*/
public function __construct()
{
$this->init();
}
/**
* Initialize the database
*
@ -30,13 +23,7 @@ class helper_plugin_struct_db extends DokuWiki_Plugin
*/
protected function init()
{
try {
/** @var \dokuwiki\plugin\sqlite\SQLiteDB $sqlite */
$this->sqlite = new \dokuwiki\plugin\sqlite\SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
} catch (Exception $exception) {
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
return;
}
$this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
// register our JSON function with variable parameters
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
@ -47,17 +34,19 @@ class helper_plugin_struct_db extends DokuWiki_Plugin
/**
* @param bool $throw throw an Exception when sqlite not available
* @return \dokuwiki\plugin\sqlite\SQLiteDB|null
* @return SQLiteDB|null
*/
public function getDB($throw = true)
{
global $conf;
$len = strlen($conf['metadir']);
if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getDbFile(), 0, $len)) {
$this->init();
}
if (!$this->sqlite && $throw) {
throw new StructException('no sqlite');
if ($this->sqlite === null) {
try {
$this->init();
} catch (\Exception $exception) {
if (defined('DOKU_UNITTEST')) throw new \RuntimeException('Could not load SQLite', 0, $exception);
ErrorHandler::logException($exception);
if ($throw) throw new StructException('no sqlite');
return null;
}
}
return $this->sqlite;
}
@ -74,7 +63,7 @@ class helper_plugin_struct_db extends DokuWiki_Plugin
if (!$file) return;
unlink($file);
clearstatcache(true, $file);
$this->init();
$this->sqlite = null;
}
/**

View File

@ -62,6 +62,9 @@ class Search
/** @var array the revisions of the result rows */
protected $result_revs = [];
/** @var bool Include latest = 1 in select query */
protected $selectLatest = true;
/**
* Search constructor.
*/
@ -376,6 +379,16 @@ class Search
return 0;
}
/**
* Allows disabling default 'latest = 1' clause in select statement
*
* @param bool $selectLatest
*/
public function setSelectLatest($selectLatest): void
{
$this->selectLatest = $selectLatest;
}
/**
* Return the number of results (regardless of limit and offset settings)
*
@ -657,4 +670,19 @@ class Search
if ($value->getColumn()->getTid() == 0) return true;
return false;
}
/**
* @param string $datatable
* @return string
*/
protected function getSpecialFlagsClause($datatable)
{
$latestClause = "IS_PUBLISHER($datatable.pid)";
if ($this->selectLatest) {
$latestClause .= " AND $datatable.latest = 1";
}
$publishedClause = "IS_PUBLISHER($datatable.pid) !=1 AND $datatable.published = 1";
return "( ($latestClause) OR ($publishedClause) )";
}
}

View File

@ -1,7 +1,7 @@
base struct
author Andreas Gohr, Michael Große, Anna Dabrowska
email dokuwiki@cosmocode.de
date 2023-06-20
date 2023-07-17
name struct plugin
desc Add and query additional structured page data
url https://www.dokuwiki.org/plugin:struct