Linux | 咸水鳄

咸水鳄



如何快速将Metadata导入MySource Matrix系统

Category: Linux, 网站开发
标签:, ,

本地的PWC进行的他们公司对外和局域网的网站建设断断续续已经进行了一年多了, 跟很多澳洲政府教育相关部门一样, PWC的新INTERNET和INTRANET网站都是在MySource Matrix这个CMS系统上进行搭建的.  这个系统特点就是…..复杂, 它包含了太多的功能, 感觉他的终极目标就是让一个不懂PHP的人也能借助这个系统和HTML/CSS/JS知识构建一个功能强大的网站. 但是降低门槛的同时也就提高了操作繁琐性, 由于我们做为外聘的开发人员不能自由访问修改服务器上代码文件, 而MATRIX系统本身虽然开源但其开发团队代码开发文档方面十分小气(SQUIZ主要要靠出售技术支持为收入), 导致很多可以简单代码完成功能不得不靠MATRIX繁琐后台各种ASSET来拼凑. 而这系统本身说实话也十分缓慢, 甚至于ASSET LISTING仅仅是排序分类仅仅一千多个PDF图纸文件就要花上整整一分钟才能完成….而进行一系列SQL语句的优化以后其实需要 半秒….

话题扯远了好像…….闲话归闲话, 一年多来也算在这个系统使用积累了些技巧经验, 现在就分享一则.

PWC面向公众的网站很重要的一部分就是他们 的Standard Drawings查询系统, 由水电公司的一班POWER SUPPLY和WATER SEWERAGE的工程师维护. 主要就提供一千多份水电相关零部件设备PDF图纸用户查询. 当初这个系统是纯手工由HTML和CSS构建的, 导入到Matrix系统以后基本是大走样, 当时最头疼的问题之一就是一千多个PDF文件, 每个文件都有两附属数据要加上, 我们可以把属性以METADATA的形式加到所有PDF类文件上, 但是这些数据怎么录入? 手动的话基本得把活人弄死, 而MATRIX本身的导入工具又无法完成类似任务, 跟同事讨论半天没啥结果, 感觉只能动动外部用PHP直接数据库导入的方法了. 但是从来没对MATRIX的数据直接操作过, 而且话说MQTRIX用的POSTGRESS数据库我也没用过, 就这么愣写, 好像有点二叉… 无奈之下求助MATRIX的论坛, 别说还真就有高人通过修改MATRIX本身带的一个导入工具写出了把METADATA导入数据的PHP程序. 代码本身刚下载下来的时候不适用, 因为原代码是以asset ID在数据库中查询的,但是我当时的数据只有asset文件名,于是我自己修改了一下总算是可以成功导入了, 但是由于导入时以asset文件名为准, 有时便会出现冲突的情况, 我就又改了下代码, 这样冲突的时候可以记下编号, 去MATRIX系统里进行修正. -_-

导入前的准备工作也比较无奈, 原来的数据都是网页形式, 而导入程序需要CSV格式数据, 我只得先把网页上数据成EXCEL格式再转存CSV…. 转换时候还闹出些笑话, 比如有的日期没写月份, 于是转出来的EXCEL文件里写着日期年份是1905年……..-____-

程序具体使用我就用我的导入经验举例了.

  1. 需要准备CSV数据文件格式为: [Asset文件名], [Metadata Schema ID], [Metadata Schema 数据]     —— 后面两个metadata schema的id和数据想放多少放多少, 中间放逗号 (文章最后有附示例文件)
  2. WINSCP把PHP文件和CSV数据放至服务器根
  3. PUTTY登入服务器ROOT.
  4. 输入命令式: php import_metadata.php [Matrix所在的目录] [csv file name]
  5. 回车导入开始.
  6. 如果有冲突会有提示, 把ID记下来以便过后调整, 然后按C继续.

全部一千多个PDF花了将近半小时导入成功. 速度慢了点, 但是毕竟比手工录入已经快了不知道多少倍了. 在这感谢代码的原修改者Tim Everist.

希望这篇文章对有相关需要朋友有所帮助.

附:

用于导入METADATA的PHP程序及示例文件下载:

metadata_import1.zip

注意, 对MATRIX数据库的直接操作一定要谨慎进行, 对于本程序造成的任何后果本人不负任何责任.



Emacs & VI

Category: Linux

 





     Vi and Emacs

NOT “Vi vs. Emacs” — Despite twenty or thirty years of abuse thrown at each other by adherents of the Church of Vi vs the Church of Emacs, I feel the two editors are complementary, rather than antagonistic. They have a very different “look and feel”, but that’s not a real reason for choosing one over the other. They were designed for different jobs, they are better at different things, and I use both of them, depending on the job.

