User:All is love love/Project page

From mediawiki.org

Submission for the October 2011 Coding Challenge/Wikipedia Slideshow.

"Evolution is a change from an indefinite, incoherent, homogeneity to a definite, coherent, heterogeneity, through continuous
differentiations and integrations." ~ Herbert Spencer, First Principles (1862)

WikiJoy is a [new] way to transmit Wikipedia articles, it make possible say: "This article give me joy, and worth". In other words, its a user friendly form to avaliate content from a article.

So, when you read a article and enjoyed it, you can vote in the page content, approving it.

The project itself is a double side app:

* the user side just send some informations (page id, city and language) to the server;
* server receive data from users and save it in a database, and make this data avaliable by a simple API;

A possible problem would be create a empire of "pop" articles, but using more data from Wikimedia dumps is possible get ranks by Category, and spread [good] articles from any subjects. And using API its easy create new applications describing trends and good content.

User side[edit]

/*
    wikiJoy
 Copyright (C) 2011 Jonas Xavier, Sóstenes Barros <jonas.agx and sostenesbarros @gmail.com>
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to
 
    Free Software Foundation, Inc.
    51 Franklin Street, Fifth Floor
    Boston, MA   02110-1301, USA.
 
    or see this page : https://www.gnu.org/licenses/gpl-2.0.txt
 */
addOnloadHook( 
    function () {
        if (wgAction === 'view') {
            addPortletLink( 'p-views', 'javascript:doEnjoy()', 'enjoy', 'ca-enjoy', 'Enjoy this article' );
            getEnjoy();
        }
    });
 
doEnjoy = function () {
    $.ajax(
        {
            url: 'http://ifpedia.recife.ifpe.edu.br/wikijoy/joy_api.php',
            dataType: 'json',
            type: 'POST',
            data: { 
                pageId: wgArticleId,
                pageName: wgPageName,
                language: wgContentLanguage
            },
            success: function ( data ) {
                console.log(data.success);
            }
        });
};
 
getEnjoy = function () {
    $.ajax( 
        {
            url: 'http://ifpedia.recife.ifpe.edu.br/wikijoy/joy_api.php',
            dataType: 'json',
            data: {
                pageId: wgArticleId
            },
            success: function( data ) {
                $('#ca-enjoy a').attr('title', 'Enjoy this article. It was enjoyed ' + data.page.page_joycount + ' times.');
            }
        });
};

Server side[edit]

PHP[edit]

/*
    wikiJoy
 Copyright (C) 2011 Jonas Xavier, Sóstenes Barros <jonas.agx and sostenesbarros @gmail.com>
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to
 
    Free Software Foundation, Inc.
    51 Franklin Street, Fifth Floor
    Boston, MA   02110-1301, USA.
 
    or see this page : https://www.gnu.org/licenses/gpl-2.0.txt
 */
/******************* joy_util.php *******************/

