JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / namespacify.sh
1 #!/bin/bash
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8 # This script is designed to make it easier to use wfpl in other projects, and
9 # not worry about include paths or name collisions.
10 #
11 # This script packs most of wfpl into a single stream (to stdout) and makes it
12 # work inside the \wfpl namespace
13 #
14 # Run like so:  bash ./namespacify > wfpl.php
15 #
16 # or:           bash ./namespacify wordpress > wfpl.php
17 #
18 # The "wordpress" option breaks/obsoletes db_connect and makes db_*() use the
19 # wordpress database and automatically prepends the wordpress table name prefix
20 #
21 # And use in your project like so:
22 #
23 #     require_once('/path/to/wfpl.php');
24 #     $template = new \wfpl\tem();
25 #     $records = \wfpl\db_get_assocs('records', 'name,quantity,date');
26
27 wfpl_rev=""
28 if [ -d .git ]; then
29         wfpl_rev="-$(git rev-parse HEAD)"
30         if ! git diff --quiet; then
31                 wfpl_rev="$wfpl_rev-dirty"
32         fi
33 fi
34
35 echo '<?php'
36 echo
37 echo '#  Copyright (C) 2005,2013 Jason Woofenden <jason@jasonwoof.com>,'
38 echo '#                2008,2009 Joshua Grams <josh@qualdan.com>'
39 echo '#'
40 echo "#  This file is generated from wfpl$wfpl_rev"
41 echo '#'
42 # output license
43 head -n 16 "template.php" | tail -n +5
44 echo
45 echo 'namespace wfpl;'
46 wordpress_regex=''
47 if [ "$1" = 'wordpress' ]; then
48         echo
49         echo '# wordpress tables are supposed to have a prefix.'
50         echo 'function add_wp_table_prefix($table) {'
51         echo '  # only add the wordpress table prefix once, just in case.'
52         echo '  if (substr($table, 0, strlen($GLOBALS['\''wpdb'\'']->prefix)) == $GLOBALS['wpdb']->prefix) {'
53         echo '          return $table;'
54         echo '  } else {'
55         echo '          return $GLOBALS['\''wpdb'\'']->prefix . $table;'
56         echo '  }'
57         echo '}'
58         wordpress_regex='s/function db_\(send_get\|insert_ish\|update\|delete\|\)(.*{/\0\n\t$table = add_wp_table_prefix($table);/;  s/\$GLOBALS\['\''wfpl_db_handle'\''\]/$GLOBALS['\''wpdb'\'']->dbh/g;'
59 fi
60 cat atexit.php csv.php db.php email.php encode.php file.php file_run.php format.php http.php messages.php misc.php template.php time.php | grep -v -e '<?php' -e '^\s*//' -e 'require_once' -e '^\s*$' | grep -v -P -e '^\s*#\s*((?!NAMESPACIFY).)*$' | sed -e "$wordpress_regex"'s/\(register_shutdown_function\|function_exists\|call_user_func_array\)(/\0'\''\\\\wfpl\\\\'\'' . /;  s/^\(\s*\)# NAMESPACIFY \(.*\)/\1\2 = '\''\\\\wfpl\\\\'\'' . \2;/'