forked from djplaner/moodle-booktool_github
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locallib.php
1076 lines (852 loc) · 36 KB
/
locallib.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Book github export lib
*
* @package booktool_github
* @copyright 2015 David Jones {@link http://djone.es}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once(dirname(__FILE__).'/lib.php');
require_once(__DIR__ . '/../../locallib.php');
require_once( __DIR__ . '/local/client/client/GitHubClient.php' );
const GITHUB_TOKEN_NAME = "github_token";
/***************************************************
* github client specific calls
*/
/**
* ( $githubclient, $githubuser ) = booktool_github_get_client( $id )
* - get OAuth connection with github
* - create a github client object to communicate with github
* - get details of github user
*/
function booktool_github_get_client( $id ) {
$attempts = 0;
// GET_TOKEN:!
$oauthtoken = booktool_github_get_oauth_token( $id);
$attempts++;
if ( $oauthtoken ) {
$client = new GitHubClient();
$client->setAuthType( 'Oauth' );
$client->setOauthToken( $oauthtoken );
// Replace this with get user details.
try {
$user = $client->users->getTheAuthenticatedUser();
} catch ( Exception $e ) {
// Oops problem, probably a 401, try and fix.
$msg = $e->getMessage();
preg_match( '/.*actual status \[([^ ]*)\].*/', $msg, $matches );
if ( $attempts > 2 ) {
print "<h1> looks like I'm in a loop ";
// Need to handle this, is this a failure?
return [false, false];
} else if ( $matches[1] == 401 ) {
// SHOULD DELETE token from session.
unset( $_SESSION['github_token'] );
// Goto GET_TOKEN;!
}
}
return [$client, $user];
}
}
/**
* does a given repo exist
*/
function booktool_github_repo_exists( $githubclient, $repodetails ) {
$data = [];
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'];
try {
$response = $githubclient->request($request, 'GET', $data, 200,
'GitHubFullRepo');
} catch ( Exception $e ) {
return false;
}
return true;
}
/**
* does a file exist within a repo
**/
function booktool_github_path_exists( $githubclient, $repodetails ) {
// $githubclient->setDebug( true );
$data = [];
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'] .
"/contents/" . rawurlencode( $repodetails['path'] );
// print "<h3> does it exist? request is $request </h3>";
try {
$response = $githubclient->request($request, 'GET', $data, 200,
'GitHubReadmeContent');
} catch ( Exception $e ) {
return false;
}
return true;
}
/**
* create an new empty file
* - return true if worked
*/
function booktool_github_create_new_file( $githubclient, $repodetails, $content='' ) {
// $githubclient->setDebug( true );
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'] .
"/contents/" . rawurlencode( $repodetails['path'] );
$data = [];
$data['message'] = 'Creating new file - Moodle book github tool';
$data['content'] = base64_encode( $content );
try {
$response = $githubclient->request($request, 'PUT', $data, 201,
'GitHubReadmeContent');
} catch ( Exception $e ) {
return false;
}
return true;
}
/**
* ( repo, path ) = get_repo_details( $bookid );
* - return an array of basic information about the connection
* that is required by the github_client
* - repo is the name of the github repo
* - path is the full path for the file from the report connected with book
* - return false if can't get the information
**/
function booktool_github_get_repo_details( $bookid ) {
global $DB;
// Repo and path are contained in database table github_connections.
$result = $DB->get_record( 'booktool_github_connections',
['bookid' => $bookid] );
// If no data, then just return false.
if ( ! $result ) {
return false;
}
return ['connection_id' => $result->id,
'bookid' => $result->bookid,
'repo' => $result->repository,
'path' => $result->path,
'pushedtime' => $result->pushedtime,
'pushedrevision' => $result->pushedrevision ];
}
/**
* bool = put_repo_details( $repodetails )
* - either insert or update repo details in the database
* - dependent on whether repo_details has an id
*/
function booktool_github_put_repo_details( $repodetails ) {
global $DB;
// Make sure all the required values available.
$checks = ['pushedrevision', 'pushedtime'];
foreach ($checks as $check) {
if ( ! array_key_exists( $check, $repodetails )) {
$repodetails[$check] = '';
}
}
$record = new StdClass();
$record->bookid = $repodetails['bookid'];
$record->repository = $repodetails['repo'];
$record->path = $repodetails['path'];
$record->pushedrevision = $repodetails['pushedrevision'];
$record->pushedtime = $repodetails['pushedtime'];
if ( array_key_exists( 'connection_id', $repodetails ) &&
$repodetails['connection_id'] > 0) {
// Update an existing entry if no id or id is 0.
// I.e. the form was empty.
$record->id = $repodetails['connection_id'];
$DB->update_record( 'booktool_github_connections', $record );
return true;
}
// Insert a new entry.
return $DB->insert_record( 'booktool_github_connections', $record );
}
/**
* $commits = getCommits( );
* - return an array of GitHubCommit objects
*/
function booktool_github_get_commits( $githubclient, $repodetails) {
try {
$commits = $githubclient->repos->commits->listCommitsOnRepository(
$repodetails['owner'], $repodetails['repo'], null,
$repodetails['path'] );
} catch ( Exception $e ) {
$msg = $e->getMessage();
echo '<xmp>Caught exception ' . $msg . "</xmp>";
return false;
}
return $commits;
}
/**
* bool = booktool_github_change_in_form( $repodetails, $form )
* - return TRUE/FALSE depending on whether there have been changes
* made in the form
**/
function booktool_github_change_in_form( $repodetails, $form ) {
// Is there anything in the database?
// If ( array_key_exists( 'repo', $repodetails ) &&!
// Array_key_exists( 'path', $repodetails ) ) {!
// Print "<h1> the repo exists </h1>";!
// Has the form changed from defaults?
$repodefault = get_string( 'repo_form_default', 'booktool_github');
$pathdefault = get_string( 'repo_path_default', 'booktool_github');
if ( strcmp( $form->repo, $repodefault ) !== 0 &&
strcmp( $form->path, $pathdefault ) !== 0 ) {
// Print "<h1> path is different </h1>";!
// Has the form data changed from content of the database?
if ( strcmp( $form->repo, $repodetails['repo']) !== 0 &&
strcmp( $form->path, $repodetails['path']) !== 0 ) {
// Print "<h1> form data changed </h1>";!
return true;
}
}
return false;
}
/***************************************************
* Views
***************************************************/
/****************************
* Display summary of what we know about details of repo
* STATUS
* - Basic prototype version. Almost hard coded.
* - Not really querying data from github
*/
function booktool_github_view_repo_details( $repodetails, $githubuser) {
$repourl = "https://github.com/" . $repodetails['owner'] . "/" .
$repodetails['repo'];
$pathurl = $repourl . "/blob/master/" . $repodetails['path'];
// Should I remove double / in path_url (but not from http://)!
$ownerurl = "https://github.com/" . $repodetails['owner'];
$string = '';
$table = new html_table();
$table->head = ['', '' ];
$table->data[] = ['Repository', '<a href="' . $repourl . '">' .
$repodetails['repo'] . '</a>' ];
$table->data[] = ['File', '<a href="' . $pathurl . '">' .
$repodetails['path'] . '</a>' ];
// Show information about current user.
$avatarurl = $githubuser->getAvatarUrl();
$name = $githubuser->getName();
$username = $githubuser->getLogin();
$userurl = $githubuser->getHtmlUrl();
$userhtml = html_writer::start_div( "githubuser" );
$userhtml .= html_writer::link( $userurl,
$name . '<br /> (' . $username .
') ' );
$userhtml .= html_writer::empty_tag( 'img', [
'src' => $avatarurl,
'alt' => 'Avatar for ' . $name,
'height' => 20, 'width' => 20 ] );
$userhtml .= html_writer::end_div();
$table->data[] = ['User', $userhtml ];
$string .= html_writer::table( $table );
return $string;
}
/***************************************************
* Given book id, github client and repo details
* display a range of status and historical information about the
* book and its connection to github
*/
function booktool_github_view_status( $cmid, $githubclient, $repodetails, $urls ) {
// If there are repo details show the commit information.
if ( array_key_exists( 'repo', $repodetails ) ) {
$commits = booktool_github_get_commits( $githubclient, $repodetails);
// Need to set default value.
$pushedrevision = $repodetails['pushedrevision'];
$pushedtime = $repodetails['pushedtime'];
$bookrevision = booktool_github_get_book_revision( $repodetails );
$lastgittime = booktool_github_get_last_gittime( $commits );
/*print "<h3>repo</h3><xmp>"; var_dump($repodetails); print "</xmp>";
print "<h3> Status situation </h3> ";
print "<ul> <li> pushed_revision $pushedrevision versus book_revision $bookrevision </li>
<li> pushed_time $pushedtime versus git_time $lastgittime</li> </ul>"; */
// Space to add push/pull etc.
booktool_github_show_push_pull( $cmid, $pushedrevision, $pushedtime, $bookrevision, $lastgittime, $urls );
if ( ! $commits ) {
// Fix this up.
print get_string('form_no_commits', 'booktool_github', $urls);
} else {
print get_string('form_history', 'booktool_github', $urls);
$string = booktool_github_view_commits( $commits );
echo $string;
}
}
}
/***************************************************
* booktool_github_get_book_revision( $repodetails)
* - return the revision number for the Moodle book
*/
function booktool_github_get_book_revision( $repodetails) {
global $DB;
$result = $DB->get_record( 'book', ['id' => $repodetails['bookid']] );
if ( ! $result ) {
return 0;
} else {
return $result->revision;
}
}
/***************************************************
* booktool_github_get_last_gittime( $commits )
*/
function booktool_github_get_last_gittime( $commits ) {
$commit = $commits[0]->getCommit();
$authordetails = $commit->getAuthor();
$date = $authordetails->getDate();
// Error checking.
return strtotime( $date );
}
/***************************************************
* - Given a a GitHubCommit object display info about each commit
* - Currently a table where each row matches a commit and shows
* - Date changed - not showing it yet
* - Message for the commit & link to more information
* - Who made the commit
*
* TO DO:
* - what happens if DateTime errors?
* - clean up the mish-mash of html-write and html
* a renderer or template would be better
*/
function booktool_github_view_commits( $commits ) {
$string = '';
$table = new html_table();
$table->head = ['Date changed', 'Details', 'Committer'];
// Message for link to commit details.
$detailslink = get_string( 'commit_details', 'booktool_github' );
$detailslink = '<span style="font-size:small">[ ' . $detailslink .
' ] </span>';
// Each row is based on a single commit to the file.
foreach ($commits as $commit) {
// Return GitHubCommitCommit object.
// Date of commit.
$commitdetails = $commit->getCommit();
$messagetext = $commitdetails->getMessage();
$htmlurl = $commit->getHtmlUrl();
$message = html_writer::link( $htmlurl, $detailslink );
$message = '<div class="commit_message"> ' . $messagetext . '</div>' .
$message;
// Author has the full name and date.
$authordetails = $commitdetails->getAuthor();
$authorname = $authordetails->getName();
$datecommit = $authordetails->getDate();
$date = new DateTime( $datecommit );
$datedisplay = $date->format( 'D, d M Y H:i:s' );
// Return GitHubUser object.
// Get the avatar, username and html url for user.
$committerdetails = $commit->getCommitter();
$username = $committerdetails->getLogin();
$avatarurl = $committerdetails->getAvatarUrl();
$userurl = $committerdetails->getHtmlUrl();
$committer = html_writer::start_div( "committer" );
$image = html_writer::empty_tag( 'img', [
'src' => $avatarurl,
'alt' => 'Avatar for ' . $username,
'height' => 20, 'width' => 20 ] );
$committer .= html_writer::link( $userurl,
$authorname . ' ' .
$image . '<br /> (' .
$username . ')' );
$committer .= html_writer::end_div();
$row = [ $datedisplay, $message, $committer ];
$table->data[] = $row;
}
$string .= html_writer::table( $table );
// Debug stuff.
/* $string .= "<xmp>";
* $string .= print_r($commits, true);
* $string .= "</xmp>";
**/
return $string;
}
/******************************************************************
* booktool_github_show_push_pull( $pushedrevision, $pushedtime, $bookrevision, $lastgittime );
* - figure out whether push pull up to date should be shown
* - BOOK PUSH
* - if pushedrevision is behind current Book revision
# - if pushedtime ahead last big commit
* - GIT PULL
* - if repo_details->timepushed is behind most recent commit
*/
function booktool_github_show_push_pull( $cmid, $pushedrevision, $pushedtime,
$bookrevision, $lastgittime, $urls ) {
$status = '';
$push = false;
$pull = false;
if ( $pushedrevision < $bookrevision ) {
$status .= get_string('book_revision', 'booktool_github');
$push = true;
}
if ( $pushedtime > $lastgittime ) {
$status .= get_string('missing_push', 'booktool_github');
$push = true;
}
if ( $pushedtime < $lastgittime ) {
$status .= get_string('behind_git', 'booktool_github');
$pull = true;
}
if ( ! $push && ! $pull ) {
$status .= get_string('consistent', 'booktool_github');
}
$urls['status'] = $status;
print get_string('form_status', 'booktool_github', $urls);
$pushurl = new moodle_url('/mod/book/tool/github/push.php', ['id' => $cmid]);
$pullurl = new moodle_url('/mod/book/tool/github/pull.php', ['id' => $cmid]);
$arr = ['push_url' => $pushurl->out(), 'pull_url' => $pullurl->out()];
print get_string('form_operations', 'booktool_github', $arr);
}
/**
* $token = booktool_github_get_oauth_token( $url )
* - return the oauth access token from github
* - if there isn't one, get one and then redirect back to $url
* - if one can't be gotten, show why
*/
function booktool_github_get_oauth_token( $id, $url='/mod/book/tool/github/index.php' ) {
if ( array_key_exists( "github_token", $_SESSION ) ) {
return $_SESSION["github_token"];
} else {
print get_string( 'github_redirect', 'booktool_github' );
// Redirect to get_oauth.php include passsing CURRENT_URL.
$url = new moodle_url( '/mod/book/tool/github/get_oauth.php',
[ 'id' => $id, 'url' => $url ]);
redirect( $url );
return false;
}
}
/*
* clientid = booktool_github_get_client_details()
* - return the base64 client id from github for this tool
* TO DO: replace this with a call to the database or other location
*/
function booktool_github_get_client_details() {
global $DB;
$result = $DB->get_record( 'booktool_github', ['id' => 1] );
if ( ! $result ) {
return [ 'clientid' => '',
'clientsecret' => '' ];
} else {
return ['clientid' => $result->clientid,
'clientsecret' => $result->clientsecret ];
}
}
/*
* $content = booktool_github_get_file_content( $githubclient, $repodetails )
* - return the contents of the github file
*/
function booktool_github_get_file_content( $githubclient, $repodetails ) {
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'] .
"/contents/" . rawurlencode( $repodetails['path'] );
$data = [];
$response = [];
try {
$response = $githubclient->request( $request, 'GET', $data, 200, 'GitHubReadmeContent' );
} catch ( Exception $e ) {
return 0;
}
return base64_decode( $response->getContent() );
}
/*****************************************************************
* PUSH | PULL functions
*/
function booktool_github_push_book( $githubclient, $repodetails, $message ) {
global $DB;
// Get the book data.
$book = $DB->get_record( 'book', ['id' => $repodetails['bookid']] );
$select = "bookid=" . $repodetails['bookid'] ." order by pagenum";
$result = $DB->get_records_select( 'book_chapters', $select);
// Generate the content.
$bookcontent = booktool_github_prepare_book_html( $book, $result );
// Do the push??
$data = [];
$data['message'] = $message;
$data['content'] = base64_encode( $bookcontent );
$gitdetails = booktool_github_git_details( $githubclient, $repodetails );
if ( $gitdetails === 0 ) {
print "<h3> Failure</h3>";
return false;
}
$data['sha'] = $gitdetails->getSha();
$data['committer'] = [ 'name' => 'de Souza Webmaster',
'email' => '[email protected]' ];
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'] .
"/contents/" . rawurlencode( $repodetails['path'] );
$data['content'] = base64_encode( $bookcontent );
try {
$response = $githubclient->request($request, 'PUT', $data, 200,
'GitHubReadmeContent');
} catch ( Exception $e ) {
return false;
}
// Need to update the book table.
// Modify pushedrevision to current book revision.
// Set pushedtime to latest git time.
$commits = booktool_github_get_commits( $githubclient, $repodetails);
$lastgittime = booktool_github_get_last_gittime( $commits );
// Print "<h3>FROM repo_details</h3><xmp>";var_dump($repodetails);print "</xmp>";!
$repodetails['pushedtime'] = $lastgittime;
$repodetails['pushedrevision'] = $book->revision;
// Print "<h3>TO repo_details</h3><xmp>";var_dump($repodetails);print "</xmp>";!
return booktool_github_put_repo_details( $repodetails );
}
/*
* return a modified GitHubReadmeContent object with details about the
* latest version of the file
* - add "date" to hold the time the file was last committed
*/
function booktool_github_git_details( $githubclient, $repodetails ) {
$request = "/repos/" . $repodetails['owner'] . "/" . $repodetails['repo'] .
"/contents/" . rawurlencode( $repodetails['path'] );
// Get the basic detail about the file.
$data = [];
$response = [];
try {
$response = $githubclient->request( $request, 'GET', $data, 200, 'GitHubReadmeContent' );
} catch ( Exception $e ) {
// Print "<h3>First failure</h3>";!
return 0;
}
return $response;
}
// Transform the contents of the book into some sort of single HTML string.
// Important information for each chapter is.
// Pagenum, subchapter, title, content, contentformat, hidden.
// Improtant information for the book.
// Name, intro, introformat, customtitles?
function booktool_github_prepare_book_html( $book, $result ) {
global $DB;
$content = "<!DOCTYPE html>\n<html>\n <head><title>" . $book->name. "</title></head>\n<body>\n";
// The book forms an article.
// Title - name of book.
// Data attributes for other database values.
$content .= "\n" . '<article title="' . $book->name .
'" dataintroformat="' . $book->introformat .
'" datacustomtitles="' . $book->customtitles .
'" datanumbering="' . $book->numbering .
'" datanavstyle="' . $book->navstyle . '">' . "\n" .
// Head to contain title and intro.
' <head><h1>' . $book->name . "</h1>\n" .
" <div class= 'intro'>" . $book->intro . "</div>\n" .
" </head>\n";
// Create each chapter as a section within the article.
// Complication: the next chapter may be a sub-chapter, if that's the.
// Case, don't want to close the section tag for the chapter.
$prevchapter = 'none';
$lastchapter = false;
$numitems = count( $result );
$i = 0;
foreach ($result as $chapter) {
// Print "<h3> Chapter $i</h3>";!
// How to set last chapter.
$lastchapter = ( ++$i === $numitems );
// Print "<p>LastChapter: $lastchapter </p>";!
// Add the appropriate HTML and set the value for last chapter.
$content = generate_chapter_html( $content, $chapter,
$prevchapter, $lastchapter );
$prevchapter = $chapter->subchapter;
}
$content .= "</body>\n</html>";
return $content;
}
/******************************************************************
* generate_chapter_html( $content, $chapter, $prevchapter, $lastchapter )
* - add HTML to $content depending on the data in $chapter and
* what the $lastchapter was
* - return new value for $lastchapter
*/
function generate_chapter_html( $content, $chapter, $prevchapter, $lastchapter ) {
// <section BODY is always needed
$body = ' <section title="' . $chapter->title .
'" datasubchapter="' . $chapter->subchapter .
'" datapagenum="' . $chapter->pagenum .
'" datacontentformat="' . $chapter->contentformat .
'" datahidden="' . $chapter->hidden . '">' . "\n" .
' <head><h1>' . $chapter->title . '</h1></head>'. "\n" .
' <div>' . $chapter->content . '</div>' . "\n";
// First chapter.
if ( $prevchapter === 'none' ) {
// print "<p>FIRST CHAPTER.... sub is " . $chapter->subchapter;
if ( $chapter->subchapter == 0 ) {
// Section BODY.
$content .= $body;
// print "<p>NOT sub chatper</p>\n";
} else {
// <section EMPTY <section BODY
$content .= ' <section></section>' . "\n " . $body;
// print "<p>sub chatper</p>\n";
}
// In the middle chapters.
} else if ( ! $lastchapter ) {
// print "<p>MIDDLE CHAPTER...." . $chapter->subchapter;
if ( $prevchapter == 0 ) {
if ( $chapter->subchapter == 0 ) {
// </section <section BODY
$content .= ' </section>' . "\n " . $body;
// print "<p>NOT sub chatper</p>\n";
} else {
// Section BODY.
$content .= $body;
// print "<p>sub chatper</p>\n";
}
} else {
if ( $chapter->subchapter == 0 ) {
// </section #subc </section #chap <section BODY
$content .= " </section>\n </section>\n " . $body;
// print "<p>NOT sub chatper</p>\n";
} else {
// </section #subc <section BODY
$content .= " </section>\n " . $body;
// print "<p>sub chatper</p>\n";
}
}
// Last chapter.
} else {
// print "<p>LAST CHAPTER...." . $chapter->subchapter;
if ( $prevchapter == 0) {
if ( $chapter->subchapter == 0 ) {
// </section <section BODY </section
$content .= " </section>\n " . $body .
"\n </section>";
// print "<p>NOT sub chatper</p>\n";
} else {
// <section BODY </section #subc </section #chap
$content .= $body . "\n </section>\n </section>";
// print "<p>sub chatper</p>\n";
}
} else {
if ( $chapter->subchapter == 0 ) {
// </section #subc </section #chap <section BODY </section
$content .= " </section>\n </section>\n" . $body .
"\n </section>";
// print "<p>NOT sub chatper</p>\n";
} else {
// </section #subc <section BODY </section #subc </section #chap
$content .= " </section>\n" . $body .
"\n </section>\n </section>";
// print "<p>sub chatper</p>\n";
}
}
}
return $content;
}
/******************************************************************
* book_tool_github_pull_book
*/
function booktool_github_pull_book( $githubclient, $repodetails, $book ) {
global $DB;
// Retrieve the content of the github file.
$content = booktool_github_get_file_content($githubclient, $repodetails);
if ( $content === 0 ) {
return false;
}
// Parse it.
$gitbook = booktool_github_parse_file_content( $content );
if ( $gitbook === false ) {
return false;
}
// Update the book table entry.
booktool_github_update_book_table( $repodetails, $gitbook );
// Remove old book chapters and add the new ones.
$result = $DB->delete_records('book_chapters', ['bookid' => $repodetails['bookid']]);
booktool_github_insert_chapters_table( $repodetails, $gitbook );
// Update the github_connections table.
// Pushedtime should equal lastcommit in git.
// Pushed revision = latest revision from book + 1.
$commits = booktool_github_get_commits( $githubclient, $repodetails);
$lastgittime = booktool_github_get_last_gittime( $commits );
// print "<h3>FROM repo_details</h3><xmp>";var_dump($repodetails);print "</xmp>";
$repodetails['pushedtime'] = $lastgittime;
$repodetails['pushedrevision'] = 1 + $book->revision;
// print "<h3>TO repo_details</h3><xmp>";var_dump($repodetails);print "</xmp>";
return booktool_github_put_repo_details( $repodetails );
}
/*
* Insert chapters from git_book into the chapters table
*/
function booktool_github_insert_chapters_table( $repodetails, $gitbook ) {
global $DB;
$chapters = [];
foreach ($gitbook->chapters as $gitchapter) {
$chapter = (object)$gitchapter;
$chapter->bookid = $repodetails['bookid'];
$chapter->timecreated = time();
$chapter->timemodified = 0;
$chapter->importsrc = '';
array_push( $chapters, $chapter );
}
return $DB->insert_records( 'book_chapters', $chapters, true );
}
/*
* update the book table with data from git
* - intro, name, introformat, customtitles, numbering, navstyle
* changed to git value
* - timemodified - gets updated to now
* - revision - gets incremented
*/
function booktool_github_update_book_table( $repodetails, $gitbook ) {
global $DB;
$updatefields = ['title', 'introformat', 'customtitles',
'numbering', 'navstyle'];
// Get the existing data for the book.
$book = $DB->get_records( "book", ['id' => $repodetails['bookid'] ]);
if ( count( $book ) == 0 ) {
return false;
}
$book = $book[$repodetails['bookid']];
// print "<h3>Changing this</h3><xmp>"; var_dump($book); print "</xmp>";
// print "<h3>gitbook</h3><xmp>"; var_dump($gitbook); print "</xmp>";
// print "<h3>book Name</h3><xmp>"; var_dump($book->name); print "</xmp>";
// print "<h3>gitbook title</h3><xmp>"; var_dump($gitbook->book->title); print "</xmp>";
// Update the right bits from git
// foreach ( $updatefields as $change ) {
// $book->$change = $gitbook->book->$change;
// }
$book->name = $gitbook->book->title;
$book->intro = $gitbook->book->intro;
$book->introformat = $gitbook->book->dataintroformat;
$book->customtitles = $gitbook->book->datacustomtitles;
$book->numbering = $gitbook->book->datanumbering;
$book->navstyle = $gitbook->book->datanavstyle;
// Update locally.
$book->revision++; // Or = 10 + $book->revision!
$book->timemodified = time();
// print "<h3>TO</h3><xmp>"; var_dump($book); print "</xmp>";
return $DB->update_record( "book", $book );
}
/*
* parse the HTML file
*/
function booktool_github_parse_file_content( $content ) {
// Book_details.
// BOOK -> name introformat customtitles.
// CHAPTERS -> one for each chapter.
$bookdetails = new StdClass;
$bookdetails->book = new StdClass;
$bookdetails->chapters = [];
$dom = new DOMDocument;
$dom->loadHTML( $content, LIBXML_NOERROR );
$document = simplexml_import_dom( $dom );
$xpath = new DOMXPath($dom);
// print "<h3>Content</h3><xmp>"; var_dump( $content ); print "</xmp>";
// print "<h3>DOM</h3><xmp>"; var_dump( $dom ); print "</xmp>";
// print "<h3>Xpath document</h3><xmp>"; var_dump( $xpath->document ); print "</xmp>";
// GET BOOK DATA.
// Attributes other than intro.
$bookinfo = $xpath->query( "//article");
// print "<h3>Book info</h3><xmp>"; var_dump( $bookinfo->item(0)->nodeValue); print "</xmp>";
// print "<h3>Book info title</h3><xmp>"; var_dump( $bookinfo->item(0)->attributes->getNamedItem('title')->nodeValue); print "</xmp>";
// print "<h3>Book info data-introformat</h3><xmp>"; var_dump( $bookinfo->item(0)->attributes->getNamedItem('data-introformat')->nodeValue); print "</xmp>";
// print "<h3>Book info data-customtitles</h3><xmp>"; var_dump( $bookinfo->item(0)->attributes->getNamedItem('data-customtitles')->nodeValue); print "</xmp>";
// print "<h3>Book info data-numbering</h3><xmp>"; var_dump( $bookinfo->item(0)->attributes->getNamedItem('data-numbering')->nodeValue); print "</xmp>";
// print "<h3>Book info data-navstyle</h3><xmp>"; var_dump( $bookinfo->item(0)->attributes->getNamedItem('data-navstyle')->nodeValue); print "</xmp>";
if ( $bookinfo === false ) {
return false;
}
foreach ($bookinfo as $book) {
// print "<h3>Book info</h3><xmp>"; var_dump( $book->item($i)->nodeValue); print "</xmp>";
$attrnames = ['title', 'dataintroformat',
'datacustomtitles', 'datanumbering', 'datanavstyle' ];
foreach ($attrnames as $name) {
$attribute = $book->attributes->getNamedItem( $name );
$bookdetails->book->$name = $attribute->nodeValue;
}
}
// print "<h3>book details</h3><xmp>"; var_dump( $bookdetails ); print "</xmp>";
// Book_intro.
$bookintro = $xpath->query( "//div[@class='intro']");
if ( $bookintro === false ) {
return false;
}
foreach ($bookintro as $intro) {
$bookdetails->book->intro = booktool_github_dominnerhtml( $intro );
}
// print "<h3>book details intro</h3><xmp>"; var_dump( $bookdetails ); print "</xmp>";
// Remove the headings for chapter titles from chapter content
$headings = $xpath->query( "//h1");
// print "<h3>Document </h3><xmp>"; var_dump( $document); print "</xmp>";
// apparently have to do the dumy array thing for it to work
// $remove = Array();
// foreach ( $headings as $heading ) {
// $remove[] = $heading;
// }
foreach ($headings as $heading) {
$heading->parentNode->removeChild($heading);
}
// print "<h3>heading</h3><xmp>"; var_dump( $heading ); print "</xmp>";
// print "<h3>Document after deletion</h3><xmp>"; var_dump( $document); print "</xmp>";
// Get the chapter data.
$chapters = $xpath->query( "//section");
if ( $chapters === false ) {
return false;
}
foreach ($chapters as $chapter) {
$newchapter = [];
$attrnames = ['title', 'datasubchapter', 'datapagenum', 'datahidden',
'datacontentformat'];
foreach ($attrnames as $name) {
$attribute = $chapter->attributes->getNamedItem($name );
$newchapter[$name] = $attribute->nodeValue;
}
$newchapter['content'] = booktool_github_dominnerhtml( $chapter );
// print "<h3>Content $newchapter[$name]</h3><xmp>";var_dump($newchapter['content']); print "</xmp>";
array_push( $bookdetails->chapters, $newchapter );
}