edit_funcs.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 
00038 function lock_index()
00039 {
00040   $book_id = WebApp::getSVar('docbook->book_id');
00041   $lock_file = WS_BOOKS.$book_id.'/lock';
00042 
00043   //if the lock file exists, wait until 
00044   //it is removed or until it is too old
00045   while (file_exists($lock_file))
00046     {
00047       clearstatcache();
00048       $lock_age = time() - filectime($lock_file);
00049       if ($lock_age > 60) break;
00050     }
00051   shell("touch $lock_file");
00052 }
00053 
00054 function unlock_index()
00055 {
00056   $book_id = WebApp::getSVar('docbook->book_id');
00057   $lock_file = WS_BOOKS.$book_id.'/lock';
00058   shell("rm $lock_file");
00059 }
00060 
00062 function add_to_modified_nodes($path =UNDEFINED)
00063 {
00064   if ($path==UNDEFINED)  $path = WebApp::getSVar('docbook->node_path');
00065 
00066   $book_id = WebApp::getSVar('docbook->book_id');
00067   $lng = WebApp::getSVar('docbook->lng');
00068 
00069   //get the lock for accessing the file
00070   $lock_file = WS_BOOKS."$book_id/$lng/modified_lock";
00071   while (file_exists($lock_file))
00072     {
00073       clearstatcache();
00074       $lock_age = time() - filectime($lock_file);
00075       if ($lock_age > 10) break;
00076     }
00077   shell("touch $lock_file");
00078 
00079   //modify the file
00080   $modified_nodes = WS_BOOKS."$book_id/$lng/modified_nodes.txt";
00081   shell("sed '\|^$path\$|d' -i $modified_nodes");
00082   shell("echo \"$path\" >> $modified_nodes");
00083 
00084   //release the lock
00085   shell("rm $lock_file");
00086 }
00087 
00089 function remove_from_modified_nodes($path =UNDEFINED, $recursive =false)
00090 {
00091   if ($path==UNDEFINED)  $path = WebApp::getSVar('docbook->node_path');
00092 
00093   $book_id = WebApp::getSVar('docbook->book_id');
00094   $lng = WebApp::getSVar('docbook->lng');
00095 
00096   //get the lock for accessing the file
00097   $lock_file = WS_BOOKS."$book_id/$lng/modified_lock";
00098   while (file_exists($lock_file))
00099     {
00100       clearstatcache();
00101       $lock_age = time() - filectime($lock_file);
00102       if ($lock_age > 10) break;
00103     }
00104   shell("touch $lock_file");
00105 
00106   //modify the file
00107   $pattern = ($recursive ? "^$path" : "^$path\$");
00108   $modified_nodes = WS_BOOKS."$book_id/$lng/modified_nodes.txt";
00109   shell("sed '\|$pattern|d' -i $modified_nodes");
00110 
00111   //release the lock
00112   shell("rm $lock_file");
00113 }
00114 
00115 /*----------------------------------------------*/
00116     
00118 function add_subsections_rs()
00119 {
00120   $subsections = process_index_node('subsections');
00121   $lines = explode("\n", $subsections);
00122 
00123   $rs = new EditableRS('subsections');
00124   for ($i=0; $i < sizeof($lines); $i++)
00125     {
00126       list($path,$title) = split(' ', $lines[$i], 2);
00127       if ($path=='')  continue;
00128       $title = trim($title);
00129       $rs->addRec(compact('path', 'title'));
00130     }
00131   global $webPage;
00132   $webPage->addRecordset($rs);
00133 }
00134 
00139 function process_index_node($transformer, $node_path =UNDEFINED)
00140 {
00141   if ($node_path==UNDEFINED)
00142     {
00143       $node_path = WebApp::getSVar('docbook->node_path');
00144     }
00145   $lng = WebApp::getSVar('docbook->lng');
00146   $transformer = "edit/${transformer}.xsl";
00147   $params = array('path'=>$node_path, 'lng'=>$lng);
00148   $output = process_index($transformer, $params);
00149 
00150   return $output;
00151 }
00152 
00157 function transform_index($action, $arr_params =array())
00158 {
00159   //process index.xml with an xsl, and get the new index
00160   $index_xml = process_index("edit/${action}.xsl", $arr_params);
00161 
00162   //construct the commit message
00163   $params = "action=\"$action\", ".array2str($arr_params);
00164   $commit_msg = date('Y-m-d H:i').' >> Modified by '.USER. ' >> '.$params;
00165 
00166   $book_id = WebApp::getSVar('docbook->book_id');
00167 
00168   //change index.xml for all the languages
00169   $langs = WebApp::getSVar('docbook->languages');
00170   $arr_langs = explode(',', $langs);
00171   for ($i=0; $i < sizeof($arr_langs); $i++)
00172     {
00173       $lng = $arr_langs[$i];
00174 
00175       //write the transformed index to index.xml
00176       $workspace_xml_file = WS_BOOKS."$book_id/$lng/index.xml";
00177       write_file($workspace_xml_file, $index_xml);
00178 
00179       //commit in svn
00180       shell("svn commit $workspace_xml_file -m '$commit_msg'");
00181 
00182       //update the public copy
00183       $tag = book_fixed_to_tag($book_id, $lng);
00184       if ($tag)
00185         {
00186           $msg = T_("The book (v_book_id, v_lng) is fixed to v_tag, \n\
00187 so the changes will not be displayed in the public copy.");
00188           $msg = str_replace('v_book_id', $book_id, $msg);
00189           $msg = str_replace('v_lng', $lng, $msg);
00190           $msg = str_replace('v_tag', $tag, $msg);
00191           WebApp::message($msg);
00192         }
00193       else
00194         {
00195           $book_dir = BOOKS."$book_id/$lng/";
00196           $public_xml_file = $book_dir.'index.xml';
00197           shell("svn update $book_dir");
00198         }
00199     }
00200 }
00201 
00203 function update_navigation_all_langs($node_path)
00204 {
00205   $langs = WebApp::getSVar('docbook->languages');
00206   $arr_langs = explode(',', $langs);
00207   for ($i=0; $i < sizeof($arr_langs); $i++)
00208     {
00209       $lng = $arr_langs[$i];
00210       update_navigation($node_path, $lng);
00211     }
00212 }
00213 
00215 function update_navigation($node_path, $lng)
00216 {
00217   $book_id = WebApp::getSVar('docbook->book_id');
00218   $xsl_file = 'edit/get_navigation.xsl';
00219   $params = array('path'=>$node_path, 'lng'=>$lng);
00220 
00221   //update the navigation file in the workspace
00222   $navigation = process_index($xsl_file, $params, WS_BOOKS);
00223   $fname = WS_CACHE."$book_id/$lng/$node_path/navigation.txt";
00224   write_file($fname, $navigation);
00225 
00226   //update the navigation file in the public copy
00227   if (!book_fixed_to_tag($book_id, $lng))
00228     {
00229       $navigation = process_index($xsl_file, $params, BOOKS);
00230       $fname = CACHE."$book_id/$lng/$node_path/navigation.txt";
00231       write_file($fname, $navigation);
00232     }
00233 }
00234 
00239 function update_subnodes_html_all_langs($node_path =UNDEFINED)
00240 {
00241   if ($node_path==UNDEFINED)
00242     {
00243       $node_path = WebApp::getSVar('docbook->node_path');
00244     }
00245 
00246   //update subnodes for each language
00247   $langs = WebApp::getSVar('docbook->languages');
00248   $arr_langs = explode(',', $langs);
00249   for ($i=0; $i < sizeof($arr_langs); $i++)
00250     {
00251       $lng = $arr_langs[$i];
00252       update_subnodes_html($node_path, $lng);
00253     }
00254 }
00255 
00264 function update_subnodes_html($node_path, $lng, $recursive ='true')
00265 {
00266   $transformer = 'edit/get_subnodes.xsl';
00267   $book_id = WebApp::getSVar('docbook->book_id');
00268   $fixed = book_fixed_to_tag($book_id, $lng);
00269 
00270   //update subnodes for this node and
00271   //for each ancestor (up to the root)
00272   while (true)
00273     {
00274       //parameters to be passed to the transformer
00275       $params = array('path'=>$node_path);
00276 
00277       //update the workspace subnodes.html
00278       $subnodes = process_index($transformer, $params, WS_BOOKS, $lng);
00279       $book_path = WS_CACHE."$book_id/$lng/";
00280       $subnodes_html = $book_path.$node_path.'subnodes.html';
00281       write_file($subnodes_html, $subnodes);
00282 
00283       //update the public subnodes.html
00284       if (!$fixed)
00285         {
00286           $subnodes = process_index($transformer, $params, BOOKS, $lng);
00287           $book_path = CACHE."$book_id/$lng/";
00288           $subnodes_html = $book_path.$node_path.'subnodes.html';
00289           write_file($subnodes_html, $subnodes);
00290         }
00291 
00292       if ($recursive!='true')  break;
00293       if ($node_path=='./')    break;
00294 
00295       //get the path of the parent, by removing the last id
00296       $node_path = ereg_replace('[^/]+/$', '', $node_path);
00297     }
00298 }
00299 
00300 /*----------------------------------------------*/
00301 
00302 function updatenavig_moveup($node_path)
00303 {
00304   //update navigation files of both nodes and their neighbours
00305   update_navigation_all_langs($node_path);
00306   $navig = get_arr_navigation($node_path);
00307   update_navigation_all_langs($navig['prev_path']);
00308   update_navigation_all_langs($navig['next_path']);
00309   $next_navig = get_arr_navigation($navig['next_path']);
00310   update_navigation_all_langs($next_navig['next_path']);
00311 }
00312 
00313 function updatenavig_movedown($node_path)
00314 {
00315   //update navigation files of both nodes and their neighbours
00316   update_navigation_all_langs($node_path);
00317   $navig = get_arr_navigation($node_path);
00318   update_navigation_all_langs($navig['prev_path']);
00319   update_navigation_all_langs($navig['next_path']);
00320   $prev_navig = get_arr_navigation($navig['prev_path']);
00321   update_navigation_all_langs($prev_navig['prev_path']);
00322 }
00323 
00324 function update_cache_files_all_langs($node_path)
00325 {
00326   update_navigation_all_langs($node_path);
00327   $navig = get_arr_navigation($node_path);
00328   update_navigation_all_langs($navig['prev_path']);
00329   update_navigation_all_langs($navig['next_path']);      
00330 
00331   //update subnodes.html of the new node and the ancestors
00332   update_subnodes_html_all_langs($node_path);
00333 }
00334 
00335 function update_cache_files($node_path, $lng =UNDEFINED)
00336 {
00337   if ($lng==UNDEFINED)  $lng = WebApp::getSVar('docbook->lng');
00338 
00339   update_navigation($node_path, $lng);
00340   $navig = get_arr_navigation($node_path);
00341   update_navigation($navig['prev_path'], $lng);
00342   update_navigation($navig['next_path'], $lng);      
00343 
00344   //update subnodes.html of the new node and the ancestors
00345   update_subnodes_html($node_path, $lng);
00346 }
00347 
00348 function delete_removefolders_all_langs($node_path)
00349 {
00350   $langs = WebApp::getSVar('docbook->languages');
00351   $arr_langs = explode(',', $langs);
00352   for ($i=0; $i < sizeof($arr_langs); $i++)
00353     {
00354       $lng = $arr_langs[$i];
00355       delete_removefolders($node_path, $lng);
00356     }
00357 }
00358 
00359 function delete_removefolders($node_path, $lng)
00360 {
00361   $book_id = WebApp::getSVar('docbook->book_id');
00362   $path = "$book_id/$lng/$node_path";
00363   $msg = date('Y-m-d H:i') . " >> Deleted by " . USER
00364     . " >> node_path='$node_path'";
00365 
00366   //remove from svn and commit
00367   shell("svn remove --force ".WS_BOOKS.$path);
00368   shell("svn update ".WS_BOOKS.$path);
00369   shell("svn commit ".WS_BOOKS.$path." -m '$msg'");
00370 
00371   //remove the workspace cache files
00372   shell("rm -rf ".WS_CACHE.$path);
00373 
00374   //update the public space files
00375   if (!book_fixed_to_tag($book_id, $lng))
00376     {
00377       //update xml files
00378       $book_dir = BOOKS."$book_id/$lng/";
00379       shell("svn update $book_dir");
00380 
00381       //update cache files
00382       shell("rm -rf ".CACHE.$path);
00383     }
00384 }
00385 
00387 function create_new_node($id, $title, $type)
00388 {
00389   $book_id = WebApp::getSVar('docbook->book_id');
00390   $node_path = WebApp::getSVar('docbook->node_path');
00391 
00392   $new_node_path = $node_path.$id;
00393 
00394   // content.html, content.xml, state.txt and commit message 
00395   $content_html = '<html/>';
00396   $content_xml = "<?xml version='1.0' encoding='utf-8' standalone='no'?>
00397 <$type id='$id'>
00398   <title>$title</title>
00399   <para/>
00400 </$type>";
00401   $state_txt = "unlocked::::\nadded:".USER.':'.EMAIL.':'.time();
00402   //commit message
00403   $msg_params = "parent_node='$node_path', new_node_id='$id'";
00404   $commit_msg = date('Y-m-d H:i'). ' >> Added by '.USER.' >> '.$msg_params;
00405 
00406   $langs = WebApp::getSVar('docbook->languages');
00407   $arr_langs = explode(',', $langs);
00408   for ($i=0; $i < sizeof($arr_langs); $i++)
00409     {
00410       $lng = $arr_langs[$i];
00411       $path = "$book_id/$lng/$new_node_path";
00412 
00413       //create the workspace xml directory
00414       $xml_path = WS_BOOKS.$path;
00415       shell("mkdir $xml_path");
00416       shell("chmod +x $xml_path");
00417       write_file("$xml_path/content.xml", $content_xml);
00418       shell("svn add $xml_path");
00419       shell("svn commit $xml_path -m '$commit_msg'");
00420       write_file("$xml_path/state.txt", $state_txt);
00421 
00422       //create the workspace cache directory
00423       $cache_path = WS_CACHE.$path;
00424       shell("mkdir $cache_path");
00425       shell("chmod +x $cache_path");
00426       write_file("$cache_path/content.html", $content_html);
00427 
00428       //create the directories and files in the public copy
00429       if (!book_fixed_to_tag($book_id, $lng))
00430         {
00431           //update the xml from svn
00432           $book_dir = BOOKS."$book_id/$lng/";
00433           shell("svn update $book_dir");
00434 
00435           //create the cache directory
00436           $cache_path = CACHE.$path;
00437       shell("mkdir $cache_path");
00438       shell("chmod +x $cache_path");
00439           write_file("$cache_path/content.html", $content_html);
00440         }
00441     }
00442 }
00443 
00445 function change_id_validate($new_id)
00446 {
00447   $node_path = WebApp::getSVar('docbook->node_path');
00448   $book_id = WebApp::getSVar('docbook->book_id');
00449 
00450   //get the current node_id 
00451   ereg('([^/]+)/$', $node_path, $regs);
00452   $old_id = $regs[1];
00453   if ($old_id=='.')  $old_id = $book_id;
00454 
00455   if ($new_id==$old_id)  return false;
00456 
00457   //the id of the root must not be changed
00458   if ($old_id==$book_id)
00459     {
00460       WebApp::message(T_("The book ID cannot be changed."));
00461       return false;
00462     }
00463 
00464   //the id cannot be empty
00465   if ($new_id=='')
00466     {
00467       $msg = T_("The id cannot be empty.");
00468       WebApp::message($msg);
00469       return false;
00470     }
00471 
00472   //check that the new id does not already exist
00473   $output = process_index('edit/get_node_id.xsl', array('id'=>$new_id));
00474   if ($output<>'')
00475     {
00476       $msg = T_("The id 'v_new_id' is already used by another section.");
00477       $msg = str_replace('v_new_id', $new_id, $msg);
00478       WebApp::message($msg);
00479       return false;
00480     }
00481 
00482   //check that this node is not referenced by any other nodes
00483   $refs = get_node_references();
00484   if ($refs != '')
00485     {
00486       $msg = T_("This node id cannot be changed because it is referenced by \n  v_refs \nFirst delete the references.");
00487       $msg = str_replace('v_refs', $refs, $msg);
00488       WebApp::message($msg);
00489       return false;
00490     }
00491 
00492   return true;
00493 }
00494 
00496 function changeid_movefolders($node_path, $new_node_path)
00497 {
00498   $book_id = WebApp::getSVar('docbook->book_id');
00499   $langs = WebApp::getSVar('docbook->languages');
00500   $arr_langs = explode(',', $langs);
00501   for ($i=0; $i < sizeof($arr_langs); $i++)
00502     {
00503       $lng = $arr_langs[$i];
00504 
00505       //move the folder in the workspace
00506       $node_folder = WS_BOOKS."$book_id/$lng/$node_path";
00507       $new_node_folder = WS_BOOKS."$book_id/$lng/$new_node_path";
00508       $parent_folder = ereg_replace('[^/]+/$', '', $new_node_folder);
00509       $msg_params = "node_path='$node_path', new_node_path='$new_node_path'";
00510       $msg = date('Y-m-d H:i').' >> Changed node id by '.USER.' >> '.$msg_params;
00511       $status = 'synchronized:'.USER.':'.EMAIL.':'.time();
00512       shell("svn update $parent_folder");
00513       shell("svn move --force $node_folder $new_node_folder");
00514       shell("svn commit $parent_folder -m '$msg'");
00515 
00516       //cache
00517       $cache_folder = WS_CACHE."$book_id/$lng/$node_path";
00518       $new_cache_folder = WS_CACHE."$book_id/$lng/$new_node_path";
00519       shell("mv $cache_folder $new_cache_folder");
00520 
00521       //update the files in the public space
00522       if (!book_fixed_to_tag($book_id, $lng))
00523         {
00524           //xml
00525           $book_dir = BOOKS."$book_id/$lng/";
00526           shell("svn update $book_dir");
00527           $new_node_folder = $book_dir.$new_node_path;
00528           $parent_folder = ereg_replace('[^/]+/$', '', $new_node_folder);
00529 
00530           //cache
00531           $cache_folder = CACHE."$book_id/$lng/$node_path";
00532           $new_cache_folder = CACHE."$book_id/$lng/$new_node_path";
00533           shell("mv $cache_folder $new_cache_folder");
00534         }
00535     }
00536 }
00537 
00539 function changeid_setid($new_id)
00540 {
00541   $book_id = WebApp::getSVar('docbook->book_id');
00542   $node_path = WebApp::getSVar('docbook->node_path');
00543 
00544   $langs = WebApp::getSVar('docbook->languages');
00545   $arr_langs = explode(',', $langs);
00546   for ($i=0; $i < sizeof($arr_langs); $i++)
00547     {
00548       $lng = $arr_langs[$i];
00549       $xml_file = file_content_xml(WS_BOOKS, $node_path, $lng);
00550 
00551       //modify id in content.xml
00552       $params = "--stringparam id \"$new_id\" ";
00553       $xsl_file = XSLT."edit/set_content_id.xsl";
00554       $content = shell("xsltproc $params $xsl_file $xml_file");
00555       write_file($xml_file, $content);
00556 
00557       //commit in svn
00558       $commit_msg = date('Y-m-d H:i') . ' >> Changed node id by ' . USER
00559         . " >> node_path='$node_path' new_id='$new_id'";
00560       shell("svn commit $xml_file -m '$commit_msg'");
00561 
00562       //update the files in the public space
00563       if (!book_fixed_to_tag($book_id, $lng))
00564         {
00565           $book_dir = BOOKS."$book_id/$lng/";
00566           shell("svn update $book_dir");
00567           $public_xml_file = file_content_xml(BOOKS, $node_path, $lng);
00568         }
00569     }
00570 }
00571 
00572 function changeid_updatecache($node_path)
00573 {
00574   //update the cache files for all the branch
00575   $book_id = WebApp::getSVar('docbook->book_id');
00576   $langs = WebApp::getSVar('docbook->languages');
00577   $arr_langs = explode(',', $langs);
00578   for ($i=0; $i < sizeof($arr_langs); $i++)
00579     {
00580       $lng = $arr_langs[$i];
00581       shell(CONTENT."cache/cache.sh $book_id $lng 'workspace' '$node_path'");
00582       if (!book_fixed_to_tag($book_id, $lng))
00583         {
00584           shell(CONTENT."cache/cache.sh $book_id $lng 'books' '$node_path'");
00585         }
00586     }
00587 
00588   //update the navigation files of the previous and next nodes
00589   $navig = get_arr_navigation($node_path);
00590   update_navigation_all_langs($navig['prev_path']);
00591   update_navigation_all_langs($navig['next_path']);      
00592 
00593   //update subnodes.html of the new node and the ancestors
00594   update_subnodes_html_all_langs($node_path);
00595 }
00596 
00607 function get_node_references()
00608 {
00609   $book_id = WebApp::getSVar('docbook->book_id');
00610   $lng = WebApp::getSVar('docbook->lng');
00611   $node_path = WebApp::getSVar('docbook->node_path');
00612 
00613   //get node_id
00614   $node_id = basename(substr($node_path,0,-1));
00615 
00616   //grep for any sections that reference this one
00617   $grep = "grep -R '<xref linkend=\"$node_id\"' ".WS_BOOKS."$book_id/";
00618   $output = shell($grep);
00619 
00620   if (trim($output)=='')  return '';  //there are no referencies
00621 
00622   //extract referencing nodes from the output
00623   $arr_lines = explode("\n", $output);
00624   for ($i=0; $i < sizeof($arr_lines); $i++)
00625     {
00626       $line = trim($arr_lines[$i]);
00627       if ($line=='') continue;
00628 
00629       $line = ereg_replace('/content\\.xml: .*', '', $line);
00630       $line = str_replace(WS_BOOKS, '', $line);
00631       $arr_refs[] = $line;
00632     }
00633 
00634   $refs = implode("\n", $arr_refs);
00635   return $refs;
00636 }
00637 ?>

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