/[drupal]/contributions/modules/advogato_import/advogato_import.module
ViewVC logotype

Diff of /contributions/modules/advogato_import/advogato_import.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7, Wed Mar 19 02:15:15 2008 UTC revision 1.8, Tue Jul 29 15:02:09 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: advogato_import.module,v 1.6 2006/11/06 21:02:53 deekayen Exp $  // $Id: advogato_import.module,v 1.7 2008/03/19 02:15:15 deekayen Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 13  Line 13 
13   */   */
14    
15  /**  /**
16     * Implementation of hook_menu().
17     *
18     * Displays a tab in users' account area.
19     *
20     * @return array
21     */
22    function advogato_import_menu($may_cache) {
23      $items = array();
24    
25      if (!$may_cache) {
26        $items[] = array(
27          'path' => 'admin/settings/advogato_import',
28          'title' => t('Advogato import'),
29          'description' => t('Set default import locations, settings, and limits.'),
30          'callback' => 'drupal_get_form',
31          'callback arguments' => array('advogato_import_admin_settings'),
32          'access' => user_access('administer site configuration'),
33          'type' => MENU_NORMAL_ITEM,
34        );
35      }
36      if (arg(0) == 'user' && is_numeric(arg(1))) {
37        $items[] = array(
38          'path' => 'user/'. arg(1) .'/advogato_import',
39          'title' => t('Advogato import'),
40          'callback' => 'drupal_get_form',
41          'callback arguments' => array('advogato_import_diary_user'),
42          'type' => MENU_LOCAL_TASK,
43          'access' => variable_get('advogato_import_configured', false),
44          'weight' => 2
45        );
46      }
47    
48      return $items;
49    }
50    
51    /**
52   * Administration variables must be set before users can queue imports   * Administration variables must be set before users can queue imports
53   *   *
54   * @return array   * @return array
55   */   */
56  function advogato_import_admin_settings() {  function advogato_import_admin_settings() {
57      $form = array();
58    $form['advogato_import_cron_limit'] = array(    $form['advogato_import_cron_limit'] = array(
59      '#type' => 'select',      '#type' => 'select',
60      '#title' => t('Import limit'),      '#title' => t('Import limit'),
# Line 91  function advogato_import_admin_settings( Line 128  function advogato_import_admin_settings(
128  }  }
129    
130  /**  /**
131   * Displays a tab in users' account area   * Accepts username, and diary entry numbers to the import queue
  *  
  * @return array  
132   */   */
133  function advogato_import_menu($may_cache) {  function advogato_import_diary_user() {
134    global $user;    global $user;
135    $items = array();    $edit = array(
136        'username' => '',
137    if(variable_get('advogato_import_configured', 0)) {      'startentry' => '',
138      if (!$may_cache) {      'endentry' => ''
139        if (arg(0) == 'user' && is_numeric(arg(1))) {    );
140          $items[] = array('path' => 'user/'. arg(1) .'/advogato_import', 'title' => t('advogato_import'),    $result = db_query('SELECT username, startentry, endentry FROM {advogato_import} WHERE uid = %d', $user->uid);
141            'callback' => 'advogato_import_diary_user', 'type' => MENU_LOCAL_TASK, 'weight' => 2);    while ($record = db_fetch_array($result)) {
142        }      if (!empty($record['username'])) {
143          $edit = $record;
144      }      }
145    }    }
146    $items[] = array(    unset($record, $result);
147      'path' => 'admin/settings/advogato_import',  
148      'title' => t('Advogato import'),    $form = array();
149      'description' => t('Set default import locations, settings, and limits.'),    $form['advogato_import'] = array(
150      'callback' => 'drupal_get_form',      '#type' => 'fieldset',
151      'callback arguments' => 'advogato_import_admin_settings',      '#title' => t('Import Advogato diary'),
152      'access' => user_access('administer site configuration'),      '#description' => t('Queue import of diary entries from Advogato.org. Entries will be added in chunks during cron jobs. Advogato diary entries are numbered starting at 0 (zero).')
153      'type' => MENU_NORMAL_ITEM,    );
154      $form['advogato_import']['username'] = array(
155        '#type' => 'textfield',
156        '#title' => t('Advogato username'),
157        '#default_value' => isset($edit['username']) ? $edit['username'] : '',
158        '#size' => 20,
159        '#maxlength' => 50,
160        '#required' => TRUE,
161        '#weight' => 1
162      );
163      $form['advogato_import']['startentry'] = array(
164        '#type' => 'textfield',
165        '#title' => t('Start entry'),
166        '#default_value' => $edit['startentry'],
167        '#size' => 5,
168        '#maxlength' => 5,
169        '#required' => TRUE,
170        '#weight' => 2
171    );    );
172      $form['advogato_import']['endentry'] = array(
173        '#type' => 'textfield',
174        '#title' => t('End entry'),
175        '#default_value' => $edit['endentry'],
176        '#size' => 5,
177        '#maxlength' => 5,
178        '#required' => TRUE,
179        '#weight' => 3
180      );
181      $form['advogato_import']['queue_import'] = array(
182        '#type' => 'submit',
183        '#value' => t('Queue Import'),
184        '#weight' => 20
185      );
186      return $form;
187    }
188    
189    return $items;  function advogato_import_diary_user_validate($form, &$form_values) {
190      if (!$form_values['username']) {
191        form_set_error('username', t('You must enter a username.'));
192      }
193      if (!isset($form_values['startentry']) || !is_numeric($form_values['startentry'])) {
194        form_set_error('startentry', t('You must enter a starting entry.'));
195      }
196      if (!isset($form_values['endentry']) || !is_numeric($form_values['endentry'])) {
197        form_set_error('endentry', t('You must enter an ending entry.'));
198      }
199  }  }
200    
201  /**  function advogato_import_diary_user_submit($form, &$form_values) {
  * Accepts username, and diary entry numbers to the import queue  
  */  
 function advogato_import_diary_user() {  
202    global $user;    global $user;
203      db_query("INSERT INTO {advogato_import} (uid, username, startentry, endentry) VALUES (%d, '%s', '%s', '%s')", $user->uid, $form_values['username'], $form_values['startentry'], $form_values['endentry']);
204    
205    if ($account = user_load(array('uid' => arg(1), 'status' => 1))) {    drupal_set_message(t('Your diary import request has been queued.'));
     $edit = $_POST['edit'];  
   
     if ($edit) {  
       if (!$edit['username']) {  
         form_set_error('username', t('You must enter a username.'));  
       }  
       if (!isset($edit['startentry']) || !is_numeric($edit['startentry'])) {  
         form_set_error('startentry', t('You must enter a starting entry.'));  
       }  
       if (!isset($edit['endentry']) || !is_numeric($edit['endentry'])) {  
         form_set_error('endentry', t('You must enter an ending entry.'));  
       }  
   
       if (!form_get_errors()) {  
         db_query("INSERT INTO {advogato_import} (uid, username, startentry, endentry) VALUES (%d, '%s', '%s', '%s')", $account->uid, $edit['username'], $edit['startentry'], $edit['endentry']);  
   
         drupal_set_message(t('Your diary import request has been queued.'));  
   
         drupal_goto("user/$account->uid");  
       }  
     }  
   
     $form['advogato_import'] = array(  
       '#type' => 'fieldset',  
       '#title' => t('Import Advogato diary'),  
       '#description' => t('Queue import of diary entries from Advogato.org. Entries will be added in chunks during Drupal cron jobs. Advogato diary entries are numbered starting at 0 (zero).')  
     );  
     $form['advogato_import']['username'] = array(  
       '#type' => 'textfield',  
       '#title' => t('Advogato username'),  
       '#default_value' => isset($edit['username']) ? $edit['username'] : '',  
       '#size' => 20,  
       '#maxlength' => 50,  
       '#required' => true  
     );  
     $form['advogato_import']['startentry'] = array(  
       '#type' => 'textfield',  
       '#title' => t('Start entry'),  
       '#default_value' => $edit['startentry'],  
       '#size' => 5,  
       '#maxlength' => 5,  
       '#required' => true  
     );  
     $form['advogato_import']['endentry'] = array(  
       '#type' => 'textfield',  
       '#title' => t('End entry'),  
       '#default_value' => $edit['endentry'],  
       '#size' => 5,  
       '#maxlength' => 5,  
       '#required' => true  
     );  
     $form['advogato_import']['queue_import'] = array(  
       '#type' => 'submit',  
       '#value' => t('Queue Import')  
     );  
   
     print theme('page', drupal_get_form('advogato_import', $form));  
   }  
   else {  
     drupal_not_found();  
   }  
206    
207      drupal_goto("user/$user->uid");
208  }  }
209    
210  /**  /**
211     * Implementation of hook_cron().
212     *
213   * This function could benefit from a transactional DB   * This function could benefit from a transactional DB
214   */   */
215  function advogato_import_cron() {  function advogato_import_cron() {
216    if(variable_get('advogato_import_configured', 0)) {    if (variable_get('advogato_import_configured', 0)) {
217    
218      $result = db_query('SELECT qid, uid, username, startentry, endentry, ABS(endentry - startentry) AS difference FROM {advogato_import}');      $result = db_query('SELECT qid, uid, username, startentry, endentry, ABS(endentry - startentry) AS difference FROM {advogato_import}');
219      if (db_num_rows($result) > 0) {      if (db_num_rows($result) > 0) {
# Line 210  function advogato_import_cron() { Line 228  function advogato_import_cron() {
228        $node->format   = 3; // Drupal's default for Full HTML; Advogato already did filtering        $node->format   = 3; // Drupal's default for Full HTML; Advogato already did filtering
229        $node->files    = array();        $node->files    = array();
230    
231        while($queue = db_fetch_object($result)) {        while ($queue = db_fetch_object($result)) {
232          $account    = user_load(array('uid' => $queue->uid));          $account    = user_load(array('uid' => $queue->uid));
233          $node->name = $account->name;          $node->name = $account->name;
234          $node->uid  = $queue->uid;          $node->uid  = $queue->uid;
# Line 240  function advogato_import_cron() { Line 258  function advogato_import_cron() {
258    
259            // node_submit() normally calls this            // node_submit() normally calls this
260            watchdog('content', t('%type: added %title.',            watchdog('content', t('%type: added %title.',
261                     array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))),              array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))),
262                     WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));              WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
263    
264            // update queue            // update queue
265            $asc === true ? $queue->startentry++ : $queue->startentry--;            $asc === true ? $queue->startentry++ : $queue->startentry--;
266            db_query('UPDATE {advogato_import} SET startentry = %d WHERE qid = %d', $queue->startentry, $queue->qid);            db_query('UPDATE {advogato_import} SET startentry = %d WHERE qid = %d', $queue->startentry, $queue->qid);
267            $abs--;            $abs--;
268            $loop_limit--;            $loop_limit--;
269          } while(($cron_limit === 0 xor $loop_limit > 0) && $abs >= 0);          } while (($cron_limit === 0 xor $loop_limit > 0) && $abs >= 0);
270    
271          if(($asc === true && $queue->startentry > $queue->endentry) || ($asc === false && $queue->startentry < $queue->endentry)) {          if (($asc === true && $queue->startentry > $queue->endentry) || ($asc === false && $queue->startentry < $queue->endentry)) {
272            db_query('DELETE FROM {advogato_import} WHERE qid = %d', $queue->qid);            db_query('DELETE FROM {advogato_import} WHERE qid = %d', $queue->qid);
273          }          }
274        }        }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.2