Specifically, I use GNU Emacs and Vim, and every time I say “Emacs” or “Vi”, assume these programs unless proven otherwise.




Vi and Emacs are alike:

  • Full-function editors
    • Indenting, reformatting.
    • Syntax highlighting.
    • Unlimited undo and redo.
    • Multiple cut and paste clipboards.
    • Spell checking.
    • Multiple language support (including Unicode, R-to-L for Hebrew, Arabic, etc.)
    • Extendable
      • Macros and functions.
      • Fully customizable keys vs. actions.
    • Handle text strings, full lines, rectangles.
    • Adapt to type of file (.txt, .c, .html, etc.) being edited.
    • No line length or file size limits, within reason.
    • Automatically handle UNIX, MAC, DOS line endings (LF, CR, CRLF respectively).
    • Binary file editing.
    • Well documented, both internally and via available books and ebooks.
  • TTY and GUI versions.
    • The command emacs will start in either TTY or GUI mode, depending on where it is invoked. Use emacs -nw to force the program to use an Xterm window in text mode instead of starting its own.
    • The command vi[m] always starts in TTY mode, even in an Xterm session; use gvim or vi[m] -g to start the graphical version.
  • Implemented on many platforms:
    • Unix/Linux
    • Win32
    • Mac
    • Etc.
  • Widespread Availability
    • Both will be on any Unix/Linux machine
    • Freely available for download for Windows, Mac, etc.
  • Several versions available:
    • The original GNU Emacs from RMS, and Xemacs, forked from an earlier version of GNU Emacs source, are the most common. Other editors like Epsilon and JOVE are Emacs derivatives.
    • The original UNIX Vi (which almost nobody uses), Nvi (New Vi), Vim (Vi Improved), Vile (Vi like Emacs), Elvis.
    • For Linux distributions at least, “emacs” gives you the GNU version, and “vi” gives you vim.



Vi and Emacs are different:

  • Most simply, Vi was designed to write PROGRAMS, with all other types of file possible. Emacs was designed to write TEXT, with all other types of file possible.
  • Vi is much smaller and loads much faster; it will fit on a diskette while Emacs quite definitely will not.
  • The Biggie: Emacs is modeless, Vi has modes.
    • This relates to how an editor performs its two main functions: entering text and executing commands relating to the text.
    • Most modern editors and word processors are modeless, so that a user may enter text (e.g., typing “A”) or a command (^S to save the file) at any time.
    • Vi has three modes:
      • Text insert mode
      • Keystroke command mode (vi mode)
      • Command line mode (ex mode)
  • Plus and Minus for modeless (Emacs):
    • (+) Familiar.
    • (+) Easily adapts to standard GUI pull-down menu style.
    • (-) Commands have complicated syntax and hard-to-type keystrokes, since a limited number of keys are available for commands.
    • (-) Some needed keys might not be available on all keyboards.
  • Plus and Minus for modes (Vi):
    • (-) Confusion about which mode program is currently in.
    • (-) Nuisance of switching modes frequently.
    • (+) Many more keystrokes (~90) available for commands.
    • (+) Touch typists tend to become very proficient because the fingers can stay in their normal positions almost all the time.
  • The bash shell can be configured for either style through the commands set -o emacs (the default) or set -o vi




Vi and Emacs Keystroke Usage

Here are a few typical commands, showing the differences between “mode” style and “modeless” style:

                        VI        EMACS
		        --        -----
Right one column        k         ^f
Right one word          w         Esc,f
Left one word           b         Esc,b
Next sentence           )         Esc,e
Previous sentence       (         Esc,a
Save file               :w        ^x,^s
Delete paragraph        d}        Esc,x,kill-p[TAB],[RET]
Edit a new file         :ename   ^x,^f,name
RegEx search for "foo"  /foo      Esc,^sfoo
Repeat search           n         ^s,[RET]
Exit                    :q or QQ  ^x,^c
Save and Exit           :x        ^x,^s,^x,k,[RET]
Repeat last search      n         ^s,[RET]
Paste from clipboard    p         ^y
Delete 7 lines          7dd       ^a,Esc,7,^k
Undo                    u         ^x,u  or ^/
Change a letter to "x"  rx        ^d,x
Go to line 6            :6[RET]   Esc,<,Esc,5,^n
   ..or                 6G        Esc,x,goto-l[TAB][RET],6[RET]

You can easily see Vi tends to have simpler commands (in command mode) because it has all the “ordinary” letters and numbers available for navigational use.

