From: <al...@us...> - 2008-10-15 18:57:38
|
Revision: 725 http://sciret.svn.sourceforge.net/sciret/?rev=725&view=rev Author: alpeb Date: 2008-10-15 18:57:19 +0000 (Wed, 15 Oct 2008) Log Message: ----------- implemented REST API Modified Paths: -------------- trunk/flowMap.php trunk/models/ArticleGateway.php Added Paths: ----------- trunk/models/RestApi.php trunk/views/Api.php Modified: trunk/flowMap.php =================================================================== --- trunk/flowMap.php 2008-10-15 18:25:37 UTC (rev 724) +++ trunk/flowMap.php 2008-10-15 18:57:19 UTC (rev 725) @@ -46,6 +46,7 @@ 'Upgrade' => array(User::ROLE_ANONYMOUS, true, true, true, false), 'UpgradeOk' => array(User::ROLE_ANONYMOUS, true, true, true, false), 'ReportArticle' => array(User::ROLE_ANONYMOUS, true, true, true, false), + 'Api' => array(User::ROLE_ANONYMOUS, true, false, false, true), ); // ClassName => array(minimumRole, loadConfiguration, allowOnlyIfPublicKB(for User::ROLE_ANONYMOUS)?) Modified: trunk/models/ArticleGateway.php =================================================================== --- trunk/models/ArticleGateway.php 2008-10-15 18:25:37 UTC (rev 724) +++ trunk/models/ArticleGateway.php 2008-10-15 18:57:19 UTC (rev 725) @@ -329,13 +329,13 @@ return new ArticleIterator($resultSet, $totalNumItems); } - public function getArticlesForUser($user) + public function getArticlesForUser($userId) { $query = 'SELECT art_id, is_bookmark, q_id, title, url, expires, question, SUBSTRING(content, 1, '.EXCERPT_LENGTH.') AS excerpt, cat_id, published, draft, user_id, views, created, modified, modified_user_id, votes_1, votes_2, votes_3, votes_4, votes_5 ' . 'FROM articles ' . 'WHERE user_id=? ' . 'ORDER BY created DESC'; - $result = DB::getInstance()->query($query, $user->id); + $result = DB::getInstance()->query($query, $userId); $resultSet = $result->fetchAll(Zend_Db::FETCH_ASSOC); $query2 = 'SELECT FOUND_ROWS()'; @@ -374,6 +374,31 @@ return $result->rowCount(); } + + public function isUserRegularContributor($userId) + { + $articlesIt = self::getArticlesForUser($userId); + $articlesPerMonth = array( + 0 => 0, // current month + 1 => 0, // last month + 2 => 0, // 2 months ago + ); + $secsPerMonth = 30*24*60*60; + while ($article = $articlesIt->fetch()) { + $date = strtotime($article->getCreationDate()); + $monthsAgo = floor((time() - $date) / $secsPerMonth); + if ($monthsAgo > 2) { + break; + } + $articlesPerMonth[$monthsAgo]++; + } + + if ($articlesPerMonth[0] > 1 && $articlesPerMonth[1] > 1 && $articlesPerMonth[2] > 1) { + return true; + } else { + return false; + } + } } ?> Added: trunk/models/RestApi.php =================================================================== --- trunk/models/RestApi.php (rev 0) +++ trunk/models/RestApi.php 2008-10-15 18:57:19 UTC (rev 725) @@ -0,0 +1,37 @@ +<?php + +class RestApi +{ + public function getLatestArticles() + { + // use html_entity_decode(strip_tags()) for text nodes instead + // of just htmlspecialchars() because XML only accepts a few HTML entities + $xml ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; + $xml .= "<articles>\n"; + $articles = ArticleGateway::getArticles(0, true, 'latest'); + while ($article = $articles->fetch()) { + $xml .= " <article>\n" + . " <id>".$article->getId()."</id>\n" + . " <title>" + .html_entity_decode(strip_tags($article->getTitle()), ENT_NOQUOTES, 'UTF-8') + ."</title>\n" + . " <author>" + .html_entity_decode(strip_tags($article->getUser()->getFullName()), ENT_NOQUOTES, 'UTF-8') + ."</author>\n" + . " <date>".$article->getFormatedCreationDate()."</date>\n" + . " <excerpt>" + .html_entity_decode(strip_tags($article->getExcerpt()), ENT_NOQUOTES, 'UTF-8') + ."</excerpt>\n" + . " </article>\n"; + } + $xml .= "</articles>\n"; + $xml = simplexml_load_string($xml); + + return $xml; + } + + public function isUserRegularContributor($userId) + { + return ArticleGateway::isUserRegularContributor($userId); + } +} Added: trunk/views/Api.php =================================================================== --- trunk/views/Api.php (rev 0) +++ trunk/views/Api.php 2008-10-15 18:57:19 UTC (rev 725) @@ -0,0 +1,22 @@ +<?php + +/* +* @copyright Copyright (C) 2005-2008 Keyboard Monkeys Ltd http://www.kb-m.com +* @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License +* @author Alejandro Pedraza +* @since Sciret 1.0 +* @package Sciret +* @packager Keyboard Monkeys +*/ + +require_once 'views/View.php'; + +class Api extends View +{ + function dispatch() + { + $server = new Zend_Rest_Server(); + $server->setClass('RestApi'); + $server->handle(); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |