1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* cleanlitterbox.php - keep a sandbox clean |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright © 2013 geekwright, LLC. All rights reserved. |
6
|
|
|
* @license gwiki/docs/license.txt GNU General Public License (GPL) |
7
|
|
|
* @since 1.0 |
8
|
|
|
* @author Richard Griffith <[email protected]> |
9
|
|
|
* @package gwiki |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/* |
13
|
|
|
This is a script you can adapt to keep a portion of your wiki |
14
|
|
|
clean. It is intended for tidying up a sandbox where people can |
15
|
|
|
practice editing. It will delete all page revisions older than a |
16
|
|
|
certain age specified in hours where the page name matches an |
17
|
|
|
SQL LIKE pattern. |
18
|
|
|
|
19
|
|
|
You must enable this by declaring a sandbox policy by setting |
20
|
|
|
the two variables below: |
21
|
|
|
$keywordpattern pattern to identify sandbox pages (i.e. 'Sandbox:%') |
22
|
|
|
$retainhours is the minimum number of hours to retain (i.e. 24 for one day) |
23
|
|
|
|
24
|
|
|
Move this file from the extras folder, up one level to the main gwiki |
25
|
|
|
folder to execute it. You can call it manually in a web browser or set |
26
|
|
|
it up to be automatically called, for example, by wget in a cron job. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
$keywordpattern = ''; |
30
|
|
|
$retainhours = 0; |
31
|
|
|
$dir = 'gwiki'; |
32
|
|
|
|
33
|
|
|
include __DIR__ . '/../../mainfile.php'; |
34
|
|
|
// if check variable is set, show like a regular module page (with debug if on) |
35
|
|
|
// otherwise, turn off logging and just get busy cleaning |
36
|
|
View Code Duplication |
if (!empty($_REQUEST['check'])) { |
|
|
|
|
37
|
|
|
$GLOBALS['xoopsOption']['template_main'] = 'gwiki_view.tpl'; |
38
|
|
|
include XOOPS_ROOT_PATH . '/header.php'; |
39
|
|
|
do_clean(); |
40
|
|
|
include XOOPS_ROOT_PATH . '/footer.php'; |
41
|
|
|
} else { |
42
|
|
|
$xoopsLogger->activated = false; |
43
|
|
|
do_clean(); |
44
|
|
|
exit; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function do_clean() |
48
|
|
|
{ |
49
|
|
|
global $xoopsDB; |
50
|
|
|
|
51
|
|
|
global $keywordpattern, $retainhours, $dir; |
52
|
|
|
|
53
|
|
|
if ($retainhours <= 0 || $keywordpattern === '') { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$lastmodifiedbefore = time() - ($retainhours * 3600); |
58
|
|
|
|
59
|
|
|
$sql = 'DELETE FROM ' . $xoopsDB->prefix('gwiki_pages') . " WHERE keyword like '{$keywordpattern}' AND lastmodified< $lastmodifiedbefore"; |
60
|
|
|
$result = $xoopsDB->queryF($sql); |
61
|
|
|
$cnt = $xoopsDB->getAffectedRows(); |
62
|
|
View Code Duplication |
if ($cnt > 0) { |
|
|
|
|
63
|
|
|
$sql = 'SELECT image_file FROM ' . $xoopsDB->prefix('gwiki_page_images'); |
64
|
|
|
$sql .= ' WHERE keyword NOT IN (SELECT keyword from ' . $xoopsDB->prefix('gwiki_pages') . ')'; |
65
|
|
|
$result = $xoopsDB->query($sql); |
66
|
|
|
while ($f = $xoopsDB->fetchArray($result)) { |
67
|
|
|
unlink(XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $f['image_file']); |
68
|
|
|
} |
69
|
|
|
$sql = 'DELETE FROM ' . $xoopsDB->prefix('gwiki_page_images'); |
70
|
|
|
$sql .= ' WHERE keyword NOT IN (SELECT keyword from ' . $xoopsDB->prefix('gwiki_pages') . ')'; |
71
|
|
|
$result = $xoopsDB->queryF($sql); |
72
|
|
|
|
73
|
|
|
$sql = 'SELECT file_path FROM ' . $xoopsDB->prefix('gwiki_page_files'); |
74
|
|
|
$sql .= ' WHERE keyword NOT IN (SELECT keyword from ' . $xoopsDB->prefix('gwiki_pages') . ')'; |
75
|
|
|
$result = $xoopsDB->query($sql); |
76
|
|
|
while ($f = $xoopsDB->fetchArray($result)) { |
77
|
|
|
unlink(XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $f['file_path']); |
78
|
|
|
} |
79
|
|
|
$sql = 'DELETE FROM ' . $xoopsDB->prefix('gwiki_page_files'); |
80
|
|
|
$sql .= ' WHERE keyword NOT IN (SELECT keyword from ' . $xoopsDB->prefix('gwiki_pages') . ')'; |
81
|
|
|
$result = $xoopsDB->queryF($sql); |
82
|
|
|
|
83
|
|
|
$sql = 'DELETE FROM ' . $xoopsDB->prefix('gwiki_pageids') . ' WHERE keyword NOT IN (SELECT keyword from ' . $xoopsDB->prefix('gwiki_pages') . ')'; |
84
|
|
|
$result = $xoopsDB->queryF($sql); |
85
|
|
|
$sql = 'OPTIMIZE TABLE ' . $xoopsDB->prefix('gwiki_pages'); |
86
|
|
|
$result = $xoopsDB->queryF($sql); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.