A typical Developer Blog
by Gordon Franke
Icon

How can i automatic add ids to my trans-unit nodes in my XML Xliff Tanslation files?

When you validate you XML Xliff file you have often the problem that a trans-unit node has no or a already existing id. So i have wrote a small script for that problem.

fix_id.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
$doc = new DOMDocument();
$doc->load($argv[1]);
 
$xpath = new DOMXPath($doc);
 
$query = '//trans-unit';
 
$entries = $xpath->query($query);
foreach ($entries as $i => $entry) {
    $entry->setAttribute('id', $i+1);
}
 
$doc->save($argv[1]);

Execute the following command in your symfony1 root directory ;)

for FILE in `find apps/*/i18n -name *\.xml`; do php fix_id.php $FILE; done;

Change from Doctrine to Doctrine_Core with one command

To change all your code from Doctrine to Doctrine_Core you can use the following command

1
for fl in `find apps/ config/ lib/ test/ -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

When you have a plugin you can use this command

1
for fl in `find . -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

How can i check and optimize all Zimbra databases and tables?

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
#
 
source ~zimbra/bin/zmshutil || exit 1
zmsetvars zimbra_home mysql_directory mysql_socket mysql_root_password
 
echo ${mysql_root_password}
 
${mysql_directory}/bin/mysqlcheck --all-databases --optimize -h localhost -P 7306 --protocol=tcp \
        --user=root --password=${mysql_root_password} $*

How can i remove all generated symfony base files automatically from svn?

call this from your project root

for DIR in `find lib -name base -type d`; do svn propset svn:ignore base $DIR/..;svn rm $DIR; done;

How can i run multiple instances of memcached on one server?

Problem

I need more then one memcached server.

Solution

Virtual machine

I can build for each memcached server a virtal machine.

Problems

  • more memory (system+memcached)
  • one ip address for each instance
  • more work

Multiple instances via ports

For this it needs to patch the init.d and start-memcached scripts to run multiple instances.

After the patch it search for configuration files that match the following pattern /etc/memcached_*.conf. When it found no file it use the /etc/memcached.conf configuration file. So it breaks no backward compatibility.

When you want to create 2 memached instances copy the configuration file and change the port, memory …

cp /etc/memcached.conf /etc/memcached_myserver_1.conf
cp /etc/memcached.conf /etc/memcached_myserver_2.conf

Commands

start all servers

/etc/init.d/memcached start

start only a specific server

/etc/init.d/memcached start myserver_1

stop all servers

/etc/init.d/memcached stop

stop only a specific server

/etc/init.d/memcached stop myserver_1

The patches

/usr/share/memcached/scripts/start-memcached

26,30d25
> if (scalar(@ARGV) == 2) {
> 	$etcfile = shift(@ARGV);
> 	$pidfile = shift(@ARGV);
> }
>

/etc/init.d/memcached

