2011-12-21 19:03

[轉載] Aptana Scripting 一個背景處理的範例

轉載自:Monkeying with Eclipse | Info Support

  1. /* 
  2. * Menu: Info Support > Find commented code 
  3. * Kudos: Peter Hendriks 
  4. * License: EPL 1.0 
  5. * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript 
  6. */ 
  7. function main() { 
  8.    loadBundle("org.eclipse.core.jobs"); 
  9.  
  10.    var ProgressMonitorDialog = Packages.org.eclipse.jface.dialogs.ProgressMonitorDialog; 
  11.    var IRunnableWithProgress = Packages.org.eclipse.jface.operation.IRunnableWithProgress; 
  12.  
  13.    var runnableWithProgress = new IRunnableWithProgress({ run: runCommentSearch }); 
  14.    new ProgressMonitorDialog(window.getShell()).run(true, true, runnableWithProgress); 
  15.  
  16.    window.getActivePage().showView("org.eclipse.ui.views.TaskList"); 
  17. } 
  18.  
  19. function runCommentSearch(monitor) { 
  20.    var files = resources.filesMatching(".*\\.java"); 
  21.    monitor.beginTask("Searching for commented code...", files.length); 
  22.    try { 
  23.        var match; 
  24.        for each( file in files ) { 
  25.            monitor.subTask(file.getEclipseObject().getName()); 
  26.            file.removeMyTasks(); 
  27.            var previousLineCodeComment = false; 
  28.            for each( line in file.lines ) { 
  29.                if (monitor.isCanceled()) { 
  30.                    return; 
  31.                } 
  32.                if (match = line.string.match(/^.*\/\/.*[;{}]\s*$/)) { 
  33.                    if (!previousLineCodeComment) { 
  34.                        line.addMyTask("Commented code: " + match[0]); 
  35.                    } 
  36.                    previousLineCodeComment = true; 
  37.                } else { 
  38.                    previousLineCodeComment = false; 
  39.                } 
  40.            } 
  41.            monitor.worked(1); 
  42.        } 
  43.    } finally { 
  44.        monitor.done(); 
  45.    } 
  46. } 
2011-12-21 18:58

Aptana Scripting - Find TODOs 範例中的參數

  1. /* 
  2. * Menu: Editors > Find TODOs 
  3. * Kudos: Ingo Muschenetz 
  4. * License: EPL 1.0 
  5. * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript 
  6. */ 
  7.  
  8. function main() { 
  9.    var files = resources.filesMatching(".*\\.js"); 
  10.    var match; 
  11.  
  12.    for each( file in files ) { 
  13.        file.removeMyTasks( ); 
  14.        for each( line in file.lines ) { 
  15.            if (match = line.string.match(/\/\/TODO: (.*)/)) { 
  16.                line.addMyTask( match[1] ); 
  17.            } 
  18.        } 
  19.    } 
  20.    window.getActivePage().showView("org.eclipse.ui.views.TaskList"); 
  21. } 

只列出當前文件的方法
  1. var files = resources.filesMatching('.*/'+editors.activeEditor.textEditor.titleToolTip); 

resources 部分的方法
  1. filesMatching(".*\\.js") 
  2. filesMatchingForProject("Project Name",".*\\.js") 
  3. filesMatchingIgnoreCase(".*\\.js") 
  4. filesMatchingForProjectIgnoreCase("Project Name",".*\\.js") 

file 部分的方法
  1. size 
  2. lines 
  3. removeMyTasks() 

line 部分的方法
  1. lineNumber 
  2. string 
  3. addMyTask('Task String') 
2011-12-21 18:46

Aptana Scripting 學習筆記

  • 在任何一個專案的頂層目錄,建立一個名稱為 scripts 或 monkey 的目錄
  • 在此目錄下建立副檔名為 *.js 或 *.em 的 JavaScript 的文件

一個空白文件的內容如下:
  1. /* 
  2. * Menu: Samples > Execute Snippet 
  3. * Key: M1+M2+M3+F 
  4. * Kudos: Jax Hu 
  5. * License: EPL 1.0 
  6. * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript 
  7. */ 
  8.  
  9. function main(){ 
  10. } 


快捷鍵的代號對應:
M1Control/Command
M2Shift
M3Alt/Option


一個可以用來顯示物件成員的函數:
  1. /**顯示物件的成員 
  2. * @param {Object} val 物件 
  3. */ 
  4. function var_dump(val){ 
  5.    var name, value, temp=[]; 
  6.  
  7.    for (name in val) { 
  8.        try { 
  9.            value = (val[name]+''). 
  10.                replace("function",'<b style="color:#00f;">$&</b>'); 
  11.        }catch (e) { 
  12.            value = (e+'').fontcolor('red'); 
  13.        } 
  14.        temp.push('<b style="color:#707;">'+name+'</b> = '+value); 
  15.    } 
  16.  
  17.    webView = views.getView("var_dump"); 
  18.    webView.showView(true); 
  19.    webView.setTitle('var_dump'); 
  20.    webView.setHTML(temp.join("<hr/>").fixed()); 
  21. } 


常用方法以及數值:
  1. /*當前文件的位置 
  2. * => D:/WorkSpace/my_project/test.js */ 
  3. location 
  4.  
  5. /*當前文件的名稱 
  6. * => test.js */ 
  7. editors.activeEditor.title 
  8.  
  9. /*當前的文件內容*/ 
  10. editors.activeEditor.source 
  11.  
  12. /*當前文件的 URI 位址*/ 
  13. editors.activeEditor.uri 
  14.  
  15. /*儲存當前文件*/ 
  16. editors.activeEditor.save(); 
  17.  
  18. /*對當前文件-開啟另存新檔的對話匡*/ 
  19. editors.activeEditor.textEditor.doSaveAs() 
  20.  
  21. /*對當前文件的目錄路徑 
  22. * => D:/WorkSpace/my_project */ 
  23. editors.activeEditor.textEditor.editorInput.file.parent.location 
  24.  
  25. /*目前開啟的所有文件*/ 
  26. editors.all[]; 
  27.  
  28. /*儲存全部編輯器,傳入 true 會開啟存檔提示*/ 
  29. window.workbench.saveAllEditors(false); 
  30.  
  31. /*重新開啟 Eclipse*/ 
  32. window.workbench.restart(); 
  33.  
  34. /*關閉 Eclipse*/ 
  35. window.workbench.close(); 
  36.  
  37. /* 開新的編輯器,可以透過這個開啟空白文件,或是已經存在的檔案, 
  38. * 之後 editors.activeEditor 會轉到這個文件上 */ 
  39. fileUtils.open('textile_to_redmine.txt'); 
  40.  
  41. /*取得當前的專案名稱*/ 
  42. editors.activeEditor.textEditor.editorInput.file.project.name; 
  43.  
  44. /*取得所有的專案*/ 
  45. Packages.org.eclipse.core.resources.ResourcesPlugin.workspace.root.projects; 
  46. editors.activeEditor.textEditor.editorInput.file.workspace.root.projects 


