By biohabit on
Does anyone have an example php or mysql script that can create everything that is need to make a new node work properly with when using Drupal 4.7? The new vid column is breaking all of my previous methods for direct importing of content. I can get the content out of the various custom apps without too much pain, just getting into drupal is causing problems.
Comments
This should help
Here is a stripped down version of some code I have used in 4.7 for creating a node from code. You will need to modify as noted in the comments
thanks! how about an example of interating over a csv file?
How would you incorporate a script interating over a csv file using PHP?
Ever figure this one out?
Did you ever get the code to import a CSV file into nodes? I need to get mine into flexinodes and I am stumped!
-Todd
http://www.toddalbert.com
node_import?
Have you taken a look at the node_import module? It is CVS and it should work with flexinode. The problem I'm running into is I want to directly insert nodes from scripts that run outside drupal and I've made it a little further but not a complete solution.
node_import is not working with 4.7
I'm not having luck with node_import, although it claims to do exactly what I need. It is not working in 4.7, though.
The reason that I asked is that I figured that if you had this working, I might be able to run a script to insert my nodes. I only have to do this once, but I have over 6,000 nodes to insert from an older database.
Good luck!
-Todd
http://www.toddalbert.com
partial success with CVS node_import on 4.7
Just adding that I have had partial success with node_import using the CVS version with 4.7.2. It's not consistent, but it is working. I am only using it with flexinodes though. 2 out of 3 flexinodes area able to import (so far) but one type that I have just won't import at all through the standard UI. So I have been using the raw format and that has been working... still testing...
I may have to check out this kind of code though... might be more useful to deal with it directly.
For CSV files though, node_import uses http://php.net/fgetcsv. There is quite a bit about CSV and PHP on Google.
HTH
--------------------
Michael
http://michaelangela.name
PHPmyadmin?
I've thought about trying to do this using PHPmyadmin (which can import CSV) to put my data in a table in the database. Then within Drupal I could do an SQL query to get all the rows (or a subset of rows) from that table and run a loop where each iteration adds a node as per above. When done, just drop that table or delete the rows that have already been processed.
---
Work: BioRAFT
How do I assign node to a Category?
Excellent. Thanks for the post. One small question: How do I say that this node belongs to a particular category?
Thanks!
To add category do this
Before the call to node_save($node) add
or if you want to assign multiple categories you can use
I've been trying to get
I've been trying to get nodes to save with taxonomy information, but it isn't working for me. I checked the database to be sure I'm using the correct tid; the node is created fine, but has no taxonomy information.
This seems to almost work (the taxonomy link shows up on the page that previews the node teaser immediately after node creation, but the link is not there when I view the node from anywhere else):
Any advice will be greatfully recieved
- L0rne
- L0rne
are you using the category
are you using the category module or the core taxonomy module?
---
Work: BioRAFT
Oh, sorry, the code I posted
Oh, sorry, the code I posted should have read:
The "$node->category[236]" thing was something I was testing.
To answer your question in a more direct way, I am using the category module.
- L0rne
- L0rne
When editing, taxonomy is just an array of tids
When editing, taxonomy is just an array of tids, so your code would read
Odd! That was the first
Odd! That was the first thing I tried. I tried it again, but it's not working.
The taxonomy bit of the node object looks like this just before node_save:
This is a newly created node object. Is there anything special I need to do to initialize the taxonomy property? What else might I be doing wrong?
Thank you for the input!
- L0rne
- L0rne
Are you setting anything other than taxonomy?
Are you setting anything other than taxonomy? I ask because you show only a partial example, creating a node object with only the taxonomy field will have no effect.
Here's a more complete
Here's a more complete account of what is happening.
I am importing content files using import_html. The stuff I am editing is in import_html.module. I found the place in the code where the node object is created, and added a few things just before node_save:
Everything works fine, except the taxonomy. From what you have told me, and from what I have found elsewhere on this site, this should work. There must be something I am missing. I wonder if my taxonomy value is being over-writen somewhere else?
- L0rne
- L0rne
Is type set and does it support the vocabulary?
$node->type needs to be set to the type of content you are creating and the vocabulary for term 236 needs to include that type.
The node type is being set
The node type is being set to "page", and this type has been added to the types for the vocabulary. I am able to add other pages to this term, using the usual interface.
Does it matter that I am using the Category module?
- L0rne
- L0rne
It is possible it matters that you are using the Category module
It is possible it matters that you are using the Category module since $node->taxonomy[] applies to the taxonomy module. So if you are talking about values for the Category module this will not work, that would be different code (what it would look like I do not know).
Having the Category module
Having the Category module active does seem to be my problem. Here is my work-around:
It seems that the taxonomy terms are brought over into Category when Category is activated, possibly thanks to category_legacy.
This worked for me because I'm doing one-time import stuff; I am certainly interested in a nice way to work with the Category module directly.
- L0rne
I just revisited this for
I just revisited this for another project. I'm not sure if it was the same last time, but my problem this time was solved by ensuring that my document type was allowed in the vocabulary (container) that I was using. The taxonomy is now being added to the node with the Category module active.
nevets asked me if i had this set properly more than a year ago...
- L0rne
- L0rne
What about if I want to add category from category.module?
This looks like it will add taxonomy from the vocabulary table:
But, suppose I want to add category ids from the category table (from the category module)?
importing into CCK?
Does anyone have an example of how to import fields into CCK created tables?
Importing nodes to CCK
This is how I've imported nodes when using CCK-generated node types with bodies and teasers:
Here's an example for CCK'd nodes with Organic Groups
Here's a snippet of how to do bulk import of nodes with a command line script.
A 'sig' is a special interest group node type that I created with CCK for use as the group home page in Organic Groups.
I put this script in the same directory as the drupal installation, so all the includes, etc. would work.
HTH!
Rich Yumul
www.sagetree.com
Do you know how to add a node to a group using node_save
Your example shows how to add a group node using node_save. But, do you know how to add a node to a group using node_save? That is, if I'm creating a "page" node, and I wish to add it to Group A using node_save, how do I code that? The following won't work:
Any ideas? Thanks!
newbie...
Should I create a module to place and run this code?
Thanks and sorry for my poor English.
I imported all the nodes by
I imported all the nodes by just making the code into a normal drupal page and just selecting PHP as the input format.
drupal 6 rss feed 2 node
exemple :
imprter : mysql 2 drupal node+ syndication 2 node+ blogsearchxml
here an exemple to add bloggersearch
put this in a news block will auto updated
*bump* for subscription
*bump* for subscription
Bump2
bump2 for subscription
My completed and working script (with taxonomy)
Hi all,
I made use of the information on this page and on others. I wanted to give back my completed script in case it helps others. This work for me on Drupal 5 importing hundreds of records.
---
Jeff Vogelsang
Senior Associate (Technical Architect, Developer)
Perrin Quarles Associates
Thanks for that script!!
Thanks for that script!! it's very usefull but i've got problem when i need to insert screenshot and attachment.
do you have any idea how to do it? the cck fieldimage handle the screenshot and upload module for attachment.
Regards,
File attachments
Not sure if this helps all the way, but this is how I've added files to nodes when mass importing stuff - this is for Drupal 4.7.
This is AFTER node_save() in the code.
thanks!! it works!
thanks!!
it works!
//$files-array contains filenames and possible descriptions
Please provide a sample $files array as described.
----
Darly
sample array
Sample Files
@ Jery, great thanks! following may work alternatively.
----
Darly
I think this is a better solution
I am sorry if it is in wrong context . I am asuming this is a normal array of files .
I would suggest the use of foreach because as the contents of the array increases
there might be a problem .
Hope this helps .
JUNED KAZI
http://junedkazi.blogspot.com/
question for end of snippet
thanks for providing the code!
why do you both submit and save the node at the end?
the previous examples only save the node.. just curious!!
thanks
fyi, this is a repost positioned incorrectly at bottom of thread
Can't import select lists
Using the above script, I have managed to create new nodes of a CCK type I have made but some fields do not seem to get set correctly. I note that all simple "text field" are fine but nodes using a "select list" type do not work. Can someone please point me in the right direction? Aslo note that these two select lists fields that don't work are set to be "required". Maybe I need to set other parameters for these fields?
What I am currently using for these fields is:
Thanks. The script that Jeff submitted is a great help.
Value validation issue?
It seems that the value I'm trying to set is validated against the allowed list of values for that select list element. For some reason, the string I use does not match those in the enumeration.
Any ideas?
I have been looking for a
I have been looking for a script that can set complete set of values for a node through script.
Thank you very much.
Issues with assigning uid when using drupal_execute() to insert
I am currently attempting to create a node in a php file that looks something like this:
This works fine but I have read here: http://www.civicactions.com/blog/cck_import_and_update that one should be using drupal_execute() to insert a node, and in this instance I need to be able to tell if the form submitted properly, and using drupal_execute seems to be the only way to do so.
So I in trying code as seen at the link above, whenever I set the uid of the node and use drupal_execute I get a new node, from an anonymous user, not the specified uid.
An example of that:
Again this works, but I get an anonymous user, unelss I use
user_authenticate('user', 'pass);for the user I'd like the node assigned to (still ignores assigning the uid) which in this case won't work for my application. Is there a method to assign the uid using drupal_execute()?assigning uid on programatically created nodes
was having a similar problem. seems counter intuitive, but setting
$newnode['name'] = $user->name;rather than$newnode['uid'] = $user->uid;seems to work.i'm doing something similar to the following:
use that as an example at your own risk, but it seems to be working for me. perhaps someone else knows of a better way?
Counter intuitive but nothing mysterious
There's a post in API documentation explaining this behaviour.
Thanks Jeff for the great
Thanks Jeff for the great script. I got it working and importing almost out of the box from what you had. However, when I started adding more and more fields and in a way piece mealing the process trying to debug it as I went, I came and hit a wall with the checkboxes in my node. I have devel and that outputs value=>1 which is in my database, however, when I try and import 1 from my csv it doesn't do it and actually puts in a 0 as if it hasn't been checked. Anyone know how to check a checkbox via php? Any help would be fantastic. This one thing is the only thing holding me up. Again, I would be half as far if it weren't for this script. Thanks.
Seth
Quick Question for you, Jeff
First off, thanks very much for your help here. It's saved me loads of time... or at least it's going to once I figure this last bit out.
You are using $node['created'] = $line[4]; to pass the created time of your content in the csv file over to Drupal's. What format is your "$line[4]" coming over in? After looking at the database (I'm using phpmyadmin) it looks like the created time for drupal is being stored in Unix format. Unfortunately, the date format in my csv file is not Unix. Howerver, using the php function - strtotime() - I'm converting it to Unix succesfully. Unfortunately, when I make the import into Drupal, The created time isn't being passed coreectly. It seems that it's being overwritten.
So just to recap the problem - I have content created in the past on my old site, and I'm trying to import that into Drupal, retaining the original date it was created. When I pass that date over through the csv file, it is being over written with the current date. Below is the php code I am using in place of your '$node['created'] = line[4]; Thanks for any suggestions you might have.
Code I'm using:
strtotime() defaults to
strtotime() defaults to "now".
Try seeing what the value of $line[10] is and breaking it up into parts to pass through checkdate()? And make sure there's a $ in front of your variable name in the actual code.
Thanks for the reply
Thanks for your help so far.
The value of $line[10] is "2002-03-03 01:19:33". The value of - strtotime($line[10]) is "1015139973". I checked that in a Unix Time Calculator and it is spitting out the correct date. In my import script, I put an echo statement on $node['created'] to see what it is being stored as, and it prints "1015139973" as well. So the variable is being set to the correct Unix timecode (which is the format that drupal's database stores the content's created date), but when it is put into the database itself, it is putting the Unix Timecode for the current day (April 3rd, 2009)!
I'm kind of at a loss. I thought maybe it was over-writting the created date with the revision date, so I commented that part of the code as well.... to no avail. Any ideas? Thanks in advance.
-jweedman
That's really strange. I have
That's really strange. I have the following code for an old wordpress importer, which doesn't need strtotime, but it should set it a similar way. And it is the created field not the changed field that's being displayed as now() right?
After you use node_save(), what's the output of $node->created (probably now, but i'm wondering).
For reference, but not relevant...
Wow... found the culprit
For anybody trying to import content, and can't figure out how to retain the original date (like me), let me save you days of frustration....
As you can see in Jeff's original post, his code both submits the node, and then saves it --- with node_submit() and node_save(). Well, I had a sneaking suspicion that $node['created'] was being overwritten or swapped out somewhere in one of these functions. So, "mradcliffe", your suspicions were right as well. If you look in the node_submit() function (list of functions here),
Bascially, if $node->date is NOT empty, make $node->created = $node->date. If it IS empty, set $node->create to time(), which uses now() by default!
So even though I was assigning $node->created, it was being overwritten because I was NOT assigning $node->date to anything.
So all in all instead of setting $node->created = $line[10] like I did, you just have to set $node->date = $line[10]. Frustrating... but glad it's fixed. Thanks 'mradcliffe' for your help. Hope this saves someone some time!
-jweedman
How to Insert into node_example table?
Excellent thread, suggested comments on inserting from CSV into node_example table as described?
----
Darly
quick question jeff
oops! this was meant to be threaded above to post: http://drupal.org/node/67887#comment-209250
i'll repost there for context
thanks for providing the code!
why do you both submit and save the node at the end?
the previous examples only save the node.. just curious!!
thanks
importing html book pages/files into Drupal
importing html book pages/files into Drupal
tough situation for me here..looking for this feature :) dunno if this is the right place to ask this..but I am using Drupal 5.1 and need this feature as i have several books in html and need to put it on my drupal site..and would like some automation there..so i can set rules for the page title or menu title etc..is something available ?
Help guys
Me2
Just seconding this request, I'm needing to import literally hundreds of html items into book pages. Used to be able to do it fairly easily with the book import module.
Some perhaps helpful tips
I'd like to contribute back some tips I got while discussing with jeff (jjv).
1) The select list CCK item seems to do some string validation based on the valid list you gave. The way to import in those fields is to temporarily turn these fields as plain text fields AND remove any valid entries for that field. As Jeff said:
2) If you are unsure about the format of a given custom node you made using CCK, you can use the "devel" module to output the node structure. As Jeff puts it:
Hope this helps some of you trying to import from external databases.
Regards
Creating nodes script
I have the script working well except that it is not writing the uid specified in the CSV. The uid is staying 0 (default). All of the other fields are updating. Any ideas?
Thanks!
Mitch
Resolved the creating nodes script issue
I resolved this with some help from various folks on IRC.
The existing function node_submit code was not recognizing that I was logged on as a user with administrator rights, It's not clear whether this problem happened because I was calling the function via PHP code or something relating to my implementation of Drupal 5.x.
Anyway, I created a new function node_submit_new that commented out the part of function node_submit that was causing me trouble. I only left the single line (i.e., did not comment it out) that replaced the uid. The parts that checked whether I was logged on as a user with correct permissions had to be commented out - at least for now (see below... just that part of the code).
Best regards,
Mitch
// $access = user_access('administer nodes');
// if ($access) {
// // Populate the "authored by" field.
// if ($account = user_load(array('name' => $node->name))) {
// $node->uid = $account->uid;
$node->uid =$user->uid;
// }
// else {
// $node->uid = 0;
// }
importing books and static content to Drupal site
can someone suggest a module for this.. I couldnt follow this discussion on how to make scripts..i am a newbie..and am comfortable with modules..pls help.
have you checked import_html?
It is designed to import static sites into Drupal using XSL filtering. See http://drupal.org/project/import_html
about this script fun-time ness, importing and things
hi there gurukripa.. ;)
umm, so i am also a noob. well, yeah. thats the easiest way to describe it.
and no, in 5.x there isnt a functioning way to get most anything into drupal via the cms interface. maybe sql queries, no auto-magic modules have been switched over that i can see.
there were a few in 4.7x if that is what you are working with your saved. the modules i recall are/were- node import. the xml api, user and book imports.. a couple static html page imports (wgHTML appears to work in 5.x!!)
Who knows maybe these might work if youre not on 5 yet..
however nothing for custom data into the current drupal environment. :(
this forum thread looks to be the best link ive found so far.
------------------
anyways, what fromw i am gathering from the oblique comments about only "previewing" the changes and not actually submitting them, is that this code / script is to be executed as part of the normal "page" editing process. answering the desperately flocks with simple help on how to implement said script doesnt seem to be forthcoming. so....
when you create or edit a page. there is a content type box mid way down, filtered html, full html, or "gasp"; php!! youll see it.
i am guessing thats the magic with these "script" things :)
simply copy and paste the code from here into a new page.
change the stuff as indicated by the helpful internal documentation (inside the code),
and then execute it by "previewing it" in drupal (the code will still get called, executed, etc..
everybody seems to be gung-ho about it working and all that, so .... give it a try??? ;)
pay heed to the bit about leavin the execute option (or whatever the variable is called)
to false until youre ready. terrible things will happen if it gets ran twice. run it on a test database, yadda yadda.
editing this mystical script? dont worry. its normal plain old php source. - thats something youre gonna have to learn a little bit about if you dont already know the basics of syntax and stuff. for more info search for drupal and hell on here (drupal.org)
who needs no stinking handbooks! ;)
or, perhaps i am completely full of shi_ite.
umm, so i need to import a bunch of data too..
and it doesnt make sense to me to hang this code in a php file by itself.
and how could you preview your changes that way! ;)
we will see.
maybe some hekpful non-n00b would like to clear this up?
or maybe point us among the unenlightened to a link about including scripts
in pages/custom blocks whatever??
anyways.
back to drupal fun 4 me
xox
c
www.chastyti.com
how to create comments?
Hi guys,
how do you do achieve the same for comments (directly create a comment for a node, given its nid, not via the web interface, e.g. when the xml-rpc interface of the module is being called)? I saw that there is comment_save but this doesnt seem to call any hooks...
node path
I am trying to insert path for the node but I anm not able to so.
This is what I have tried.
I have also tried it with
But it is not working.
Pls Help
JUNED KAZI
junedkazi@gmail.com
Have you tried, $node->path
Have you tried,
instead... you have used array notation whereas my example is class->field notation... that's off the top of my head after a Double-Sausage and Egg breakfast so the E-numbers might be affecting me.
:)
Sean Charles
Great topic
I spent the last few day importing data from an old (and terribly designed) access database into drupal. This thread helped me a great deal, fist I went with a more direct aproach inserting into the database through the drupal abstractation layer. This however was a bit messy, and you have to rewrite a lot of stuff that is essensially already in drupal, plus no modules gets a chance to hook in. Emulating form submission using the api really feels like the way to go (with nodes anyway). I made a script inspired by some in this thread:
(It's pretty specific for my needs, and not very pretty, but since those posted in this thread helped increase my understanding the process of submitting a node maybe this can be useful for others)
Some pitfalls:
Found out that if node->date is not set the following code in node_submit will result in the 'created' property losing its value:
Import functions
missing import_functions.php
----
Darly
The script I posted was
The script I posted was pretty specific for that particular import, as is the functions in "import_functions.php". Had to import data from 4 different tables (represented as 4 different node types in drupal) and thanks to thanks to the unbelievebly messy database they came from I had to make all kind of hacks for each import. Later found out about the node import module, but I could not have used it anyway as the data had to be sanitised while importing. It would be easier to write a nice looking script if one had nice looking data to start with, but I really should try to write a more generic script that also can handle messy imports. I posted it, quick and dirty as it was, mosty so that people could copy some relevant parts, even though most of whats in my scripts can be found in others in this thread.
Hi, This is a great script
Hi,
This is a great script for batch-importing data from another database.
Anyone made sort of script for real time importing nodes from another database?
May be through rss or something like that?
I think of rssfeed on the "foreign" database to "post" the data
feedapi rss import to fetch the data into Drupal?
Thanks in advance for thinking about this!
Greetings,
Martijn
With an image field
thanks for this script
my script to import images in an image cck field :
figureslibres.cc
sorry,
i can't delete this comment (this not previous)
figureslibres.cc
This is a similar code i used, might help
Ali Hammad Raza
WordsValley
Hi, I am looking for a
Hi,
I am looking for a solution to output many nodes related to the input of a matrix of fields (drupal 5).
Like a letter-template and a database with addresses. So every letter-instance can be personalized.
I would like to use a cck-multireference field with many instances and output them to as many nodes as instances there are.
Did anybody allready do this?
Will pieces of code stated here help?
Thanks a lot in advance for your reply!
greetings,
Martijn
Where do I run it from?
I saw some wonderful examples of how to create nodes. But I don't think that I read it any where I should run this code from? What directory? Because I tried running it from home directory and this is what i got:
PHP Fatal error: Call to undefined function node_submit() in whicn.h is basically complaining about the node_save function
You can run this code from
You can run this code from within any node be it a page or article. But you need to set the filter to allow php values for execution. By executing it from within a drupal node you have access to drupal api because the bootstrap loads all the basic drupal functions.
You can also create a module to run this code but for a beginner I believe it can be slightly complex.
Ali Hammad Raza
WordsValley.org
Where do I run it from?
saw some wonderful examples of how to create nodes. But I don't think that I read it any where I should run this code from? What directory? Because I tried running it from home directory and this is what i got:
PHP Fatal error: Call to undefined function node_submit() in whicn.h is basically complaining about the node_save function
you will have to create a custom module to run this code
jatbot ,
you will have to run this code from a custom module .
The tutorial of how to create a module is located here
http://drupal.org/node/508 .
Pld do let me know if this helps .
JUNED KAZI
http://junedkazi.blogspot.com/
or create a node
You can also create a node with PHP input format and contain your code there. Just be sure you don't refresh the page again, since it will execute that code on every reload.
User Data Column
Hi I'm using the trying to enter in user data (which I have linked to a node) from another Joomla database to Drupal 6.
The user_save function can handle this, the problem I have is the serialized 'data' column. I'm unsure what it is needed for and what I should put in there. It seems to be a serialized array for the new node but there is extra info in there like '_error_element'. Is there a way of programmatically running a form (that a user would enter on the front end), this would make any migration much easier as we have already developed the form and its validation, why can't be just send data to the form?
If not can someone tell me what the 'data' column is used for?
Ronan
Strike that
Check it out here: http://drupal.org/node/178506#comment-895418