php-general Digest 25 Jan 2010 19:40:35 -0000 Issue 6557

Topics (messages 301398 through 301411):

importNode issue
        301398 by: Michael A. Peters
        301399 by: Michael A. Peters
        301400 by: Jochem Maas
        301403 by: Michael A. Peters

How to change a filename for download (e.g. jpeg, pdf etc.)
        301401 by: SED
        301402 by: Lester Caine
        301404 by: Richard
        301405 by: Ashley Sheridan
        301407 by: Mourad Boufarguine

Re: Enforce a constant in a class.
        301406 by: Pete Ford
        301408 by: Richard Quadling

Recursion issue with Zend_Soap_AutoDiscovery.
        301409 by: Richard Quadling
        301410 by: Nathan Rixham

Re: memory efficient hash table extension? like lchash ...
        301411 by: J Ravi Menon

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message --- I'm experiencing a slight problem with importNODE putting unwanted carriage returns in the the output.

Here's my function:

// syntax highlighting
include_once('Text/Highlighter.php');

function syntaxHighlight($dom,$lang,$code) {
   $hl =& Text_Highlighter::factory($lang);
   $out = $hl->highlight($code);
   //die($out);
   $tmpDOM = new DOMDocument('1.0','UTF-8');
   $tmpDOM->loadXML($out);
   $foo = $tmpDOM->saveXML();
   //die($foo);

   $nodeList = $tmpDOM->getElementsByTagName('div');
   $impDIV = $nodeList->item(0);
   $returnDIV = $dom->importNode($impDIV,true);

   return $returnDIV;
   }

-=-

Here's my test:

$code  ="<?php" . "\n\n";
$code .="require_once('/path/to/something');" . "\n";
$code .="function somefunc(\$myfoo,\$mybar) {" . "\n";
$code .="   \$myfoobar = \$myfoo . \$mybar;" . "\n";
$code .="   return \$myfoobar;" . "\n";
$code .="   }" . "\n";
$code .="?>" . "\n";

$fooTest = syntaxHighlight($dom,'PHP',$code);

-=-

If I uncomment the die($out) - I get what I expect spit to the screen, view source shows code that will do what I want.

If instead I uncomment die($foo) - I also get what I expect spit to screen. view source shows code that will do what I want.

However, if the function is allowed to continue, the imported div has carriage returns between each and every </span><span> which of course completely breaks the browser display because they are inside a <pre></pre> node.

Anyone know why importNode does this and how to fix it?

The only (untried) solution I can think of is to replace each carriage return with a <br /> and every space with &#160; and then replace the <pre> with a <div class='monospace'> or some such hackery before running loadXML() on it. But I would rather not do that.

php 5.2.12 built against libxml 2.6.26

--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:


The only (untried) solution I can think of is to replace each carriage return with a <br /> and every space with &#160; and then replace the <pre> with a <div class='monospace'> or some such hackery before running loadXML() on it. But I would rather not do that.

Even that isn't really working but what I think I may be able to do, though it would be a PITA, is go through the list of nodes one by one and create identical nodes and append them to a node that isn't imported.
--- End Message ---
--- Begin Message ---
highlight_string() function might be an easier route?

Op 1/25/10 9:55 AM, Michael A. Peters schreef:
> I'm experiencing a slight problem with importNODE putting unwanted
> carriage returns in the the output.
> 
> Here's my function:
> 
> // syntax highlighting
> include_once('Text/Highlighter.php');
> 
> function syntaxHighlight($dom,$lang,$code) {
>    $hl =& Text_Highlighter::factory($lang);
>    $out = $hl->highlight($code);
>    //die($out);
>    $tmpDOM = new DOMDocument('1.0','UTF-8');
>    $tmpDOM->loadXML($out);
>    $foo = $tmpDOM->saveXML();
>    //die($foo);
> 
>    $nodeList = $tmpDOM->getElementsByTagName('div');
>    $impDIV = $nodeList->item(0);
>    $returnDIV = $dom->importNode($impDIV,true);
> 
>    return $returnDIV;
>    }
> 
> -=-
> 
> Here's my test:
> 
> $code  ="<?php" . "\n\n";
> $code .="require_once('/path/to/something');" . "\n";
> $code .="function somefunc(\$myfoo,\$mybar) {" . "\n";
> $code .="   \$myfoobar = \$myfoo . \$mybar;" . "\n";
> $code .="   return \$myfoobar;" . "\n";
> $code .="   }" . "\n";
> $code .="?>" . "\n";
> 
> $fooTest = syntaxHighlight($dom,'PHP',$code);
> 
> -=-
> 
> If I uncomment the die($out) - I get what I expect spit to the screen,
> view source shows code that will do what I want.
> 
> If instead I uncomment die($foo) - I also get what I expect spit to
> screen. view source shows code that will do what I want.
> 
> However, if the function is allowed to continue, the imported div has
> carriage returns between each and every </span><span> which of course
> completely breaks the browser display because they are inside a
> <pre></pre> node.
> 
> Anyone know why importNode does this and how to fix it?
> 
> The only (untried) solution I can think of is to replace each carriage
> return with a <br /> and every space with &#160; and then replace the
> <pre> with a <div class='monospace'> or some such hackery before running
> loadXML() on it. But I would rather not do that.
> 
> php 5.2.12 built against libxml 2.6.26
> 