<?php
    function getJoyConnection() {
        $conn = mysql_connect( 'localhost', 'root', 'root' ) or die ( 'MySQL Error.' );
        mysql_select_db( 'joy', $conn ) or die ( 'MySQL Error.' );
        return $conn;
    }

    /* Retorna um Array com as informacoes de uma entrada na tabela joy */
    function getJoyByPageId( $pageId ) {
        $conn = getJoyConnection();
        $query = sprintf( "SELECT * FROM joy WHERE page_id = '%s'", mysql_real_escape_string($pageId) );
        $result = mysql_query( $query, $conn ) or die ('MySQL Error.');

        if (mysql_num_rows( $result )) {
            $row = mysql_fetch_assoc($result);
        }
        mysql_close($conn);
        return $row;
    }

    /* Retorna um booleano informando se a pagina foi "enjoyed" */
    function pageEnjoyed($pageId) {
        $conn = getJoyConnection();
        $query = sprintf( "SELECT page_id FROM joy WHERE page_id = '%s'", mysql_real_escape_string($pageId) );
        $result = mysql_query( $query, $conn ) or die ('MySQL Error.');
        $numRows = mysql_num_rows( $result );
        mysql_close($conn);
        return ( $numRows > 0 ) ? true : false;
    }

    /* Insere um Joy referente a uma pagina. */
    function insertJoyPage($pageId, $pageTitle, $language) {
        $conn = getJoyConnection();
        $date =  new DateTime();
        $datefmt = $date->format('Y-m-d H:i:s');
        $success = false;
        $query = sprintf( "INSERT INTO joy (page_id, page_title, page_joycount, joy_timestamp, language) values ('%s', '%s', '%s', '%s', '%s')",
                          mysql_real_escape_string($pageId),
                          mysql_real_escape_string($pageTitle),
                          mysql_real_escape_string(1),
                          mysql_real_escape_string($datefmt),
                          mysql_real_escape_string($language) );
       $success = mysql_query( $query, $conn );
       mysql_close($conn);
       return $succes;
    }

    /* Atualiza o numero de Joys da pagina com id = $pageId  */
    function updateJoyPage($pageId, $joyCount) {
        $conn = getJoyConnection();
        $date = new DateTime();
        $datefmt = $date->format('Y-m-d H:i:s');
        $success = false;
        $query = sprintf("UPDATE joy SET page_joycount = '%s' WHERE page_id = '%s'",
                          mysql_real_escape_string($joyCount),
                          mysql_real_escape_string($pageId));
        $success = mysql_query( $query, $conn );

        mysql_close($conn);
        return $success;
    }

    function addJoy($pageId, $pageTitle, $language) {
        $success = false;
        if ( pageEnjoyed( $pageId ) ) {
            //echo "UPADATE: ";
            $page = getJoyByPageId( $pageId );
            $success = updateJoyPage( $pageId, $page['page_joycount'] + 1 );
        } else {
            //echo "INSERT: ";
            $success = insertJoyPage( $pageId, $pageTitle, $language );
        }
        return $success;
    }

    function getRanking(){
        $conn = getJoyConnection();
        $query = "SELECT j.page_joycount, j.page_title FROM joy j order by j.page_joycount desc limit 10";
        $result = mysql_query($query, $conn);
        while($r[]=mysql_fetch_array($sql));
        return json_encode( $r );
    }

?>
/******************* joy_api.php *******************/

<?php
    require_once './joy_utils.php';

    $request_method = strtolower( $_SERVER['REQUEST_METHOD'] );

    switch ($request_method) {
        case 'post':
            if ( isset( $_POST['pageId'] )
                && isset( $_POST['pageName'] )
                && isset( $_POST['language'] )) {
                $result = addJoy($_POST['pageId'], $_POST['pageName'], $_POST['language']);

            } else {
                echo "Required Fields:<br />
                      pageId: Article Id.<br />
                      pageName: Article Name.<br />
                      language: Article Language (ex.: pt-BR).<br />";
            }
            $response = array('succes' => $result);
            echo json_encode($response);
            return;
        break;
        case 'get':
            if ( isset( $_GET['pageId'] ) ) {
                $result = getJoyByPageId($_GET['pageId']);
                if (empty( $result )) {
                    $result['page_joycount'] = 0;
                    echo json_encode(array( 'page' => $result ));

                } else {
                    echo json_encode(array('page' => $result));
                }
            }
            return;
        break;
        default:
           echo 'Request Method not suported: ' . $request_method;
    }
?>

SQL[edit]

/*
    wikiJoy
 Copyright (C) 2011 Jonas Xavier, Sóstenes Barros <jonas.agx and sostenesbarros @gmail.com>
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to
 
    Free Software Foundation, Inc.
    51 Franklin Street, Fifth Floor
    Boston, MA   02110-1301, USA.
 
    or see this page : https://www.gnu.org/licenses/gpl-2.0.txt
 */
create table joy(
	page_id int primary key, 
	page_title varchar(255),
	page_joycount int, 
	joy_timestamp timestamp, 
	language varchar(10)
);

create table category(
	page_id int, 
	category_id int, 
	category varchar(255)
);

create table user(
	user_id int unsigned NOT NULL primary key,
	page_id int unsigned NOT NULL,
	joy_timestamp timestamp
);