2011-03-21 23:51:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* Microapp plugin for Questions and Answers
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category QnA
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
// This check helps protect against security problems;
|
|
|
|
// your code file can't be executed directly from the web.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Question and Answer plugin
|
|
|
|
*
|
|
|
|
* @category Plugin
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
class QnAPlugin extends MicroAppPlugin
|
|
|
|
{
|
2019-06-03 01:56:52 +01:00
|
|
|
const PLUGIN_VERSION = '2.0.0';
|
2014-07-01 14:48:34 +01:00
|
|
|
|
|
|
|
var $oldSaveNew = true;
|
|
|
|
|
2011-03-21 23:51:38 +00:00
|
|
|
/**
|
|
|
|
* Set up our tables (question and answer)
|
|
|
|
*
|
|
|
|
* @see Schema
|
|
|
|
* @see ColumnDef
|
|
|
|
*
|
|
|
|
* @return boolean hook value; true means continue processing, false means stop.
|
|
|
|
*/
|
|
|
|
function onCheckSchema()
|
|
|
|
{
|
|
|
|
$schema = Schema::get();
|
|
|
|
|
|
|
|
$schema->ensureTable('qna_question', QnA_Question::schemaDef());
|
|
|
|
$schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
|
|
|
|
$schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-24 01:05:31 +00:00
|
|
|
public function newFormAction() {
|
|
|
|
return 'qnanewquestion';
|
|
|
|
}
|
|
|
|
|
2011-03-21 23:51:38 +00:00
|
|
|
/**
|
|
|
|
* Map URLs to actions
|
|
|
|
*
|
2014-11-07 14:24:05 +00:00
|
|
|
* @param URLMapper $m path-to-action mapper
|
2011-03-21 23:51:38 +00:00
|
|
|
*
|
|
|
|
* @return boolean hook value; true means continue processing, false means stop.
|
|
|
|
*/
|
|
|
|
|
2014-11-07 14:24:05 +00:00
|
|
|
public function onRouterInitialized(URLMapper $m)
|
2011-03-21 23:51:38 +00:00
|
|
|
{
|
|
|
|
$UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
|
|
|
|
2019-07-11 19:14:03 +01:00
|
|
|
$m->connect('main/qna/newquestion',
|
|
|
|
['action' => 'qnanewquestion']);
|
|
|
|
|
|
|
|
$m->connect('answer/qna/closequestion',
|
|
|
|
['action' => 'qnaclosequestion']);
|
|
|
|
|
|
|
|
$m->connect('main/qna/newanswer',
|
|
|
|
['action' => 'qnanewanswer']);
|
|
|
|
|
|
|
|
$m->connect('main/qna/reviseanswer',
|
|
|
|
['action' => 'qnareviseanswer']);
|
|
|
|
|
|
|
|
$m->connect('question/vote/:id',
|
|
|
|
['action' => 'qnavote',
|
|
|
|
'type' => 'question'],
|
|
|
|
['id' => $UUIDregex]);
|
|
|
|
|
|
|
|
$m->connect('question/:id',
|
|
|
|
['action' => 'qnashowquestion'],
|
|
|
|
['id' => $UUIDregex]);
|
|
|
|
|
|
|
|
$m->connect('answer/vote/:id',
|
|
|
|
['action' => 'qnavote',
|
|
|
|
'type' => 'answer'],
|
|
|
|
['id' => $UUIDregex]);
|
|
|
|
|
|
|
|
$m->connect('answer/:id',
|
|
|
|
['action' => 'qnashowanswer'],
|
|
|
|
['id' => $UUIDregex]);
|
2011-03-21 23:51:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:03:30 +01:00
|
|
|
public function onPluginVersion(array &$versions): bool
|
2011-03-21 23:51:38 +00:00
|
|
|
{
|
|
|
|
$versions[] = array(
|
|
|
|
'name' => 'QnA',
|
2019-06-03 01:56:52 +01:00
|
|
|
'version' => self::PLUGIN_VERSION,
|
2011-03-21 23:51:38 +00:00
|
|
|
'author' => 'Zach Copley',
|
2016-01-22 16:38:42 +00:00
|
|
|
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/QnA',
|
2011-03-21 23:51:38 +00:00
|
|
|
'description' =>
|
2011-04-19 20:54:56 +01:00
|
|
|
// TRANS: Plugin description.
|
2011-03-21 23:51:38 +00:00
|
|
|
_m('Question and Answers micro-app.')
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function appTitle() {
|
2011-04-19 20:54:56 +01:00
|
|
|
// TRANS: Application title.
|
2011-06-05 10:06:08 +01:00
|
|
|
return _m('TITLE','Question');
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tag() {
|
|
|
|
return 'question';
|
|
|
|
}
|
|
|
|
|
|
|
|
function types() {
|
|
|
|
return array(
|
|
|
|
QnA_Question::OBJECT_TYPE,
|
|
|
|
QnA_Answer::OBJECT_TYPE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a parsed ActivityStreams activity, save it into a notice
|
|
|
|
* and other data structures.
|
|
|
|
*
|
|
|
|
* @param Activity $activity
|
|
|
|
* @param Profile $actor
|
|
|
|
* @param array $options=array()
|
|
|
|
*
|
|
|
|
* @return Notice the resulting notice
|
|
|
|
*/
|
2014-07-02 10:37:46 +01:00
|
|
|
function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
|
2011-03-21 23:51:38 +00:00
|
|
|
{
|
|
|
|
if (count($activity->objects) != 1) {
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when there are too many activity objects.
|
2011-04-19 20:54:56 +01:00
|
|
|
throw new Exception(_m('Too many activity objects.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$questionObj = $activity->objects[0];
|
|
|
|
|
2011-04-06 19:15:32 +01:00
|
|
|
if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when an incorrect object type is encountered.
|
2011-04-19 20:54:56 +01:00
|
|
|
throw new Exception(_m('Wrong type for object.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$notice = null;
|
|
|
|
|
|
|
|
switch ($activity->verb) {
|
|
|
|
case ActivityVerb::POST:
|
2011-03-22 03:57:19 +00:00
|
|
|
$notice = QnA_Question::saveNew(
|
2011-03-21 23:51:38 +00:00
|
|
|
$actor,
|
2011-03-22 03:57:19 +00:00
|
|
|
$questionObj->title,
|
|
|
|
$questionObj->summary,
|
|
|
|
$options
|
2011-03-21 23:51:38 +00:00
|
|
|
);
|
|
|
|
break;
|
2011-03-22 03:57:19 +00:00
|
|
|
case Answer::ObjectType:
|
2013-08-18 12:04:58 +01:00
|
|
|
$question = QnA_Question::getKV('uri', $questionObj->id);
|
2011-03-21 23:51:38 +00:00
|
|
|
if (empty($question)) {
|
|
|
|
// FIXME: save the question
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when answering a non-existing question.
|
2011-04-19 20:54:56 +01:00
|
|
|
throw new Exception(_m('Answer to unknown question.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
2011-03-22 03:57:19 +00:00
|
|
|
$notice = QnA_Answer::saveNew($actor, $question, $options);
|
2011-03-21 23:51:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when an object type is encountered that cannot be handled.
|
|
|
|
throw new Exception(_m('Unknown object type.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $notice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a Notice into an activity object
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
|
|
|
*
|
|
|
|
* @return ActivityObject
|
|
|
|
*/
|
|
|
|
|
2014-07-02 10:37:46 +01:00
|
|
|
function activityObjectFromNotice(Notice $notice)
|
2011-03-21 23:51:38 +00:00
|
|
|
{
|
|
|
|
$question = null;
|
|
|
|
|
|
|
|
switch ($notice->object_type) {
|
|
|
|
case QnA_Question::OBJECT_TYPE:
|
|
|
|
$question = QnA_Question::fromNotice($notice);
|
|
|
|
break;
|
|
|
|
case QnA_Answer::OBJECT_TYPE:
|
|
|
|
$answer = QnA_Answer::fromNotice($notice);
|
|
|
|
$question = $answer->getQuestion();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($question)) {
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when an object type is encountered that cannot be handled.
|
2011-04-19 20:54:56 +01:00
|
|
|
throw new Exception(_m('Unknown object type.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$notice = $question->getNotice();
|
|
|
|
|
|
|
|
if (empty($notice)) {
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Exception thrown when requesting a non-existing question notice.
|
2011-04-19 20:54:56 +01:00
|
|
|
throw new Exception(_m('Unknown question notice.'));
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new ActivityObject();
|
|
|
|
|
|
|
|
$obj->id = $question->uri;
|
|
|
|
$obj->type = QnA_Question::OBJECT_TYPE;
|
|
|
|
$obj->title = $question->title;
|
2014-04-29 18:46:58 +01:00
|
|
|
$obj->link = $notice->getUrl();
|
2011-03-21 23:51:38 +00:00
|
|
|
|
|
|
|
// XXX: probably need other stuff here
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2011-04-04 00:57:59 +01:00
|
|
|
/**
|
|
|
|
* Output our CSS class for QnA notice list elements
|
|
|
|
*
|
|
|
|
* @param NoticeListItem $nli The item being shown
|
|
|
|
*
|
|
|
|
* @return boolean hook value
|
|
|
|
*/
|
|
|
|
|
2015-06-06 21:14:56 +01:00
|
|
|
function onStartOpenNoticeListItemElement(NoticeListItem $nli)
|
2011-04-04 00:57:59 +01:00
|
|
|
{
|
|
|
|
$type = $nli->notice->object_type;
|
|
|
|
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case QnA_Question::OBJECT_TYPE:
|
|
|
|
$id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
|
2014-06-21 20:01:17 +01:00
|
|
|
$class = 'h-entry notice question';
|
2011-04-05 18:46:44 +01:00
|
|
|
if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
|
|
|
|
$class .= ' limited-scope';
|
|
|
|
}
|
2011-04-18 21:53:14 +01:00
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$question = QnA_Question::getKV('uri', $nli->notice->uri);
|
2011-04-18 21:53:14 +01:00
|
|
|
|
|
|
|
if (!empty($question->closed)) {
|
|
|
|
$class .= ' closed';
|
|
|
|
}
|
|
|
|
|
2011-04-04 00:57:59 +01:00
|
|
|
$nli->out->elementStart(
|
|
|
|
'li', array(
|
2011-04-05 18:46:44 +01:00
|
|
|
'class' => $class,
|
2011-04-04 00:57:59 +01:00
|
|
|
'id' => 'notice-' . $id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
Event::handle('EndOpenNoticeListItemElement', array($nli));
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case QnA_Answer::OBJECT_TYPE:
|
|
|
|
$id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
|
2011-04-04 01:25:44 +01:00
|
|
|
|
2014-06-21 20:01:17 +01:00
|
|
|
$cls = array('h-entry', 'notice', 'answer');
|
2011-04-04 01:25:44 +01:00
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$answer = QnA_Answer::getKV('uri', $nli->notice->uri);
|
2011-04-04 01:25:44 +01:00
|
|
|
|
2011-04-04 09:27:38 +01:00
|
|
|
if (!empty($answer) && !empty($answer->best)) {
|
|
|
|
$cls[] = 'best';
|
2011-04-04 01:25:44 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 00:57:59 +01:00
|
|
|
$nli->out->elementStart(
|
2011-04-04 09:27:38 +01:00
|
|
|
'li',
|
|
|
|
array(
|
|
|
|
'class' => implode(' ', $cls),
|
2011-04-04 00:57:59 +01:00
|
|
|
'id' => 'notice-' . $id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
Event::handle('EndOpenNoticeListItemElement', array($nli));
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-12 10:22:47 +01:00
|
|
|
/**
|
|
|
|
* Output the HTML for this kind of object in a list
|
|
|
|
*
|
|
|
|
* @param NoticeListItem $nli The list item being shown.
|
|
|
|
*
|
|
|
|
* @return boolean hook value
|
|
|
|
*
|
2011-06-05 10:06:08 +01:00
|
|
|
* @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
|
2011-04-12 10:22:47 +01:00
|
|
|
*/
|
2015-06-06 21:14:56 +01:00
|
|
|
function onStartShowNoticeItem(NoticeListItem $nli)
|
2011-04-12 10:22:47 +01:00
|
|
|
{
|
|
|
|
if (!$this->isMyNotice($nli->notice)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = $nli->out;
|
|
|
|
$notice = $nli->notice;
|
|
|
|
|
2014-08-17 18:37:43 +01:00
|
|
|
$nli->showNotice($notice, $out);
|
2011-04-12 10:22:47 +01:00
|
|
|
|
|
|
|
$nli->showNoticeLink();
|
|
|
|
$nli->showNoticeSource();
|
|
|
|
$nli->showNoticeLocation();
|
2014-11-05 18:34:39 +00:00
|
|
|
$nli->showPermalink();
|
2011-04-12 10:22:47 +01:00
|
|
|
|
|
|
|
$nli->showNoticeOptions();
|
|
|
|
|
|
|
|
if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
|
|
|
|
$user = common_current_user();
|
|
|
|
$question = QnA_Question::getByNotice($notice);
|
|
|
|
|
2012-07-26 12:10:20 +01:00
|
|
|
if (!empty($user) and !empty($question)) {
|
2011-04-12 10:22:47 +01:00
|
|
|
$profile = $user->getProfile();
|
|
|
|
$answer = $question->getAnswer($profile);
|
|
|
|
|
|
|
|
// Output a placeholder input -- clicking on it will
|
|
|
|
// bring up a real answer form
|
2011-04-18 10:27:56 +01:00
|
|
|
|
|
|
|
// NOTE: this whole ul is just a placeholder
|
|
|
|
if (empty($question->closed) && empty($answer)) {
|
|
|
|
$out->elementStart('ul', 'notices qna-dummy');
|
|
|
|
$out->elementStart('li', 'qna-dummy-placeholder');
|
2011-04-12 10:22:47 +01:00
|
|
|
$out->element(
|
|
|
|
'input',
|
2011-04-18 10:27:56 +01:00
|
|
|
array(
|
|
|
|
'class' => 'placeholder',
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Placeholder value for a possible answer to a question
|
|
|
|
// TRANS: by the logged in user.
|
2011-04-18 10:27:56 +01:00
|
|
|
'value' => _m('Your answer...')
|
|
|
|
)
|
2011-04-12 10:22:47 +01:00
|
|
|
);
|
|
|
|
$out->elementEnd('li');
|
|
|
|
$out->elementEnd('ul');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-17 18:37:43 +01:00
|
|
|
function adaptNoticeListItem($nli) {
|
|
|
|
return new QnAListItem($nli);
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 09:27:38 +01:00
|
|
|
static function shorten($content, $notice)
|
|
|
|
{
|
|
|
|
$short = null;
|
|
|
|
|
|
|
|
if (Notice::contentTooLong($content)) {
|
|
|
|
common_debug("content too long");
|
|
|
|
$max = Notice::maxContent();
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Link description for link to full notice text if it is longer than
|
|
|
|
// TRANS: what will be dispplayed.
|
|
|
|
$ellipsis = _m('…');
|
2011-04-04 09:27:38 +01:00
|
|
|
$short = mb_substr($content, 0, $max - 1);
|
2011-06-05 10:06:08 +01:00
|
|
|
$short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
|
2014-04-30 19:44:23 +01:00
|
|
|
$notice->getUrl(),
|
2011-06-05 10:06:08 +01:00
|
|
|
// TRANS: Title for link that is an ellipsis in English.
|
|
|
|
_m('more...'),
|
|
|
|
$ellipsis);
|
2011-04-04 09:27:38 +01:00
|
|
|
} else {
|
|
|
|
$short = $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $short;
|
|
|
|
}
|
|
|
|
|
2011-03-21 23:51:38 +00:00
|
|
|
/**
|
|
|
|
* Form for our app
|
|
|
|
*
|
|
|
|
* @param HTMLOutputter $out
|
|
|
|
* @return Widget
|
|
|
|
*/
|
|
|
|
function entryForm($out)
|
|
|
|
{
|
2011-04-04 22:33:20 +01:00
|
|
|
return new QnanewquestionForm($out);
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When a notice is deleted, clean up related tables.
|
|
|
|
*
|
|
|
|
* @param Notice $notice
|
|
|
|
*/
|
2014-07-02 10:37:46 +01:00
|
|
|
function deleteRelated(Notice $notice)
|
2011-03-21 23:51:38 +00:00
|
|
|
{
|
|
|
|
switch ($notice->object_type) {
|
|
|
|
case QnA_Question::OBJECT_TYPE:
|
|
|
|
common_log(LOG_DEBUG, "Deleting question from notice...");
|
|
|
|
$question = QnA_Question::fromNotice($notice);
|
|
|
|
$question->delete();
|
|
|
|
break;
|
|
|
|
case QnA_Answer::OBJECT_TYPE:
|
|
|
|
common_log(LOG_DEBUG, "Deleting answer from notice...");
|
|
|
|
$answer = QnA_Answer::fromNotice($notice);
|
|
|
|
common_log(LOG_DEBUG, "to delete: $answer->id");
|
|
|
|
$answer->delete();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
common_log(LOG_DEBUG, "Not deleting related, wtf...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onEndShowScripts($action)
|
|
|
|
{
|
2011-04-05 01:31:38 +01:00
|
|
|
$action->script($this->path('js/qna.js'));
|
|
|
|
return true;
|
2011-03-21 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onEndShowStyles($action)
|
|
|
|
{
|
|
|
|
$action->cssLink($this->path('css/qna.css'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|