--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
highlight_string() function might be an easier route?

If I only ever wanted to highlight php it might be.

I found a workaround, though I don't like it.

add

$dom->formatOutput = false;

to the function and it displays perfectly, though viewing the generated source isn't as nice (hence why I add it only when that function is called).
--- End Message ---
--- Begin Message ---
Hi,

Can anyone point me to tutorials on how to change a filename for each
download? My goal is to give the downloader a random name for a picture or a
document, so he will never know what the original filename is.

Regards,
Summi




--- End Message ---
--- Begin Message ---
SED wrote:
Hi,

Can anyone point me to tutorials on how to change a filename for each
download? My goal is to give the downloader a random name for a picture or a
document, so he will never know what the original filename is.

http://uk.php.net/manual/en/function.tempnam.php any use ...

--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

--- End Message ---
--- Begin Message ---
Hi,

> Can anyone point me to tutorials on how to change a filename for each
> download? My goal is to give the downloader a random name for a picture or a
> document, so he will never know what the original filename is.

Try adding a Content-Disposition header:

<?php
   header('Content-disposition: attachment; filename=fname.ext');
?>

--
Richard Heyes
HTML5 canvas graphing: RGraph - http://www.rgraph.net (updated 23rd January)

--- End Message ---
--- Begin Message ---
On Mon, 2010-01-25 at 10:03 +0000, SED wrote:

> Hi,
> 
> Can anyone point me to tutorials on how to change a filename for each
> download? My goal is to give the downloader a random name for a picture or a
> document, so he will never know what the original filename is.
> 
> Regards,
> Summi
> 
> 
> 
> 


I save the file using the temp filename given to it by PHP during the
upload process. If that's not possible, then some sort of hash (MD5 for
example) of the original file name would suffice. Then, when the user
requests that file, they have to request it with a URL like
file.php?file=hashname

The added benefit of this is that you can verify the user is logged in
or not too. The only problem would be if you were serving media files,
as no plugins I know of send all the correct headers for the media file
request, so your browser would see it as an anonymous request.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
take a look at this :

http://www.phpclasses.org/browse/package/3220.html


On Mon, Jan 25, 2010 at 11:32 AM, Ashley Sheridan
<[email protected]>wrote:

> On Mon, 2010-01-25 at 10:03 +0000, SED wrote:
>
> > Hi,
> >
> > Can anyone point me to tutorials on how to change a filename for each
> > download? My goal is to give the downloader a random name for a picture
> or a
> > document, so he will never know what the original filename is.
> >
> > Regards,
> > Summi
> >
> >
> >
> >
>
>
> I save the file using the temp filename given to it by PHP during the
> upload process. If that's not possible, then some sort of hash (MD5 for
> example) of the original file name would suffice. Then, when the user
> requests that file, they have to request it with a URL like
> file.php?file=hashname
>
> The added benefit of this is that you can verify the user is logged in
> or not too. The only problem would be if you were serving media files,
> as no plugins I know of send all the correct headers for the media file
> request, so your browser would see it as an anonymous request.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

--- End Message ---
--- Begin Message ---
Richard Quadling wrote:
2010/1/22 Pete Ford <[email protected]>:
IMHO, a constant is not the correct beastie in this case - if you want it to
be different depending on the implementation then it ain't a constant!

You should probably have protected static variables in the interface, and
use the implementation's constructor to set the implementation-specific
value (or override the default)

interface SetKillSwitch
{
       protected static $isSet = TRUE;
       protected static $notes;
       protected static $date = '2010-01-22T11:23:32+0000';
}

