Mercurial > hg > pnewss
comparison dbreindex.php @ 6:bc6045ed0b2e
Added script to fix and re-index database fields and references
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 12 Apr 2011 11:41:35 +0200 |
parents | |
children | 01dc7eeaf5df |
comparison
equal
deleted
inserted
replaced
5:5d62af5270dd | 6:bc6045ed0b2e |
---|---|
1 #!/usr/bin/php | |
2 <?php | |
3 chdir(__DIR__); | |
4 require_once './common.php'; | |
5 | |
6 $lastposted = NULL; | |
7 while (TRUE) { | |
8 if ($lastposted === NULL) { | |
9 $articles = $db->evalAllAssoc('SELECT * FROM `messages` LIMIT 10'); | |
10 } else { | |
11 $articles = $db->evalAllAssoc('SELECT * FROM `messages` WHERE `id` > ? LIMIT 10', $lastposted); | |
12 } | |
13 if (!count($articles)) break; | |
14 foreach ($articles as $article) { | |
15 $headers = array(); | |
16 $header = array(); | |
17 $headerchanged = FALSE; | |
18 foreach (explode("\r\n", $article['header']) as $line) { | |
19 if (!strlen($line) || $line == '.') { | |
20 print("Article $article[id] Contains empty or terminating header line\n"); | |
21 continue; | |
22 } | |
23 $parts = explode(': ', $line, 2); | |
24 $headername = strtoupper($parts[0]); | |
25 switch ($headername) { | |
26 case 'PATH': case 'FROM': case 'NEWSGROUPS': case 'SUBJECT': case 'DATE': case 'MESSAGE-ID': case 'SENDER': | |
27 if (isset($headers[$headername])) { | |
28 print("Article $article[id] Contains duplicate header $headername, removing.\n"); | |
29 $headerchanged = TRUE; | |
30 break; | |
31 } | |
32 $header[] = $line; | |
33 $headers[strtoupper($parts[0])] = $parts[1]; | |
34 break; | |
35 case 'ORGANIZATION': case 'LINES': | |
36 case 'MIME-VERSION': case 'CONTENT-TYPE': case 'CONTENT-TRANSFER-ENCODING': case 'USER-AGENT': | |
37 case 'REFERENCES': case 'REPLY-TO': case 'SENDER': case 'FOLLOWUP-TO': case 'IN-REPLY-TO': | |
38 case 'EXPIRES': case 'CONTROL': case 'DISTRIBUTION': case 'KEYWORDS': case 'SUMMARY': | |
39 $header[] = $line; | |
40 break; | |
41 case 'NNTP-POSTING-HOST': case 'X-TRACE': case 'XREF': case 'X-COMPLAINTS-TO': | |
42 case 'NNTP-POSTING-DATE': | |
43 print("Article $article[id] Contains unacceptable header $headername\n"); | |
44 $headerchanged = TRUE; | |
45 break; | |
46 default: | |
47 $header[] = $line; | |
48 break; | |
49 } | |
50 } | |
51 foreach (explode("\r\n", $article['body']) as $line) if ($line == '.') print("Article $article[id] Contains terminating body line\n"); | |
52 if (!isset($headers['NEWSGROUPS'])) { | |
53 print("Article $article[id] Missing required Newsgroups header\n"); | |
54 continue; | |
55 } | |
56 $newsgroups = array(); | |
57 foreach (explode(',', $headers['NEWSGROUPS']) as $groupname) { | |
58 $group = $db->evalRowAssoc('SELECT * FROM `groups` WHERE `name` = ?', $groupname); | |
59 if ($group === FALSE) continue; | |
60 $newsgroups[] = $group['id']; | |
61 } | |
62 if (!count($newsgroups)) { | |
63 print("Article $article[id] No known newsgroups listed\n"); | |
64 continue; | |
65 } | |
66 if (!isset($headers['MESSAGE-ID'])) { | |
67 print("Article $article[id] Missing required Message-ID header\n"); | |
68 continue; | |
69 } | |
70 $msgid = $headers['MESSAGE-ID']; | |
71 if (strlen($msgid) <= 2 || $msgid[0] != '<' || $msgid[strlen($msgid)-1] != '>') { | |
72 print("Article $article[id] Malformed Message-ID\n"); | |
73 } else { | |
74 $msgid = substr($msgid, 1, -1); | |
75 if ($msgid != $article['messageid']) { | |
76 print("Article $article[id] Message-ID header does not match database, fixing.\n"); | |
77 $db->update('UPDATE `messages` SET `messageid` = ? WHERE `id` = ?', array($msgid, $article['id'])); | |
78 } | |
79 } | |
80 if ($headerchanged) { | |
81 print("Article $article[id] Updating headers.\n"); | |
82 $db->update('UPDATE `messages` SET `header` = ? WHERE `id` = ?', array(implode("\r\n", $header), $article['id'])); | |
83 } | |
84 foreach ($newsgroups as $groupid) { | |
85 if (FALSE === $db->evalRow('SELECT * FROM `groupmessages` WHERE `group` = ? AND `message` = ?', array($groupid, $article['id']))) { | |
86 print("Article $article[id] Missing link in group $groupid, fixing.\n"); | |
87 $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($groupid, $article['id'])); | |
88 } | |
89 } | |
90 if ($article['id'] > $lastposted) $lastposted = $article['id']; | |
91 } | |
92 } |