Blog

Publishing to drupal with org2blog

org2blog is an org-mode plugin which allows to publish an org file to a wordpress blog. The connection between org-mode and wordpress is achived through an xmlrpc call using the metaweblog api.

My blog is powered by drupal which supports xmlrpc calls and the metaweblog api. The setup although was not straightforward. In this post I'm going to show the steps that I followed in order to make it work.

Drupal

First of all in order to enable xmlrpc calls to our drupal blog we need to install the blog api:

root@mazinga:/var/www# drush dl blogapi
Project blogapi (7.x-2.0-rc1) downloaded to /var/www/sites/all/modules/blogapi.                                                                     [success]
Project blogapi contains 4 modules: blogapi_movabletype, blogapi_blogger, blogapi_metaweblog, blogapi.
root@mazinga:/var/www# 

root@mazinga:/var/www# drush en blogapi
The following projects have unmet dependencies:
blogapi requires services
Would you like to download them? (y/n): y
Project services (7.x-3.19) downloaded to /var/www/sites/all/modules/services. [success]
Project services contains 4 modules: services_oauth, rest_server, xmlrpc_server, services.
The following projects have unmet dependencies:
blogapi requires ctools
Would you like to download them? (y/n): y
Project ctools (7.x-1.12) downloaded to /var/www/sites/all/modules/ctools.     [success]
Project ctools contains 10 modules: ctools_ajax_sample, page_manager, views_content, bulk_export, 
ctools_plugin_example, ctools_custom_content, ctools_access_ruleset, term_depth, stylizer, ctools.
file_put_contents(/root/.drush/cache/default/5.4-commandfiles--14588496b4e561311940398d5ea7851d.cache): 
failed to open stream: No such file or                                         [warning]
directory cache.inc:433
The following extensions will be enabled: blogapi, ctools, services
Do you really want to continue? (y/n): y
blogapi was enabled successfully.                                              [ok]
ctools was enabled successfully.                                               [ok]
services was enabled successfully.                                             [ok]
root@mazinga:/var/www#

Then we need to enable some stuff, so we have to log in as user/1 on our site, go to modules section and check:

  • BlogAPI
  • MetaWeblog Provider for BlogAPI
  • Services
  • XMLRPC Server

The services module has to be configured so go to Modules/services/Configure and select Edit Resources. Here we can choose which methods we would like to enable. I selected all the methods.

In Configuration/Web services/BlogAPI enable MetaWeblog as the default provider for xmlrpc APIs.

Configuration in drupal is almost done, the last step is to write down the endpoint of the xmlrpc service. This information is in: Modules/services/Configure/Modify/Path to endpoint

Emacs

We need to install org2blog and enable it, then we define the handle to our blog:

(require 'org2blog-autoloads)
;;; org2blog
(setq org2blog/wp-blog-alist
      '(("my-blog"
         :url "http://www.example.org/blogapi/xmlrpc"
         :default-categories ("linux")
         :tags-as-categories nil
         :id "blog"
         :username "XXXX"
         :password "YYYY" )))

The relevant parts here are:

  • url is composed by site_url plus xmlrpc path end point
  • id is the id our blog, we must specify this option otherwise org2blog defaults to id 1 which is not present in drupal

org2blog uses some wordpress apis which are not compatible with drupal: if we try to connect to our blog we get an error. We have to redefine two functions to bypass those errors:

(eval-after-load "metaweblog"
  '(defun wp-get-tags (blog-xmlrpc user-name password blog-id)
     "Retrieves list of tags from the weblog system. Uses wp.getTags"
     (xml-rpc-method-call blog-xmlrpc
                          "taxonomy_term.index")))

(eval-after-load "metaweblog"
  '(defun wp-get-pagelist (blog-xmlrpc user-name password blog-id)
     (xml-rpc-method-call blog-xmlrpc
                          "node.index")))

That should be all that is needed.

What's next

I have to understand how to upload images (I receive a SQL error when I publish a post with an image inside) and how to attach categories to a post, but I'm pretty satisfied with the actual integration: this post for example is published using org2blog!

Blog: 

Programmazione: 

Monitorare la dimensione di un filesystem

In Linux per conoscere quanto spazio resta ai filesystems si usa il comando df. Un esempio di output del comando è:

root@mazinga:~# df -lh
Filesystem                 Size  Used Avail Use% Mounted on
rootfs                      20G  3.3G   16G  18% /
udev                        10M     0   10M   0% /dev
tmpfs                       50M  144K   50M   1% /run
/dev/disk/by-label/DOROOT   20G  3.3G   16G  18% /
tmpfs                      5.0M     0  5.0M   0% /run/lock
tmpfs                      100M     0  100M   0% /run/shm
root@mazinga:~#

Se si volesse quindi monitorare lo stato di un particolare mount point si può usare uno script come il seguente:

#!/bin/bash
 
# lo script confronta la percenuale di spazio utilizzata nel filesystem / con $SOGLIA
# se la percentuale supera $SOGLIA invia una mail a $RECIPIENTS
 
# imposto una soglia del 90%
SOGLIA=90
 
# imposto i destinatari della mail
RECIPIENTS="pippo@example.com"
 
now=`date +%y%m%d-%H%M%S`
 
perc_root=`df -Ph -x tmpfs -x rootfs | grep "/$" |  tr -s ' ' '\t' | cut -f5 -s | cut -d '%' -f1`
 
found=false
msg="Analizzatore dell'uso dello spazio su disco (SOGLIA impostata a "${SOGLIA}"):\n\n"
 
if [[ "${perc_root}" -ge "${SOGLIA}" ]] ; then
    found=true
    msg+="Il file system '/' sta utilizzando il "${perc_var}"% del suo spazio disponibile\n"
fi
 
if "${found}" ; then
    echo -e ${msg} | mail -s "Controllo su spazio disco del ${now}" $RECIPIENTS
fi

La parte interessante è il comando che ricava la percentuale da df:

  • la prima parte (df -Ph -x tmpfs -x rootfs) invoca il comando df usando il flag -x che permette di escludere un filesystem e il flag -P che imposta l'output in formato POSIX (i dati dei filesystem sono tutti nella stessa riga);
  • la seconda parte ( grep "/$" ) serve a puntare il filesystem desiderato;
  • la terza parte serve a puntare la colonna desiderata e ripulisce il campo dal carattere % in modo da poterlo usare in un test.

Lo script se messo in cron inizierà ad inviarvi una mail non appena lo spazio utilizzato nel filesystem root arriverà a superare il 90%.

Blog: 

Programmazione: 

Postgres da record set a csv

Oggi mi è capitato di dover ottenere un csv a partire da un record set di chiavi. Supponiamo quindi di avere un'entita che ha una chiave numerica e di voler ottenere un csv con tutte le chiavi.

L'entità è così formata:

test=> \d utente
           Table "public.utente"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | numeric(9,0)          | not null
 name   | character varying(50) | 
Indexes:
    "utente_id_key" UNIQUE CONSTRAINT, btree (id)
 
test=> 
test=> select * from utente;
 id |   name   
----+----------
  1 | ciccio
  2 | paperino
  3 | pluto
  4 | minnie
  5 | paperina
(5 rows)
 
test=>

Per avere il csv di tutte le chiavi useremo la seguente query che sfrutta la funzione string_agg:

test=> select string_agg(id::text, ',') as csv from (select id from utente) as foo;
    csv    
-----------
 1,2,3,4,5
(1 row)
 
test=>

Questa e altre funzioni le trovate documentate nel sito di postgres.

Blog: 

Programmazione: 

Pagine

Subscribe to Feed RSS - blog