English Sentence Loading...
英语句子加载中...
预览模式: 普通 | 列表

从Joomla浅谈工厂方法(Factory - Method)模式

在平时的代码工作中,你是否会遇到过这种情形,或者说有类似的感觉:在刚开始拿到一个项目时,会大概地理一下思路,想一下程序的简单架构,在反复的思考确认这是一种最好的设计模式的时候,你开始动工了,各种功能在这样的结构下也很快搭建了起来。但是项目最初的策划却总是不合我们的心意,于是我们再来修改功能模块,修改流程,使它更加趋于完美。这个时候,我们就要修改我们的代码,可能会添加一些功能,可能会改进一些程序,一开始你可能还对这些工作安排的非常妥当,还对这些代码的位置了如指掌,但是随着项目功能越来越完善,代码堆积也越来越多,你可能已经记不清类似的功能函数被重写了多少次,最后,一个看似完美的产品,却总是漏洞百出,可能大病不犯,小病不断,在修复这些bug的过程中,你开始厌倦了。

于是,我想,有必要来介绍一下工厂方法这种设计模式,使我们在做代码部署工作时,能更加妥当;在做功能完善的工作时,能更加得心应手。

我们先不去讲解什么是工厂方法模式,我们先从最简单的代码入手,来看一些代码优化的工作。

1、我们以session的操作为例,这样既可以帮助我们了解这个模式,也可以顺便让我们了解一下如何用其他方法存储session。

  1. <?php
  2.  class JSession
  3.  {
  4.   function __construct(){
  5.    //...
  6.   }
  7.  
  8.   function read(){
  9.    // read session
  10.   }
  11.  
  12.   function write(){
  13.    // write session
  14.   }
  15.  }
  16. ?>

这应该是一个最简单的session操作类了,由于我们只是说明这个设计模式,有一些其他的功能函数在这里就不列了。

2、下面我们的项目有个需求的变更,要求可以通过文件和数据库两种方式来存储session。那么我们将代码修改如下:

  1. <?php
  2.  class JSession
  3.  {
  4.   function __construct(){
  5.    //...
  6.   }
  7.  
  8.   function read($type='file'){
  9.    switch ( $type ) {
  10.     case 'file' :
  11.      // read session from file
  12.      break;
  13.     case 'database' :
  14.      // read session from db
  15.      break;
  16.     default :
  17.      // read session from file
  18.    }
  19.   }
  20.  
  21.   function write($type='file'){
  22.    switch ( $type ) {
  23.     case 'file' :
  24.      // write session to file
  25.      break;
  26.     case 'database' :
  27.      // write session to db
  28.      break;
  29.     default :
  30.      // write session to file
  31.    }
  32.   }
  33.  }
  34. ?>


这样,我们就完成了这个功能的扩展。

3、但是这样一来就有了一个弊端,如果日后我们想添加更多存储session的方式,那我们就是加更多的case段,代码被修改的

[阅读全文]

CentOS 安装 Sphinx 全文检索

1. 下载Sphinx最新版 http://www.sphinxsearch.com/downloads.html

2. 安装
# tar -zxvf sphinx-0.9.9.tar.gz
# cd sphinx-0.9.9/
# ./configure --prefix=/usr/local/sphinx --with-mysql-includes=/usr/local/mysql/include/mysql/ --with-mysql-libs=/usr/local/mysql/lib/mysql --with-iconv
# make && make install


3. 配置
# cd /usr/local/sphinx/etc
# cp sphinx-min.conf.dist joomla.conf
# vi joomla.conf


#
# Minimal Sphinx configuration sample (clean, simple, functional)
#

source joomla_src
{
        type                   = mysql

        sql_host               = localhost
        sql_user               = root
        sql_pass               = 123456
        sql_db                 = joomla
        sql_port               = 3306  # optional, default is 3306

        sql_query              = SELECT * FROM jos_content

        sql_attr_uint          = id
        sql_attr_timestamp     = created

        sql_query_info         = SELECT * FROM jos_content WHERE id=$id
}


index joomla_index
{
        source                 = joomla_src
        path                   = /usr/local/sphinx/var/data/joomla_index
        docinfo                = extern
        charset_type           = utf-8
}


indexer
{
        mem_limit              = 32M
}


searchd
{
        port                   = 9312
        log                    = /usr/local/sphinx/var/log/searchd.log
        query_log              = /usr/local/sphinx/var/log/query.log
        read_timeout           = 5
        max_children           = 30
        pid_file               = /usr/local/sphinx/var/log/searchd.pid
        max_matches            = 1000
        seamless_rotate        = 1
        preopen_indexes        = 0
        unlink_old             = 1
}

保存退出

4. 建立索引
# /usr/local/sphinx/bin/indexer  --config joomla.conf  --all joomla_index

5. 启动sphinx后台服务
# /usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/joomla.conf  -i joomla_index &