Note that the Emacs documentation makes frequent mention of the “Meta” key, including key sequences like M-a, etc. Since most keyboards do not have such a key, M-a, for example, can be done two different ways:

  • Esc,a That is, hit the Escape key, let go, then hit the “a” key. This will work on all keyboards.
  • Alt-a That is, while holding down the Alt key, press “a”, then release both. This will usually work on a local PC keyboard (one actually attached to the machine where the editing is taking place), but almost certainly will not work on a remote terminal.



Vi Mode Navigation

  • [ESC] always enters vi (keystroke command) mode. If already there, it just beeps. (Hint: Bottom line is blank)
  • From vi mode, colon (:) enters ex (command line) mode. (Hint: colon (:) and cursor show on bottom line)
  • From vi mode, various commands enter insert mode: i (insert), a(append), o (open line), cw (change word), etc. (Hint: Cursor doesn’t move, –INSERT– shows on bottom line)



GUI vs TTY

  • The Vi TTY program loads very quickly, and is particularly useful for small changes to files.
  • SSH and Telnet are text-only, so you need to use the TTY version of either editor if remotely connected.
  • If you plan to spend a long time in the editor, the GUI versions are the better choice:
    • They start in different windows, so the command line is still there.
    • Font size, color scheme, window dimensions easily adjustable.
    • Editing several files at once is easier.



Emacs LISP

Emacs actually consists of a LISP interpreter executing a few pre-compiled primitive routines written in C plus about 200,000 lines of LISP code to implement all the functions of the editor. This makes Emacs extremely flexible, since an experienced LISP programmer can change anything and everything, as well as create new actions the program’s author never thought of.

Emacs can be made into an e-mail client, a web browser, a chess opponent, etc. by simply adding the proper code. A single keystroke can be mapped to execute an entire LISP program, which uses the file being edited as its subject matter. For example, here is a segment of my .emacs file, which defines a function called lookat-file and then “binds” it to the CTRL-F key.

(defun lookat-file ()
"Edit file with name delimited by colon at beginning of current line."
  (interactive)
  (save-excursion
    (save-match-data
      (beginning-of-line)
      (search-forward-regexp "^\\(.*\\):")
      (find-file (match-string 1)))))

