<commit_before><?php

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2018
 */

namespace OCA\Music\Migration;

use OCP\IConfig;
use OCP\ILogger;
use OCP\IDBConnection;
use OCA\Music\BusinessLayer\TrackBusinessLayer;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class PlaylistCleanup implements IRepairStep {

	/** @var IDBConnection */
	private $db;

	/** @var IConfig */
	private $config;

	/** @var TrackBusinessLayer */
	private $tracker;

	public function __construct(IDBConnection $connection, IConfig $config, TrackBusinessLayer $tracker) {
		$this->db = $connection;
		$this->config = $config;
		$this->tracker = $tracker;
	}

	public function getName() {
		return 'Remove playlist files from music_tracks table';
	}

	/**
	 * @inheritdoc
	 */
	public function run(IOutput $output) {
		$installedVersion = $this->config->getAppValue('music', 'installed_version');

		// Get version of the core
		$coreVersion = \OCP\Util::getVersion();

		if ($coreVersion[0] >= 13) {
			$n = $this->removePlaylistFiles();
			$output->info("$n files with audio/mpegurl or audio/x-scpls mime type removed from library");
			// Clean cache
			$this->db->executeQuery("DELETE FROM `*PREFIX*music_cache`");
		}
	}

	private function removePlaylistFiles() {
		//find tracks with mime audio/mpegurl and audio/x-scpls
		$sql = "SELECT `file_id` FROM `*PREFIX*music_tracks` WHERE `mimetype` = 'audio/mpegurl' OR `mimetype` = 'audio/x-scpls'";
		$sth = $this->db->executeQuery($sql);
		$file_ids = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);
		// remove corresponding tracks and albums/artists if not needed anymore
		if (count($file_ids)) {
			$result = $this->tracker->deleteTracks($file_ids);
		}
		return count($file_ids);
	}

}<commit_msg>Do not rely on TrackBusinessLayer in Migration/PlaylistCleanup<commit_after><?php

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2018
 * @copyright <NAME> 2019
 */

namespace OCA\Music\Migration;

use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class PlaylistCleanup implements IRepairStep {

	/** @var IDBConnection */
	private $db;

	/** @var IConfig */
	private $config;

	public function __construct(IDBConnection $connection, IConfig $config) {
		$this->db = $connection;
		$this->config = $config;
	}

	public function getName() {
		return 'Remove any playlist files from music_tracks table';
	}

	/**
	 * @inheritdoc
	 */
	public function run(IOutput $output) {
		$installedVersion = $this->config->getAppValue('music', 'installed_version');

		// Music version 0.9.3 and older may have scanned playlist files as tracks,
		// depending on the MIME type configuration of the cloud.
		if (\version_compare($installedVersion, '0.9.4', '<')) {
			$n = $this->removePlaylistFiles();
			$output->info("$n files with audio/mpegurl or audio/x-scpls mime type removed from the music library");
			// Clean cache
			$this->db->executeUpdate("DELETE FROM `*PREFIX*music_cache`");
		}
	}

	private function removePlaylistFiles() {
		// Find and delete tracks with mime audio/mpegurl and audio/x-scpls.
		// This may leave some stray albums and artists in the DB but that is not a major probelm
		// since the background cleanup task should get rid of those, eventually.
		$sql = "DELETE FROM `*PREFIX*music_tracks` WHERE `mimetype` = 'audio/mpegurl' OR `mimetype` = 'audio/x-scpls'";
		return $this->db->executeUpdate($sql);
	}

}
{'fraction_non_alphanumeric': 0.13615652640042883, 'fraction_numerical': 0.006432591798445457, 'mean_word_length': 4.140495867768595, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4985355', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1216, 'n_words': 347}