| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 153 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | function boinc_solr_comments_apachesolr_index_documents_alter(array &$documents, $entity, $entity_type, $env_id) { |
||
| 61 | $comdocs = array(); |
||
| 62 | |||
| 63 | // Array of node types that will have comments indexed. |
||
| 64 | $node_types = variable_get('boinc_solr_comments_nodetypes', ''); |
||
| 65 | if (empty($node_types)) { |
||
| 66 | // If the variable has not been set, then by default index |
||
| 67 | // comments for all known node types. |
||
| 68 | $node_types = array( |
||
| 69 | 'forum' => 'forum', |
||
| 70 | 'news' => 'news', |
||
| 71 | 'page' => 'page', |
||
| 72 | 'story' => 'story', |
||
| 73 | 'team_forum' => 'team_forum', |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | // Loop over all documents and alter: |
||
| 78 | foreach ($documents as $document) { |
||
| 79 | //dd($document, "index documents alter - document"); |
||
| 80 | |||
| 81 | $to_process = ( array_key_exists($document->bundle, $node_types) AND $node_types[$document->bundle] ); |
||
| 82 | if ( $document->entity_type=='node' AND $to_process) { |
||
| 83 | |||
| 84 | // Remove ts_comments if present. |
||
| 85 | if (isset($document->ts_comments)) { |
||
| 86 | unset($document->ts_comments); |
||
| 87 | } |
||
| 88 | // Node information. |
||
| 89 | $nid = $document->entity_id; |
||
| 90 | $node = node_load($nid); |
||
| 91 | |||
| 92 | // Query database for comments of node. Only return comments |
||
| 93 | // with status 0 (0 is published for comments). |
||
| 94 | $sql = 'SELECT cid from {comments} WHERE nid = %d AND status = 0 ORDER by timestamp DESC'; |
||
| 95 | $resource = db_query($sql, $nid); |
||
| 96 | while ($row = db_fetch_array($resource)) { |
||
| 97 | // Load the comment from cid. |
||
| 98 | $comment = _comment_load($row['cid']); |
||
| 99 | |||
| 100 | // Code derived from _apachesolr_index_process_entity_get_document |
||
| 101 | $url_options = array('absolute' => TRUE); |
||
| 102 | $comment_document = new ApacheSolrdocument(); |
||
| 103 | |||
| 104 | $comment_document->id = apachesolr_document_id($comment->cid, 'comment'); |
||
| 105 | $comment_document->site = $document->site; |
||
| 106 | $comment_document->hash = apachesolr_site_hash(); |
||
| 107 | |||
| 108 | $comment_document->entity_id = $comment->cid; |
||
| 109 | $comment_document->entity_type = 'comment'; |
||
| 110 | $comment_document->bundle = 'Comment'; |
||
| 111 | $comment_document->bundle_name = 'Comment'; |
||
| 112 | |||
| 113 | $comment_document->path = 'goto/comment/' . $comment->cid; |
||
| 114 | $comment_document->url = url($comment_document->path, $url_options); |
||
| 115 | |||
| 116 | // Comment object has no language |
||
| 117 | $comment_document->ss_language = 'und'; |
||
| 118 | if (function_exists('drupal_get_path_alias')) { |
||
| 119 | $output = drupal_get_path_alias($comment_document->path, NULL); |
||
| 120 | if ($output && $output != $document->path) { |
||
| 121 | $comment_document->path_alias = $output; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // Code derived from apachesolr_index_node_solr_document |
||
| 126 | $comment_document->label = truncate_utf8(apachesolr_clean_text($comment->comment), 32, TRUE); |
||
| 127 | $comment_document->content = apachesolr_clean_text($comment->comment); |
||
| 128 | $comment_document->teaser = truncate_utf8($document->content, 300, TRUE); |
||
| 129 | |||
| 130 | // Author information |
||
| 131 | if ($comment->uid == 0 || strlen($comment->name) == 0) { |
||
| 132 | // @see user_validate_name(). !'0' === TRUE. |
||
| 133 | $comment_document->ss_name = '0'; |
||
| 134 | } |
||
| 135 | else { |
||
| 136 | $comment_document->ss_name = $comment->name; |
||
| 137 | // We want the name to be searchable for keywords. |
||
| 138 | $comment_document->tos_name = $comment->name; |
||
| 139 | } |
||
| 140 | |||
| 141 | // Index formatted username so it can be searched and sorted |
||
| 142 | // on. |
||
| 143 | $account = (object) array('uid' => $comment->uid, 'name' => $comment->name); |
||
| 144 | $username = check_plain($account->name); |
||
| 145 | $comment_document->ss_name_formatted = $username; |
||
| 146 | $comment_document->tos_name_formatted = $username; |
||
| 147 | $comment_document->is_uid = $comment->uid; |
||
| 148 | $comment_document->bs_status = $comment->status; |
||
| 149 | |||
| 150 | // Timestamp of the comment |
||
| 151 | $comment_document->ds_created = apachesolr_date_iso($comment->timestamp); |
||
| 152 | $comment_document->ds_changed = apachesolr_date_iso($comment->timestamp); |
||
| 153 | |||
| 154 | // Add field for comment parent node id. |
||
| 155 | $comment_document->tos_content_extra = $nid; |
||
| 156 | |||
| 157 | // Unset the following fields (if set) that don't make sense |
||
| 158 | // for a comment. |
||
| 159 | if (isset($comment_document->bs_sticky)) { |
||
| 160 | unset($comment_document->bs_sticky); |
||
| 161 | } |
||
| 162 | if (isset($comment_document->bs_promote)) { |
||
| 163 | unset($comment_document->bs_promote); |
||
| 164 | } |
||
| 165 | if (isset($comment_document->is_tnid)) { |
||
| 166 | unset($comment_document->is_tnid); |
||
| 167 | } |
||
| 168 | if (isset($comment_document->bs_translate)) { |
||
| 169 | unset($comment_document->bs_translate); |
||
| 170 | } |
||
| 171 | if (isset($comment_document->ts_last_comment_timestamp)) { |
||
| 172 | unset($comment_document->ts_last_comment_timestamp); |
||
| 173 | } |
||
| 174 | if (isset($comment_document->ds_last_comment_or_change)) { |
||
| 175 | unset($comment_document->ds_last_comment_or_change); |
||
| 176 | } |
||
| 177 | if (isset($comment_document->is_comment_count)) { |
||
| 178 | unset($comment_document->is_comment_count); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Loop over fields in document and copy relevant values into |
||
| 182 | // comment_document. |
||
| 183 | foreach ($document as $fieldName => $fieldValue) { |
||
| 184 | |||
| 185 | // Copy over fields [im_taxonomy_vid_1], [tid], [im_vid_1], |
||
| 186 | // [im_vid_Forums], [tm_vid_1_names] |
||
| 187 | if ( ( preg_match('/tid/', $fieldName) OR preg_match('/vid/', $fieldName) ) AND is_array($fieldValue) ) { |
||
| 188 | foreach ($fieldValue as $subkey => $subvalue) { |
||
| 189 | $comment_document->addField($fieldName, $subvalue); |
||
| 190 | } |
||
| 191 | }// if preg_match |
||
| 192 | |||
| 193 | // Set the access keys so this module will work with |
||
| 194 | // apachesolr_access. |
||
| 195 | if (module_exists('apachesolr_access')) { |
||
| 196 | if (preg_match('/^access/', $fieldName) AND is_array($fieldValue)) { |
||
| 197 | foreach ($fieldValue as $subkey => $subvalue) { |
||
| 198 | $comment_document->addField($fieldName, $subvalue); |
||
| 199 | } |
||
| 200 | }// if preg_match |
||
| 201 | }// if module_exist |
||
| 202 | |||
| 203 | }// foreach document |
||
| 204 | |||
| 205 | $comdocs[] = $comment_document; |
||
| 206 | }// while |
||
| 207 | }// if entity_type=node |
||
| 208 | }// documents |
||
| 209 | |||
| 210 | // Merge the comment documents with the input documents array. These |
||
| 211 | // documents will now be added to the Solr search index. |
||
| 212 | $documents = array_merge($documents, $comdocs); |
||
| 213 | } |
||
| 435 |