(define-key global-map "^F" 'lookat-file)

LISP functions are all defined the same way:

(funcname arg1 arg2 …)

The last line in the example calls the function define-key with three arguments: the variable global-map, the literal ^F (the CTRL-F key), and the function name lookat-file, previously defined.

All built-in functions (like define-key and variables (like global-map) are fully documented in the extensive Emacs help system. In the definition of lookat-file shown above, every single keyword is a built-in LISP function.

Note that Emacs is fairly easy to port to a new architecture, because the “look and feel” is entirely defined by the LISP code, and that is unchanged whether Emacs is running on Solaris, Linux, Win98, or whatever. The only code that has to be modified is the LISP interpreter itself plus the primitives that handle platform-dependent stuff like file i/o and the other interfaces to the OS.




LISP Structure

LISP is actually very simple, because everything has the same form, namely a list of items enclosed in parentheses, and all program operations are function calls. Variables are untyped. For example:

(+ 2 3)             ; add 2 and 3, return 5 as the value of the function.
(setq foo "John")   ; store the string "John" into the variable foo.
(setq foo (+ 2 3))  ; store 5 into foo
(setq foo (and huey dewey louie))
                    ; set foo true if all three are true, else false
(setq bar (* (+ 2 3) (- 6 2) (* 2 2) (sqrt 9)))
                    ; store 240 into bar.
(setq ans (if (< foo bar) 1 2))
                    ; ans is 1 if foo is less than bar, 2 otherwise
(defun myfun (arglist) statements)
                    ; define function myfun

From these examples, it is easy to see why it’s a standard joke that LISP (which really stands for LISt Processor) is an acronym for “Lots of Irritating Silly Parentheses”. BTW, Vi enthusiasts tend to claim that Emacs is an acronym for “Eight Meg and Continuously Swapping”. Note that this joke has been around since the days when eight MB was a lot of memory. On the other hand, Emacs bigots will refer to Vi as “six”. (In which case Vim would be what, 994?)

I have to correct one statement I made earlier. Actually, Emacs also has a command-line. Typing Alt-x (or Esc,x) puts the cursor into what Emacs calls the “mini-buffer” at the bottom of the screen, where the user can execute thousands of built-in or user-defined LISP routines. For example, I could execute my lookat-file function by typing Alt-x lookat-file[ENTER]. (Emacs has tab completion just like bash or zsh, so in practice I would have typed Alt,x loo[TAB][ENTER], there being no other LISP function on my machine that starts with those three letters.)

Bottom Line

I use both editors regularly. Sometimes the choice is random, but for some tasks I will always use either Vi or Emacs.

  • Emacs GUI is much easier to use for large text jobs – writing the Great American Novel or whatever – and producing a “clean” file.
  • Vi TTY is always used for editing .rc files, producing quick-and-dirty scripts, etc.
  • I have extended Emacs to perform a couple of tasks that neither editor will do automatically in their virgin state.
  • Gvim is better integrated with Xwindows – easier “look and feel”, better fonts, better syntax highlighting, a button bar, etc. It looks very different from the TTY version, whereas the TTY and GUI versions of Emacs look very much the same, but some of that is because Emacs managed to implement a “pull-down” menu system in TTY mode!
  • Emacs is better at editing many files at once. It is simple, for example, to switch to a directory containing a programming project and say: emacs Makefile *.[ch] to edit the whole project at the same time. Both editors can issue the actual “make” from within the editor, capture and parse the error messages, and position the cursor on the correct line in the correct file.


    Useful Links

    My .emacs customization
    My .vimrc customization
    Vim Home Page
    Gnu Emacs Home Page
    Win32 Emacs precompiled binary
    Win32 Vim precompiled binaries You need files gvim70.exe and vim70d32.zip.
    Cygwin Unix Utilities for Win32 (rm, ls, egrep, etc.) Click on “Install Now”
    O’Reilly & Associates, publisher of full books and pocket references for both GNU Emacs and Vi (including Vim).
    New Riders, publisher of Vi Improved, by Steve Oualline.


    Last modified: Wed Apr 11 16:28:18 2007


Emacs常用命令速查

Category: Linux

1)与文件操作有关的命令
C-x C-f 查找文件并且在新缓冲区中打开
C-x C-v 读入另一个文件替换掉用C-x C-f打开的文件
C-x i 把文件插入到光标的当前位置
C-x C-s 保存文件
C-x C-w 把缓冲区内容写入一个文件
C-x C-c 退出Emacs 2)与光标移动操作有关的命令
C-f 光标前移一个字符(右)
C-b 光标后移一个字符(左)
C-p 光标前移一行(上)
C-n 光标后移一行(下)
M-f 前移一个单词
M-b 后移一个单词
C-a 移动到行首
C-e 移动到行尾
M-e 前移一个句子
M-a 后移一个句子
M-} 前移一个段落
M-{ 后移一个段落
C-v 屏幕上卷一屏
M-v 屏幕下卷一屏
C-x ] 前移一页
C-x [ 后移一页
M-< 前移到文件头
M->; 后移到文件尾
C-l 重新绘制屏幕,当前行放在画面中心
M-n 或者 C-u n 重复执行n次后续命令
按下M-x后在辅助输入区中输入”goto-line”跳到指定的行,输入”goto-char”跳到指定的字符

3)与文件删除操作有关的命令
C-d 删除光标位置上的字符
DEL 删除光标前面的字符
M-d 删除光标后面的单词
M-DEL 删除光标前面的单词
C-k 从光标位置删除到行尾
M-k 删除光标后面的句子
C-x DEL 删除光标前面的句子
C-y 恢复被删除的文本或者粘贴最近删除或复制的文本
C-w 删除文件块
按下M-x后在辅助输入区中输入”kill-paragraph”删除光标后面的段落,按下”backward-kill-paragraph”删除光标前面的段落

4)与文本块操作有关的命令
C-@ 标记文本块的开始(或结束)位置
C-x C-x 互换插入点和文本标记的位置
C-w 或 SHIFT-DEL 删除文本块
M-w 复制文本块
M-h 标记段落
C-x C-p 标记页面
C-x h 标记整个缓冲区

5)与位置交换操作有关的命令
C-t 交换两个字符的位置
M-t 交换两个单词的位置
C-x C-t 交换两个文本行的位置
按下M-x后在辅助输入区中输入”transpose-sentences”交换两个句子的位置,按下”transpose-paragraph”交换两个段落的位置

6)与改变字母大小写操作有关的命令
M-c 单词首字母改为大写
M-u 单词的字母全部改为大写
M-l 单词的字母全部改为小写

7)与查找操作相关的命令
C-s 向前递增查找
C-r 向后递增查找
C-s C-w 开始递增查找,把光标位置的单词做查找字符串
C-s C-y 开始递增查找,把光标位置到行尾之间的文本做查找字符串
C-s return searchstring return 向前开始非递增查找操作
C-r return searchstring return 向后开始非递增查找操作
C-s return C-w 向前开始单词查找(不受换行符、空格、标点符号影响)
C-r return C-w 向后开始单词查找(不受换行符、空格、标点符号影响)

