about summary refs log tree commit diff
path: root/laravel/app/Console/Commands/AddSongs.php
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@gmail.com>2018-12-26 12:29:49 -0500
committerZach DeCook <zachdecook@gmail.com>2018-12-26 12:29:49 -0500
commit83b5a599c9859dae70baf29fd57c8d5e19507ea9 (patch)
tree3414718f8f8af399ccf7c95a6c7fb04210e70e01 /laravel/app/Console/Commands/AddSongs.php
parent97e82af677235e991b2f580794fe728358569b0a (diff)
downloadprosongsa-83b5a599c9859dae70baf29fd57c8d5e19507ea9.tar.gz
* Database: Allow songs to be imported from a textfile
Diffstat (limited to 'laravel/app/Console/Commands/AddSongs.php')
-rw-r--r--laravel/app/Console/Commands/AddSongs.php91
1 files changed, 91 insertions, 0 deletions
diff --git a/laravel/app/Console/Commands/AddSongs.php b/laravel/app/Console/Commands/AddSongs.php
new file mode 100644
index 0000000..94cf3c0
--- /dev/null
+++ b/laravel/app/Console/Commands/AddSongs.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Song;
+use Illuminate\Console\Command;
+
+class AddSongs extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'prosongsa:import {file}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Import songs from an inputfile.';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+    	define( 'AUTHOR_REGEX', "/^-(([A-Za-z.]+ ?)+)/" );
+    	$filename = $this->argument('file');
+		$handle = fopen($filename, "r");
+
+		$theSong = [
+			'text' => '',
+			'verse' => '',
+			'number' => FALSE,
+		];
+		
+		if ( !$handle ){
+			$this->error("Couldn't read file '$filename'");
+			return 1;
+		}
+		while (($line = fgets($handle)) !== false)
+		{
+			// If we see a number, then that is what song we are on.
+			$matches = array();
+			if ( preg_match("(^(X?C?B?\d+)\. )", $line, $matches) )
+			{
+				if ( $theSong['text'] && $theSong['number'] && $theSong['title']
+				&& ( $theSong['number'] != $matches[1] ) ) {
+					Song::create( $theSong );
+					$this->line( "Created $theSong[title]" );
+					$theSong = [
+						'text' => '',
+						'verse' => '',
+						'key' => NULL,
+						'author' => NULL,
+					];
+				}
+				$theSong['number'] = $matches[1];
+				$theSong['title'] = trim($line);
+				$this->line( "Creating $theSong[title]..." );
+			}
+
+			$theSong['text'] .= $line;
+
+			if ( preg_match( "/\{p?\d*\((.+m?)\)\}/", $line, $matches)
+				|| preg_match("/^{Key: ?([^ ]*).*}/i", $line, $matches)
+			) {
+				$theSong['key'] = $matches[1];
+			}
+			if ( preg_match( AUTHOR_REGEX, $line, $matches ) ) {
+				$theSong['author'] = $matches[1];
+			}
+		}
+		fclose($handle);
+		Song::create( $theSong );
+    }
+}