6. 测试
#/usr/local/php/bin/php-cgi test.php -h localhost -p 9312 -i joomla_index -ph title
X-Powered-By: PHP/5.2.10
Content-type: text/html

Query 'title ' retrieved 16 of 16 matches in 0.001 sec.
Query stats:
    'title' found 61 times in 16 documents

Matches:
1. doc_id=44, weight=2, created=1970-01-01 07:33:27
2. doc_id=1, weight=1, created=1970-01-01 07:33:28
3. doc_id=4, weight=1, created=1970-01-01 07:33:28
4. doc_id=5, weight=1, created=1970-01-01 07:33:28
5. doc_id=10, weight=1, created=1970-01-01 07:33:28
6. doc_id=11, weight=1, created=1970-01-01 07:33:28
7. doc_id=20, weight=1, created=1970-01-01 07:33:28
8. doc_id=21, weight=1, created=1970-01-01 07:33:28
9. doc_id=24, weight=1, created=1970-01-01 07:33:28
10. doc_id=25, weight=1, created=1970-01-01 07:33:28
11. doc_id=26, weight=1, created=1970-01-01 07:33:28
12. doc_id=27, weight=1, created=1970-01-01 07:33:28
13. doc_id=28, weight=1, created=1970-01-01 07:33:28
14. doc_id=30, weight=1, created=1970-01-01 07:33:28
15. doc_id=38, weight=1, created=1970-01-01 07:33:28
16. doc_id=45, weight=1, created=1970-01-01 07:33:27

mysql root密码丢失的解决办法

我曾经也遇到过这种情况,一个误操作,把mysql的root密码修改了。我们可以按以下步骤解决:

1. 停掉mysql服务。
sudo /etc/init.d/mysql stop


2. 启动mysql服务,带--skip-grant-tables参数。
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &

这样我们就可以跳过mysql的授权表

3. 修改root密码
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > flush priviledges;
mysql > exit;

注意,这里不是md5加密

4. 重启mysql服务。
sudo /etc/init.d/mysql restart
标签: mysql

Ubuntu 安装 python-mysqldb

首先下载MySQLdb库,然后进行安装。

./setup.py build
Error:
sh: mysql_config: not found
Traceback (most recent call last):
  File "./setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/home/liubing/Downloads/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/home/liubing/Downloads/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found


Solution:
修改setup_posix.py文件,Line26: mysql_config:mysql_config.path = "/usr/local/mysql/bin/mysql_config"

./setup.py build
Error:
Python.h: No such file or directory

Solution:
sudo apt-get install python2.6-dev


sudo ./setup.py install
安装成功。

测试

#! /usr/bin/python
#coding=utf-8
 
import MySQLdb
 
conn = MySQLdb.connect(host='localhost',user='root',passwd='123456')
 
cursor = conn.cursor()
 
conn.select_db('test')
 
values = []
for i in range(10):
values.append(('Hello mysqldb, I am recoder '+str(i)))
 
cursor.executemany("""INSERT INTO test(title) values(%s)""",values);
cursor.close();

shell脚本创建trac项目

服务器环境:[192.168.1.3] Ubuntu 9.10 + Apache2 + svn + trac

由于apache的配置非常简单灵活,所以公司在建项目时可能会因为创建者不一样而形式不一样的习惯,比如配置文件存放的位置,虚拟目录设置路径等,所以写个脚本来规范一下,方便后续的项目维护。
#!/bin/sh
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script!"
exit 1
fi

# Check project parameters
if [ "-$1" = "-" ]; then
echo "Error: You need input project name!\nType: sudo trac-init newproject"
exit 1
fi

if [ ! -d "/home/flow/websites/$1.flow" ]; then
confpath="/etc/apache2/sites-enabled/$1.conf"

# Checkout project from svn
echo "Checkout project from svn...\n"
svn co http://192.168.1.3/svn/joomla-projects/$1.flow /home/flow/websites/$1.flow

# Add virtualhost
echo "Add virtualhost for new project...\n"
touch $confpath
echo "<virtualHost *>" >> $confpath
echo " ServerName $1.flow" >> $confpath
echo " DocumentRoot /home/flow/websites/$1.flow/trunk" >> $confpath
echo "</VirtualHost>" >> $confpath
fi

# Create new project on trac
if [ ! -d "/home/trac/$1" ]; then
echo "Create project on trac...\n"
trac-admin /home/trac/$1 initenv $1 sqlite:db/trac.db svn /home/svn/joomla-projects/$1.flow/trunk --inherit=/home/trac/test2/conf/trac.ini
chown -R www-data:www-data /home/trac/$1
trac-admin /home/trac/$1 permission add marc TRAC_ADMIN
fi

# Restart Apache...
echo "Restart Apache..."
/etc/init.d/apache2 restart

存为trac-init文件,并赋可执行权限,可将此文件拷贝到/usr/local/bin/目录下。

使用方法
sudo trac-init newproject

标签: shell trac linux