<commit_before><?php
/*
  iTunes XML parser for PHP
  Copyright (C) 2013 <NAME> [http://github.com/shuckster]
  version: 1.4
  	Changes:
		* 1.4: Parse info and playlists
		* 1.3: New example, delete old/deprecated stuff
		* 1.2: Now a class, improved sort-method
		* 1.1: Type-cast integers and booleans

  based on:

  Copyright (C) 2005 <NAME> [http://www.wirsindecht.org]
  version: 1.00
  based on:

  iTunes XML PhP Parser
  Copyright (C) 2003 <NAME> [http://codetriangle.com/]
  version: 1.00

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
  as published by the Free Software Foundation; either version 2.1
  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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA

  iTunes is a product by Apple Computer, Inc.
  http://www.apple.com/

*/

class iTunesXMLParser {

	public $file_name = '';

	public $infos = array();
	public $tracks = array();
	public $playlists = array();

	public $sort_field = NULL;
	public $sort_direction = 'ascending';

	protected $plist_node = NULL;
	protected $tracks_node = NULL;
	protected $playlists_node = NULL;

	public function open( $path ) {

		if ( !file_exists( $path ) ) {
			die( 'iTunes XML file not found: ' . $path );
		}

		// Open the XML document in the DOM
		$dom = new DomDocument();
		if ( !$dom->load( $path ) ) {
			die( 'Could not parse iTunes XML file: ' . $path );
		}

		// Get the root element <plist>
		$plist_node = $dom->documentElement;
		$infos_node = NULL;
		$tracks_node = NULL;
		$playlists_node = NULL;

		// First <dict> contains version-info + tracks-node
		foreach ( $plist_node->childNodes as $child ) {
			if ( 'dict' === $child->nodeName ) {
				$infos_node = $child;
				break;
			}
		}

		// <dict> in the version-node contains the tracks, <array> contains the playlists
		$break_out = false;
		foreach ( $infos_node->childNodes as $child ) {

			if ( 'dict' === $child->nodeName ) {
				$tracks_node = $child;
				if ( $break_out ) {
					break;
				}
				$break_out = true;
			}

			if ( 'array' === $child->nodeName ) {
				$playlists_node = $child;
				if ( $break_out ) {
					break;
				}
				$break_out = true;
			}

		}

		// Fell-through: Set public vars to successful parse-data
		$this->file_name = $path;

		$this->plist_node = $plist_node;
		$this->tracks_node = $tracks_node;
		$this->playlists_node = $playlists_node;

		$this->parseInfos();
		$this->parseTracks();
		$this->parsePlaylists();

	}

	protected function parseInfos() {

		$infos = $this->parseDict( $this->plist_node, NULL,  array( 'dict', 'array' ) );

		if ( NULL !== $infos ) {
			$this->infos = $infos[ 0 ];
		}

		return $infos;

	}

	protected function parseTracks() {

		$tracks = $this->parseDict( $this->tracks_node, 'Track ID' );

		if ( NULL !== $tracks ) {
			$this->tracks = $tracks;
		}

		return $tracks;

	}

	protected function parsePlaylists() {

		$playlists = $this->parseDict( $this->playlists_node, 'Playlist ID' );

		if ( NULL !== $playlists ) {

			// Match playlist-items to actual tracks
			foreach ( $playlists as &$playlist ) {
				$new_items = array();

				if ( isset( $playlist[ 'Playlist Items' ] ) ) {
					foreach ( $playlist[ 'Playlist Items' ] as $item ) {
						$track_id = $item[ 'Track ID' ];
						$new_items[] = $this->tracks[ $track_id ];
					}

					$playlist[ 'Playlist Items' ] = $new_items;
				}

			}

			$this->playlists = $playlists;
		}

		return $playlists;

	}

