forked from DHTMLGoodies/chessParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PgnParser.php
167 lines (135 loc) · 4.27 KB
/
PgnParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
class PgnParser
{
private $pgnFile;
private $pgnContent;
private $pgnGames;
private $gameParser;
private $pgnGameParser;
private $_fullParsing = true;
public function __construct($pgnFile = "", $fullParsing = true)
{
if ($pgnFile) {
$this->pgnFile = $this->sanitize($pgnFile);
}
$this->_fullParsing = $fullParsing;
$this->gameParser = new GameParser();
$this->pgnGameParser = new PgnGameParser();
}
private function sanitize($filePath)
{
$extension = $this->getExtension($filePath);
if ($extension != 'pgn') return null;
if (class_exists("LudoDBRegistry")) {
$tempPath = LudoDBRegistry::get('FILE_UPLOAD_PATH');
}else{
$tempPath = null;
}
if (isset($tempPath) && substr($filePath, 0, strlen($tempPath)) == $tempPath) {
} else {
if (substr($filePath, 0, 1) === "/") return null;
}
$filePath = preg_replace("/[^0-9\.a-z_\-\/]/si", "", $filePath);
if (!file_exists($filePath)) return null;
return $filePath;
}
private function getExtension($filePath)
{
$tokens = explode(".", $filePath);
return strtolower(array_pop($tokens));
}
public function setPgnContent($content)
{
$this->pgnContent = $content;
}
private function cleanPgn()
{
$c = $this->pgnContent;
$c = preg_replace("/{\s{0,6}\[%emt[^\}]*?\}/","",$c);
$c = preg_replace("/\\$[0-9]+/s", "", $c);
$c = str_replace("({", "( {", $c);
$c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c);
$c = preg_replace("/\r/s", "", $c);
$c = preg_replace("/\t/s", "", $c);
$c = preg_replace("/\]\s+\[/s", "]\n[", $c);
$c = str_replace(" [", "[", $c);
$c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c);
$c = preg_replace("/\n{3,}/s", "\n\n", $c);
$c = str_replace("-SB-", "[", $c);
$c = str_replace("0-0-0", "O-O-O", $c);
$c = str_replace("0-0", "O-O", $c);
return $c;
}
public static function getArrayOfGames($pgn)
{
return self::getPgnGamesAsArray($pgn);
}
private function splitPgnIntoGames($pgnString)
{
return $this->getPgnGamesAsArray($pgnString);
}
private function getPgnGamesAsArray($pgn)
{
$ret = array();
$content = "\n\n" . $pgn;
$games = preg_split("/\n\n\[/s", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 1, $count = count($games); $i < $count; $i++) {
$gameContent = trim("[" . $games[$i]);
if(strlen($gameContent) > 10){
array_push($ret, $gameContent);
}
}
return $ret;
}
public function getGamesAsJSON()
{
return json_encode($this->getGames());
}
private function fullParsing()
{
return $this->_fullParsing;
}
public function getUnparsedGames()
{
if (!isset($this->pgnGames)) {
if ($this->pgnFile && !isset($this->pgnContent)) {
$this->pgnContent = file_get_contents($this->pgnFile);
}
$this->pgnGames = $this->splitPgnIntoGames($this->cleanPgn($this->pgnContent));
}
return $this->pgnGames;
}
public function getCleanPgn(){
return $this->cleanPgn($this->pgnContent);
}
public function getFirstGame()
{
return $this->getGameByIndex(0);
}
public function getGameByIndex($index)
{
$games = $this->getUnparsedGames();
if (count($games) && count($games) > $index) {
return $this->getParsedGame($games[$index]);
}
return null;
}
public function getGames()
{
$games = $this->getUnparsedGames();
$ret = array();
for ($i = 0, $count = count($games); $i < $count; $i++) {
$ret[] = $this->getParsedGame($games[$i]);
}
return $ret;
}
private function getParsedGame($unParsedGame)
{
$this->pgnGameParser->setPgn($unParsedGame);
$ret = $this->pgnGameParser->getParsedData();
if ($this->fullParsing()) {
$ret = $this->gameParser->getParsedGame($ret);
}
return $ret;
}
}