This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta-archive-process.php
executable file
·703 lines (568 loc) · 41.9 KB
/
meta-archive-process.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php
/* ---------------------------------------------
Author : Quentinv57 (2011 - 2014)
Steinsplitter (2014 - )
Licence : GNU General Public License v3
(see http://www.gnu.org/licenses/)
Date of creation : 2011-03
Meta Archival Script
-> archive steward requests on the following pages :
* [[Talk:Spam blacklist]]
* [[Steward requests/Checkuser]]
* [[Steward requests/Global]]
* [[Steward requests/Global permissions]]
* [[Steward requests/Bot status]]
* [[Steward requests/Permissions]]
> temp sysop requests are transfered to [[Steward requests/Permissions/Approved temporary]] now
> the requests on [[Steward requests/Permissions/Approved temporary]], once the right is removed, are archived as well
* [[Steward requests/SUL requests]] (removed, outdated)
* [[Meta:Changing username]] (removed, outdated)
* [[Steward requests/Username changes]]
* [[Steward requests/Miscellaneous]]
---------------------------------------------
Here are the settings
(you can set a value of $nbdays to 0 to skip the page)
*/
$settings_match = array ( 'status_template' => '#\{\{status\|((not |not)?done|cannot|withdrawn|local)+(|\|.*)\}\}#i',
'status_param' => '#(\| *status *= *(<!--.*-->)?((not |not)?done|cannot|withdrawn|local)|\{\{status\|((not |not)?done|cannot|withdrawn|local)\}\})#iU',
// 20111009 : replace '#\| *status *= *(<!--.*-->)?(not |not)?done#iU' by '#(\| *status *= *(<!--.*-->)?(not |not)?done|\{\{status\|(not |not)?done\}\})#iU' > enable the use of the status template everywhere
'sb_templates' => '#\{\{(added|declined|cannot|removed|(not |not)?done|closing)\}\}#i',
'sc_templates' => '#\| *status *= *(<!--.*-->)?(not |not)?done|cannot|withdrawn|local#iU',
'tempsysop' => '#\{\{TempSysop\|([0-9]+)\|([0-9]{4,})\|([0-9]+)\|([0-9]+)(\||\}\})#i',
'st_templates' => '#\{\{status\|(added|declined|removed|(not |not)?done|closing|cannot|local)\}\}#i',
'removetemp' => '#\'\'\'removed\'\'\'|\'\'\'extended\'\'\'|\{\{removed\}\}|\{\{extended\}\}#i' );
$settings_archives = array (
'Talk:Spam blacklist' => array( 'subpage' => 'Talk:Spam blacklist/Archives/',
'nbdays' => 3,
'subsections' => array('Proposed additions','Proposed removals'),
'match' => $settings_match['sb_templates']),
'Steward requests/Checkuser' => array( 'subpage' => 'Steward requests/Checkuser/',
'nbdays' => 4, // was 7 before
'subsections' => array('Requests'),
'match' => $settings_match['status_param']), // #\{\{status\|(not )?done\}\}#
'Steward requests/Global' => array( 'subpage' => 'Steward requests/Global/',
'nbdays' => 2, // previously 3
'subsections' => array('Requests for global (un)block','Requests for global (un)lock and (un)hiding'),
'match' => $settings_match['status_template']),
'Steward requests/Global permissions' => array( 'subpage' => 'Steward requests/Global permissions/',
'nbdays' => 2,
'subsections' => array('Requests for global IP block exemption','Requests for global rename permissions','Requests for other global permissions'),
'match' => $settings_match['status_param']),
'Steward requests/Bot status' => array( 'subpage' => 'Steward requests/Bot status/',
'nbdays' => 2, // previously 3
'subsections' => array('Global bot status requests','Removal of global bot status','Bot status requests','Removal of bot status'),
'match' => $settings_match['status_param']),
'Steward requests/Permissions' => array( 'subpage' => 'Steward requests/Permissions/',
'nbdays' => 2,
'subsections' => array('Administrator access','Bureaucrat access','CheckUser access','Oversight access','Removal of access','Miscellaneous requests'),
'match' => $settings_match['status_param'],
'lvlsect' => 3),
'Steward requests/Miscellaneous' => array( 'subpage' => 'Steward requests/Miscellaneous/',
'nbdays' => 2,
'subsections' => array('Manual requests'),
'match' => $settings_match['status_param']),
'Steward requests/Username changes' => array( 'subpage' => 'Steward requests/Username changes/',
'nbdays' => 2, // previously 3
'subsections' => array('Simple rename requests','Requests involving merges, usurps or other complications'),
'match' => $settings_match['status_param'])
);
$settings_archives_tempsysop = array ( 'page' => 'Steward requests/Permissions',
'suffix' => 'Approved temporary',
'subsections' => array('Administrator access'),
'match' => $settings_match['tempsysop'] ); // temp sysop rights archival
$settings_archives_approvedtemp = array ( 'page' => 'Steward requests/Permissions/Approved temporary',
'arc_subpage' => 'Steward requests/Permissions/',
'arc_subsection' => 'Temporary permissions (expired and rejected requests only)',
'nbdays' => 0,
'match' => $settings_match['removetemp'] ); // archive requests on [[Steward requests/Permissions/Approved temporary]]
define ('DONTARCHIVESECT','{{User:SteinsplitterBot/DoNotArchiveSect}}');
//Dependency: https://github.com/MW-Peachy/Peachy
require( '/data/project/sbot/Peachy/Peachy/Init.php' );
$site = Peachy::newWiki( "meta" );
$site->set_runpage( null );
/* ************************************************************************
****************************** Fonctions **********************************
************************************************************************ */
function preg_quote_magic ($foo)
# Retire tous les meta caractères (avec le #)
{
return str_replace('#','\#',preg_quote($foo));
}
function get_content_by_section ($texte, $lvlsect)
{
$result = array();
$motif = '';
for ($i=1; $i<=$lvlsect; $i++)
$motif .= "=";
$motif = $motif . " *([^=^ ][^=^\n]*[^=^ ]) *" . $motif . "[^=]" ;
// si la sous-section n'apparait pas, on renvoie le texte
if (!preg_match('#'.$motif.'#', $texte))
return array('error'=>'NOTFOUND', 'content'=>$texte);
// liste des sections
preg_match_all ("#".$motif."#U", $texte, $listesections);
$listesections = $listesections[1];
// sinon : intro
preg_match ("#^(.*)".preg_quote_magic($listesections[0])."#sU", $texte, $matches);
$result[0]['wikititle'] = '';
$result[0]['title'] = '';
$result[0]['content'] = $matches[1];
$i=1;
foreach ($listesections as $key => $value) {
// sections (1 à n-1)
if ($i<count($listesections))
{
$motif_1 = str_replace("[^=^ ][^=^\n]*[^=^ ]", preg_quote_magic($listesections[($i-1)]), $motif);
$motif_2 = str_replace("[^=^ ][^=^\n]*[^=^ ]", preg_quote_magic($listesections[$i]), $motif);
preg_match ("#(".$motif_1.")(.*)".$motif_2."#sU", $texte, $matches);
$result[$i]['wikititle'] = $matches[1];
$result[$i]['title'] = $matches[2];
$result[$i]['content'] = $matches[3];
$i++;
}
// section n
else
{
$motif_2 = str_replace("[^=^ ][^=^\n]*[^=^ ]", preg_quote_magic($listesections[($i-1)]), $motif);
preg_match ("#(".$motif_2.")(.*)$#sU", $texte, $matches);
$result[$i]['wikititle'] = $matches[1];
$result[$i]['title'] = $matches[2];
$result[$i]['content'] = $matches[3];
}
}
return $result;
}
function NumberMonth ($foo)
{
switch ($foo)
{
case 'January': return 1;
case 'February': return 2;
case 'March': return 3;
case 'April': return 4;
case 'May': return 5;
case 'June': return 6;
case 'July': return 7;
case 'August': return 8;
case 'September': return 9;
case 'October': return 10;
case 'November': return 11;
case 'December': return 12;
default: return 0;
}
}
function FooMonth ($nb)
{
switch ($nb)
{
case 1: return 'January';
case 2: return 'February';
case 3: return 'March';
case 4: return 'April';
case 5: return 'May';
case 6: return 'June';
case 7: return 'July';
case 8: return 'August';
case 9: return 'September';
case 10: return 'October';
case 11: return 'November';
case 12: return 'December';
default: return 0;
}
}
function get_last_date ($text)
{ // retourne la dernière date trouvée dans le texte
$date = preg_match_all("#([1-9]|[1-2][0-9]|3[01]) (January|February|March|April|May|June|July|August|September|October|November|December) ([0-9]{4}) \(UTC\)#i", $text, $matches);
$lastdate = array();
foreach ($matches[0] as $key => $value) {
$d = intval($matches[1][$key]);
$m = NumberMonth($matches[2][$key]);
$y = intval($matches[3][$key]);
if ($lastdate==array()) {
$lastdate = array('d'=>$d, 'm'=>$m, 'y'=>$y);
} else {
if ($y>$lastdate['y'] OR ($y==$lastdate['y'] AND $m>$lastdate['m']) OR ($y==$lastdate['y'] AND $m==$lastdate['m'] AND $d>$lastdate['d']))
$lastdate = array('d'=>$d, 'm'=>$m, 'y'=>$y);
}
}
return $lastdate;
}
function get_expiring_date ($sectiontext)
{ // extensions temp_sysop
preg_match ("#([1-9]|[1-2][0-9]|3[01]) (January|February|March|April|May|June|July|August|September|October|November|December) ([0-9]{4})#i", $sectiontext, $matches);
$d = intval($matches[1]);
$m = NumberMonth($matches[2]);
$y = intval($matches[3]);
$lastdate = array('d'=>$d, 'm'=>$m, 'y'=>$y);
return $lastdate;
}
function is_last_x_days ($date, $nbdays)
{ // TRUE si la date $date est plus vieille qu'il y a $nbdays jours
$time = mktime (0,0,1, $date['m'], $date['d'], $date['y']);
$time += $nbdays * 3600 * 24;
if ($time<=time()) return TRUE;
else return FALSE;
}
function lastmonth ()
{
if (date('m')!=1) return date('Y') . '-' . str_pad((date('m')-1), 2, 0, STR_PAD_LEFT) ;
else return (date('Y')-1) . '-' . '12';
}
function numtoken ($length)
{
$result = '';
for ($i=0; $i<=$length; $i++) {
$result .= rand(0, 9);
}
return $result;
}
function zerofill ($num, $length)
{
$result = $num;
for ($i=0; $i<($length-strlen($num)); $i++)
$result = '0'.$result;
return $result;
}
/* ************************************************************************
************************ Fonction principale *****************************
************************************************************************ */
/* $contentpagename est le nom de la page principale
$archivepagename est le préfixe du nom de la page d'archive (avec /)
$subsections est un tableau contenant les titres des sections de niveau 2 à archiver
$nbdaysexec est le nombre de jours sans réponse min. après lesquels on archive le sujet
$matchrgx (facultatif) est une régex qui doit être trouvée dans la sous-section pour qu'elle soit archivée
<<$notmatchrgx (facultatif) est une régex qui doit être trouvée dans la sous-section pour qu'elle soit archivée>>
$lvlsect (facultatif) est le niveau des sections données dans $subsections
*/
function archiveprocess ($contentpagename, $archivepagename, $subsections, $nbdaysexec, $matchregx=NULL, $lvlsect=2)
{
global $site;
echo "Working on [[$contentpagename]]...\n";
$contentpage = $site->initPage( $contentpagename )->get_text( $force = true );
$difflen = strlen($contentpage);
$archivepage = array();
$archivedrequests = array('total'=>0);
// Pour monter jusqu'au niveau de section $lvlsect, s'il est strict. supérieur à deux, on prend seulement le contenu de la première section de titre niveau 2, etc.
for ($i=2; $i<=$lvlsect; $i++) {
if ($i==2) $sub = get_content_by_section ($contentpage, 2);
else $sub = get_content_by_section ($sub[1]['content'], $i);
}
unset($sub[0]);
foreach ($subsections as $nsub => $title)
{
$id = NULL;
foreach ($sub as $key2=>$temp) {
if ($sub[$key2]['title']==$title) $id = $key2;
}
if ($id != NULL)
{
echo "-> ".$sub[$id]['title']."\n";
$token = numtoken(100);
$motif = "== *([^=^ ][^=^\n]*[^=^ ]) *==( *\n|$)"; // + "^\n", + " *" (2 lines), changing place of "\n" on $actumotif - " *\n" replaced by "( *\n|$)"
$actumotif = "\n== *(" .preg_quote_magic($sub[$id]['title']). ") *== *";
foreach ($archivepage as $key => $value)
{
## INIT ARCHIVE PAGE WITH TOKEN - Begin
if (preg_match("#".$actumotif.".*(".$motif.")#sU", $archivepage[$key], $matches))
$archivepage[$key] = str_replace ($matches[2], $token.$matches[2], $archivepage[$key]);
else // le problème d'archivage *venait* d'ici
{
if (!preg_match("#".$actumotif."#",$archivepage[$key])) exit('Script Aborted : Error in ARCHIVE PAGE WITH TOKEN (1) - unable to find "'.$actumotif.'" in "'.$key.'"');
else $archivepage[$key] .= $token;
}
if (!preg_match("#\n".$token."#",$archivepage[$key]))
$archivepage[$key] = str_replace($token, "\n".$token, $archivepage[$key]);
## INIT ARCHIVE PAGE WITH TOKEN - End
}
$sect1 = get_content_by_section ($sub[$id]['content'], ($lvlsect+1));
unset($sect1[0]);
foreach ($sect1 as $key => $value)
{
if ((empty($matchregx) OR preg_match($matchregx, $sect1[$key]['content'])) AND !preg_match('#'.preg_quote_magic(DONTARCHIVESECT).'#i', $sect1[$key]['content']))
{ // 20111011 - Adding a condition that will prevent requests containing the DONTARCHIVESECT template to be archived
$lastdate = get_last_date($sect1[$key]['content']);
if (!empty($lastdate) AND is_last_x_days($lastdate,$nbdaysexec))
{
$lastdate_formated = $lastdate['y'].'-'.str_pad($lastdate['m'], 2, 0, STR_PAD_LEFT);
if (empty($archivepage[$lastdate_formated]))
{ // si on a pas encore eu besoin de cette page d'archive, on l'initialise
$archivepage[$lastdate_formated] = $site->initPage( $archivepagename.$lastdate_formated )->get_text( $force = true );
$archivedrequests[$lastdate_formated] = 0;
## INIT ARCHIVE PAGE WITH TOKEN - Begin
if (preg_match("#".$actumotif.".*(".$motif.")#sU", $archivepage[$lastdate_formated], $matches))
$archivepage[$lastdate_formated] = str_replace ($matches[2], $token.$matches[2], $archivepage[$lastdate_formated]);
else // le problème d'archivage *venait* d'ici
{
if (!preg_match("#".$actumotif."#",$archivepage[$lastdate_formated])) {
// var_dump($archivepage[$lastdate_formated]);
exit('Script Aborted : Error in ARCHIVE PAGE WITH TOKEN (2) - unable to find '.$actumotif.' in '.$lastdate_formated);
} else {
$archivepage[$lastdate_formated] .= $token;
//echo "Attention ! (<2>)\n";
}
}
if (!preg_match("#\n".$token."#",$archivepage[$lastdate_formated]))
$archivepage[$lastdate_formated] = str_replace($token, "\n".$token, $archivepage[$lastdate_formated]);
if (!preg_match("#".$token."\n#",$archivepage[$lastdate_formated]))
$archivepage[$lastdate_formated] = str_replace($token, $token."\n", $archivepage[$lastdate_formated]);
## INIT ARCHIVE PAGE WITH TOKEN - End
}
// on archive la requête $key
$contentpage = str_replace($sect1[$key]['wikititle'].$sect1[$key]['content'], '', $contentpage);
$archivepage[$lastdate_formated] = str_replace($token, $sect1[$key]['wikititle'].$sect1[$key]['content']."\n".$token, $archivepage[$lastdate_formated]);
$archivedrequests['total']++;
$archivedrequests[$lastdate_formated]++;
# Test (Attention certaines requetes sont supprimées mais non archivées !)
echo $lastdate_formated."<->".$sect1[$key]['title']."\n";
}
}
}
foreach ($archivepage as $key => $value)
$archivepage[$key] = str_replace($token, '', $archivepage[$key]);
}
else echo "Warning : section $title not found.\n";
}
// Retrait des doubles espaces dans la page d'archive
foreach ($archivepage as $key => $value)
$archivepage[$key] = preg_replace("#\n\n\n#", "\n\n", $archivepage[$key]);
// Substitution de la page
$difflen -= strlen($contentpage); // diff de longueur (en bytes) de la page
if ( $archivedrequests['total']>2 || ($difflen>4000) )
{ // on n'archive que si on a au moins deux requêtes (ou si la page perd plus de 5000 bytes)
$archivesummary = ($archivedrequests['total']>1) ? $archivedrequests['total'] . " requests archived" : "1 request archived";
$site->initPage( $contentpagename )->edit( $contentpage, $archivesummary );
foreach ($archivepage as $key => $value) {
sleep(5);
$archivesummary = ($archivedrequests[$key]>1) ? $archivedrequests[$key] . " requests archived" : "1 request archived";
$site->initPage( $archivepagename.$key )->edit( $value, $archivesummary );
}
echo $archivedrequests['total'] . " request(s) archived with success !\n\n";
sleep(20);
}
else echo "No edit(".$archivedrequests['total']." request(s) - $difflen bytes)\n\n";
}
/* ************************************************************************
************************ Extension temp_sysop ****************************
************************************************************************ */
function archiveprocess_tempsysop ($contentpagename, $archivepagename, $subsections, $nbdaysexec, $matchregx=NULL, $lvlsect=2)
{
global $site;
echo "Working on [[$contentpagename]] (temp sysop requests) ...\n";
$contentpage = $site->initPage( $contentpagename )->get_text( $force = true );
$archivepage = $site->initPage( $archivepagename )->get_text( $force = true );
$difflen = strlen($contentpage);
$archivedrequests = 0;
// Pour monter jusqu'au niveau de section $lvlsect, s'il est strict. supérieur à deux, on prend seulement le contenu de la première section de titre niveau 2, etc.
for ($i=2; $i<=$lvlsect; $i++) {
if ($i==2) $sub = get_content_by_section ($contentpage, 2);
else $sub = get_content_by_section ($sub[1]['content'], $i);
}
unset($sub[0]);
foreach ($subsections as $nsub => $title)
{
$id = NULL;
foreach ($sub as $key2=>$temp) {
if ($sub[$key2]['title']==$title) $id = $key2;
}
if ($id != NULL)
{
echo "-> ".$sub[$id]['title']."\n";
$motif = "== *([^=^ ][^=^\n]*[^=^ ]) *==( *\n|$)"; // + "^\n", + " *" (2 lines), changing place of "\n" on $actumotif - " *\n" replaced by "( *\n|$)"
$actumotif = "\n== *(" .preg_quote_magic($sub[$id]['title']). ") *== *";
// No need to archive page with tooken - especially because this page does not have the same format
$sect1 = get_content_by_section ($sub[$id]['content'], ($lvlsect+1));
unset($sect1[0]);
$archivesect = get_content_by_section ($archivepage, 2);
foreach ($archivesect as $key => $value)
{
$expiredate = get_expiring_date($archivesect[$key]['title']);
$archivesect[$key]['date'] = $expiredate['y'].zerofill($expiredate['m'],2).zerofill($expiredate['d'],2);
}
// Sort by date BEGIN
$archivedsect_date=array();
foreach ($archivedsect as $value2)
$archivedsect_date[]=$value2['date'];
array_multisort($archivedsect_date,SORT_NUMERIC,$archivedsect);
// Sort by date END
foreach ($sect1 as $key => $value)
{
if ((empty($matchregx) OR preg_match($matchregx, $sect1[$key]['content'], $matches)) AND !preg_match('#'.preg_quote_magic(DONTARCHIVESECT).'#i', $sect1[$key]['content']))
{ // 20111011 - Adding a condition that will prevent requests containing the DONTARCHIVESECT template to be archived
$lastdate = get_last_date($sect1[$key]['content']);
if (!empty($lastdate) AND is_last_x_days($lastdate,$nbdaysexec))
{
// get template data :
$template_data['delay'] = $matches[1];
$template_data['year'] = $matches[2];
$template_data['month'] = $matches[3];
$template_data['day'] = $matches[4];
$datefoo = $template_data['year'].$template_data['month'].$template_data['day'];
$token = numtoken(100);
$title = '== Expiring: '.number_format($template_data['day']).' '.FooMonth($template_data['month']).' '.$template_data['year'].' ==';
$enddate = 1; // la date située juste après notre section
foreach ($archivesect as $key2 => $value2)
{
if ($archivesect[$key2]['date'] == $datefoo)
$enddate=0;
elseif ($archivesect[$key2]['date'] > $datefoo) {
if ($enddate) { // si la section n'existe pas encore
$archivepage = str_replace ($archivesect[$key2]['wikititle'].$archivesect[$key2]['content'], $title."\n".$token."\n".$archivesect[$key2]['wikititle'].$archivesect[$key2]['content'], $archivepage);
} else { // si la section existe déjà, on ne la crée pas
$archivepage = str_replace ($archivesect[$key2]['wikititle'].$archivesect[$key2]['content'], $token."\n".$archivesect[$key2]['wikititle'].$archivesect[$key2]['content'], $archivepage);
}
$enddate=0;
break;
}
}
if ($enddate)
{ // Cas où on met la requête à la fin de la liste
$archivepage .= "\n\n".$title."\n".$token;
}
// on archive la requête $key
$contentpage = str_replace($sect1[$key]['wikititle'].$sect1[$key]['content'], '', $contentpage);
$archivepage = str_replace($token, $sect1[$key]['wikititle'].$sect1[$key]['content'], $archivepage);
$archivedrequests++;
# Test (Attention certaines requetes sont supprimées mais non archivées !)
echo "->".$sect1[$key]['title']."\n";
// Add it in the array $archivesect (avoids problems if we archive two temp requests that expire the same day)
$archivesect[] = array ('wikititle'=>$sect1[$key]['wikititle'], 'title'=>$sect1[$key]['title'], 'content'=>$sect1[$key]['content'], 'date'=>$datefoo);
// Sort by date BEGIN
$archivedsect_date=array();
foreach ($archivedsect as $value2)
$archivedsect_date[]=$value2['date'];
array_multisort($archivedsect_date,SORT_NUMERIC,$archivedsect);
// Sort by date END
}
}
}
}
else echo "Warning : section $title not found.\n";
}
// Retrait des doubles espaces dans la page d'archive
$archivepage = preg_replace("#\n\n\n#", "\n\n", $archivepage);
// Substitution de la page
$difflen -= strlen($contentpage); // diff de longueur (en bytes) de la page
if ( $archivedrequests>0 )
{ // pas de condition d'archivage pour les temp requests
$archivesummary = ($archivedrequests>1) ? $archivedrequests . " requests moved to [[Steward requests/Permissions/Approved temporary]]" : "1 request moved to [[Steward requests/Permissions/Approved temporary]]";
$site->initPage( $contentpagename )->edit( $contentpage, $archivesummary );
sleep(5);
$archivesummary = ($archivedrequests>1) ? $archivedrequests . " temp sysop requests archived" : "1 temp sysop request archived";
$site->initPage( $archivepagename )->edit( $archivepage, $archivesummary );
echo $archivedrequests . " request(s) archived with success !\n\n";
sleep(20);
}
else echo "No edit(".$archivedrequests['total']." request(s) - $difflen bytes)\n\n";
}
/* ************************************************************************
************************ Extension approved_temp ****************************
************************************************************************ */
function archiveprocess_approvedtemp ($contentpagename, $archivepagename, $archive_subsection, $nbdaysexec, $matchregx=NULL, $lvlsect=2)
{
global $site;
echo "Working on [[$contentpagename]]...\n";
$contentpage = $site->initPage( $contentpagename )->get_text( $force = true );
$difflen = strlen($contentpage);
$archivepage = array();
$archivedrequests = array('total'=>0);
$token = numtoken(100);
$motif = "== *([^=^ ][^=^\n]*[^=^ ]) *==( *\n|$)"; // + "^\n", + " *" (2 lines), changing place of "\n" on $actumotif - " *\n" replaced by "( *\n|$)"
$actumotif = "\n== *(" .preg_quote_magic($archive_subsection). ") *== *";
$expiredates = get_content_by_section ($contentpage, 2);
unset($expiredates[0]);
foreach ($expiredates as $ndate => $contentdate)
{
$sect1 = get_content_by_section ($expiredates[$ndate]['content'], ($lvlsect+1));
unset($expiredates[0]);
$nbarchivedinsect1 = 0; // nb de requêtes archivées dans la section sect1
foreach ($sect1 as $key => $value)
{
if ((empty($matchregx) OR preg_match($matchregx, $sect1[$key]['content'])) AND !preg_match('#'.preg_quote_magic(DONTARCHIVESECT).'#i', $sect1[$key]['content']))
{ // 20111011 - Adding a condition that will prevent requests containing the DONTARCHIVESECT template to be archived
$lastdate = get_last_date($sect1[$key]['content']);
if (!empty($lastdate) AND is_last_x_days($lastdate,$nbdaysexec))
{
$lastdate_formated = $lastdate['y'].'-'.str_pad($lastdate['m'], 2, 0, STR_PAD_LEFT);
if (empty($archivepage[$lastdate_formated]))
{ // si on a pas encore eu besoin de cette page d'archive, on l'initialise
$archivepage[$lastdate_formated] = $site->initPage( $archivepagename.$lastdate_formated )->get_text( $force = true );
$archivedrequests[$lastdate_formated] = 0;
## INIT ARCHIVE PAGE WITH TOKEN - Begin
if (preg_match("#".$actumotif.".*(".$motif.")#sU", $archivepage[$lastdate_formated], $matches))
$archivepage[$lastdate_formated] = str_replace ($matches[2], $token.$matches[2], $archivepage[$lastdate_formated]);
else // le problème d'archivage *venait* d'ici
{
if (!preg_match("#".$actumotif."#",$archivepage[$lastdate_formated])) {
// var_dump($archivepage[$lastdate_formated]);
exit('Script Aborted : Error in ARCHIVE PAGE WITH TOKEN (2) - unable to find '.$actumotif.' in '.$lastdate_formated);
} else {
$archivepage[$lastdate_formated] .= $token;
//echo "Attention ! (<2>)\n";
}
}
if (!preg_match("#\n".$token."#",$archivepage[$lastdate_formated]))
$archivepage[$lastdate_formated] = str_replace($token, "\n".$token, $archivepage[$lastdate_formated]);
if (!preg_match("#".$token."\n#",$archivepage[$lastdate_formated]))
$archivepage[$lastdate_formated] = str_replace($token, $token."\n", $archivepage[$lastdate_formated]);
## INIT ARCHIVE PAGE WITH TOKEN - End
}
// on archive la requête $key
$nbarchivedinsect1++;
$contentpage = str_replace($sect1[$key]['wikititle'].$sect1[$key]['content'], '', $contentpage);
// important : retirer le titre niveau 2 (date d'expiration) si c'est la seule / la dernière requête archivée pour ce jour
if ((count($sect1)-1)==$nbarchivedinsect1)
$contentpage = str_replace($expiredates[$ndate]['wikititle'], '', $contentpage);
$archivepage[$lastdate_formated] = str_replace($token, $sect1[$key]['wikititle'].$sect1[$key]['content']."\n".$token, $archivepage[$lastdate_formated]);
$archivedrequests['total']++;
$archivedrequests[$lastdate_formated]++;
# Test (Attention certaines requetes sont supprimées mais non archivées !)
echo $lastdate_formated."<->".$sect1[$key]['title']."\n";
}
}
}
}
// Retrait des doubles espaces dans la page d'archive
foreach ($archivepage as $key => $value)
{
$archivepage[$key] = preg_replace("#(== *" .preg_quote_magic($archive_subsection). " *== *)\n\n#", "$1\n", $archivepage[$key]); // + (pour faire joli, retrait des espaces indésirables)
$archivepage[$key] = str_replace($token, "\n", $archivepage[$key]); // + (idem)
$archivepage[$key] = preg_replace("#\n\n\n#", "\n\n", $archivepage[$key]);
}
// Substitution de la page
$difflen -= strlen($contentpage); // diff de longueur (en bytes) de la page
if ( $archivedrequests['total']>2 || ($difflen>4000) ) // + lastdate <---------- todo !
{ // on n'archive que si on a au moins deux requêtes (ou si la page perd plus de 5000 bytes)
$archivesummary = ($archivedrequests['total']>1) ? $archivedrequests['total'] . " requests archived" : "1 request archived";
$site->initPage( $contentpagename )->edit( $contentpage, $archivesummary );
foreach ($archivepage as $key => $value) {
sleep(5);
$archivesummary = ($archivedrequests[$key]>1) ? $archivedrequests[$key] . " requests archived" : "1 request archived";
$site->initPage( $archivepagename.$key )->edit( $value, $archivesummary );
}
echo $archivedrequests['total'] . " request(s) archived with success !\n\n";
sleep(20);
}
else echo "No edit(".$archivedrequests['total']." request(s) - $difflen bytes)\n\n";
}
/* ************************************************************************
************************* Corps de programme ******************************
************************************************************************ */
/* done 2011-04-23 */
$prefix = '/data/project/sbot/meta/script_cabot/';
foreach ($settings_archives as $arcpagetitle => $arcpageset)
{
if (!empty($arcpagetitle) && !empty($arcpageset['subpage']) && !empty($arcpageset['subsections']) && is_int($arcpageset['nbdays']))
{
$arcpageset['match'] = (empty($arcpageset['match'])) ? 2 : $arcpageset['match'];
$arcpageset['lvlsect'] = (empty($arcpageset['lvlsect'])) ? 2 : $arcpageset['lvlsect'];
if ($arcpageset['nbdays']>0)
if ($arcpagetitle==$settings_archives_tempsysop['page'])
{
// archives temp sysop requests first
archiveprocess_tempsysop ($settings_archives_tempsysop['page'], $settings_archives_tempsysop['page'].'/'.$settings_archives_tempsysop['suffix'], $settings_archives_tempsysop['subsections'], $arcpageset['nbdays'], $settings_archives_tempsysop['match'], $arcpageset['lvlsect']);
archiveprocess_approvedtemp ($settings_archives_approvedtemp['page'], $settings_archives_approvedtemp['arc_subpage'], $settings_archives_approvedtemp['arc_subsection'], $settings_archives_approvedtemp['nbdays'], $settings_archives_approvedtemp['match'], $arcpageset['lvlsect']);
}
archiveprocess ($arcpagetitle, $arcpageset['subpage'], $arcpageset['subsections'], $arcpageset['nbdays'], $arcpageset['match'], $arcpageset['lvlsect']);
}
else exit('Error : please check settings definition.');
}
?>