建立一個新的視圖,或開啟已存在的視圖:
  1. webView = views.getView("my_view_name"); 
  2.  
  3. /*顯示視圖*/ 
  4. webView.showView(true); 
  5.  
  6. /*設定標題*/ 
  7. webView.setTitle("My View Title"); 
  8.  
  9. /*設定內容的HTML*/ 
  10. webView.setHTML('<h1>OK</h1>'); 
  11.  
  12. /*或指定內容的網址*/ 
  13. webView.url = "http://www.google.com"; 
  14. webView.addEventListener("LocationChanging", function(event){ 
  15.    var location = event.innerEvent.location; 
  16.  
  17.    // Print out the location to the Java console 
  18.    Packages.java.lang.System.out.println("You clicked on: " + location); 
  19. }); 


替換選擇的文字區段:
  1. /*選擇的起始位置*/ 
  2. var starting = editors.activeEditor.selectionRange.startingOffset; 
  3.  
  4. /*選擇的結束位置*/ 
  5. var ending = editors.activeEditor.selectionRange.endingOffset; 
  6.  
  7. /*選擇的文字內容*/ 
  8. var text = editors.activeEditor.source.substring(starting, ending); 
  9.  
  10. /*文字跳脫處理,或其他自訂的處理*/ 
  11. text = escape(text); 
  12.  
  13. /*替換選擇的文字*/ 
  14. editors.activeEditor.applyEdit(starting, ending-starting, text); 
  15.  
  16. /*重新選擇文字區段*/ 
  17. editors.activeEditor.selectAndReveal(starting, text.length); 


檔案存取:
  1. var file = new File("myFile.txt"); 
  2. file.createNewFile(); 
  3. file.write("Date: "); 
  4. var text = file.readLines(); 


Web 資料請求的方式:
  1. var req = new WebRequest(); 
  2. req.open("GET", "http://xml.weather.yahoo.com/forecastrss?p=94103"); 
  3. var text = req.send(); 
2011-12-21 15:05

[轉載] Aptana Scripting File.js Class Document

轉載自:Koders Code Search: File.js - JavaScript
  1. /** 
  2. * File.js 
  3. * 
  4. * Adding this file to your Active Libraries will give you code assist for the 
  5. * Aptana Studio scripting engine. 
  6. * 
  7. * @author Kevin Lindsey 
  8. * @version 1.0 
  9. */ 
  10.  
  11. /** 
  12. * This object represents a file or a directory in the file system 
  13. * 
  14. * @constructor 
  15. * @param {String} name 
  16. *      The relative or absolute path to a file or directory 
  17. */ 
  18. function File(name) {} 
  19.  
  20. /* 
  21. * Properties 
  22. * 
  23.  
  24. /** 
  25. * Get the absolute path for this file or directory 
  26. * 
  27. * @type {String} Returns the absolute path to this file or directory 
  28. */ 
  29. File.prototype.absolutePath = ""; 
  30.  
  31. /** 
  32. * Get the file's base name. This is the filename only without the extension. 
  33. * If the file does not have an extension, then this will return the full 
  34. * name 
  35. * 
  36. * @type {String} Returns this file's base name 
  37. */ 
  38. File.prototype.baseName = ""; 
  39.  
  40. /** 
  41. * Determine if this file is readable 
  42. *  
  43. * @type {Boolean} Returns true if this File is readable 
  44. */ 
  45. File.prototype.canRead = false; 
  46.  
  47. /** 
  48. * Determine if ths file is writable 
  49. *  
  50. * @type {Boolean} Returns true if this File is writable 
  51. */ 
  52. File.prototype.canWrite = false; 
  53.  
  54. /** 
  55. * Determine if this file or directory exists in the file system 
  56. * 
  57. * @type {Boolean} Returns true if this File exists in the file system 
  58. */ 
  59. File.prototype.exists = false; 
  60.  
  61. /** 
  62. * Returns the file extension of this File 
  63. * 
  64. * @type {String} Returns the last instance of "." and the text after it. 
  65. *      An empty string will be returned if no extension if found. The return 
  66. *      value includes the '.' 
  67. */ 
  68. File.prototype.extension = ""; 
  69.  
  70. /** 
  71. * Determines if thie File is a file in the file system 
  72. * 
  73. * @type {Boolean} Returns true if this File is a file in the file system 
  74. */ 
  75. File.prototype.isFile = false; 
  76.  
  77. /** 
  78. * Determines if this File is a directory in the file system 
  79. * 
  80. * @type {Boolean} Returns true if this File is a directory in the file 
  81. *      system 
  82. */ 
  83. File.prototype.isDirectory = false; 
  84.  
  85. /** 
  86. * Returns a list of File objects for all files in the File. This is equivalent 
  87. * to listing out all files in a directory 
  88. * 
  89. * @type {Array} Returns an array of File objects, one for each file and 
  90. *      directory in this File 
  91. */ 
  92. File.prototype.list = []; 
  93.  
  94. /** 
  95. * Returns the file's name without path information 
  96. * 
  97. * @type {String} Returns the file's name 
  98. */ 
  99. File.prototype.name = ""; 
  100.  
  101. /** 
  102. * Returns a new File object of this object's parent directory 
  103. * 
  104. * @type {File} Returns this file's parent File 
  105. */ 
  106. File.prototype.parentFile = {}; 
  107.  
  108. /** 
  109. * Returns the character used to separate directories on the underlying OS 
  110. * 
  111. * @type {String} Returns the directory separator 
  112. */ 
  113. File.prototype.separator = ""; 
  114.  
  115. /* 
  116. * Methods 
  117. */ 
  118.  
  119. /** 
  120. * Create a new file in the file system at the location specified by this 
  121. * object 
  122. * 
  123. * @return {Boolean} Returns true if the file was created successfully. 
  124. */ 
  125. File.prototype.createNewFile = function() {}; 
  126.  
  127. /** 
  128. * Return all lines from this File's text file 
  129. *  
  130. * @return {Array} Returns an array of strings, one for each line in the file. 
  131. */ 
  132. File.prototype.readLines = function() {}; 
  133.  
  134.  
  135. //eof 
2011-12-21 15:05

[轉載] Aptana Scripting PrintStream.js Class Document

轉載自:Koders Code Search: PrintStream.js - JavaScript
  1. /** 
  2. * PrintStream.js 
  3. * 
  4. * Adding this file to your Active Libraries will give you code assist for the 
  5. * Aptana Studio scripting engine. 
  6. * 
  7. * @author Kevin Lindsey 
  8. * @version 1.0 
  9. */ 
  10.  
  11. /** 
  12. * This object represents an underlying Java PrintStream 
  13. * 
  14. * @constructor 
  15. * 
  16. / 
  17. function PrintStream() {} 
  18.  
  19. /* 
  20. * Methods 
  21. */ 
  22.  
  23. /** 
  24. * Print the given text to the underlying stream 
  25. * 
  26. * @param {String} text 
  27. *      The text to send to the stream 
  28. */ 
  29. PrintStream.prototype.print = function(text) {}; 
  30.  
  31. /** 
  32. * Print the given text to the underlying stream followed by end-of-line 
  33. * 
  34. * @param {String} text 
  35. *      The text to send to the stream 
  36. */ 
  37. PrintStream.prototype.println = function(text) {}; 
  38.  
  39.  
  40. //eof 
