1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 08:42:41 +01:00

move ponder writes into editors; better search indexing

Summary:
* Moved the remaining `->save()` calls into editors
* for the `PonderQuestion`, separate `attachRelated` (needed for
  indexing and viewing) from `attachVotes` (needed for viewing
  but not indexing)
* Update the indexer to include comment and answer content
  in the search index.

Test Plan:
Stuff works as before. Also: add a comment or answer with some
easily-identifiable text and search for it; observe that
it gets indexed appropriately.

Reviewers: epriestley, nh, vrana

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T1869

Differential Revision: https://secure.phabricator.com/D3654
This commit is contained in:
Pieter Hooimeijer 2012-10-07 14:35:01 -07:00
parent 741469d7df
commit 74644d5210
10 changed files with 151 additions and 32 deletions

View file

@ -1201,6 +1201,7 @@ phutil_register_library_map(array(
'PonderAnswerSaveController' => 'applications/ponder/controller/PonderAnswerSaveController.php',
'PonderAnswerViewController' => 'applications/ponder/controller/PonderAnswerViewController.php',
'PonderComment' => 'applications/ponder/storage/PonderComment.php',
'PonderCommentEditor' => 'applications/ponder/editor/PonderCommentEditor.php',
'PonderCommentListView' => 'applications/ponder/view/PonderCommentListView.php',
'PonderCommentQuery' => 'applications/ponder/query/PonderCommentQuery.php',
'PonderCommentSaveController' => 'applications/ponder/controller/PonderCommentSaveController.php',
@ -1212,6 +1213,7 @@ phutil_register_library_map(array(
'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php',
'PonderQuestionAskController' => 'applications/ponder/controller/PonderQuestionAskController.php',
'PonderQuestionDetailView' => 'applications/ponder/view/PonderQuestionDetailView.php',
'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php',
'PonderQuestionPreviewController' => 'applications/ponder/controller/PonderQuestionPreviewController.php',
'PonderQuestionQuery' => 'applications/ponder/query/PonderQuestionQuery.php',
'PonderQuestionSummaryView' => 'applications/ponder/view/PonderQuestionSummaryView.php',

View file

@ -64,8 +64,6 @@ final class PonderAnswerSaveController extends PonderController {
->setAnswer($res)
->saveAnswer();
PhabricatorSearchPonderIndexer::indexQuestion($question);
return id(new AphrontRedirectResponse())->setURI(
id(new PhutilURI('/Q'. $question->getID())));
}

View file

@ -54,10 +54,12 @@ final class PonderCommentSaveController extends PonderController {
$res
->setContent($content)
->setAuthorPHID($user->getPHID())
->setTargetPHID($target)
->save();
->setTargetPHID($target);
PhabricatorSearchPonderIndexer::indexQuestion($question);
id(new PonderCommentEditor())
->setQuestion($question)
->setComment($res)
->save();
return id(new AphrontRedirectResponse())
->setURI(

View file

@ -50,9 +50,10 @@ final class PonderQuestionAskController extends PonderController {
'ip' => $request->getRemoteAddr(),
));
$question->setContentSource($content_source);
$question->save();
PhabricatorSearchPonderIndexer::indexQuestion($question);
id(new PonderQuestionEditor())
->setQuestion($question)
->save();
return id(new AphrontRedirectResponse())
->setURI('/Q'.$question->getID());

View file

@ -33,7 +33,8 @@ final class PonderQuestionViewController extends PonderController {
if (!$question) {
return new Aphront404Response();
}
$question->attachRelated($user->getPHID());
$question->attachRelated();
$question->attachVotes($user->getPHID());
$object_phids = array($user->getPHID(), $question->getAuthorPHID());
$answers = $question->getAnswers();

View file

@ -34,10 +34,10 @@ final class PonderAnswerEditor {
public function saveAnswer() {
if (!$this->question) {
throw new Exception("Must set question before saving vote");
throw new Exception("Must set question before saving answer");
}
if (!$this->answer) {
throw new Exception("Must set answer before saving vote");
throw new Exception("Must set answer before saving it");
}
$question = $this->question;
@ -60,5 +60,8 @@ final class PonderAnswerEditor {
$trans->endReadLocking();
$trans->saveTransaction();
$question->attachRelated();
PhabricatorSearchPonderIndexer::indexQuestion($question);
}
}

View file

@ -0,0 +1,51 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PonderCommentEditor {
private $question;
private $comment;
public function setComment(PonderComment $comment) {
$this->comment = $comment;
return $this;
}
public function setQuestion(PonderQuestion $question) {
$this->question = $question;
return $this;
}
public function save() {
if (!$this->comment) {
throw new Exception("Must set comment before saving it");
}
if (!$this->question) {
throw new Exception("Must set question before saving comment");
}
$comment = $this->comment;
$question = $this->question;
$comment->save();
$question->attachRelated();
PhabricatorSearchPonderIndexer::indexQuestion($question);
}
}

View file

@ -0,0 +1,41 @@
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PonderQuestionEditor {
private $question;
private $viewer;
public function setQuestion(PonderQuestion $question) {
$this->question = $question;
return $this;
}
public function save() {
if (!$this->question) {
throw new Exception("Must set question before saving it");
}
$question = $this->question;
$question->save();
$question->attachRelated();
PhabricatorSearchPonderIndexer::indexQuestion($question);
}
}

View file

@ -20,6 +20,7 @@ final class PhabricatorSearchPonderIndexer
extends PhabricatorSearchDocumentIndexer {
public static function indexQuestion(PonderQuestion $question) {
// note: we assume someone's already called attachrelated on $question
$doc = new PhabricatorSearchAbstractDocument();
$doc->setPHID($question->getPHID());
@ -38,17 +39,29 @@ final class PhabricatorSearchPonderIndexer
PhabricatorPHIDConstants::PHID_TYPE_USER,
$question->getDateCreated());
$comments = $question->getComments();
foreach ($comments as $curcomment) {
$doc->addField(
PhabricatorSearchField::FIELD_COMMENT,
$curcomment->getContent()
);
}
$answers = $question->getAnswers();
$touches = array();
foreach ($answers as $comment) {
if (strlen($comment->getContent())) {
$doc->addField(
foreach ($answers as $curanswer) {
if (strlen($curanswer->getContent())) {
$doc->addField(
PhabricatorSearchField::FIELD_COMMENT,
$comment->getContent());
$curanswer->getContent());
}
$author = $comment->getAuthorPHID();
$touches[$author] = $comment->getDateCreated();
$answer_comments = $curanswer->getComments();
foreach ($answer_comments as $curcomment) {
$doc->addField(
PhabricatorSearchField::FIELD_COMMENT,
$curcomment->getContent()
);
}
}
self::reindexAbstractDocument($doc);

View file

@ -56,10 +56,31 @@ final class PonderQuestion extends PonderDAO
return PhabricatorContentSource::newFromSerialized($this->contentSource);
}
public function attachRelated($user_phid) {
public function attachRelated() {
$this->answers = $this->loadRelatives(new PonderAnswer(), "questionID");
$qa_phids = mpull($this->answers, 'getPHID') + array($this->getPHID());
if ($qa_phids) {
$comments = id(new PonderCommentQuery())
->withTargetPHIDs($qa_phids)
->execute();
$comments = mgroup($comments, 'getTargetPHID');
}
else {
$comments = array();
}
$this->setComments(idx($comments, $this->getPHID(), array()));
foreach ($this->answers as $answer) {
$answer->setQuestion($this);
$answer->setComments(idx($comments, $answer->getPHID(), array()));
}
}
public function attachVotes($user_phid) {
$qa_phids = mpull($this->answers, 'getPHID') + array($this->getPHID());
$edges = id(new PhabricatorEdgeQuery())
->withSourcePHIDs(array($user_phid))
->withDestinationPHIDs($qa_phids)
@ -77,23 +98,9 @@ final class PonderQuestion extends PonderDAO
$edges[$user_phid][PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_ANSWER];
$edges = null;
if ($qa_phids) {
$comments = id(new PonderCommentQuery())
->withTargetPHIDs($qa_phids)
->execute();
$comments = mgroup($comments, 'getTargetPHID');
}
else {
$comments = array();
}
$this->setUserVote(idx($question_edge, $this->getPHID()));
$this->setComments(idx($comments, $this->getPHID(), array()));
foreach ($this->answers as $answer) {
$answer->setQuestion($this);
$answer->setUserVote(idx($answer_edges, $answer->getPHID()));
$answer->setComments(idx($comments, $answer->getPHID(), array()));
}
}