<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Database;

use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\GServer;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\Post\Category;
use Friendica\Model\Tag;
use Friendica\Model\Verb;
use Friendica\Protocol\ActivityPub\Processor;
use Friendica\Protocol\ActivityPub\Receiver;
use Friendica\Util\JsonLD;
use Friendica\Util\Strings;

/**
 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
 *
 * If there is a need for a intensive migration after a database structure change, update this file
 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
 */
class PostUpdate
{
	// Needed for the helper function to read from the legacy term table
	const OBJECT_TYPE_POST  = 1;

	const VERSION = 1452;

	/**
	 * Calls the post update functions
	 */
	public static function update()
	{
		if (!self::update1297()) {
			return false;
		}
		if (!self::update1322()) {
			return false;
		}
		if (!self::update1329()) {
			return false;
		}
		if (!self::update1341()) {
			return false;
		}
		if (!self::update1342()) {
			return false;
		}
		if (!self::update1345()) {
			return false;
		}
		if (!self::update1346()) {
			return false;
		}
		if (!self::update1347()) {
			return false;
		}
		if (!self::update1348()) {
			return false;
		}
		if (!self::update1349()) {
			return false;
		}
		if (!self::update1383()) {
			return false;
		}
		if (!self::update1384()) {
			return false;
		}
		if (!self::update1400()) {
			return false;
		}
		if (!self::update1424()) {
			return false;
		}
		if (!self::update1425()) {
			return false;
		}
		if (!self::update1426()) {
			return false;
		}
		if (!self::update1427()) {
			return false;
		}
		if (!self::update1452()) {
			return false;
		}
		return true;
	}

	/**
	 * Set the delivery queue count to a negative value for all items preceding the feature.
	 *
	 * @return bool "true" when the job is done
	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
	 */
	private static function update1297()
	{
		// Was the script completed?
		if (DI::config()->get('system', 'post_update_version') >= 1297) {
			return true;
		}

		if (!DBStructure::existsTable('item-delivery-data')) {
			DI::config()->set('system', 'post_update_version', 1297);
			return true;
		}

		$max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
		$max_iid = (is_array($max_item_delivery_data) && isset($max_item_delivery_data['iid']) ? $max_item_delivery_data['iid'] : 0);

		Logger::info('Start update1297 with max iid: ' . $max_iid);<commit_msg>Shorter code, thanks to @annando pointing this out<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Database;

use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\GServer;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\Post\Category;
use Friendica\Model\Tag;
use Friendica\Model\Verb;
use Friendica\Protocol\ActivityPub\Processor;
use Friendica\Protocol\ActivityPub\Receiver;
use Friendica\Util\JsonLD;
use Friendica\Util\Strings;

/**
 * These database-intensive post update routines are meant to be executed in the background by the cronjob.
 *
 * If there is a need for a intensive migration after a database structure change, update this file
 * by adding a new method at the end with the number of the new DB_UPDATE_VERSION.
 */
class PostUpdate
{
	// Needed for the helper function to read from the legacy term table
	const OBJECT_TYPE_POST  = 1;

	const VERSION = 1452;

	/**
	 * Calls the post update functions
	 */
	public static function update()
	{
		if (!self::update1297()) {
			return false;
		}
		if (!self::update1322()) {
			return false;
		}
		if (!self::update1329()) {
			return false;
		}
		if (!self::update1341()) {
			return false;
		}
		if (!self::update1342()) {
			return false;
		}
		if (!self::update1345()) {
			return false;
		}
		if (!self::update1346()) {
			return false;
		}
		if (!self::update1347()) {
			return false;
		}
		if (!self::update1348()) {
			return false;
		}
		if (!self::update1349()) {
			return false;
		}
		if (!self::update1383()) {
			return false;
		}
		if (!self::update1384()) {
			return false;
		}
		if (!self::update1400()) {
			return false;
		}
		if (!self::update1424()) {
			return false;
		}
		if (!self::update1425()) {
			return false;
		}
		if (!self::update1426()) {
			return false;
		}
		if (!self::update1427()) {
			return false;
		}
		if (!self::update1452()) {
			return false;
		}
		return true;
	}

	/**
	 * Set the delivery queue count to a negative value for all items preceding the feature.
	 *
	 * @return bool "true" when the job is done
	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
	 */
	private static function update1297()
	{
		// Was the script completed?
		if (DI::config()->get('system', 'post_update_version') >= 1297) {
			return true;
		}

		if (!DBStructure::existsTable('item-delivery-data')) {
			DI::config()->set('system', 'post_update_version', 1297);
			return true;
		}

		$max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
		$max_iid = $max_item_delivery_data['iid'] ?? 0;

		Logger::info('Start update1297 with max iid: ' . $max_iid);
{'fraction_non_alphanumeric': 0.12529129540781356, 'fraction_numerical': 0.029061000685400958, 'mean_word_length': 3.8253968253968256, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6858672', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2279, 'n_words': 750}