arm1.ru

Tag: «скрипты»

PHP script for building all views in CouchDB

event Jan 10, 2013 at 15:20

I wrote this little script before New Year. It goes through all databases in CouchDB and builds all views one by one. Leaving it here.

<?php

            set_time_limit( 0 );

            // all dbs
            $ch = prepareCurlResource();
            curl_setopt( $ch, CURLOPT_URL, 'http://localhost:5984/_all_dbs' );
            $data = curl_exec( $ch );
            $dbs = json_decode( $data );

            foreach ( $dbs as $db ) {
                // skip _users and _replicator
                if ( substr( $db, 0, 1 ) == '_' )
                    continue;

                // getting all databases
                $chCC = prepareCurlResource();
                curl_setopt( $chCC, CURLOPT_URL, 'http://localhost:5984/' . $db . '/_all_docs?startkey="_design/"&endkey="_design0"&include_docs=true' );
                $data = json_decode( curl_exec( $chCC ) );

                if ( false === empty( $data->rows ) ) {
                    foreach( $data->rows as $design ) {

                        // creating views of design
                        if ( false === empty( $design->doc->views ) ) {
                            foreach( $design->doc->views as $viewName => $tmp ) {
                                echo 'Creating view: ' . $db . '/' . $design->id . '/_view/' . $viewName . "
";

                                $chCC = prepareCurlResource();
                                curl_setopt( $chCC, CURLOPT_URL, 'http://localhost:5984/' . $db . '/' . $design->id . '/_view/' . $viewName . '?limit=0' );
                                curl_exec( $chCC );
                            }
                        }
                    }
                }
            }

            function prepareCurlResource() {
            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_PORT, 5984 );
            curl_setopt( $ch, CURLOPT_HEADER, false );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );

            return $ch;
            }
            

Script for pinning a block while scrolling

event Mar 28, 2012 at 14:03

I had wanted to make something like this for a long time, so that when the page is scrolled some block would scroll together with the page up to a certain point and then stick. Today I found a plugin called jQuery Sticky Scroller

You can download it here.

It is connected quite simply:

var scroller = new StickyScroller("#menu",
{
    start: 270,
    end: 50800,
    interval: 200,
    range: 100,
    margin: 0
});

As the parameter name suggests, when scrolling goes more than 270 pixels past the top edge of the page, the #menu element is fixed on the page, that is, it gets the CSS property position: fixed;. The end parameter is responsible for the lower bound of scroll tracking, in case the fixed element should not stay fixed all the time. The margin parameter defines how far from the top edge of the browser window the element will be positioned.

The plugin also has several public methods; I do not see much point in describing them here, the description is available on the plugin page.

Posting to Twitter via PHP

event Aug 15, 2011 at 18:44

Long meant to share my modest set of functions for posting tweets to Twitter. Maybe useful to someone — none of it came to me right away, especially the signature generation. Plus this is my attempt to lock into my head what I’ve learnt and coded. The best way to do that, I think, is to try to explain it to someone else :) Description and code below the cut.

Resizing animated GIF images with Imagick

event Jul 13, 2011 at 19:46

At work I ran into the need to process animated GIF avatars. The source images can be of any size, and they need to be downsized to a target size with cropping to a square. Below the cut — how we solved it.

Simple online TimeStamp converter

event May 13, 2011 at 00:57

At work I occasionally have to look at some data from the database. Times we mostly store as TimeStamp (the number of seconds since the dawn of the Unix world — 1 January 1970).

A number like 1305233826 tells you nothing about what date it is. Writing the conversion in some script every time gets tedious, so I made an online converter. It shows the time in a human-readable form.

Enjoy. Online TimeStamp Converter