Need a PHP -> Python conversion ( 7 Views )

no kitty!
  1. I'm assuming Python would be the neatest, quickest way to do this. I am using a function that was inspired by Kevin Yank. I use it in my CMS to rebuild pages. The problem is it requires the directories to be 777 since PHP runs as nobody. Since this is meant to be a semi-portable solution, I am opting not to call on my host to make config changes such as creating a group and applying group perms, as that is not a lowest common denominator in all hosting circumstances.

    So here I turn to something a little more robust. My first inclination was toward Perl but since I have the great disdain for Perl, my second inclination was Python. But now the problem is that I do not yet know Python.

    So based on this PHP function, would someone be so kind as to translate it to python.

    Actually, it is a function now, but if it could be its own separate file that would be executed by a commandline call to it, that would be great. It makes it easier to setup cron jobs then. Thanks.

    Aaron

    PHP Code:

    <?php
    function createStatic($shortname)
        {
        
    //Function loosely based on Kevin Yank's Semi-dynammic Page tutorial in "Build Your own Database Driven Website using PHP &
        //MySQL" available from Sitepoint.com
        
    global $DOCROOT, $WEBPATH;
    chmod($DOCROOT,0777);
        
    $sql = mysql_query("SELECT * FROM pages WHERE shortname = '$shortname'");
        while(
    $row = mysql_fetch_array($sql))
            {
            @
    extract($row);
            }

        @
    unlink($tempname);
        
    $tempname = "../temp.html";
        
    $target = "../$shortname.html";
        
    $dynfile =$WEBPATH."generate.php?shortname=$shortname";
        
    $dynpage = fopen($dynfile,'r') or die("Error: Cannot Read Dynamic Page");
        
    $data = fread($dynpage,1024*1024);
        
    fclose($dynpage);
        
    $tempfile = fopen($tempname,'w+') or die("Error: Unable to open temp file for writing");
        
    fwrite($tempfile,$data);
        
    fclose($tempfile);
        
    $copy = copy($tempname,$target);
        
    unlink($tempname);
    chmod($DOCROOT,0755);
        }
    ?>


    (samet, Costa Rica)

  2. Aaron, sorry I didn't get back to you sooner about this yesterday. I am going to try and get a working version to you today and I may hit you up on AIM if I have some questions.

    (aslı, Iran, Islamic Republic of)

  3. If you have a better way, I'm open. What is happening is probably illustrated a bit easier.

    Code:

    Click Link to Build Pages
      |
      |
    Loop through database extracting and extrapolating static page name. Name deduced by taking the 'shortname' field of the pages tables and appending .php to the end.
      |
      |
    In the context of the loop, $shortname is fed to the function createstatic() (displayed above).
      |
      |
    createstatic() takes a variable (also called $shortname) and calls the pages table again with a WHERE shortname = '$shortname' clause. This time, all content is called as well for a single page.
      |
      |
    Content is fed to generate.php and then in turn a [mostly] static PHP page is generated. Loop repeats through entire iteration of pages.

    That's how it works. Of course, it's porblably not the most efficient, so I'm open to ideas. The end justifies the means and the end is generating individual pages.

    (Cengiz, Eritrea)

  4. So the template needs to fill out some parts of the file that are being created? hmmm... this could get interesting.

    (selma , French Southern Territories)

  5. No that is only a functioun. Generate.php is a [mostly] HTML template with some PHP in it to fill out the content. You will need to call it.

    I'd like as little as possible to be 777. the files must be written too and owned by me, not apache/nobody like PHP would have me do.

    (bengü, Gabon)

  6. Ok, been working on this a little and had a couple of questions for ya...

    1. The generate.php file is actually the file above correct? Or do I need to call it?

    2. And from the looks of it you want the directory to be 755? Do you want the files themselves to be 777?

    (fatih, Benin)

  7. ok, let me know AQAP. :) Maybe we can collaborate on this software? :)

    (semih, Congo, the Democratic Republic of the)

  8. I will take a look at the CMS some more... The command line arguments won't be difficult, the whole program will basically be a function (much like you have) with the $shortname as an argument.

    (ridvan, Belize)

  9. Yeah...not sure how that would work, though. In essence, I need the script to loop through the resultset of a query (to find the pages) and spit out html. I guess I didn't give you the full picture since the PHP was built as a function that was included into another file. But yeah, the way the process works is PHP loops through a table with all the names and content of the various pages. With each iteration, the php function is called and a static HTML page is created. Like I said, this process depends on the files/directories being 777 and I don't want that. :\

    BTW, Stephan, if you wanted to look at the actual portion of code calling this function, I know you have the CMS download from me. Look in the processit.php file under the build section.

    Aaron

    (efe, Poland)

  10. So you want to call this from the command line with $shortname being taken by it and spitting out the .html of it?

    (nilay, Uruguay)

  11. Nevermind. No offense, but it took too long and I found another way. ;)

    Aaron

    (EMİN, Western Sahara)



Related Topics ... (or search in 1.720.883 topics !)

python base conversion (2)
python base conversion (2)
php to python (2)
python vs php (14)
php vs python (11)
php vs python (18)
converting python to php (1)
executing python from php (4)
php vs python, scalability (4)




copyright © 2007-2031 Pfodere.COM ( 5 Pfoyihuee Online )

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
0.9051