f25ee1b7a5cf584a305b1e0292d4fd8f0bbea4e0
[atutor.git] / mods / wiki / doc / AdminTasksSql
1
2 Administrative tasks with SQL
3 -----------------------------
4
5 If you run your Wiki with one of the SQL backends, you can leverage
6 the advanced functions of your database server in some cases. It is
7 not necessary to create a complicated or slow script to clean things
8 up then.
9
10 Suddenly many settings in ewiki pages are encoded into the {meta}
11 field, which isn't accessible from SQL queries. But the most basic
12 page flags always remain accessible, so you can utilize it within
13 queries.
14
15 A few examples for common tasks follow. It is recommended that you
16 always perform a SELECT, before you start an unrecoverable DELETE
17 or UPDATE or something else.
18 Enter the given SQL commands into PhpMySqlAdmin or your commandline
19 'mysql' program, if you have a direct or shell/ssh access to the
20 Web server near your Wikis database. The examples here are wrapped
21 across multiple lines to make them more readable, but you should
22 enter them without any newline in between, and append the ;
23 semicolon always at the end.
24
25
26
27 deleting old binary entries
28 ---------------------------
29
30 If your users upload lots of images and data
31 files, and you want to get rid of them, this
32 recipie may be for you.
33
34    The {flags} field is an integer, with basic page settings encoded
35    bit wise. Of course you can utilize this in SQL with the bit
36    operations that every database supports. You however first need
37    to look up the page flag values in the core script (ewiki.php).
38
39    The EWIKI_DB_F_BINARY flag for example corresponds to the 2 (two,
40    decimal).
41
42    You also have a {lastmodified} field with every page / file entry
43    in your ewiki database, which you can query for. You however need
44    to calculate your wanted time frame into seconds, because all time
45    fields in the ewiki database use Unix timestamps (seconds since
46    01-01-1970 00:00).
47    Once day has 24 hours, 60 minutes, a 60 seconds. So for 120 days
48    you would multiply 24*60*60 with *120 and get 10368000 seconds,
49    which we could use in the SQL query (we however simply embed the
50    formula here):
51
52      SELECT pagename, version
53        FROM ewiki
54       WHERE  (lastmodified + 120*24*60*60 < unix_timestamp())
55            AND (flags & 2)  ;
56
57    And after testing it, to really delete the old binary entries
58    then:
59
60      DELETE
61        FROM ewiki
62       WHERE  (lastmod ...
63            AND (flags ...
64
65
66
67 deleting similarily named but redundant pages
68 ---------------------------------------------
69
70 If some spammer pestered you with almost same-
71 named pages (by using a script or so), you can
72 easily get rid of it.
73
74    Regular expression provide a powerful means to select multiple
75    database entries at once. Allthough you could do the same with
76    the 'ewikictl' utility or a similar of our database tools/
77    collection, the according SQL query may help you too.
78
79    You probably know *.* from old DOS days, Windows UIs or the Linux
80    commandline - regular expressions are almost the same, only that
81    you write ".+" to say "one or more random characters". There are
82    other things like "[0-9]" or "[a-z]" supported in regular
83    expressions, but you should better read a real reference on this;
84    perlre(1) or regex(3) are a good start.
85
86    But just the example, assuming you wanted to delete a bunch of
87    pages called "SsSs..." (with some numbers at the end) from your
88    Wiki database:
89
90      DELETE FROM ewiki
91       WHERE (pagename REGEXP '^SsSs.+')   ;
92
93    Or for PostgreSQL:
94
95      DELETE FROM ewiki
96       WHERE (pagename ~ '^SsSs.+')   ;
97
98
99
100 delete non-existent _BINARY + _DISABLED entries
101 -----------------------------------------------
102
103    Referenced images, that ewiki couldn't find on a remote web server,
104    and therefore didn't cache as internal:// entry remain in the DB
105    as empty entries. You may want to delete them (allthough they don't
106    occupy much space).
107
108      DELETE FROM ewiki
109       WHERE (flags & (2+4))
110           AND (pagename REGEXP 'http://')
111
112
113
114