Skip to content

Commit

Permalink
Re-implement main SQLite query after some recent WhatsApp update
Browse files Browse the repository at this point in the history
  • Loading branch information
fwiep committed Jan 4, 2023
1 parent c0f05a0 commit 775ad28
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
5 changes: 2 additions & 3 deletions README.md
Expand Up @@ -36,7 +36,7 @@ In your terminal or console session, call the export script:
```plain
$ ./wa-chat-export.php --help
WA Chat Export v0.1
WA Chat Export v0.2
Chat-export tool for unencrypted WhatsApp databases
Usage: ./wa-chat-export.php OPTIONS
Expand Down Expand Up @@ -71,7 +71,7 @@ After a while, the script finishes and reports the number of exported chats.
A possible single chat export might look like this:

```plain
; WA Chat Export v0.1
; WA Chat Export v0.2
; Creates a readable export of WhatsApp's chat history
;
; Export started: 2022-02-03 17:19:08 +01:00
Expand Down Expand Up @@ -121,4 +121,3 @@ them a visit and enjoy their work!
[3]: https://git-scm.com/downloads
[4]: https://twrp.me/
[5]: https://github.com/B16f00t/whapa
[6]: https://github.com/ElDavoo/WhatsApp-Crypt14-Decrypter
51 changes: 31 additions & 20 deletions src/ChatExporter.php
Expand Up @@ -98,13 +98,12 @@ public function __construct(
// Add nameless contacts to the address book, to export their messages, too
$stmt = $db->prepare(
"SELECT
DISTINCT `key_remote_jid`
DISTINCT `user` || '@' || `server` AS `key_remote_jid`
FROM
`messages`
WHERE
`status` != 6
`jid`
ORDER BY
`key_remote_jid` ASC"
`user` ASC,
`server` ASC"
);
if ($res = $stmt->execute()) {
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
Expand All @@ -123,21 +122,33 @@ public function __construct(
foreach ($this->_addressBook as $jID => $contact) {
$stmt = $db->prepare(
"SELECT
`key_remote_jid`,
`key_from_me`,
`data`,
`remote_resource`,
`media_caption`,
`forwarded`,
`timestamp`
`J`.`user` || '@' || `J`.`server` AS `key_remote_jid`,
`M`.`from_me` AS `key_from_me`,
`M`.`text_data` AS `data`,
`JG`.`user` || '@' || `JG`.`server` AS `remote_resource`,
`MM`.`media_caption`,
CASE
WHEN `MF`.`forward_score` > 0 THEN 1 ELSE 0
END AS `forwarded`,
`M`.`timestamp`
FROM
`messages`
`message` AS `M`
JOIN `chat` AS `C`
ON `M`.`chat_row_id` = `C`.`_id`
JOIN `jid` AS `J`
ON `C`.`jid_row_id` = `J`.`_id`
LEFT JOIN `jid` AS `JG`
ON `M`.`sender_jid_row_id` = `JG`.`_id`
LEFT JOIN `message_forwarded` AS `MF`
ON `M`.`_id` = `MF`.`message_row_id`
LEFT JOIN `message_media` AS `MM`
ON `M`.`_id` = `MM`.`message_row_id`
WHERE
`key_remote_jid` == :jid
AND `status` != 6
`key_remote_jid` == :jid
AND `M`.`status` != 6
ORDER BY
`key_remote_jid` ASC,
`timestamp` ASC"
`key_remote_jid` ASC,
`M`.`timestamp` ASC"
);
$stmt->bindValue(':jid', $jID, SQLITE3_TEXT);
if ($res = $stmt->execute()) {
Expand All @@ -147,7 +158,7 @@ public function __construct(
->setFromMe($row['key_from_me'] == 1)
->setForwarded($row['forwarded'] == 1)
->setInGroup(
(strpos($row['key_remote_jid'], '-') !== false)
(strpos($row['key_remote_jid'], '@g.us') !== false)
);
$remoteJid = $row['key_remote_jid'];
$groupJid = null;
Expand Down Expand Up @@ -194,7 +205,7 @@ public function exportAllChats() : int
if ($contact !== $this->_me) {
$o = $this->exportSingleChat($jID);
if (!empty($o)) {
$ok =file_put_contents(
$ok = file_put_contents(
$this->_outputDirecory.DIRECTORY_SEPARATOR.$jID.'.txt',
$o
);
Expand Down Expand Up @@ -412,4 +423,4 @@ protected function addMessage(Contact $c, Message $m) : self
* @var string
*/
private $_outputDirecory;
}
}
4 changes: 2 additions & 2 deletions wa-chat-export.php
Expand Up @@ -15,7 +15,7 @@
use FWieP\RuntimeData as RD;
require __DIR__.'/vendor/autoload.php';

define('PROG_VERSION', '0.1');
define('PROG_VERSION', '0.2');
$validSQLite3mimes = ['application/x-sqlite3', 'application/vnd.sqlite3'];
$wacDBfile = null;
$msgDBfile = null;
Expand Down Expand Up @@ -155,4 +155,4 @@
$ce->exportAllChats(),
PHP_EOL
);
exit(0);
exit(0);

0 comments on commit 775ad28

Please sign in to comment.