2011-12-21 15:04

[轉載] Aptana Scripting Editor.js Class Document

轉載自:Koders Code Search: Editor.js - JavaScript
  1. /** 
  2. * Editor.js 
  3. * 
  4. * Adding this file to your Active Libraries will give you code assist for the 
  5. * Aptana Studio scripting engine. 
  6. * 
  7. * @author Kevin Lindsey 
  8. * @version 1.0 
  9. */ 
  10.  
  11. /** 
  12. * Editor 
  13. * 
  14. * @constructor 
  15. * @extends {EventTarget} 
  16. */ 
  17. function Editor() {} 
  18.  
  19. /* 
  20. * Properties 
  21. */ 
  22.  
  23. /** 
  24. * Get/set the position of the cursor in this editor 
  25. *  
  26. * @type {Number} The current cursor offset position 
  27. */ 
  28. Editor.prototype.currentOffset = 0; 
  29.  
  30. /** 
  31. * Get the File object that this editor is editing 
  32. * 
  33. * @type {File} Returns a File object or undefined 
  34. */ 
  35. Editor.prototype.file = {}; 
  36.  
  37. /** 
  38. * Get the language MIME type for this editor 
  39. *  
  40. * @type {String} Returns this editors language type 
  41. */ 
  42. Editor.prototype.language = ""; 
  43.  
  44. /** 
  45. * Get the lexemes associated with this editor 
  46. * 
  47. * @type {Array} Returns an array of Lexemes 
  48. */ 
  49. Editor.prototype.lexemes = []; 
  50.  
  51. /** 
  52. * Get the line delimiter for this editor 
  53. *  
  54. * @type {String} Returns the editor's line terminator 
  55. */ 
  56. Editor.prototype.lineDelimiter = ""; 
  57.  
  58. /** 
  59. * Get the source associated with this editor 
  60. * 
  61. * @type {String} Returns the source text in this editor 
  62. */ 
  63. Editor.prototype.source = ""; 
  64.  
  65. /** 
  66. * Get the length of the source in this editor 
  67. *  
  68. * @type {Number} Returns the number of characters in this editor's document 
  69. */ 
  70. Editor.prototype.sourceLength = 0; 
  71.  
  72. /** 
  73. * Get the number of columns in a tab 
  74. *  
  75. * @type {Number} Returns the number of spaces that equal one tab 
  76. */ 
  77. Editor.prototype.tabWidth = 0; 
  78.  
  79. /** 
  80. * Get the zero-based line number of the line at the top of the editor 
  81. *  
  82. * @type {Number} The top-most line's index 
  83. */ 
  84. Editor.prototype.topIndex = 0; 
  85.  
  86. /** 
  87. * Get/set the editor's word wrap setting. Setting this to true turns on word 
  88. * wrapping. 
  89. *  
  90. * @type {Boolean} The word wrap setting. 
  91. */ 
  92. Editor.prototype.wordWrap = false; 
  93.  
  94. /* 
  95. * Methods 
  96. */ 
  97.  
  98. /** 
  99. * Apply an edit to the current document. This function allows you to delete 
  100. * and insert text in one operation, if desired. 
  101. * 
  102. * @param {Number} offset 
  103. *      The offset within the source where this edit is to take place 
  104. * @param {Number} deleteLength 
  105. *      The number of characters to remove before inserting the new text 
  106. * @param {String} insertText 
  107. *      The new text to insert at the given offset 
  108. */ 
  109. Editor.prototype.applyEdit = function(offset, deleteLength, insertText) {}; 
  110.  
  111. /** 
  112. * Get the zero-based line number at the specified character offset 
  113. *  
  114. * @param {Number} offset 
  115. *       The character offset within the editor's document 
  116. */ 
  117. Editor.prototype.getLineAtOffset = function(offset) {}; 
  118.  
  119. /** 
  120. * Scroll the editor to bring the current selection or caret position into 
  121. * view. 
  122. */ 
  123. Editor.prototype.showSelection = function() {}; 
  124.  
  125.  
  126. //eof 
2011-12-21 15:04

[轉載] Aptana Scripting Global.js Document

轉載自:Koders Code Search: Global.js - JavaScript
  1. /** 
  2. * Global.js 
  3. * 
  4. * Adding this file to your Active Libraries will give you code assist for the 
  5. * Aptana Studio scripting engine. 
  6. * 
  7. * @author Kevin Lindsey 
  8. * @version 1.0 
  9. */ 
  10.  
  11. /* 
  12. * Properties 
  13. */ 
  14.  
  15. /** 
  16. * Retrieve the Editors object to access editors and editor events 
  17. * 
  18. * @type {Editors} Returns the global Editors object 
  19. */ 
  20. var editors = {}; 
  21.  
  22. /** 
  23. * Retrieve the error print stream. 
  24. * 
  25. * @type {PrintStream} Returns the output stream used to display errors 
  26. */ 
  27. var err = {}; 
  28.  
  29. /** 
  30. * This is a reference to the only instance of this object. All scripts run in 
  31. * their own protected scope. However, this Global is accessible from all 
  32. * scripts. Properties placed on "global" will be accessible to all scripts 
  33. * 
  34. * @type {Global} Returns a reference to the global scope 
  35. */ 
  36. var global = {}; 
  37.  
  38. /** 
  39. * Retrieve the Menus object to access menus and menu events 
  40. * 
  41. * @type {Menus} Returns the global Menus object 
  42. */ 
  43. var menus = {}; 
  44.  
  45. /** 
  46. * Retrieve the standard output print stream. 
  47. * 
  48. * @type {PrintStream} Returns the standard output stream 
  49. */ 
  50. var out = {}; 
  51.  
  52. /** 
  53. * Retrieve the View object to access views and view events 
  54. * 
  55. * @type {Views} Returns the global Views object 
  56. */ 
  57. var views = {}; 
  58.  
  59. /* 
  60. * Methods 
  61. */ 
  62.  
  63. /** 
  64. * Display an alert dialog with the given message 
  65. * 
  66. * @param {String} message 
  67. *      The message to display in the dialog 
  68. */ 
  69. var alert = function(message) {}; 
  70.  
  71. /** 
  72. * Execute a string in the current shell. This is experimental and may be 
  73. * removed in a future version of the scripting environment 
  74. * 
  75. * @param {String} command 
  76. *      The command to execute in the shell 
  77. * @return {Object} Returns an object with the following properties: code, 
  78. *      stdout, stderr. Code is the return code from the command. Stdout 
  79. *      contains any text that was emitted to standard out while it was 
  80. *      executing. Likewise, stderr contains any errors that were emitted. 
  81. */ 
  82. var execute = function(command) {}; 
  83.  
  84. /** 
  85. * Call Java's System.getProperty. 
  86. * 
  87. * @param {String} property 
  88. *      The name of the property to retrieve 
  89. * @return {String} Returns the specified property value or the string 
  90. *      "undefined" if the property does not exist 
  91. */ 
  92. var getProperty = function(property) {}; 
  93.  
  94. /** 
  95. * Include a JavaScript file into the current script's scope. this is used to  
  96. * load dependent libraries into the script that invokes this function. 
  97. *  
  98. * @param {String} filename 
  99. *      The name of the file to include in the script 
  100. */ 
  101. var include = function(filename) {}; 
  102.  
  103. /** 
  104. * Load a library into the scripting environment. Each script loaded with this 
  105. * function will be assigned a unique ID and, if it exists, the init() function 
  106. * will be invoked. This gives each script the ability to initialize itself and 
  107. * to setup any event listeners it wishes to subscribe to. 
  108. * 
  109. * Each script will exist in its own scope; however, this Global is also 
  110. * included in the scope chain. All variables and functions defined in the 
  111. * script will not collide with any other scripts. 
  112. * 
  113. * Shared properties can be placed on the "global" property. All scripts loaded 
  114. * via this function will then be able to see those properties. This can be 
  115. * used to share data between scripts. 
  116. * 
  117. * @param {String} filename 
  118. *      The file system path to the script to load 
  119. * @return {String} Returns a unique string identifier for the loaded script. 
  120. *      This identifier can be used later to invoke functions within the 
  121. *      script; however, this is more for internal use at this point. If the 
  122. *      script fails to load, this will return undefined. 
  123. */ 
  124. var loadBundle = function(filename) {}; 
  125.  
  126.  
  127. //eof 