与使用编辑缓冲区和窗口有关的命令
C-x b 如果输入一个新的文件名则新建一个文件并且编辑,否则打开该文件
C-x s 保存全部缓冲区
C-x b 删除缓冲区
M-x rename-buffer 重命名当前缓冲区
C-x C-q 把当前编辑缓冲区设置为只读属性
C-x 0 删除当前所在的窗口
C-x 1 当前缓冲区满屏显示
C-x 2 创建上下排列的窗口
C-x 3 创建左右排列的窗口
C-x o 在窗口之间移动



Vi 简单用法

Category: Linux

一、基本命令介绍  —- 1.光标命令

k、j、h、l–上、下、左、右光标移动命令。虽然您可以在linux中使用键盘右边的4个光标键,但是记住这4个命令还是非常有用的。这4个键正是右手在键盘上放置的基本位置。

nG–跳转命令。n为行数,该命令立即使光标跳到指定行。

Ctrl+G–光标所在位置的行数和列数报告。

w、b–使光标向前或向后跳过一个单词。

—- 2.编辑命令

i、a、r–在光标的前、后以及所在处插入字符命令(i=insert、a=append、r=replace)。

cw、dw–改变(置换)/删除光标所在处的单词的命令 (c=change、d=delete)。

x、d$、dd–删除一个字符、删除光标所在处到行尾的所有字符以及删除整行的命令。

—- 3.查找命令

—- /string、?string–从光标所在处向后或向前查找相应的字符串的命令。

—- 4.拷贝复制命令

—- yy、p–拷贝一行到剪贴板或取出剪贴板中内容的命令。

二、常见问题及应用技巧

—- 1.在一个新文件中读/etc/passwd中的内容,取出用户名部分。

—- vi file

—- :r /etc/passwd 在打开的文件file中光标所在处读入/etc/passwd

—- :%s/:.*//g 删除/etc/passwd中用户名后面的从冒号开始直到行尾的所有部分。

—- 您也可以在指定的行号后读入文件内容,例如使用命令”:3r /etc/passwd”从新文件的第3行开始读入 /etc/passwd的所有内容。

—- 我们还可以使用以下方法删掉文件中所有的空行及以#开始的注释行。

—- #cat squid.conf.default | grep -v ‘^$’ | grep -v ‘^#’

—- 2.在打开一个文件编辑后才知道登录的用户对该文件没有写的权限,不能存盘,需要将所做修改存入临时文件。

—- vi file

—- :w /tmp/1 保存所做的所有修改,也可以将其中的某一部分修改保存到临时文件,例如仅仅把第20~59行之间的内容存盘成文件/tmp/1,我们可以键入如下命令。

—- vi file

—- :20,59w /tmp/1

—- 3.用VI编辑一个文件,但需要删除大段的内容。

—- 首先利用编辑命令”vi file”打开文件,然后将光标移到需要删除的行处按Ctrl+G显示行号,再到结尾处再按Ctrl+G,显示文件结尾的行号。

—- :23,1045d 假定2次得到的行号为23和1045,则把这期间的内容全删除,也可以在要删除的开始行和结束行中用ma、mb命令标记,然后利用”:’a,’bd”命令删除。

—- 4.在整个文件的各行或某几行的行首或行尾加一些字符串。

—- vi file

—- :3,$s/^/some string / 在文件的第一行至最后一行的行首插入”some string”。

—- :%s/$/some string/g 在整个文件每一行的行尾添加”some string”。

—- :%s/string1/string2/g 在整个文件中替换”string1″成”string2″。

—- :3,7s/string1/string2/ 仅替换文件中的第3行到第7行中的”string1″成”string2″。

—- 注意: 其中s为substitute,%表示所有行,g表示global。

—- 5.同时编辑2个文件,拷贝一个文件中的文本并粘贴到另一个文件中。

—- vi file1 file2

—- yy 在文件1的光标处拷贝所在行

—- :n 切换到文件2 (n=next)

—- p 在文件2的光标所在处粘贴所拷贝的行

—- :n 切换回文件1

—- 6.替换文件中的路径。

—- 使用命令”:%s#/usr/bin#/bin#g”可以把文件中所有路径/usr/bin换成/bin。也可以使用命令”:%s//usr/bin//bin/g”实现,其中”"是转义字符,表明其后的”/”字符是具有实际意义的字符,不是分隔符。本文来自:http://www.linuxpk.com/126.html