class KilledClass implements SetKillSwitch
{
       public function __construct()
       {
               self::$isSet = FALSE;
               self::$date = '2010-01-21T09:30:00+0000';
               self::$notes = "Test";
       }
}

Cheers
Pete Ford

And of course, "Fatal error: Interfaces may not include member variables".




Ooops, sorry :)

I tend to end up using abstract base classes rather than interfaces for that sort of reason...
--- End Message ---
--- Begin Message ---
2010/1/25 Pete Ford <[email protected]>:
> Richard Quadling wrote:
>>
>> 2010/1/22 Pete Ford <[email protected]>:
>>>
>>> IMHO, a constant is not the correct beastie in this case - if you want it
>>> to
>>> be different depending on the implementation then it ain't a constant!
>>>
>>> You should probably have protected static variables in the interface, and
>>> use the implementation's constructor to set the implementation-specific
>>> value (or override the default)
>>>
>>> interface SetKillSwitch
>>> {
>>>       protected static $isSet = TRUE;
>>>       protected static $notes;
>>>       protected static $date = '2010-01-22T11:23:32+0000';
>>> }
>>>
>>> class KilledClass implements SetKillSwitch
>>> {
>>>       public function __construct()
>>>       {
>>>               self::$isSet = FALSE;
>>>               self::$date = '2010-01-21T09:30:00+0000';
>>>               self::$notes = "Test";
>>>       }
>>> }
>>>
>>> Cheers
>>> Pete Ford
>>
>> And of course, "Fatal error: Interfaces may not include member variables".
>>
>>
>>
>
> Ooops, sorry :)
>
> I tend to end up using abstract base classes rather than interfaces for that
> sort of reason...
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Essentially I was starting with the idea that a subclass with constant
X _MUST_ have constant Y and Z. That's what I wanted to enforce.

But, finding that defined() was enough, I now realize that Y and Z are
_not_ mandatory, but they are constants. So simply ...

$KillSwitchNotes = defined(get_called_class() . '::KILL_SWITCH_NOTES') ?: Null;

is enough and now the whole _MUST_ is gone and is now optional.

Much better.


Thank you to everyone who chipped in. Old dog should really have known
that old trick!


-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--- End Message ---
--- Begin Message ---
Hi.

I'm in the process of building a web service which incorporates the
ability for the server to inform the client that a particular call has
been superseded by another.