2011-12-21 15:04

[轉載] Aptana Scripting Editors.js Class Document

轉載自:Koders Code Search: Editors.js - JavaScript
  1. /** 
  2. * Editors.js 
  3. * 
  4. * Adding this file to your Active Libraries will give you code assist for the 
  5. * Aptana Studio scripting engine. 
  6. * 
  7. * @author Kevin Lindsey 
  8. * @version 1.0 
  9. */ 
  10.  
  11. /** 
  12. * Editors 
  13. * 
  14. * @constructor 
  15. */ 
  16. function Editors(){} 
  17.  
  18. /* 
  19. * Properties 
  20. */ 
  21.  
  22. /** 
  23. * Get all editors currently being displayed 
  24. * 
  25. * @type {Array} Returns an array of Editor instances, one for each open editor 
  26. */ 
  27. Editors.prototype.all = []; 
  28.  
  29. /** 
  30. * Get the currently active editor 
  31. * 
  32. * @type {Editor} Returns the currently active editor or undefined if no editor is open 
  33. */ 
  34. Editors.prototype.activeEditor = {}; 
  35.  
  36. /* 
  37. * Methods 
  38. */ 
  39.  
  40. /** 
  41. * Get the object that represents all editors of a given type. This is 
  42. * typically used to register event handlers for a given event type for 
  43. * all editors of a specified language. 
  44. * 
  45. * @param {String} type 
  46. *      The editor type to retrieve. Currently, this is the MIME type for the 
  47. *      language the editor supports. 
  48. * @return {EditorType} Returns the editor type for the given MIME type or 
  49. *      undefined if no editor type exists for the given MIME type 
  50. */ 
  51. Editors.prototype.getEditorType = function(type) {}; 
  52.  
  53. /** 
  54. * Open the specified filename in a new editor 
  55. * 
  56. * @param {String} filename 
  57. *      The name of the file to open in the file system 
  58. * @return {Editor} Returns a new Editor object for the newly opened editor 
  59. */ 
  60. Editors.prototype.open = function(filename) {}; 
  61.  
  62.  
  63. //eof 
2011-12-21 14:32

[PHP] 兩種 while 的應用

滿足其中一個條件,就再來一次
  1. <?php 
  2. $a = 'OK'; 
  3. while(true){ 
  4.    if($a == "OK"){ 
  5.        var_dump('OK');         
  6.        $a = "NO"; 
  7.        continue; 
  8.  
  9.    }elseif($a == "NO"){ 
  10.        var_dump('NO');         
  11.        $a = "XXX"; 
  12.        continue; 
  13.  
  14.    }else{ 
  15.        var_dump('default');         
  16.    } 
  17.  
  18.    break;         
  19. } 


滿足其中一個條件,就跳過之後的判斷,同樣的程式流,也可以在 function 中用 return 做到。
  1. <?php 
  2. $a = 'OK'; 
  3. do{ 
  4.    if($a == "OK"){ 
  5.        echo 'OK'; break; 
  6.    }     
  7.  
  8.    if($a == "NO"){ 
  9.        echo 'NO'; break; 
  10.    }         
  11.  
  12.    if($a == "Off"){ 
  13.        echo 'NO'; break; 
  14.    }         
  15.  
  16. }while(false); 
2011-12-16 15:04

Sphinx 增量索引的方法

之前有寫過一篇 MySQL 全文檢索引擎 - Sphinx 的文章,最近又把它拿出來用了,不過當時即時更新索引的問題,如今則找到解決的方法了,透過更新增量索引的方式達到即時更新。

簡單的說就是利用兩個索引表的合併查詢來做到,一個是完整的索引表,一個是針對當日資料變動的增量索引。

部分的設定檔如下:
  1. # ... 
  2.  
  3. source _source_base 
  4. { 
  5.    # 來源-共用的設定 
  6. } 
  7.  
  8. source people_full : _source_base 
  9. { 
  10.    sql_query      = \ 
  11.        SELECT \ 
  12.            people_profile.id, \ 
  13.            people_profile.main_name \ 
  14.        FROM people_profile 
  15.  
  16.    sql_query_killlist = \ 
  17.        SELECT id FROM people_profile \ 
  18.        WHERE update_date >= CURDATE() 
  19. } 
  20.  
  21. # 增量索引來源,這邊只會抓出當日變動的資料 
  22. # 建議在 update_date 欄位加上 MySQL INDEX 
  23. source people_delta : people_full 
  24. { 
  25.    sql_query      = \ 
  26.        SELECT \ 
  27.            people_profile.id, \ 
  28.            people_profile.main_name \ 
  29.        FROM people_profile \ 
  30.        WHERE people_profile.update_date >= CURDATE() 
  31. } 
  32.  
  33.  
  34. index _index_base 
  35. { 
  36.    # 索引-共用的設定 
  37. } 
  38.  
  39. index people_full : _index_base 
  40. { 
  41.    source          = people_full 
  42.    path            = /var/lib/sphinxsearch/data/people_full 
  43. } 
  44.  
  45. # 增量索引 
  46. index people_delta : _index_base 
  47. { 
  48.    source          = people_delta 
  49.    path            = /var/lib/sphinxsearch/data/people_delta 
  50. } 
  51.  
  52. # 透過 distributed 類型來合併索引 
  53. index people 
  54. { 
  55.    type            = distributed 
  56.    local           = people_full 
  57.    local           = people_delta 
  58. } 
  59.  
  60. # ... 


