about summary refs log tree commit diff
path: root/laravel/app
diff options
context:
space:
mode:
Diffstat (limited to 'laravel/app')
-rw-r--r--laravel/app/Console/Commands/AddSongs.php91
-rw-r--r--laravel/app/Song.php11
2 files changed, 102 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 );
+    }
+}
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
new file mode 100644
index 0000000..876617f
--- /dev/null
+++ b/laravel/app/Song.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Song extends Model
+{
+	public $fillable = ['number', 'title', 'author', 'key', 'text'];
+    //
+}