serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
http://mobiledetect.net
MIT License
10.54k stars 2.68k forks source link

Suggestion: check for updates #361

Closed ghost closed 9 years ago

ghost commented 9 years ago

Would be cool to have an api or entry point where we could check the current version, and download a new script into place when it changes. I'd set up a cron job to update my site once a week or so perhaps....

Thanks to all for sharing.

robhadfield commented 9 years ago

+1 that idea.

nicktacular commented 9 years ago

Given that this is a Packagist-enabled repository, that's exactly what composer update does. You can even update only this package by specifying its name: https://getcomposer.org/doc/03-cli.md#update

ghost commented 9 years ago

You are correct thank you. But that is asking too much of my customers, nor would I want to support that method for my customers. Better to bury it in my existing product logic, which has it's own installation and update procedures. I guess I will have to dig into composer to see how it fetches what from where, and if that logic can be extracted. Not a composer dude here :( I don't think I'm asking for new code, I think I'm exposing my ignorance of github and composer. The versions are online here https://github.com/serbanghita/Mobile-Detect/tags so I guess that is a start. Or perhaps I can run composer on my server, and make the latest available there.

I apologize for commenting further, but I think this may be common. I'm talking about updating "dumb users". I do not require a reply and you may close again. Thank you.

nicktacular commented 9 years ago

Each change is pushed to https://packagist.org/packages/mobiledetect/mobiledetectlib automatically. Composer uses that by default to determine if an update is available for the require or require-dev line.

What you're asking already exists and is definitely something you're going to be re-inventing the wheel with. Trying to do anything else will be more work. Should you wish to do so, the simplest way would be to write a script, set it up in cron, and do git fetch regularly to determine if there's a new version available. An even easier solution is to install composer and simply run composer update mobiledetect/mobiledetectlib in your app.

ghost commented 9 years ago

Imagine servers without git and composer. Imagine strict company policies about installing git and composer. Imagine users and support! I appreciate your points, but the extra work is small, and I get paid for it. Value for the customer is not having to appreciate your points, nor install and manage more software from more sources. Tis done. My own server is now a relay. I'll provide the latest to my apps. Thanks.

ghost commented 9 years ago

I think you can use this rss instead of a real API: https://packagist.org/feeds/package.mobiledetect/mobiledetectlib.rss Ping that RSS to see if the latest version number has changed. If it has then download the file from github here: https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.php

Would that work?

serbanghita commented 9 years ago

@HankBrown I understand where you're coming from, the same policies are applied on some projects on the company I work. Although I also suggest advocating automation tools like composer like @nicktacular explained, you can implement the solution given by @ChefGaby

Previously we had users requesting us to keep the @version tag in the Mobile_Detect.php file. If you look at https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.php you can parse the line containing the version:

 * @version     2.8.11

On every update I bump this version.

nicktacular commented 9 years ago

Imagine strict company policies about installing git and composer.

@HankBrown every place I've worked at has strict policies like this, so I do understand where you're coming from. However, regardless what your path, you need to whitelist github.com so that you can get this data (or Packagist). Also, composer is a PHP phar file, so if you can run PHP files, you can run composer. Given that this is a PHP repo, I'm going to assume that you can run PHP so running composer after you have whitelisted git access means you can do this.

Am I missing something here?

nolybom commented 9 years ago

@ChefGaby i like your suggestion. Can you provide us here e step by step explanation please?

ghost commented 9 years ago

@nicktacular yes and no. You are not wrong. You are just not coming right down to the idiot level. The web was built for sharing and it's easy to overthink that. ChefGaby's solution is almost perfect, it's just a pity there has to be protocol and IP restrictions and complications. But the if you are going to put something out there for free, I guess somebody has to pay the rent.

Another use case is adult, social, and very very stupid. Freelancers want to release a $5 widget without a technical manual.

Finally I notice my question garnered two others who appear to be in the same boat. The cut and paste crowd who want their code on a platter please.

You guys are right and your advice is sound. Thank you for your suggestions and pointers, they are very helpful.

Perhaps what we are suggesting is some code in the example folder to show us how to have a self-updating script. I can make a first effort at this later in the week.

ghost commented 9 years ago

Here's what I've come up with so far. Might not be perfect but it works. Create a file called check-mobile-detect-version.php, place it in the same folder as the file Mobile_Detect.php and then add this code in it:

<?php
$current_version_file = 'Mobile_Detect.php'; //path to your current Mobile_Detect.php file
$latest_version_file = 'https://raw.githubusercontent.com/serbanghita/Mobile-Detect/master/Mobile_Detect.php';
$rss_url = 'https://packagist.org/feeds/package.mobiledetect/mobiledetectlib.rss';
$rss = file_get_contents($rss_url);
$new_version = preg_match('#\((.*?)\)#', $rss, $matches);
$new_version = $matches[1];

$file = new SplFileObject($current_version_file);
$file->seek(24);
$current_version = $file->current();
$current_version = preg_replace("/([^0-9.])/", "", $current_version);

if($current_version != $new_version) {
    $new_version_file = file_get_contents($latest_version_file);
    if($new_version_file !== false) {
        $fp = fopen($current_version_file, 'w');
        fwrite($fp, $new_version_file);
        fclose($fp);
    }
}
?>

Then create a cron job from your hosting control panel or from the command prompt and have it run the file check-mobile-detect-version.php once a week. You could make it once a day but that would be kind of pointless since new versions are not released that frequently and in the worst case scenario you use an old version for a maximum of 1 week.

nicktacular commented 9 years ago

@HankBrown:

The web was built for sharing and it's easy to overthink that.

Ok, I don't think what I was saying is in conflict with this.

Another use case is adult, social, and very very stupid. Freelancers want to release a $5 widget without a technical manual.

That's fine, it should be accessible. I'm just suggesting going a path of least resistance to these types of users.

@ChefGaby:

If someone chooses this solution, please be careful about seek(24) since there's no guarantee of whitespace or formatting. You would be better off just looking at all T_DOC_COMMENT of token_get_all() and just do a preg_match against /@version\\s(?<version>[0-9.]+)/. Otherwise, what you're doing is fine, assuming you've considered rate limits and adding proper error handling.