建立 SphinxSE 表
特別注意在 CONNECTION 中所指定索引表為 people。
  1. CREATE TABLE people_sphinx( 
  2.    id BIGINT UNSIGNED NOT NULL   COMMENT '搜尋結果的 Id', 
  3.    weight INT NOT NULL           COMMENT '搜尋結果的權重', 
  4.    query VARCHAR(3072) NOT NULL  COMMENT '搜尋的查詢條件', 
  5.  
  6.    INDEX(query) 
  7. )ENGINE=SPHINX  
  8. CONNECTION="sphinx://localhost:9312/people"  
  9. COMMENT='People Sphinx搜尋連接介面'; 


SQL 的查詢測試
這裡使用 INNER JOIN 方式作查詢,這樣對於刪除資料的變動,就不會出現在查詢結果中。
  1. SELECT A.id, A.main_name, B.weight 
  2. FROM people_sphinx B 
  3. INNER JOIN people_profile A 
  4. USING(id)  
  5. WHERE B.query='馬丁尼茲;mode=any;limit=1000' 


透過 PHP 更新增量索引
在資料 INSERT 或 UPDATE 時,去呼叫索引更新,這樣在第一時間就可以更新索引。
  1. shell_exec('sudo indexer --quiet --rotate people_delta 2>&1'); 

如果 Server 是 Ubuntu,請記得在 vim /etc/sudoers 中賦予 apache 使用 indexer 的權限。
www-data ALL=(root) NOPASSWD: /usr/bin/indexer


在 crontab 中加上排程
利用離峰時間來更新完整的索引表,由於刪除資料的變動需要更新完整索引表才有辦法移除。
00 00 * * * indexer --quiet --rotate --all > /dev/null 2>&1
2011-12-16 13:01

[轉載][MySQL] Replication option binlog_format

轉載自:MYSQL5.1复制参数binlog_format

MySQL 5.1 中,在復制方面的改進就是引進了新的複制技術:基於行的複制。 簡言之,這種新技術就是關注表中發生變化的記錄,而非以前的照抄binlog 模式。 從MySQL 5.1.12 開始,可以用以下三種模式來實現:基於SQL語句的複制(statement-based replication, SBR),基於行的複制(row-based replication, RBR),混合模式複制(mixed-based replication, MBR)。 相應地,binlog的格式也有三種:STATEMENT,ROW,MIXED。 MBR 模式中,SBR 模式是默認的。


在運行時可以動態低改變binlog的格式,除了以下幾種情況:
  1. 存儲過程或者觸發器中間
  2. 啟用了NDB
  3. 當前會話試用RBR 模式,並且已打開了臨時表


如果binlog採用了MIXED 模式,那麼在以下幾種情況下會自動將binlog的模式由SBR 模式改成RBR 模式。
  1. 當DML語句更新一個NDB表時
  2. 當函數中包含UUID() 時
  3. 2個及以上包含AUTO_INCREMENT 字段的表被更新時
  4. 行任何INSERT DELAYED 語句時
  5. 用UDF 時
  6. 視圖中必須要求使用RBR 時,例如創建視圖是使用了UUID() 函數


設定主從復制模式的方法非常簡單,只要在以前設定複製配置的基礎上,再加一個參數:
  1. binlog_format="STATEMENT"  
  2. #binlog_format="ROW"  
  3. #binlog_format="MIXED" 


當然了,也可以在運行時動態修改binlog的格式。 例如
  1. mysql> SET SESSION binlog_format = 'STATEMENT';  
  2. mysql> SET SESSION binlog_format = 'ROW';  
  3. mysql> SET SESSION binlog_format = 'MIXED';  
  4.  
  5. mysql> SET GLOBAL binlog_format = 'STATEMENT';  
  6. mysql> SET GLOBAL binlog_format = 'ROW';  
  7. mysql> SET GLOBAL binlog_format = 'MIXED';  


現在來比較以下SBR 和RBR 2中模式各自的優缺點

SBR 的優點:
  1. 歷史悠久,技術成熟
  2. binlog文件較小
  3. binlog中包含了所有數據庫更改信息,可以據此來審核數據庫的安全等情況
  4. binlog可以用於實時的還原,而不僅僅用於復制
  5. 主從版本可以不一樣,從服務器版本可以比主服務器版本高

SBR 的缺點:
  1. 不是所有的UPDATE語句都能被複製,尤其是包含不確定操作的時候。
  2. 調用具有不確定因素的UDF 時復制也可能出問題
  3. 使用以下函數的語句也無法被複製:
    • LOAD_FILE()
    • UUID()
    • USER()
    • FOUND_ROWS()
    • SYSDATE() (除非啟動時啟用了--sysdate-is-now 選項)
  4. INSERT ... SELECT 會產生比RBR 更多的行級鎖
  5. 複製需要進行全表掃描(WHERE 語句中沒有使用到索引)的UPDATE 時,需要比RBR 請求更多的行級鎖
  6. 對於有AUTO_INCREMENT 字段的InnoDB表而言,INSERT 語句會阻塞其他INSERT 語句
  7. 對於一些複雜的語句,在從服務器上的耗資源情況會更嚴重,而RBR 模式下,只會對那個發生變化的記錄產生影響
  8. 存儲函數(不是存儲過程)在被調用的同時也會執行一次NOW() 函數,這個可以說是壞事也可能是好事
  9. 確定了的UDF 也需要在從服務器上執行
  10. 數據表必須幾乎和主服務器保持一致才行,否則可能會導致複製出錯
  11. 執行複雜語句如果出錯的話,會消耗更多資源


RBR 的優點:
  1. 任何情況都可以被複製,這對複制來說是最安全可靠的
  2. 和其他大多數數據庫系統的複制技術一樣
  3. 多數情況下,從服務器上的表如果有主鍵的話,複製就會快了很多
  4. 複製以下幾種語句時的行鎖更少:
    • INSERT ... SELECT
    • 包含AUTO_INCREMENT 字段的INSERT
    • 沒有附帶條件或者並沒有修改很多記錄的UPDATE 或DELETE 語句
  5. 執行INSERT,UPDATE,DELETE 語句時鎖更少
  6. 從服務器上採用多線程來執行複製成為可能

RBR 的缺點:
  1. binlog 大了很多
  2. 複雜的回滾時binlog 中會包含大量的數據
  3. 主服務器上執行UPDATE 語句時,所有發生變化的記錄都會寫到binlog 中,而SBR 只會寫一次,這會導致頻繁發生binlog 的並發寫問題
  4. UDF 產生的大BLOB 值會導致複製變慢
  5. 無法從binlog 中看到都複製了寫什麼語句
  6. 當在非事務表上執行一段堆積的SQL語句時,最好採用SBR 模式,否則很容易導致主從服務器的數據不一致情況發生


