Conditions | 30 |
Paths | 295 |
Total Lines | 176 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
99 | private function pageProcess(): bool |
||
100 | { |
||
101 | $e = null; |
||
102 | $this->pageWorkStatus = new PageWorkStatus(); |
||
103 | $this->bot->checkStopOnTalkpage(true); |
||
104 | |||
105 | // get a random queue line |
||
106 | $json = $this->db->getAllRowsToEdit(self::CITATION_LIMIT); |
||
107 | $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
||
108 | |||
109 | if (empty($data)) { |
||
110 | $this->log->alert("SKIP : OuvrageEditWorker / getAllRowsToEdit() no row to process\n"); |
||
111 | sleep(60); |
||
112 | throw new Exception('no row to process'); |
||
113 | } |
||
114 | |||
115 | try { |
||
116 | $title = $data[0]['page']; |
||
117 | echo Color::BG_CYAN . $title . Color::NORMAL . " \n"; |
||
118 | $page = ServiceFactory::wikiPageAction($title, false); // , true ? |
||
119 | } catch (Exception $e) { |
||
120 | $this->log->warning("*** WikiPageAction error : " . $title . " \n"); |
||
121 | sleep(20); |
||
122 | |||
123 | return false; |
||
124 | } |
||
125 | |||
126 | // Page supprimée ? todo event listener |
||
127 | if ($page->getLastRevision() === null) { |
||
128 | $this->log->warning("SKIP : page supprimée !\n"); |
||
129 | $this->db->deleteArticle($title); |
||
130 | |||
131 | return false; |
||
132 | } |
||
133 | |||
134 | // HACK |
||
135 | if ($page->getLastEditor() == getenv('BOT_NAME')) { |
||
136 | $this->log->notice("SKIP : édité recemment par bot.\n"); |
||
137 | $this->db->skipArticle($title); |
||
138 | |||
139 | return false; |
||
140 | } |
||
141 | // todo include a sandbox page ? |
||
142 | if ($page->getNs() !== 0) { |
||
143 | $this->log->notice("SKIP : page n'est pas dans Main (ns 0)\n"); |
||
144 | $this->db->skipArticle($title); |
||
145 | |||
146 | return false; |
||
147 | } |
||
148 | $this->pageWorkStatus->wikiText = $page->getText(); |
||
149 | |||
150 | if (empty($this->pageWorkStatus->wikiText)) { |
||
151 | $this->log->warning("SKIP : this->wikitext vide\n"); |
||
152 | $this->db->skipArticle($title); |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | // Featured/Good article (AdQ/BA) todo event listener |
||
157 | if (preg_match('#{{ ?En-tête label ?\| ?AdQ#i', $this->pageWorkStatus->wikiText)) { |
||
158 | $this->db->setLabel($title, 2); |
||
159 | $this->log->warning("Article de Qualité !\n"); |
||
160 | $this->pageWorkStatus->botFlag = false; |
||
161 | $this->pageWorkStatus->featured_article = true; // to add star in edit summary |
||
162 | } |
||
163 | if (preg_match('#{{ ?En-tête label ?\| ?BA#i', $this->pageWorkStatus->wikiText)) { |
||
164 | $this->db->setLabel($title, 1); |
||
165 | $this->pageWorkStatus->botFlag = false; |
||
166 | $this->pageWorkStatus->featured_article = true; // to add star in edit summary |
||
167 | $this->log->warning("Bon article !!\n"); |
||
168 | } |
||
169 | |||
170 | // todo event listener |
||
171 | if (WikiBotConfig::isEditionTemporaryRestrictedOnWiki($this->pageWorkStatus->wikiText)) { |
||
172 | // TODO Gestion d'une repasse dans X jours |
||
173 | $this->log->info("SKIP : protection/3R/travaux.\n"); |
||
174 | $this->db->skipArticle($title); |
||
175 | |||
176 | return false; |
||
177 | } |
||
178 | |||
179 | if ($this->bot->minutesSinceLastEdit($title) < 10) { |
||
180 | // TODO Gestion d'une repasse dans X jours |
||
181 | $this->log->notice( |
||
182 | sprintf( |
||
183 | "SKIP : édition humaine dans les dernières %s minutes.\n", |
||
184 | self::DELAY_MINUTES_AFTER_HUMAN_EDIT |
||
185 | ) |
||
186 | ); |
||
187 | sleep(60 * self::DELAY_MINUTES_AFTER_HUMAN_EDIT); // hack: waiting cycles |
||
188 | |||
189 | return false; |
||
190 | } |
||
191 | |||
192 | |||
193 | // GET all article lines from db |
||
194 | $this->log->info(sprintf("%s rows to process\n", is_countable($data) ? count($data) : 0)); |
||
195 | |||
196 | // foreach line |
||
197 | $changed = false; |
||
198 | foreach ($data as $dat) { |
||
199 | // hack temporaire pour éviter articles dont CompleteProcess incomplet |
||
200 | if (empty($dat['opti']) || empty($dat['optidate']) || $dat['optidate'] < $this->db->getOptiValidDate()) { |
||
201 | $this->log->notice("SKIP : Amélioration incomplet de l'article. sleep 10min"); |
||
202 | sleep(600); |
||
203 | |||
204 | return false; |
||
205 | } |
||
206 | $success = $this->dataProcess($dat); |
||
207 | $changed = ($success) ? true : $changed; |
||
208 | } |
||
209 | if (!$changed) { |
||
210 | $this->log->debug("Rien à changer..."); |
||
211 | $this->db->skipArticle($title); |
||
212 | |||
213 | return false; |
||
214 | } |
||
215 | |||
216 | // EDIT THE PAGE |
||
217 | if ($this->pageWorkStatus->wikiText === '' || $this->pageWorkStatus->wikiText === '0') { |
||
218 | return false; |
||
219 | } |
||
220 | |||
221 | $miniSummary = $this->generateSummary(); |
||
222 | $this->log->notice($miniSummary); |
||
223 | $this->log->debug("sleep 2..."); |
||
224 | sleep(2); // todo ??? |
||
225 | |||
226 | pageEdit: |
||
227 | |||
228 | try { |
||
229 | // corona Covid :) |
||
230 | //$miniSummary .= (date('H:i') === '20:00') ? ' 🏥' : ''; // 🏥🦠 |
||
231 | |||
232 | $editInfo = ServiceFactory::editInfo($miniSummary, $this->pageWorkStatus->minorFlag, $this->pageWorkStatus->botFlag, 5); |
||
233 | $success = $page->editPage(Normalizer::normalize($this->pageWorkStatus->wikiText), $editInfo); |
||
234 | } catch (Throwable $e) { |
||
235 | // Invalid CSRF token. |
||
236 | if (strpos($e->getMessage(), 'Invalid CSRF token') !== false) { |
||
237 | $this->log->alert("*** Invalid CSRF token \n"); |
||
238 | throw new Exception('Invalid CSRF token', $e->getCode(), $e); |
||
239 | } else { |
||
240 | $this->log->warning('Exception in editPage() ' . $e->getMessage()); |
||
241 | sleep(10); |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | $this->log->info($success ? "Edition Ok\n" : "***** Edition KO !\n"); |
||
248 | |||
249 | if ($success) { |
||
250 | // updata DB |
||
251 | foreach ($data as $dat) { |
||
252 | $this->db->sendEditedData(['id' => $dat['id']]); |
||
253 | } |
||
254 | |||
255 | try { |
||
256 | if (self::EDIT_SIGNALEMENT && !empty($this->pageWorkStatus->errorWarning[$title])) { |
||
257 | $this->sendOuvrageErrorsOnTalkPage($data, $this->log); |
||
258 | } |
||
259 | } catch (Throwable $e) { |
||
260 | $this->log->warning('Exception in editPage() ' . $e->getMessage()); |
||
261 | unset($e); |
||
262 | } |
||
263 | |||
264 | if (!$this->pageWorkStatus->botFlag) { |
||
265 | $this->log->debug("sleep " . self::DELAY_NO_BOTFLAG_SECONDS); |
||
266 | sleep(self::DELAY_NO_BOTFLAG_SECONDS); |
||
267 | } |
||
268 | if ($this->pageWorkStatus->botFlag) { |
||
269 | $this->log->debug("sleep " . self::DELAY_BOTFLAG_SECONDS); |
||
270 | sleep(self::DELAY_BOTFLAG_SECONDS); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | return $success; |
||
275 | } |
||
432 |