1 : <?php
2 : /**
3 : * Roman de Renart
4 : *
5 : * PHP version 5
6 : *
7 : * @category Rdr
8 : * @package Edit
9 : * @author Michel Corne <mcorne@yahoo.com>
10 : * @copyright 2010 Michel Corne
11 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 : * @link http://roman-de-renart.blogspot.com/
13 : * @version SVN: $Id$
14 : */
15 :
16 : require_once 'Episode.php';
17 :
18 : /**
19 : * Fixing the text of Meon of an episode
20 : *
21 : * @category Rdr
22 : * @package Edit
23 : * @author Michel Corne <mcorne@yahoo.com>
24 : * @copyright 2010 Michel Corne
25 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
26 : */
27 :
28 : class Fix extends Episode
29 : {
30 : /**
31 : * The message reported when there is nothing to fix
32 : */
33 : const MSG_NOTHING_FIXED = 'No line need fixing.';
34 :
35 : /**
36 : * The name of the file containing the fixes to clean a Meon text
37 : */
38 : const MEON_CLEANING_LIST = 'meon-cleaning-list.php';
39 :
40 : /**
41 : * The name of the file containing the fixes to correct a Meon text
42 : */
43 : const MEON_FIXES_LIST = 'meon-fixes-list.php';
44 :
45 : /**
46 : * The name of the file storing the fixes
47 : * @var string
48 : */
49 : public $fixesFile;
50 :
51 : /**
52 : * The number of the column to fix
53 : * @var int
54 : */
55 : protected $inputColumnNumber;
56 :
57 : /**
58 : * The number of the column with the fixes
59 : * @var int
60 : */
61 : protected $outputColumnNumber;
62 :
63 : /**
64 : * The list of fixes
65 : * @var array
66 : */
67 : protected $replace = array();
68 :
69 : /**
70 : * The list pattern to search for
71 : * @var array
72 : */
73 : protected $search = array();
74 :
75 : /**
76 : * Constructor
77 : *
78 : * @param array $config the configuration directives
79 : * @param int $inputColumnNumber the number of the column to fix
80 : * @param int $outputColumnNumber the number of the column with the fixes
81 : * @param string $fixesFile the name of the file of the fixes
82 : * @param boolean $setFileNames sets the names of the working files if true, no setting if false
83 : * @return void
84 : */
85 : public function __construct($config, $inputColumnNumber, $outputColumnNumber,
86 : $fixesFile = null, $setFileNames = true)
87 : {
88 3 : parent::__construct($config, false);
89 :
90 3 : $this->inputColumnNumber = $inputColumnNumber;
91 3 : $this->outputColumnNumber = $outputColumnNumber;
92 :
93 3 : $this->setFileNames($fixesFile, $setFileNames);
94 3 : $fixesFile and $this->completeFixes();
95 :
96 3 : }
97 :
98 : /**
99 : * Completes the fixes to be used by preg_replace
100 : * @param string $fixesFile the name of the file of the fixes
101 : *
102 : * @return array the search list and the replace list
103 : */
104 : public function completeFixes()
105 : {
106 3 : $fixes = $this->includeFile($this->fixesFile);
107 :
108 3 : foreach($fixes as $search => $replace) {
109 3 : if (strpos($search, '~') === false) {
110 : // this is a straight word to search for
111 :
112 3 : $ucsearch = ucfirst($search);
113 3 : $ucreplace = ucfirst($replace);
114 :
115 : // creates 2 preg_* patterns to find the word
116 : // with or without a capitalized first letter
117 3 : $this->search[] = "~(?<!\w)$search(?!\w)~u";
118 3 : $this->search[] = "~(?<!\w)$ucsearch(?!\w)~u";
119 :
120 3 : $this->replace[] = $replace;
121 3 : $this->replace[] = $ucreplace;
122 :
123 3 : } else {
124 : // this is preg_* pattern
125 3 : $this->search[] = $search . 'u';
126 3 : $this->replace[] = $replace;
127 : }
128 3 : }
129 :
130 : // returns the search and replace lists
131 : // as a convenience for testing purposes
132 3 : return array($this->search, $this->replace);
133 : }
134 :
135 : /**
136 : * Fixes a text of an episode
137 : *
138 : * @return string the list of fixes or a message if there is nothing to fix
139 : */
140 : public function fixEpisode()
141 : {
142 1 : $this->readSheet();
143 :
144 : // reads the text to fix
145 1 : $column = $this->readColumn($this->inputColumnNumber);
146 : // fixes the text
147 1 : $fixed = $this->arrayMap('fixString', $column);
148 :
149 : // writes the fixed text in another column
150 1 : $this->writeColumn($fixed, $this->outputColumnNumber);
151 1 : $this->writeSheet();
152 :
153 : // reports the differences (the fixes) between the text before and after the fixing
154 1 : $result = $this->arrayDiffToString($column, $fixed) or
155 1 : $result = self::MSG_NOTHING_FIXED;
156 :
157 1 : return $result;
158 : }
159 :
160 : /**
161 : * Fixes a string
162 : *
163 : * @param string $string the string to fix
164 : * @return array the fixed string
165 : */
166 : public function fixString($string)
167 : {
168 2 : return preg_replace($this->search, $this->replace, $string);
169 : }
170 :
171 : /**
172 : * Sets the names of the working files
173 : *
174 : * @param string $fixesFile the name of the fixes file
175 : * @param string $setFileNames sets episode file names if true, no setting otherwise
176 : * @return void
177 : */
178 : public function setFileNames($fixesFile = null, $setFileNames = true)
179 : {
180 3 : $fixesFile and $this->fixesFile = $this->makeFilePath($fixesFile, 'data-dir');
181 3 : $setFileNames and parent::setFileNames();
182 3 : }
|