So, cut down (I've removed all the other details), ...

class ServiceDetails
        {
        /**
         * Superseded by
         *
         * Details of the replacement service that is now available.
         *
         * @var ServiceDetails
         */
        public $SupersededBy = Null;
        }

When I try to use Zend_Soap_AutoDiscover() against this class, I get ...

"Infinite recursion, cannot nest 'ServiceDetails' into itsself." (sic)

There has to be recursion, as there could be many levels of
supersedence, each one providing the details of their own replacement.

The call to return the service details read the requested
services/class constants. If there is a superseded entry, it creates a
new request for service details on the new class (the recursion).

If the value is Null, then there is no recursion.



I'm using ...

new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');

as the strategy as the service has arrays of complex types in the output.



If I use @var string and then manually replace the type in the WSDL
file from ...

          <xsd:element name="SupersededBy" type="xsd:string" />

to

          <xsd:element name="SupersededBy" type="tns:ServiceDetails" />

and use wsdl2php against this, it all _SEEMS_ to work OK.

So. Is this my best option? Or is there a way to do this that I'm missing?


Any ideas really.


Is this even a bug in the Zend_Soap_AutoDiscover class?



Regards,

Richard Quadling.




-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--- End Message ---
--- Begin Message ---
Richard Quadling wrote:
> Hi.
> 
> I'm in the process of building a web service which incorporates the
> ability for the server to inform the client that a particular call has
> been superseded by another.
> 
> So, cut down (I've removed all the other details), ...
> 
> class ServiceDetails
>       {
>       /**
>        * Superseded by
>        *
>        * Details of the replacement service that is now available.
>        *
>        * @var ServiceDetails
>        */
>       public $SupersededBy = Null;
>       }
> 
> When I try to use Zend_Soap_AutoDiscover() against this class, I get ...
> 
> "Infinite recursion, cannot nest 'ServiceDetails' into itsself." (sic)
> 
> There has to be recursion, as there could be many levels of
> supersedence, each one providing the details of their own replacement.
> 
> The call to return the service details read the requested
> services/class constants. If there is a superseded entry, it creates a
> new request for service details on the new class (the recursion).
> 
> If the value is Null, then there is no recursion.
> 
> 
> 
> I'm using ...
> 
> new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
> 
> as the strategy as the service has arrays of complex types in the output.
> 
> 
> 
> If I use @var string and then manually replace the type in the WSDL
> file from ...
> 
>           <xsd:element name="SupersededBy" type="xsd:string" />
> 
> to
> 
>           <xsd:element name="SupersededBy" type="tns:ServiceDetails" />
> 
> and use wsdl2php against this, it all _SEEMS_ to work OK.
> 
> So. Is this my best option? Or is there a way to do this that I'm missing?
> 
> 
> Any ideas really.
> 

http://wso2.org/projects/wsf/php ;)

helpful eh


--- End Message ---
--- Begin Message ---
PHP does expose sys V shared-memory apis (shm_* functions):

http://us2.php.net/manual/en/book.sem.php

If you already have apc installed, you could also try:

http://us2.php.net/manual/en/book.apc.php

APC also allows you to store user specific data too (it will be in a
shared memory).

Haven't tried these myself, so I would do some quick tests to ensure
if they meet your performance requirements. In theory, it should be
faster than berkeley-db like solutions (which is also another option
but it seems something similar like MongoDB was not good enough?).

I  am curious to know if someone here has run these tests. Note that
with memcached installed locally (on the same box running php), it can
be surprisingly efficient - using pconnect(),  caching the handler in
a static var for a given request cycle etc...

Ravi






On Sun, Jan 24, 2010 at 9:39 AM, D. Dante Lorenso <[email protected]> wrote:
> shiplu wrote:
>>
>> On Sun, Jan 24, 2010 at 3:11 AM, D. Dante Lorenso <[email protected]>
>> wrote:
>>>
>>> All,
>>>
>>> I'm loading millions of records into a backend PHP cli script that I
>>> need to build a hash index from to optimize key lookups for data that
>>> I'm importing into a MySQL database.  The problem is that storing this
>>> data in a PHP array is not very memory efficient and my millions of
>>> records are consuming about 4-6 GB of ram.
>>>
>>
>> What are you storing? An array of row objects??
>> In that case storing only the row id is will reduce the memory.
>
> I am querying a MySQL database which contains 40 million records and mapping
> string columns to numeric ids.  You might consider it normalizing the data.
>
> Then, I am importing a new 40 million records and comparing the new values
> to the old values.  Where the value matches, I update records, but where
> they do not match, I insert new records, and finally I go back and delete
> old records.  So, the net result is that I have a database with 40 million
> records that I need to "sync" on a daily basis.
>
>> If you are loading full row objects, it will take a lot of memory.
>> But if you just load the row id values, it will significantly decrease
>> the memory amount.
>
> For what I am trying to do, I just need to map a string value (32 bytes) to
> a bigint value (8 bytes) in a fast-lookup hash.
>
>> Besides, You can load row ids in a chunk by chunk basis. if you have
>> 10 millions of rows to process. load 10000 rows as a chunk. process
>> them then load the next chunk.  This will significantly reduce memory
>> usage.
>
> When importing the fresh 40 million records, I need to compare each record
> with 4 different indexes that will map the record to existing other records,
> or into a "group_id" that the record also belongs to.  My current solution
> uses a trigger in MySQL that will do the lookups inside MySQL, but this is
> extremely slow.  Pre-loading the mysql indexes into PHP ram and processing
> that was is thousands of times faster.
>
> I just need an efficient way to hold my hash tables in PHP ram.  PHP arrays
> are very fast, but like my original post says, they consume way too much
> ram.
>
>> A good algorithm can solve your problem anytime. ;-)
>
> It takes about 5-10 minutes to build my hash indexes in PHP ram currently
> which makes up for the 10,000 x speedup on key lookups that I get later on.
>  I just want to not use the whole 6 GB of ram to do this.   I need an
> efficient hashing API that supports something like:
>
>        $value = (int) fasthash_get((string) $key);
>        $exists = (bool) fasthash_exists((string) $key);
>        fasthash_set((string) $key, (int) $value);
>
> Or ... it feels like a "memcached" api but where the data is stored locally
> instead of accessed via a network.  So this is how my search led me to what
> appears to be a dead "lchash" extension.
>
> -- Dante
>
> ----------
> D. Dante Lorenso
> [email protected]
> 972-333-4139
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---

Reply via email to