另外,針對系統庫 mysql 裡面的表發生變化時的處理規則如下:
  1. 如果是採用INSERT,UPDATE,DELETE 直接操作表的情況,則日誌格式根據binlog_format 的設定而記錄
  2. 如果是採用GRANT,REVOKE,SET PASSWORD 等管理語句來做的話,那麼無論如何都採用SBR 模式記錄

注:採用RBR 模式後,能解決很多原先出現的主鍵重複問題
2011-12-16 12:41

[MySQL] Replication Master to Slave

Master上的設定

my.cnf
  1. # 主機ID,不可以重複,範圍 0~4294967295 
  2. server-id       = 1 
  3.  
  4. # Log 的名稱或路徑,只有名稱會將檔案放在 datadir 下 
  5. log_bin         = mysql-bin 
  6.  
  7. # Log 的格式{ROW,STATEMENT,MIXED},最低支援版本 5.1.8   
  8. binlog_format = MIXED 
  9.  
  10. # 需要同步的資料庫,多個寫多行 
  11. binlog_do_db    = test 
  12. binlog_do_db    = mydb 
  13.  
  14. # 針對 InnoDB 且有用 transaction 的設定 
  15. innodb_flush_log_at_trx_commit=1 
  16. sync_binlog=1  

進入Master mysql
mysql -u root -p

建立同步連接帳號 db_sync,密碼12345,給 slave_host,
mysql> GRANT REPLICATION SLAVE ON *.* TO 'db_sync''slave_host' IDENTIFIED BY '12345';

鎖定資料庫寫入
mysql> FLUSH TABLES WITH READ LOCK;

取得 MASTER 上的 Log File 名稱與 Position 編號,這個編號代表當前的 Log 戳記
mysql> SHOW MASTER STATUS\G;
File:mysql-bin.000010
Position:106
Binlog_Do_DB:test,mydb
Binlog_Ignore_DB:

離開 mysql
mysql> quit

匯出 MASTER 上的資料
mysqldump -uroot -p --flush-logs --opt --master-data test > test.sql

解除鎖定
mysql -u root -p -e UNLOCK TABLES;



Slave上的設定

my.cnf
  1. # 主機ID,不可以重複,範圍 0~4294967295 
  2. server-id       = 2 
  3.  
  4. # 需要同步的資料庫,或指定的資料表 
  5. replicate_wild_do_table = test.% 
  6. replicate_wild_do_table = mydb.category 
  7. replicate_wild_do_table = mydb.blog 
  8.  
  9. # 與 Master 斷線後,重新嘗試連接的時間(sec)  
  10. master-connect-retry = 60 

匯入資料
mysql -uroot -p test < test.sql

進入 Slave mysql
mysql -uroot -p

停止跟清除之前的 SLAVE 連接
mysql> STOP SLAVE; RESET SLAVE;

設定Slave 與 Master 的連接
  1. mysql> CHANGE MASTER TO 
  2.    MASTER_HOST='master_host', 
  3.    MASTER_USER='db_sync', 
  4.    MASTER_PASSWORD='12345', 
  5.    MASTER_LOG_FILE='mysql-bin.000011', 
  6.    MASTER_LOG_POS=106; 

啟動 SLAVE 的連接
mysql> START SLAVE;

察看連接狀態
mysql> SHOW SLAVE STATUS\G;
Slave_IO_State:Connecting to master
Master_Host:master_host
Master_User:db_sync
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.000011
Read_Master_Log_Pos:106
Relay_Log_File:master_host-relay-bin.000011
Relay_Log_Pos:4
Relay_Master_Log_File:mysql-bin.000011
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:test.%,mydb.category,mydb.blog
Replicate_Wild_Ignore_Table:
Last_Errno:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:106
Relay_Log_Space:106
Until_Condition:None
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed:No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:0

接著到 Master 上新增一筆資料測試看看吧!


參考文章:
MYSQL 主从服务器配置
mysql主从同步快速设置
MySQL 設定 Replication (Master – Slave)
多主一从mysql replication同步表的大胆尝试
MySQL复制的数据库过滤若干问题
MYSQL5.1复制参数binlog_format
2011-12-16 06:49

[PHP] 縮圖方式的比較

這兩種的缺點是,都會根據圖片的像素大小,而佔用PHP的記憶體,會造成 Fatal error: Out of memory 的錯誤出現,有一種狀況是一個 3MB 大小的 JPEG 實際的像素大小卻是 128MB,再來 GD 支援的圖片類型有限,大約就是四五種常用類型。

優點是指需要安裝 GD 套件,這個套件不管是在 Windows 或 Linux 上很容易找到跟安裝,在處理圖片的類型明確跟尺寸不大的情況下,使用這兩個函數是不錯的。

為了改善 imageCopyResampled 效率,可以利用 imageCopyResized 做預先縮圖,例如要縮圖的大小為 100*100 時,可以先將圖片縮小成四倍或八倍,如 400*400 或 800*800,可改善超大圖造成的效率不好。

  • ImageMagick + Imagick 使用外部的程式來處理圖片,使用指令的方式或透過 Imagick 套件來處理縮圖。
缺點是透過指令的方式很不友善,且容易受到系統權限的限制,而 Imagick 套件在 Windows 上不容易找到合適的 DLL。

優點是支援下面多種格式,效率快且沒有記憶體錯誤的問題。
3FR, A, AI, ART, ARW, AVI, AVS, B, BGR, BGRA, BMP, BMP2, BMP3, BRF, BRG, C, CAL, CALS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DFONT, DJVU, DNG, DOT, DPX, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, EXR, FAX, FITS, FRACTAL, FTS, G, G3, GBR, GIF, GIF87, GRADIENT, GRAY, GRB, GROUP4, HALD, HISTOGRAM, HRZ, HTM, HTML, ICB, ICO, ICON, INFO, INLINE, IPL, ISOBRL, J2C, JNG, JP2, JPC, JPEG, JPG, JPX, K, K25, KDC, LABEL, M, M2V, M4V, MAP, MAT, MATTE, MIFF, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NULL, O, ORF, OTB, OTF, PAL, PALM, PAM, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PGX, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG24, PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSB, PSD, PTIF, PWP, R, RADIAL-GRADIENT, RAF, RAS, RBG, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UBRL, UIL, UYVY, VDA, VICAR, VID, VIFF, VST, WBMP, WMF, WMV, WMZ, WPG, X, X3F, XBM, XC, XCF, XPM, XPS, XV, XWD, Y, YCbCr, YCbCrA, YUV
2011-12-15 18:51

[PHP] Documentor (phpdoc) 備忘

Linux 安裝指令
sudo pear install -o PhpDocumentor


Windows 安裝指令
C:\wamp\bin\php\php5.2.4\pear.bat install -o PhpDocumentor


