site_admin.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/bash
  2. #
  3. # The all-in-one administrator's script. It contains the following
  4. # features:
  5. #
  6. # Database Initialization:
  7. #
  8. # Creates a data model suitable for the hosting of articles in a blog,
  9. # in which case I will use it for my own self-hosting purposes. Uses SQLite
  10. #
  11. # Article publishing:
  12. #
  13. # Enables you to publish a new article, automatically connecting you to the
  14. # vim text editor (:D) and stamping the published date on it.
  15. #
  16. # Article deleting and editing:
  17. #
  18. # Enables you to edit or delete an existing article.
  19. #
  20. # Note on choice of editor:
  21. #
  22. # The default text editor of this app is vim because it's my favorite and
  23. # pretty much ubiquitous on any unix-ish system (for which this engine is
  24. # intended). If you would like to use another editor, change the EDITOR line
  25. # to the editor of your choice.
  26. #
  27. # Copyright 2015 K. Zimmermann - https://notabug.org/kzimmermann
  28. #
  29. # This program is free software: you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation, either version 3 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License
  40. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  41. #
  42. # This is the editor that you will be using to write/edit your posts:
  43. EDITOR="vim"
  44. # Change this to the name of whatever database file you want (leave the
  45. # .db extension, though), like a salt:
  46. db="main.db"
  47. # Dates must be something that `date' can understand. I personally use the
  48. # YYYY-MM-DD format because it's the most readable one IMO.
  49. DATEFORMAT="%Y-%m-%d"
  50. #-- Functions --#
  51. initialize() {
  52. sqlite3 $db <<-EOF
  53. CREATE TABLE IF NOT EXISTS articles (
  54. id INTEGER PRIMARY KEY AUTOINCREMENT,
  55. title TEXT,
  56. body TEXT,
  57. pubdate TEXT,
  58. pageviews INTEGER
  59. );
  60. EOF
  61. chmod 644 $db
  62. }
  63. publish() {
  64. printf "Enter a title for the post: "
  65. read title
  66. if [[ "$EDITOR" == "vim" ]]; then
  67. $EDITOR -c 'set linebreak' -c 'set wrap' .temppub
  68. else
  69. $EDITOR .temppub
  70. fi
  71. # This is required to clean evil quotation marks:
  72. cat .temppub | sed 's/\"/\&quot;/g' > .cleanpub
  73. sqlite3 $db <<-EOF
  74. INSERT INTO articles (title, body, pubdate, pageviews)
  75. VALUES
  76. ("$title", "$(cat .cleanpub)", "$(date +$DATEFORMAT)", 0)
  77. ;
  78. EOF
  79. [[ $? -eq 0 ]] && rm .temppub .cleanpub || echo "Error inserting into database. Please try again."
  80. }
  81. edit() {
  82. printf "Which post ID would you like to edit? "
  83. read id
  84. title=$(sqlite3 $db "SELECT title FROM articles WHERE id=$id")
  85. if [ -z "$title" ]
  86. then
  87. echo "This post doesn't exist."
  88. else
  89. printf "Edit '$title'? (y/n) "
  90. read decision
  91. if [[ "$decision" != "y" ]]
  92. then
  93. echo "Aborted."
  94. else
  95. printf "Enter a new title for the post (leave blank for no change): "
  96. read newtitle
  97. if [[ -z "$newtitle" ]]
  98. then
  99. newtitle=$title
  100. fi
  101. # Extract the text content in to an external file:
  102. sqlite3 $db "SELECT body FROM articles WHERE id=$id" > .temppub
  103. if [[ "$EDITOR" == "vim" ]]; then
  104. $EDITOR -c 'set linebreak' -c 'set wrap' .temppub
  105. else
  106. $EDITOR .temppub
  107. fi
  108. cat .temppub | sed 's/\"/\&quot;/g' > .cleanpub
  109. sqlite3 $db <<-EOF
  110. UPDATE articles
  111. SET
  112. title="$newtitle",
  113. body="$(cat .cleanpub)",
  114. pubdate="$(date +$DATEFORMAT)"
  115. WHERE id=$id
  116. ;
  117. EOF
  118. [[ $? -eq 0 ]] && rm .temppub .cleanpub || echo "Error inserting into database. Please try again."
  119. fi
  120. fi
  121. }
  122. delete() {
  123. printf "Which post ID would you like to delete? "
  124. read id
  125. title=$(sqlite3 $db "SELECT title FROM articles WHERE id=$id")
  126. if [ -z "$title" ]
  127. then
  128. echo "This post doesn't exist."
  129. else
  130. printf "Proceed with deleting '$title'? (y/n) "
  131. read decision
  132. if [[ "$decision" != "y" ]]
  133. then
  134. echo "aborted."
  135. else
  136. sqlite3 $db "DELETE FROM articles WHERE id=$id"
  137. echo "Post deleted"
  138. fi
  139. fi
  140. }
  141. #-- /Functions --#
  142. case "$1" in
  143. "init" ) initialize;;
  144. "post" ) publish;;
  145. "edit" ) edit;;
  146. "delete") delete ;;
  147. "-h" | "--help" ) echo "options are 'init', 'post', 'edit', 'delete'";;
  148. * ) echo "Error: missing arguments"
  149. exit 1
  150. ;;
  151. esac