	protected function parseDict( $baseNode, $primary_key = NULL, $ignore_nodes = array() ) {

		$dicts = array();<commit_msg>Initialise array after test<commit_after><?php
/*
  iTunes XML parser for PHP
  Copyright (C) 2013 <NAME> [http://github.com/shuckster]
  version: 1.4
  	Changes:
		* 1.4: Parse info and playlists
		* 1.3: New example, delete old/deprecated stuff
		* 1.2: Now a class, improved sort-method
		* 1.1: Type-cast integers and booleans

  based on:

  Copyright (C) 2005 <NAME> [http://www.wirsindecht.org]
  version: 1.00
  based on:

  iTunes XML PhP Parser
  Copyright (C) 2003 <NAME> [http://codetriangle.com/]
  version: 1.00

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
  as published by the Free Software Foundation; either version 2.1
  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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA

  iTunes is a product by Apple Computer, Inc.
  http://www.apple.com/

*/

class iTunesXMLParser {

	public $file_name = '';

	public $infos = array();
	public $tracks = array();
	public $playlists = array();

	public $sort_field = NULL;
	public $sort_direction = 'ascending';

	protected $plist_node = NULL;
	protected $tracks_node = NULL;
	protected $playlists_node = NULL;

	public function open( $path ) {

		if ( !file_exists( $path ) ) {
			die( 'iTunes XML file not found: ' . $path );
		}

		// Open the XML document in the DOM
		$dom = new DomDocument();
		if ( !$dom->load( $path ) ) {
			die( 'Could not parse iTunes XML file: ' . $path );
		}

		// Get the root element <plist>
		$plist_node = $dom->documentElement;
		$infos_node = NULL;
		$tracks_node = NULL;
		$playlists_node = NULL;

		// First <dict> contains version-info + tracks-node
		foreach ( $plist_node->childNodes as $child ) {
			if ( 'dict' === $child->nodeName ) {
				$infos_node = $child;
				break;
			}
		}

		// <dict> in the version-node contains the tracks, <array> contains the playlists
		$break_out = false;
		foreach ( $infos_node->childNodes as $child ) {

			if ( 'dict' === $child->nodeName ) {
				$tracks_node = $child;
				if ( $break_out ) {
					break;
				}
				$break_out = true;
			}

			if ( 'array' === $child->nodeName ) {
				$playlists_node = $child;
				if ( $break_out ) {
					break;
				}
				$break_out = true;
			}

		}

		// Fell-through: Set public vars to successful parse-data
		$this->file_name = $path;

		$this->plist_node = $plist_node;
		$this->tracks_node = $tracks_node;
		$this->playlists_node = $playlists_node;

		$this->parseInfos();
		$this->parseTracks();
		$this->parsePlaylists();

	}

	protected function parseInfos() {

		$infos = $this->parseDict( $this->plist_node, NULL,  array( 'dict', 'array' ) );

		if ( NULL !== $infos ) {
			$this->infos = $infos[ 0 ];
		}

		return $infos;

	}

	protected function parseTracks() {

		$tracks = $this->parseDict( $this->tracks_node, 'Track ID' );

		if ( NULL !== $tracks ) {
			$this->tracks = $tracks;
		}

		return $tracks;

	}

	protected function parsePlaylists() {

		$playlists = $this->parseDict( $this->playlists_node, 'Playlist ID' );

		if ( NULL !== $playlists ) {

			// Match playlist-items to actual tracks
			foreach ( $playlists as &$playlist ) {

				if ( isset( $playlist[ 'Playlist Items' ] ) ) {
					$new_items = array();

					foreach ( $playlist[ 'Playlist Items' ] as $item ) {
						$track_id = $item[ 'Track ID' ];
						$new_items[] = $this->tracks[ $track_id ];
					}

					$playlist[ 'Playlist Items' ] = $new_items;
				}

			}

			$this->playlists = $playlists;
		}

		return $playlists;

	}

	protected function parseDict( $baseNode, $primary_key = NULL, $ignore_nodes = array() ) {

		$dicts = array();
{'fraction_non_alphanumeric': 0.13303769401330376, 'fraction_numerical': 0.011086474501108648, 'mean_word_length': 3.104651162790698, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1042371', 'n_tokens_mistral': 3189, 'n_tokens_neox': 2694, 'n_words': 842}