1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class FlaggedRevisionLib extends TikiDb_Bridge
9{
10	const ACTION = 'Flagged';
11
12	function flag_revision($pageName, $version, $flag, $value, $comment = '')
13	{
14		global $prefs;
15		$attributelib = TikiLib::lib('attribute');
16		$histlib = TikiLib::lib('hist');
17
18		if ($version_info = $histlib->get_version($pageName, $version)) {
19			$tx = TikiDb::get()->begin();
20
21			if ($prefs['feature_actionlog'] == 'y') {
22				$logslib = TikiLib::lib('logs');
23				$logslib->add_action(self::ACTION, $pageName, 'wiki page', "flag=$flag&version=$version&value=$value");
24			}
25
26			$attribute = $this->get_attribute_for_flag($flag);
27			$attributelib->set_attribute('wiki history', $version_info['historyId'], $attribute, $value, $comment);
28
29			require_once('lib/search/refresh-functions.php');
30			refresh_index('pages', $pageName);
31			refresh_index('pages', "$pageName~~latest");
32			$tx->commit();
33
34			return true;
35		} else {
36			return false;
37		}
38	}
39
40	function get_version_with($pageName, $flag, $value)
41	{
42		$this->get_version_query($pageName, $flag, $value, $query, $bindvars);
43
44		$result = $this->fetchAll($query, $bindvars, 1);
45
46		$first = reset($result);
47		return $first;
48	}
49
50	function get_versions_with($pageName, $flag, $value)
51	{
52		$this->get_version_query($pageName, $flag, $value, $query, $bindvars, 'version');
53		$result = $this->fetchAll($query, $bindvars);
54
55		$versions = [];
56		foreach ($result as $row) {
57			$versions[] = $row['version'];
58		}
59
60		return $versions;
61	}
62
63	function get_flag_comment($pageName, $version, $flag, $value)
64	{
65		$query = 'SELECT toa.`comment` FROM `tiki_history` th INNER JOIN `tiki_object_attributes` toa ON toa.`itemId` = `historyId` AND toa.`type` = ? WHERE toa.`attribute` = ? AND toa.`value` = ? AND th.`pageName` = ? AND th.`version`=?';
66		$bindvars = [
67			'wiki history',
68			$this->get_attribute_for_flag($flag),
69			$value,
70			$pageName,
71			$version,
72		];
73
74		$result = $this->fetchAll($query, $bindvars, 1);
75		$first = reset($result);
76		return $first['comment'];
77	}
78
79	private function get_version_query($pageName, $flag, $value, & $query, & $bindvars, $fields = 'th.*')
80	{
81		// NOTE : These are out variables
82		$query = 'SELECT ' . $fields . ' FROM `tiki_history` th INNER JOIN `tiki_object_attributes` toa ON toa.`itemId` = `historyId` AND toa.`type` = ? WHERE toa.attribute = ? AND toa.value = ? AND th.pageName = ? ORDER BY `th`.`version` DESC';
83
84		$bindvars = [
85			'wiki history',
86			$this->get_attribute_for_flag($flag),
87			$value,
88			$pageName,
89		];
90	}
91
92	public function version_is_flagged($pageName, $version, $flag, $value)
93	{
94		$query = 'SELECT th.historyId FROM `tiki_history` th INNER JOIN `tiki_object_attributes` toa ON toa.`itemId` = `historyId` AND toa.`type` = ? WHERE toa.`attribute` = ? AND toa.`value` = ? AND th.`pageName` = ? AND th.`version` = ? ORDER BY `th`.`version` DESC';
95
96		$bindvars = [
97			'wiki history',
98			$this->get_attribute_for_flag($flag),
99			$value,
100			$pageName,
101			$version,
102		];
103
104		$result = $this->fetchAll($query, $bindvars);
105
106		return (bool)$result;
107	}
108
109	function page_requires_approval($pageName)
110	{
111		global $prefs, $tikilib;
112
113		if ($prefs['flaggedrev_approval'] != 'y') {
114			return false;
115		}
116
117		if ($prefs['feature_categories'] == 'y') {
118			$categlib = TikiLib::lib('categ');
119			$approvalCategories = $tikilib->get_preference('flaggedrev_approval_categories', [], true);
120
121			$objectCategories = $categlib->get_object_categories('wiki page', $pageName);
122
123			return count(array_intersect($approvalCategories, $objectCategories)) > 0;
124		}
125
126		return false;
127	}
128
129	function find_approval_information($page, $version)
130	{
131		global $prefs;
132
133		if ($prefs['feature_actionlog'] == 'y') {
134			$logs = $this->table('tiki_actionlog');
135			return $logs->fetchRow(
136				['user', 'lastModif', 'ip'],
137				[
138					'action' => self::ACTION,
139					'object' => $page,
140					'objectType' => 'wiki page',
141					'comment' => "flag=moderation&version=$version&value=OK",
142				]
143			);
144		}
145	}
146
147	private function get_attribute_for_flag($flag)
148	{
149		return 'tiki.history.' . $flag;
150	}
151}
152