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
8if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
9	header("location: index.php");
10	exit;
11}
12
13/**
14 * Somewhat work around https://dev.tiki.org/item6014
15 * @param $installer
16 */
17function upgrade_optional_20170801_initialize_article_nbreads_tiki($installer)
18{
19	// Articles
20	{
21		$tikilib = TikiLib::lib('tiki');
22		$minimumAbnormal = $tikilib->getOne('SELECT MIN(articleId) FROM tiki_articles WHERE nbreads IS NULL');
23		$maximumNormal = $tikilib->getOne('SELECT MAX(articleId) FROM tiki_articles WHERE nbreads IS NOT NULL');
24	if (is_null($minimumAbnormal)) {
25		return true;
26	}
27	if (! is_null($maximumNormal) && $minimumAbnormal < $maximumNormal) {
28		throw new Exception('Some articles with a regular counter were created after articles with an irregular counter. Please manually fix the fields if this is expected.');
29	}
30		$tikilib->query('UPDATE tiki_articles SET nbreads=0 WHERE nbreads IS NULL');
31	$tikilib = TikiLib::lib('tiki');
32	}
33
34	// Submissions
35	{
36		$minimumAbnormal = $tikilib->getOne('SELECT MIN(subId) FROM tiki_submissions WHERE nbreads IS NULL');
37		$maximumNormal = $tikilib->getOne('SELECT MAX(subId) FROM tiki_submissions WHERE nbreads IS NOT NULL');
38	if (is_null($minimumAbnormal)) {
39		return true;
40	}
41	if (! is_null($maximumNormal) && $minimumAbnormal < $maximumNormal) {
42		throw new Exception('Some article submissions with a regular counter were created after article submissions with an irregular counter. Please manually fix the fields if this is expected.');
43	}
44		$tikilib->query('UPDATE tiki_submissions SET nbreads=0 WHERE nbreads IS NULL');
45	}
46}
47