| Total Complexity | 62 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OuvrageEditWorker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OuvrageEditWorker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class OuvrageEditWorker |
||
| 33 | { |
||
| 34 | use OuvrageEditSummaryTrait, TalkPageEditTrait; |
||
|
1 ignored issue
–
show
|
|||
| 35 | |||
| 36 | public const TASK_NAME = '📗 Amélioration bibliographique'; // 📖📔📘📗 |
||
| 37 | public const LUCKY_MESSAGE = ' 🇺🇦'; // ☘️ |
||
| 38 | /** |
||
| 39 | * poster ou pas le message en PD signalant les erreurs à résoudre |
||
| 40 | */ |
||
| 41 | public const EDIT_SIGNALEMENT = true; |
||
| 42 | public const CITATION_LIMIT = 150; |
||
| 43 | public const DELAY_BOTFLAG_SECONDS = 60; |
||
| 44 | public const DELAY_NO_BOTFLAG_SECONDS = 60; |
||
| 45 | public const DELAY_MINUTES_AFTER_HUMAN_EDIT = 10; |
||
| 46 | public const ERROR_MSG_TEMPLATE = __DIR__ . '/../templates/message_errors.wiki'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var PageWorkStatus |
||
| 50 | */ |
||
| 51 | protected $pageWorkStatus; |
||
| 52 | |||
| 53 | private $db; |
||
| 54 | /** |
||
| 55 | * @var WikiBotConfig |
||
| 56 | */ |
||
| 57 | private $bot; |
||
| 58 | /** |
||
| 59 | * @var MemoryInterface |
||
| 60 | */ |
||
| 61 | private $memory; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var LoggerInterface |
||
| 65 | */ |
||
| 66 | private $log; |
||
| 67 | |||
| 68 | public function __construct( |
||
| 69 | DbAdapterInterface $dbAdapter, |
||
| 70 | WikiBotConfig $bot, |
||
| 71 | MemoryInterface $memory, |
||
| 72 | ?LoggerInterface $log = null |
||
| 73 | ) |
||
| 74 | { |
||
| 75 | $this->db = $dbAdapter; |
||
| 76 | $this->bot = $bot; |
||
| 77 | $this->memory = $memory; |
||
| 78 | $this->log = $log ?? new NullLogger(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @throws Exception |
||
| 83 | */ |
||
| 84 | public function run(): void |
||
| 85 | { |
||
| 86 | while (true) { |
||
| 87 | echo "\n-------------------------------------\n\n"; |
||
| 88 | echo date("Y-m-d H:i:s") . " "; |
||
| 89 | $this->log->info($this->memory->getMemory(true)); |
||
| 90 | $this->pageProcess(); |
||
| 91 | sleep(2); // précaution boucle infinie |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @throws UsageException |
||
| 97 | * @throws Exception |
||
| 98 | */ |
||
| 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 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param array $data |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | * @throws Exception |
||
| 282 | */ |
||
| 283 | private function dataProcess(array $data): bool |
||
| 284 | { |
||
| 285 | $origin = $data['raw']; |
||
| 286 | $completed = $data['opti']; |
||
| 287 | $this->printDebug($data); |
||
| 288 | |||
| 289 | if (WikiTextUtil::isCommented($origin) || $this->isTextCreatingError($origin)) { |
||
| 290 | $this->log->notice("SKIP: template avec commentaire HTML ou modèle problématique."); |
||
| 291 | $this->db->skipRow((int)$data['id']); |
||
| 292 | |||
| 293 | return false; |
||
| 294 | } |
||
| 295 | |||
| 296 | $find = mb_strpos($this->pageWorkStatus->wikiText, $origin); |
||
| 297 | if ($find === false) { |
||
| 298 | $this->log->notice("String non trouvée."); |
||
| 299 | $this->db->skipRow((int)$data['id']); |
||
| 300 | |||
| 301 | return false; |
||
| 302 | } |
||
| 303 | |||
| 304 | $this->checkErrorWarning($data); |
||
| 305 | |||
| 306 | // Replace text |
||
| 307 | $newText = WikiPageAction::replaceTemplateInText($this->pageWorkStatus->wikiText, $origin, $completed); |
||
| 308 | |||
| 309 | if (!$newText || $newText === $this->pageWorkStatus->wikiText) { |
||
| 310 | $this->log->warning("newText error"); |
||
| 311 | |||
| 312 | return false; |
||
| 313 | } |
||
| 314 | $this->pageWorkStatus->wikiText = $newText; |
||
| 315 | $this->pageWorkStatus->minorFlag = ('1' === $data['major']) ? false : $this->pageWorkStatus->minorFlag; |
||
| 316 | $this->pageWorkStatus->citationVersion = $data['version']; |
||
| 317 | $this->pageWorkStatus->citationSummary[] = $data['modifs']; |
||
| 318 | $this->pageWorkStatus->nbRows++; |
||
| 319 | |||
| 320 | return true; |
||
| 321 | } |
||
| 322 | |||
| 323 | private function isTextCreatingError(string $string): bool |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * todo extract |
||
| 331 | * Vérifie alerte d'erreurs humaines. |
||
| 332 | * |
||
| 333 | * @param array $data |
||
| 334 | * |
||
| 335 | * @throws Exception |
||
| 336 | */ |
||
| 337 | private function checkErrorWarning(array $data): void |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * todo extract |
||
| 412 | * Pour éviter les doublons dans signalements d'erreur. |
||
| 413 | * |
||
| 414 | * @param string $page |
||
| 415 | * @param string $text |
||
| 416 | */ |
||
| 417 | private function addErrorWarning(string $page, string $text): void |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | private function printDebug(array $data) |
||
| 430 | } |
||
| 431 | } |
||
| 432 |