使用方式
  1. phpdoc --parseprivate \ 
  2.    --output HTML:frames:earthli \ 
  3.    --ignore Smarty/ \ 
  4.    --directory /var/www/lib,/var/www/data_mod,/var/www/etc \ 
  5.    --target /var/www/docs 


phpdoc 參數說明
-f--filename要解析的檔案名稱,可使用 ‘,’分隔多個檔案 “file1,file2”,可以包含完整路徑和使用 * ? 萬用符號。
-d--directory要解析的目錄路徑,可使用 ‘,’分隔多個目錄路徑 “directory1,directory2”。
-t--target指定要輸出的目錄。
-i--ignore要忽略的檔案名稱,可使用 ‘,’分隔多個檔案 “file1,file2”,可以使用 * ? 萬用符號。
-is--ignoresymlinks忽略系統連結的檔案或目錄,預設是關閉的。
-it--ignore-tags忽略解析的標籤。 @package, @subpackage, @access, @ignore 可能是無法忽視。
-q--quiet不顯示解析/轉換的訊息,在 cron 排程時可以選擇開啟,預設是關閉的。
-ti--title產生出的文件的標題,預設為 ‘Generated Documentation’。
-h--help顯示幫助訊息。
-pp--parseprivate將私有(private)成員函式或私有變數也都加入程式文件裡。不然產生出的文件裡只會有公開(public)和保護(protected)的成員函式和變數。
-o--output設置輸出文件的格式
  • HTML:frames:* - 包含iframe的HTML格式
  • HTML:frames:default – Javadoc風格的文檔模板,很少有格式
  • HTML:frames:earthli – 漂亮的模板(作者:Marco von Ballmoos)
  • HTML:frames:l0l33t – 流行模板
  • HTML:frames:phpdoc.de – 類似於phpdoc.de的PHPDoc輸出
  • HTML:frames:phphtmllib – 非常棒的用戶貢獻模板
  • HTML:frames:phpedit – 基於PHPEdit Help Generator的文檔
  • HTML:Smarty:* - 不使用iframe的HTML格式
  • HTML:Smarty:default – 使用css控制的黑體模板
  • HTML:Smarty:HandS – 基於PHP的格式,但是經過優化,帶有logo圖片
  • HTML:Smarty:PHP – 風格接近PHP官網
  • CHM:default:* - 輸出CHM幫助文檔
  • CHM:default:default – windows幫助文檔,基於HTML:frames:l0l33t
  • PDF:default:* - PDF格式
  • PDF:default:default – 標準純文本PDF格式
  • XML:DocBook:* - 以DocBook格式輸出的XML
  • XML:DocBook/peardoc2:default – 可以被編譯成peardoc的文檔
-j--javadocdesc相容 JavaDoc 的格式,預設是關閉的


中文亂碼的問題
新版的 phpdoc 輸出的格式已經是 UTF-8 了,所以只要在目錄下增加一個 .htaccess 文件,然後內容為:
AddCharset UTF-8 .html


參考文件:
多采多姿的程式筆記: phpDocumentor筆記 - 0 立即體驗
phpDocumentor Tutorial
phpDocumentor学习记录
2011-12-14 21:02

[轉載] Linux Rsync 參數說明

轉載自: Rsync 中文參數說明

內容開始
rsync 資料同步敘述:
rsync和rcp的模式很相似,不過rsync有需多參數可以使用來加速檔案傳送.
rsync遠端更新協定(remote-update protocol)可以透過網路來傳輸兩端檔案不同的地方.語法:
rsync [OPTION]... SRC [SRC]... DEST
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
rsync [OPTION]... [USER@]HOST:SRC [DEST]
rsync [OPTION]... [USER@]HOST::SRC [DEST]
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]


rsyncd 的設定檔:
/usr/local/etc/rsyncd.conf

說明:
[ohaha]: rsync區段的設定名稱
path: 備份資料存放的路徑
auth users: 連結rsync服務的帳號
uid,gid: 採用何種身分進行檔案存取
secerts file: 帳號密碼檔位置
read only: 是否唯讀


