approve.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00025   //we need the function xml_to_text()
00026 include_once DOCBOOK.'/edit/content/process_content.php';
00027 
00028 //we need the function update_cache_files()
00029 include_once DOCBOOK.'/edit/edit_funcs.php';
00030 
00038 class approve extends WebObject
00039 {
00040   function on_set_lock($event_args)
00041   {
00042     $lock = $event_args['lock'];
00043     set_node_lock($lock, 'approve');
00044   }
00045 
00046   function on_revert($event_args)
00047   {
00048     $recursive = ($event_args['recursive'] == 'true');
00049     if ($recursive)
00050       $this->recursive_revert();
00051     else
00052       $this->non_recursive_revert();
00053   }
00054 
00055   function non_recursive_revert()
00056   {
00057     $workspace_xml_file = file_content_xml(WS_BOOKS);
00058     $node_dir = dirname($workspace_xml_file);
00059 
00060     //revert
00061     shell(SCRIPTS."approve/svn_revert.sh $node_dir");
00062 
00063     //update the cache file content.html in the workspace
00064     update_cache();
00065 
00066     //update subnodes.html and navigation.txt as well
00067     $node_path = WebApp::getSVar('docbook->node_path');
00068     update_cache_files($node_path);
00069 
00070     //set the status of the node to synchronized
00071     set_node_status('synchronized');
00072 
00073     //remove this node from the list of the modified nodes
00074     remove_from_modified_nodes();
00075   }
00076 
00077   function recursive_revert()
00078   {
00079     $workspace_xml_file = file_content_xml(WS_BOOKS);
00080     $node_dir = dirname($workspace_xml_file);
00081     $node_dir = str_replace('./', '', $node_dir);
00082 
00083     //revert all the content.xml files 
00084     $result = shell("svn revert -R $node_dir");
00085     $result = str_replace($node_dir.'/', '', $result);
00086     if ($result != '')  WebApp::message($result);
00087 
00088     //update cache files in workspace
00089     $book_id = WebApp::getSVar('docbook->book_id');
00090     $lng = WebApp::getSVar('docbook->lng');
00091     $node_path = WebApp::getSVar('docbook->node_path');
00092     shell(CONTENT."cache/cache.sh $book_id $lng 'workspace' '$node_path'");
00093 
00094     //set the status of this node and all the subnodes to synchronized
00095     $this->set_all_synchronized($node_dir);
00096 
00097     //remove this node and the subnodes from the list of the modified nodes
00098     remove_from_modified_nodes($node_path, true);
00099   }
00100 
00101   function set_all_synchronized($node_dir)
00102   {
00103     shell(SCRIPTS."approve/set_synchronized.sh $node_dir");
00104   }
00105 
00106   function on_commit($event_args)
00107   {
00108     $log = trim($event_args['log']);
00109 
00110     $recursive = ($event_args['recursive'] == 'true');
00111     if ($recursive) 
00112       $this->recursive_commit($log);
00113     else
00114       $this->non_recursive_commit($log);
00115   }
00116 
00117   function non_recursive_commit($log)
00118   {
00119     //make up the commit message
00120     if ($log!='')  $log = "\nLog: ".$log;
00121     $arr_state = get_node_state();
00122     extract($arr_state);
00123     $m_time = date('Y-m-d H:i', $m_timestamp);
00124     $time = date('Y-m-d H:i');
00125     $msg = ("Modified by $m_user, $m_time. "
00126             . "Approved by ".USER.", $time".$log);
00127 
00128     //commit any modifications
00129     $workspace_xml_file = file_content_xml(WS_BOOKS);
00130     $node_dir = dirname($workspace_xml_file);
00131     shell(SCRIPTS."approve/svn_commit.sh $node_dir '$msg'");
00132 
00133     //check whether the book is fixed to a tag
00134     $book_id = WebApp::getSVar('docbook->book_id');
00135     $lng = WebApp::getSVar('docbook->lng');
00136     $tag = book_fixed_to_tag($book_id, $lng);
00137 
00138     if ($tag)
00139       $this->msg_book_fixed_to_tag($tag);
00140     else
00141       {
00142         //update the content.xml and media files in the public copy
00143         $public_xml_file = file_content_xml(BOOKS);
00144         $node_dir = dirname($public_xml_file);
00145         shell("svn update -N $node_dir");
00146 
00147         //update the cache file content.html of the public copy
00148         update_cache('public');
00149         //update subnodes.html and navigation.txt as well
00150         $node_path = WebApp::getSVar('docbook->node_path');
00151         update_cache_files($node_path);
00152       }
00153 
00154     //set the status of the node to synchronized
00155     set_node_status('synchronized');
00156 
00157     //remove this node from the list of the modified nodes
00158     remove_from_modified_nodes();
00159   }
00160 
00161   function msg_book_fixed_to_tag($tag)
00162   {
00163     $book_id = WebApp::getSVar('docbook->book_id');
00164     $lng = WebApp::getSVar('docbook->lng');
00165 
00166     $msg = T_("The book (v_book_id, v_lng) is fixed to v_tag, \
00167 so the changes will not be displayed in the public copy.");
00168 
00169     $msg = str_replace('v_book_id', $book_id, $msg);
00170     $msg = str_replace('v_lng', $lng, $msg);
00171     $msg = str_replace('v_tag', $tag, $msg);
00172 
00173     WebApp::message($msg);
00174   }
00175 
00176   function recursive_commit($log)
00177   {
00178     //commit all the content.xml files 
00179     if ($log!='')  $log = "\nLog: ".$log;
00180     $workspace_xml_file = file_content_xml(WS_BOOKS);
00181     $node_dir = dirname($workspace_xml_file);
00182     $msg = date('Y-m-d H:i').' >> Approved by '.USER . $log;
00183     shell("svn commit $node_dir -m '$msg'");
00184 
00185     //check whether the book is fixed to a tag
00186     $book_id = WebApp::getSVar('docbook->book_id');
00187     $lng = WebApp::getSVar('docbook->lng');
00188     $tag = book_fixed_to_tag($book_id, $lng);
00189 
00190     if ($tag)
00191       $this->msg_book_fixed_to_tag($tag);
00192     else
00193       {
00194         //update the content.xml and media files in the public copy
00195         $public_xml_file = file_content_xml(BOOKS);
00196         $node_dir = dirname($public_xml_file).'/';
00197         shell("svn update $node_dir");
00198 
00199         //update cache files
00200         $node_path = WebApp::getSVar('docbook->node_path');
00201         shell(CONTENT."cache/cache.sh $book_id $lng 'books' '$node_path'");
00202         //update subnodes.html as well
00203         update_cache_files($node_path);
00204       }
00205 
00206     //set the status of this node and all the subnodes to synchronized
00207     $node_dir = dirname($workspace_xml_file);
00208     $this->set_all_synchronized($node_dir);
00209 
00210     //remove this node and the subnodes from the list of the modified nodes
00211     $node_path = WebApp::getSVar('docbook->node_path');
00212     remove_from_modified_nodes($node_path, true);
00213   }
00214 
00215   function onRender()
00216   {
00217     //add variables {{status}}, {{m_user}}, {{m_email}}, {{m_time}}
00218     $arr_state = get_node_state();
00219     WebApp::addVar('status', $arr_state['status']);
00220     $m_user = $arr_state['m_user'];
00221     $m_email = $arr_state['m_email'];
00222     WebApp::addVar('m_user', "<a href='mailto:$m_email'>$m_user</a>");
00223     $m_time = get_date_str($arr_state['m_timestamp']);
00224     WebApp::addVar('m_time', $m_time);
00225 
00226     //add the variables {{locked_by_somebody}} and {{locked}}
00227     $locked_by_somebody = locked_by_somebody($arr_state);
00228     $str_locked_by_somebody = ($locked_by_somebody ? 'true' : 'false');
00229     $str_locked = (is_locked($arr_state) ? 'true' : 'false');
00230     WebApp::addVar('locked_by_somebody', $str_locked_by_somebody);
00231     WebApp::addVar('locked', $str_locked);
00232 
00233     //display a notification message if the node is locked
00234     if ($locked_by_somebody)
00235       {
00236         extract($arr_state);
00237         $msg = T_("This node is locked for v_mode\n\
00238 by v_l_user (v_l_email).\n\
00239 Please try again later.");
00240         $msg = str_replace('v_mode', $mode, $msg);
00241         $msg = str_replace('v_l_user', $l_user, $msg);
00242         $msg = str_replace('v_l_email', $l_email, $msg);
00243         WebApp::message($msg);
00244         WebApp::addVar('l_mode', $mode);
00245         WebApp::addVar('l_user', "<a href='mailto:$l_email'>$l_user</a>");
00246         return;
00247       }
00248 
00249     //add {{id}}
00250     ereg('([^/]+)/$', $node_path, $regs);
00251     $id = $regs[1];
00252     if ($id=='.')  $id = WebApp::getSVar('docbook->book_id');
00253     WebApp::addVar("id", $id);
00254 
00255     //add the {{content_diff}} variable
00256     $content_diff = $this->get_content_diff();
00257     WebApp::addVar('content_diff', $content_diff."\n");
00258 
00259     //add the {{svn_status}} variable
00260     $svn_status = $this->get_svn_status();
00261     WebApp::addVar('svn_status', $svn_status."\n");
00262 
00263     $view_revisions = (trim($diff)=='' ? 'true' : 'false');
00264     WebApp::addVar('view_revisions', $view_revisions);
00265   }
00266 
00268   function get_content_diff()
00269   {
00270     $file_xml = file_content_xml();
00271     $get_title_xsl = XSLT.'edit/get_content_title.xsl';
00272 
00273     //get the HEAD revision of the file
00274     $xml_HEAD = shell("svn cat $file_xml -r HEAD");
00275     $file_xml_head = write_tmp_file($xml_HEAD);
00276 
00277     //convert to TextWiki the HEAD revision
00278     $title = shell("xsltproc $get_title_xsl $file_xml_head");
00279     $txt = get_node_content($file_xml_head, 'text');
00280     $txt = 'T: '.$title."\n\n".trim($txt)."\n";
00281     $file_txt_head = write_tmp_file($txt);
00282 
00283     //convert to TextWiki the working copy
00284     $title = shell("xsltproc $get_title_xsl $file_xml");
00285     $txt = get_node_content($file_xml, 'text');
00286     $txt = 'T: '.$title."\n\n".trim($txt)."\n";
00287     $file_txt = write_tmp_file($txt);
00288 
00289     //get the difference
00290     $diff = shell("diff -ubB $file_txt_head $file_txt | sed '1,2d'");
00291 
00292     //clean the temporary files
00293     unlink($file_xml_head);
00294     unlink($file_txt_head);
00295     unlink($file_txt);
00296 
00297     //format it for propper display in html
00298     $diff = htmlspecialchars($diff);
00299     $patterns = array(
00300                       '/^(@@.*@@)$/m',
00301                       '/^-(.*)$/m',
00302                       '/^\+(.*)$/m',
00303                       '/^ (.*)$/m'
00304                       );
00305     $replacements = array(
00306                           '<div class="line_nr">\\1</div>',
00307                           '<div class="removed">&nbsp;\\1</div>',
00308                           '<div class="added">&nbsp;\\1</div>',
00309                           '<div class="context">&nbsp;\\1</div>'
00310                           );
00311     $diff = preg_replace($patterns, $replacements, $diff);
00312 
00313     return $diff;
00314   }
00315 
00320   function get_svn_status()
00321   {
00322     //get the path of the section
00323     $path = dirname(file_content_xml());
00324     $path = str_replace('./', '', $path);
00325     $path .= '/';
00326 
00327     //get the difference
00328     $sed_expr = "-e '/^\\? /d' -e '/state\\.txt/d' -e 's#$path##'";
00329     $status = shell("svn status $path | sed $sed_expr");
00330 
00331     //format it for propper display in html
00332     $status = trim($status);
00333     $patterns = array(
00334                       '/^(M +.*)$/m',
00335                       '/^(D +.*)$/m',
00336                       '/^(A +.*)$/m'
00337                       );
00338     $replacements = array(
00339                           '<span class="modified">\\1</span>',
00340                           '<span class="removed">\\1</span>',
00341                           '<span class="added">\\1</span>'
00342                           );
00343     $status = preg_replace($patterns, $replacements, $status);
00344 
00345     return $status;
00346   }
00347 }
00348 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2