16a17
> DAEMONNAME=memcached
18d18
< NAME=memcached
20d19
< PIDFILE=/var/run/$NAME.pid
26a26,63
> FILES=(/etc/memcached_*.conf);
> # check for alternative config schema
> if [ -r "${FILES[0]}" ]; then
>   CONFIGS=();
>   for FILE in "${FILES[@]}";
>   do
>     # remove prefix
>     NAME=${FILE#/etc/};
>     # remove suffix
>     NAME=${NAME%.conf};
>
>     # check optional second param
>     if [ $# -ne 2 ];
>     then
>       # add to config array
>       CONFIGS+=($NAME);
>     elif [ "memcached_$2" == "$NAME" ];
>     then
>       # use only one memcached
>       CONFIGS=($NAME);
>       break;
>     fi;
>   done;
>
>   if [ ${#CONFIGS[@]} == 0 ];
>   then
>     echo "Config not exist for: $2" >&2;
>     exit 1;
>   fi;
> else
>   CONFIGS=(memcached);
> fi;
>
> CONFIG_NUM=${#CONFIGS[@]};
> for ((i=0; i < $CONFIG_NUM; i++)); do
>   NAME=${CONFIGS[${i}]};
>   PIDFILE="/var/run/${NAME}.pid";
>
30c67
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
50c87
<       start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
---
>       start-stop-daemon --start --quiet --exec "$DAEMONBOOTSTRAP" -- /etc/${NAME}.conf $PIDFILE
54c91
<       N=/etc/init.d/$NAME
---
>       N=/etc/init.d/$DAEMONNAME
59a97
> done;

Bash script to build symfony project with sfDoctrinePlugin, sfDoctrineGuardPlugin and sfDoctrineGuardExtraPlugin

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
#!/bin/bash
 
# config
PROJECTNAME=TEST;
APPNAME=frontend;
AUTHORNAME=gfranke;
CSRFSECRET=UniqueSecret;
 
#DBDSN="sqlite:%SF_DATA_DIR%/sandbox.db";
DBDSN="mysql:host=localhost;dbname=symfony_build_with_guard";
DBUSER=root;
DBPASS="";
 
SYMFONYTAG=RELEASE_1_2_4;
 
# sfGaurdExtraPlugin don't work with propel
ORM=doctrine;
 
 
# remove existing files
rm apps/ web/ plugins/ lib/ log/ config/ cache/ data/ doc/ symfony test/ vendor/symfony_plugins/ -Rf;
 
# make vendor dir
if ! [ -d vendor ]; then
  mkdir vendor;
fi
 
# make symfony vendor dir
if ! [ -d vendor/symfony ]; then
  mkdir vendor/symfony;
fi
 
# get symfony
if ! [ -d vendor/symfony/${SYMFONYTAG}_lib ]; then
  svn export http://svn.symfony-project.com/tags/${SYMFONYTAG}/lib vendor/symfony/${SYMFONYTAG}_lib;
fi
if ! [ -d vendor/symfony/${SYMFONYTAG}_data ]; then
  svn export http://svn.symfony-project.com/tags/${SYMFONYTAG}/data vendor/symfony/${SYMFONYTAG}_data;
fi
 
# make current links for fast switch
if ! [ -L vendor/symfony/current_lib ]; then
  ln -s RELEASE_1_2_4_lib vendor/symfony/current_lib;
fi
if ! [ -L vendor/symfony/current_data ]; then
  ln -s RELEASE_1_2_4_data vendor/symfony/current_data;
fi
 
# only for build
if ! [ -L vendor/symfony/lib ]; then
  ln -s current_lib vendor/symfony/lib;
fi
if ! [ -L vendor/symfony/data ]; then
  ln -s current_data vendor/symfony/data;
fi
 
# init symfony
php vendor/symfony/data/bin/symfony generate:project $PROJECTNAME;
 
# correct symfony lib
sed "3d"  config/ProjectConfiguration.class.php2;
sed "3irequire_once dirname(__FILE__).'/../vendor/symfony/current_lib/autoload/sfCoreAutoload.class.php';"  config/ProjectConfiguration.class.php;
 
# remove build temp dirs
rm vendor/symfony/lib vendor/symfony/data;
 
# set author name
./symfony configure:author $AUTHORNAME;
 
# generate app
./symfony generate:app --escaping-strategy=on --csrf-secret=$CSRFSECRET $APPNAME;
 
# get plugins
mkdir vendor/symfony_plugins;
if [ "$ORM" = "propel" ]; then
  SVNVERSION=`svn info http://svn.symfony-project.com/plugins/sfGuardPlugin/branches/1.2/ | sed '8s/[^0-9]*//' | sed '1,7d' | sed '2,4d'`;
  svn export http://svn.symfony-project.com/plugins/sfGuardPlugin/branches/1.2/ vendor/symfony_plugins/sfGuardPlugin;
 
  ln -s sfGuardPlugin_$SVNVERSION vendor/symfony_plugins/sfGuardPlugin_current;
  ln -s ../vendor/symfony_plugins/sfGuardPlugin_current plugins/sfGuardPlugin;
 
  SVNVERSION=`svn info http://svn.symfony-project.com/plugins/sfGuardExtraPlugin/branches/1.2/ | sed '8s/[^0-9]*//' | sed '1,7d' | sed '2,4d'`;
  svn export http://svn.symfony-project.com/plugins/sfGuardExtraPlugin/branches/1.2/ vendor/symfony_plugins/sfGuardExtraPlugin;
 
  ln -s sfGuardExtraPlugin_$SVNVERSION vendor/symfony_plugins/sfGuardExtraPlugin_current;
  ln -s ../vendor/symfony_plugins/sfGuardExtraPlugin_current plugins/sfGuardExtraPlugin;
 
  # database config
  ./symfony configure:database "sqlite:%SF_DATA_DIR%/sandbox.db"
else
  sed '11s/sfDoctrinePlugin/sfPropelPlugin/'  config/ProjectConfiguration.class.php2;
  mv config/ProjectConfiguration.class.php2 config/ProjectConfiguration.class.php;
 
  SVNVERSION=`svn info http://svn.symfony-project.com/plugins/sfDoctrineGuardPlugin/branches/1.2/ | sed '8s/[^0-9]*//' | sed '1,7d' | sed '2,4d'`;
  svn export http://svn.symfony-project.com/plugins/sfDoctrineGuardPlugin/branches/1.2/ vendor/symfony_plugins/sfDoctrineGuardPlugin_$SVNVERSION;
 
  ln -s sfDoctrineGuardPlugin_$SVNVERSION vendor/symfony_plugins/sfDoctrineGuardPlugin_current;
  ln -s ../vendor/symfony_plugins/sfDoctrineGuardPlugin_current plugins/sfDoctrineGuardPlugin;
 
  SVNVERSION=`svn info http://svn.symfony-project.com/plugins/sfDoctrineGuardExtraPlugin/branches/1.2/ | sed '8s/[^0-9]*//' | sed '1,7d' | sed '2,4d'`;
  svn export http://svn.symfony-project.com/plugins/sfDoctrineGuardExtraPlugin/branches/1.2/ vendor/symfony_plugins/sfDoctrineGuardExtraPlugin_$SVNVERSION;
 
  ln -s sfDoctrineGuardExtraPlugin_$SVNVERSION vendor/symfony_plugins/sfDoctrineGuardExtraPlugin_current;
  ln -s ../vendor/symfony_plugins/sfDoctrineGuardExtraPlugin_current plugins/sfDoctrineGuardExtraPlugin;
 
  # database config
  ./symfony configure:database --name=doctrine --class=sfDoctrineDatabase $DBDSN $DBUSER $DBPASS
 
  # remove propel connection
  sed '2,6s/propel/doctrine/'  config/databases.yml2;
  mv config/databases.yml2 config/databases.yml;
fi
 
# enabled modules and change login module and action
sed "31i    # Activated modules from plugins or from the symfony coren    enabled_modules: [default, sfGuardAuth, sfGuardRegister, sfGuardForgotPassword]n"  apps/frontend/config/settings.yml2;
mv apps/frontend/config/settings.yml2 apps/frontend/config/settings.yml;
 
# deactivate guard routes
sed "2d"  apps/frontend/config/app.yml2;
echo "all:n  sf_guard_plugin:n    routes_register: falsen" >> apps/frontend/config/app.yml2;
mv apps/frontend/config/app.yml2 apps/frontend/config/app.yml;
 
# insert guard routes
sed "6isf_guard_signin:n  url:   /loginn  param: { module: sfGuardAuth, action: signin }nnsf_guard_signout:n  url:   /logoutn  param: { module: sfGuardAuth, action: signout }n"  apps/frontend/config/routing.yml2;
mv apps/frontend/config/routing.yml2 apps/frontend/config/routing.yml;
 
# create db file
touch data/sandbox.db
chmod 777 data
chmod 777 data/sandbox.db
 
# extends sfGuardSecurityUser
sed '3s/sfBasicSecurityUser/sfGuardSecurityUser/'  apps/frontend/lib/myUser.class.php2;
mv apps/frontend/lib/myUser.class.php2 apps/frontend/lib/myUser.class.php;
 
# clear cache
./symfony cc
 
# fix permissions
./symfony fix-perms
 
# build db model filter and forms
if [ "$ORM" = "propel" ]; then
  ./symfony propel:build-all --no-confirmation
else
  ./symfony doctrine:build-model
 
  echo "hasColumn('email', 'string', 80, array('notnull' => true, 'unique' => true));
  }
 
  public function getEmailAddress()
  {
    return $this->email;
  }
 
  public function setEmailAddress($email)
  {
    $this->email = $email;
  }
}" > lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUser.class.php;
 
  echo "from('sfGuardUser u')->where( '(u.username = ? OR u.email = ?) AND u.is_active = ?', array( $usernameOrEmail, $usernameOrEmail, $isActive ) )->execute()->getFirst();
  }
}
" > lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUserTable.class.php;
 
  # schema to inndb
  echo "---
options:
  type: INNODB
"  > config/doctrine/schema.yml
 
  # cleanup
  rm config/schema.yml config/propel.ini
 
  ./symfony doctrine:build-all-reload --no-confirmation
fi
 
# publish assets don't use publish-assets symlink must go to current link
rm web/sfPropelPlugin web/sfProtoculousPlugin;
ln -s ../vendor/symfony/current_data/web/sf/ web/sf;
ln -s ../vendor/symfony/current_lib/plugins/sfProtoculousPlugin/web web/sfProtoculousPlugin;
if [ "$ORM" = "propel" ]; then
  ln -s ../vendor/symfony/current_lib/plugins/sfPropelPlugin/web web/sfPropelPlugin;
else
  ln -s ../vendor/symfony/current_lib/plugins/sfDoctrinePlugin/web web/sfDoctrinePlugin;
fi
 
# clear cache
./symfony cc

Unix helper

Some questions some awnsers. For each command you can find more options with

man command

How can i show the difference between two files or directories?

simple use diff

diff file1 file2
  • -r rekursive
  • -w –ignore-all-space ignore space
  • -y –side-by-side show both files side by side

How can i copy with the same right, group and user settings?

with the option -a

cp source target -a

How can i show metacharacters like special whit spaces?

use cat

cat -A file

How can i get one file from a SVN source?

svn export http://framework.zend.com/svn/framework/standard/tags/release-1.7.6/library/Zend/Acl.php

How can i remove all .svn folder from a folder and his subfolders?

for DIR in `find . -name .svn -type d`; do rm $DIR -Rf; done;

How can display a directory as a tree?

simple with tree

tree directory
  • -L depth