參數:
-h,--help顯示rsync求助資訊.
--version顯示rsync版本.
-v,--verbose複雜的輸出訊息.
-q,--quiet安靜模式,幾乎沒有訊息產生.常用在以cron執行rsync.
-I,--ignore-times通常rsync為了加快速度會忽略同樣檔案大小且同樣存取時間點的檔案. 可以透過此參數關閉此快速檢查.
--size-onlyrsync只檢查檔案大小是否改變,不管時間存取點是否改變. 通常用在mirror,且對方時間不太正確時.
-c,--checksum在傳送之前透過128bit的md4檢查碼來檢查所有要傳送的檔案.(會拖慢速度.)
-a,--archivearchive mode 權限保存模式,相當於 -rlptgoD 參數. 很快速的保存幾乎所有的權限設定,除了硬式連結(透過-H設定).
-r,--recursive複製所有下層的資料(遞迴)
-R,--relative使用相對路徑.
如: rsync foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo.c檔案
rsync -R foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo/bar/foo.c 檔案
-R,--relative不使用相對路徑.
-b,--backup目的地端先前已經存在的檔案在傳輸或刪除前會被備份.
--backup-dir=DIR設定備份的資料夾.
--suffix=SUFFIX指定備份的檔案名稱字尾形式(預設為~).
-K,--keep-dirlinks接收方將連結到資料夾的檔案視為資料夾處理
-l,--links複製所有的連結
-H,--hard-links保留硬式連結
-p,--perms保留檔案權限
-o,--owner保留檔案擁有者(root only)
-g,--group保留檔案群組
-D,--devices保留device資訊(root only)
-t,--times保留時間點
-n,--dry-run不實際執行傳送,只顯示將會有的傳輸動作
-S,--sparse嘗試去處理稀疏的檔案,讓這些檔案在目的端佔去較少的磁碟空間.
-W,--whole-file複製所有的檔案,不額外作檢查.
--no-whole-file關閉 --whole-file 參數
-x,--one-file-system不要跨越檔案系統分界(只在一個檔案系統處理)
-B,--block-size=SIZE強制透過rsync程式去比對修復block-sizeforce
-e--rsh=COMMAND定義所使用的remote shell
--rsync-path=PATH定義rsync在遠端機器存放資料的路徑
--existing只比對更新目的端已經存在的檔案
--ignore-existing忽略目的端已經存在的檔案(也就是不更新)
--delete刪除傳送端已經不存在,而目的端存在的檔案
--delete-excluded除了把傳送端已經不存在,而目的端存在的檔案刪除之外, 也刪除 --exclude 參數所包含的檔案.
--delete-afterrsync預設會在檔案傳送前進行相關刪除動作確保接收端有足夠的檔案空間, 但可以透過 --delete-after 讓刪除動作在檔案傳送後再行刪除.
--ignore-errors忽略任何錯誤既使是I/O error 也進行 --delete 刪除動作.
--max-delete=NUM定義rsync不要刪除超過 NUM 個檔案.
--partialrsync若遇到傳輸過程中斷時,會把那些已經傳輸的檔案刪除.
在某種狀況下保留那些部分傳送的檔案是令人高興的.
你可以透過 --partial 參數達到這個目的.
--partial-dir=DIR在 --partial 參數啟動時,你還可以定義rsync把那些部分傳送的檔案
寫入定義的資料夾,而非直接寫入目的端.需要注意的是, 此資料夾不應該被其他使用者可以寫入.(如:/tmp)
--force當目的端資料夾被傳送端非資料夾名稱覆蓋時,強制rsync刪除資料夾, 即使該資料夾不是空的.
--numeric-ids不將傳送端檔案的uid及gid值,與目的端的使用者/群組進行配對. 若傳送端並沒有uid及gid的對應名稱(如:原帳號群組被刪除的遺留檔案), 或目的端沒有相對應的帳號/群組,保留數字型態的uid/gid
--timeout=TIMEOUT設定 I/O 逾時的時間(秒). 超過這個秒數而沒有資料傳送,rsync將會結束. 預設為0,也就是沒有定義逾時時間.
-T,--temp-dir=DIR定義rsync在接收端產生暫時性的複製檔案時使用資料夾暫存. 預設是直接在接收端資料夾直接產生暫存檔案.
--compare-dest=DIR定義rsync在目的端建立資料夾來比對傳送過來的檔案.
--link-dest=DIR與 --compare-dest 相同,但同時會針對無法改變的檔案建立硬式連結.
-z,--compress壓縮模式,當資料在傳送到目的端進行檔案壓縮.
-P-P參數和 --partial --progress 相同.只是為了把參數簡單化.
-C,--cvs-exclude排除那些通常不希望傳送的檔案.定義的方式與CVS傳送相同:
RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS .make.state
.nse_depinfo *~ *$ *.old *.bak *.BAK *.orig *.rej
*.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/
符合以上條件的都會被忽略而不傳送.
--exclude=PATTER符合PATTERN(規則表示式)樣式的檔案不進行傳送
--exclude-from=FILE和--exclude參數相同,不過是把不進行傳送的檔案事先寫入某一檔案. 執行時,透過此參數讓rsync讀取. (; #開頭的行列或空白行會被rsync忽略)
--include=PATTERN定義rsync不要排除符合pattern樣式的檔案.
--include-from=FILE和--include參數相同,只不過把要包含的檔案寫到某一檔案.
--files-from=FILE把要傳送的檔案名稱都精確的寫入某一檔案,讓rsync讀取. 如: rsync -a --files-from=/tmp/foo /usr remote:/backup
-0--from0定義檔案所要讀取的檔案是null字元結尾.
--version顯示版本訊息.
--daemon定義rsync以daemon型態執行.
--no-detach當以daemon型態執行時,不要進行分裂且變成背景程序.
--address=ADDRESS定義所要連結(bind)的ip位址或是host名稱(daemon限定)
--config=FILE定義所要讀取的設定檔rsyncd.conf位置(daemon限定) 預設值為 /usr/local/etc/rsyncd.conf
--port=PORT定義rsyncd(daemon)要執行的port(預設為tcp 873)
--blocking-io使用blocking I/O連結遠端的shell,如rsh , remsh
--no-blocking-io使用non-blocking連結遠端的shell,如ssh (預設值)
--stats顯示檔案傳送時的資訊狀態
--progress顯示傳送的進度.(給檔案傳送時,怕無聊的人用的..)
--log-format=FORMAT定義log的格式(在rsyncd.conf設定)
--password-file=FILE從檔案讀取與遠端rsync伺服器連結的密碼
--bwlimit=KBPS定義傳輸頻寬的大小(KBytes/秒)
--write-batch=FILE把紀錄資料寫入一個檔案(給其他相同環境且相同需求的機器使用)
--read-batch=FILE透過讀取紀錄檔案來進行傳輸.(檔案由 --write-batch 參數產生)
--checksum-seed=NUM定義檔案 checksum-seed 的大小(byte)
-4--ipv4使用IPv4協定
-6--ipv6使用IPv6協定
2011-12-14 16:22

[PHP] 取得用戶真實 IP

轉載自:精進你的程式碼 - 從取得用戶端 IP 的函式談起

  1. <?php 
  2. function get_client_ip(){ 
  3.    foreach (array( 
  4.        'HTTP_CLIENT_IP', 
  5.        'HTTP_X_FORWARDED_FOR', 
  6.        'HTTP_X_FORWARDED', 
  7.        'HTTP_X_CLUSTER_CLIENT_IP', 
  8.        'HTTP_FORWARDED_FOR', 
  9.        'HTTP_FORWARDED', 
  10.        'REMOTE_ADDR' 
  11.    ) as $key) { 
  12.        if(!array_key_exists($key, $_SERVER)){ continue; } 
  13.  
  14.        foreach (explode(',', $_SERVER[$key]) as $ip) { 
  15.            $ip = trim($ip); 
  16.            if ((bool) filter_var($ip,  
  17.                FILTER_VALIDATE_IP, 
  18.                FILTER_FLAG_IPV4 | 
  19.                FILTER_FLAG_NO_PRIV_RANGE | 
  20.                FILTER_FLAG_NO_RES_RANGE 
  21.            )){ return $ip; } 
  22.        } 
  23.    } 
  24.    return null; 
  25. } 
2011-12-14 15:26

[Ubuntu] 掛載 Windows 分享目錄

  • 安裝 smbfs 套件
    sudo apt-get install smbfs
  • 建立掛載目錄
    sudo mkdir /mnt/windows_share
  • 開放最大權限
    sudo chmod 777
  • 測試掛載
    1. sudo mount -t cifs //192.168.0.10/my_share /mnt/windows_share -o username=jax,password=1234,noacl,noperm,codepage=cp950,iocharset=big5,file_mode=0666,dir_mode=0777 
  • 察看是否成功
    df -h
    Filesystem Size Used Avail Use% Mounted on
    //192.168.0.10/my_share 7.9G 208K 7.9G 1% /mnt/windows_share
  • 卸載
    sudo umount /mnt/windows_share
  • 修改開機掛載設定檔
    sudo vim /etc/fstab
    1. /mnt/windows_share was on //192.168.0.10/my_share 
    2. //192.168.0.10/my_share /mnt/windows_share cifs username=jax,password=1234,noacl,noperm,codepage=cp950,iocharset=big5,file_mode=0666,dir_mode=0777 0 0 
  • 套用開機設定檔
    mount -a
  • 再次察看是否成功
    df -h
    Filesystem Size Used Avail Use% Mounted on
    //192.168.0.10/my_share 7.9G 208K 7.9G 1% /mnt/windows_share

參考資料:
在 ubuntu 中 mount windows 分享的目錄
載掛(Mount)遠端資料夾的應用
ubuntu的几个技巧: 自动连接Windows共享文件夹, 定时文件同步备份