Initial commit

This commit is contained in:
Mauricio Dinarte 2024-12-04 10:11:27 -06:00
commit c5e731d8ae
2773 changed files with 600767 additions and 0 deletions

View file

@ -0,0 +1,11 @@
name = Blog
description = Enables multi-user blogs.
package = Core
version = VERSION
core = 7.x
files[] = blog.test
; Information added by Drupal.org packaging script on 2024-03-06
version = "7.100"
project = "drupal"
datestamp = "1709734591"

View file

@ -0,0 +1,23 @@
<?php
/**
* @file
* Install, update and uninstall functions for the blog module.
*/
/**
* Implements hook_install().
*/
function blog_install() {
// Ensure the blog node type is available.
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['blog']);
}
/**
* Implements hook_uninstall().
*/
function blog_uninstall() {
variable_del('blog_block_count');
}

View file

@ -0,0 +1,272 @@
<?php
/**
* @file
* Enables multi-user blogs.
*/
/**
* Implements hook_node_info().
*/
function blog_node_info() {
return array(
'blog' => array(
'name' => t('Blog entry'),
'base' => 'blog',
'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
)
);
}
/**
* Implements hook_user_view().
*/
function blog_user_view($account) {
if (user_access('create blog content', $account)) {
$account->content['summary']['blog'] = array(
'#type' => 'user_profile_item',
'#title' => t('Blog'),
// l() escapes the attributes, so we should not escape !username here.
'#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
'#attributes' => array('class' => array('blog')),
);
}
}
/**
* Implements hook_help().
*/
function blog_help($path, $arg) {
switch ($path) {
case 'admin/help#blog':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("The Blog module allows registered users to maintain an online journal, or <em>blog</em>. Blogs are made up of individual <em>blog entries</em>. By default, the blog entries are displayed by creation time in descending order, with comments enabled, and are promoted to the site's front page. For more information, see the online handbook entry for <a href='@blog'>Blog module</a>.", array('@blog' => 'http://drupal.org/documentation/modules/blog/')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Single-user blogs') . '</dt>';
$output .= '<dd>' . t("Each user's blog entries are automatically displayed with a link to the user's main blog page. You can create as many single-user blogs as you have site users with permission to create blog content.") . '</dd>';
$output .= '<dt>' . t('Multi-user blogs') . '</dt>';
$output .= '<dd>' . t("Blog entries from each single-user blog are also aggregated into one central multi-user blog, which displays the blog content of all users in a single listing.") . '</dd>';
$output .= '<dt>' . t('Navigation') . '</dt>';
$output .= '<dd>' . t("There is an optional <em>Blogs</em> menu item added to the Navigation menu, which displays all blogs available on your site, and a <em>My blog</em> item displaying the current user's blog entries.") . '</dd>';
$output .= '<dt>' . t('Blocks') . '</dt>';
$output .= '<dd>' . t('The Blog module also creates a default <em>Recent blog posts</em> block that may be enabled at the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_form().
*/
function blog_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Implements hook_view().
*/
function blog_view($node, $view_mode) {
if ($view_mode == 'full' && node_is_page($node)) {
// Breadcrumb navigation. l() escapes title, so we should not escape !name.
drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'), l(t("!name's blog", array('!name' => format_username($node))), 'blog/' . $node->uid)));
}
return $node;
}
/**
* Implements hook_node_view().
*/
function blog_node_view($node, $view_mode) {
if ($view_mode != 'rss') {
if ($node->type == 'blog' && (arg(0) != 'blog' || arg(1) != $node->uid)) {
// This goes to l(), which escapes !username in both title and attributes.
$links['blog_usernames_blog'] = array(
'title' => t("!username's blog", array('!username' => format_username($node))),
'href' => "blog/$node->uid",
'attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($node)))),
);
$node->content['links']['blog'] = array(
'#theme' => 'links__node__blog',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
}
}
/**
* Implements hook_menu().
*/
function blog_menu() {
$items['blog'] = array(
'title' => 'Blogs',
'page callback' => 'blog_page_last',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
'file' => 'blog.pages.inc',
);
$items['blog/%user_uid_optional'] = array(
'title' => 'My blog',
'page callback' => 'blog_page_user',
'page arguments' => array(1),
'access callback' => 'blog_page_user_access',
'access arguments' => array(1),
'file' => 'blog.pages.inc',
);
$items['blog/%user/feed'] = array(
'title' => 'Blogs',
'page callback' => 'blog_feed_user',
'page arguments' => array(1),
'access callback' => 'blog_page_user_access',
'access arguments' => array(1),
'type' => MENU_CALLBACK,
'file' => 'blog.pages.inc',
);
$items['blog/feed'] = array(
'title' => 'Blogs',
'page callback' => 'blog_feed_last',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'blog.pages.inc',
);
return $items;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function blog_menu_local_tasks_alter(&$data, $router_item, $root_path) {
global $user;
// Add action link to 'node/add/blog' on 'blog' page.
if ($root_path == 'blog') {
$item = menu_get_item('node/add/blog');
if ($item['access']) {
$item['title'] = t('Create new blog entry');
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
// Provide a helper action link to the author on the 'blog/%' page.
elseif ($root_path == 'blog/%' && isset($router_item['page_arguments'][0]->uid) && $router_item['page_arguments'][0]->uid == $user->uid) {
$data['actions']['output']['blog'] = array(
'#theme' => 'menu_local_action',
);
if (user_access('create blog content')) {
$data['actions']['output']['blog']['#link']['title'] = t('Post new blog entry.');
$data['actions']['output']['blog']['#link']['href'] = 'node/add/blog';
}
else {
$data['actions']['output']['blog']['#link']['title'] = t('You are not allowed to post a new blog entry.');
}
}
}
/**
* Access callback for user blog pages.
*/
function blog_page_user_access($account) {
// The visitor must be able to access the site's content.
// For a blog to 'exist' the user must either be able to
// create new blog entries, or it must have existing posts.
return $account->uid && user_access('access content') && (user_access('create blog content', $account) || _blog_post_exists($account));
}
/**
* Helper function to determine if a user has blog posts already.
*/
function _blog_post_exists($account) {
return (bool)db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', 'blog')
->condition('uid', $account->uid)
->condition('status', 1)
->range(0, 1)
->addTag('node_access')
->execute()
->fetchField();
}
/**
* Implements hook_block_info().
*/
function blog_block_info() {
$block['recent']['info'] = t('Recent blog posts');
$block['recent']['properties']['administrative'] = TRUE;
return $block;
}
/**
* Implements hook_block_configure().
*/
function blog_block_configure($delta = '') {
if ($delta == 'recent') {
$form['blog_block_count'] = array(
'#type' => 'select',
'#title' => t('Number of recent blog posts to display'),
'#default_value' => variable_get('blog_block_count', 10),
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
);
return $form;
}
}
/**
* Implements hook_block_save().
*/
function blog_block_save($delta = '', $edit = array()) {
if ($delta == 'recent') {
variable_set('blog_block_count', $edit['blog_block_count']);
}
}
/**
* Implements hook_block_view().
*
* Displays the most recent 10 blog titles.
*/
function blog_block_view($delta = '') {
global $user;
if (user_access('access content')) {
$result = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created'))
->condition('type', 'blog')
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0, variable_get('blog_block_count', 10))
->addTag('node_access')
->execute();
if ($node_title_list = node_title_list($result)) {
$block['subject'] = t('Recent blog posts');
$block['content']['blog_list'] = $node_title_list;
$block['content']['blog_more'] = array(
'#theme' => 'more_link',
'#url' => 'blog',
'#title' => t('Read the latest blog entries.'),
);
return $block;
}
}
}
/**
* Implements hook_rdf_mapping().
*/
function blog_rdf_mapping() {
return array(
array(
'type' => 'node',
'bundle' => 'blog',
'mapping' => array(
'rdftype' => array('sioc:Post', 'sioct:BlogPost'),
),
),
);
}

View file

@ -0,0 +1,127 @@
<?php
/**
* @file
* Page callback file for the blog module.
*/
/**
* Menu callback; displays a Drupal page containing recent blog entries of a given user.
*/
function blog_page_user($account) {
global $user;
drupal_set_title($title = t("@name's blog", array('@name' => format_username($account))), PASS_THROUGH);
$build = array();
$query = db_select('node', 'n')->extend('PagerDefault');
$nids = $query
->fields('n', array('nid', 'sticky', 'created'))
->condition('type', 'blog')
->condition('uid', $account->uid)
->condition('status', 1)
->orderBy('sticky', 'DESC')
->orderBy('created', 'DESC')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access')
->execute()
->fetchCol();
if (!empty($nids)) {
$nodes = node_load_multiple($nids);
$build += node_view_multiple($nodes);
$build['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
}
else {
if ($account->uid == $user->uid) {
drupal_set_message(t('You have not created any blog entries.'));
}
else {
drupal_set_message(t('!author has not created any blog entries.', array('!author' => theme('username', array('account' => $account)))));
}
}
drupal_add_feed('blog/' . $account->uid . '/feed', t('RSS - !title', array('!title' => $title)));
return $build;
}
/**
* Menu callback; displays a Drupal page containing recent blog entries of all users.
*/
function blog_page_last() {
global $user;
$build = array();
$query = db_select('node', 'n')->extend('PagerDefault');
$nids = $query
->fields('n', array('nid', 'sticky', 'created'))
->condition('type', 'blog')
->condition('status', 1)
->orderBy('sticky', 'DESC')
->orderBy('created', 'DESC')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access')
->execute()
->fetchCol();
if (!empty($nids)) {
$nodes = node_load_multiple($nids);
$build += node_view_multiple($nodes);
$build['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
}
else {
drupal_set_message(t('No blog entries have been created.'));
}
drupal_add_feed('blog/feed', t('RSS - blogs'));
return $build;
}
/**
* Menu callback; displays an RSS feed containing recent blog entries of a given user.
*/
function blog_feed_user($account) {
$nids = db_select('node', 'n')
->fields('n', array('nid', 'created'))
->condition('type', 'blog')
->condition('uid', $account->uid)
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0, variable_get('feed_default_items', 10))
->addTag('node_access')
->execute()
->fetchCol();
$channel['title'] = t("!name's blog", array('!name' => format_username($account)));
$channel['link'] = url('blog/' . $account->uid, array('absolute' => TRUE));
node_feed($nids, $channel);
}
/**
* Menu callback; displays an RSS feed containing recent blog entries of all users.
*/
function blog_feed_last() {
$nids = db_select('node', 'n')
->fields('n', array('nid', 'created'))
->condition('type', 'blog')
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0, variable_get('feed_default_items', 10))
->addTag('node_access')
->execute()
->fetchCol();
$channel['title'] = t('!site_name blogs', array('!site_name' => variable_get('site_name', 'Drupal')));
$channel['link'] = url('blog', array('absolute' => TRUE));
node_feed($nids, $channel);
}

View file

@ -0,0 +1,213 @@
<?php
/**
* @file
* Tests for blog.module.
*/
class BlogTestCase extends DrupalWebTestCase {
protected $big_user;
protected $own_user;
protected $any_user;
public static function getInfo() {
return array(
'name' => 'Blog functionality',
'description' => 'Create, view, edit, delete, and change blog entries and verify its consistency in the database.',
'group' => 'Blog',
);
}
/**
* Enable modules and create users with specific permissions.
*/
function setUp() {
parent::setUp('blog');
// Create users.
$this->big_user = $this->drupalCreateUser(array('administer blocks'));
$this->own_user = $this->drupalCreateUser(array('create blog content', 'edit own blog content', 'delete own blog content'));
$this->any_user = $this->drupalCreateUser(array('create blog content', 'edit any blog content', 'delete any blog content', 'access administration pages'));
}
/**
* Confirm that the "You are not allowed to post a new blog entry." message
* shows up if a user submitted blog entries, has been denied that
* permission, and goes to the blog page.
*/
function testUnprivilegedUser() {
// Create a blog node for a user with no blog permissions.
$this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->big_user->uid));
$this->drupalLogin($this->big_user);
$this->drupalGet('blog/' . $this->big_user->uid);
$this->assertResponse(200);
$this->assertTitle(t("@name's blog", array('@name' => format_username($this->big_user))) . ' | Drupal', 'Blog title was displayed');
$this->assertText(t('You are not allowed to post a new blog entry.'), 'No new entries can be posted without the right permission');
}
/**
* View the blog of a user with no blog entries as another user.
*/
function testBlogPageNoEntries() {
$this->drupalLogin($this->big_user);
$this->drupalGet('blog/' . $this->own_user->uid);
$this->assertResponse(200);
$this->assertTitle(t("@name's blog", array('@name' => format_username($this->own_user))) . ' | Drupal', 'Blog title was displayed');
$this->assertText(t('@author has not created any blog entries.', array('@author' => format_username($this->own_user))), 'Users blog displayed with no entries');
}
/**
* Login users, create blog nodes, and test blog functionality through the admin and user interfaces.
*/
function testBlog() {
// Login the admin user.
$this->drupalLogin($this->big_user);
// Enable the recent blog block.
$edit = array();
$edit['blocks[blog_recent][region]'] = 'sidebar_second';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->assertResponse(200);
// Verify ability to change number of recent blog posts in block.
$edit = array();
$edit['blog_block_count'] = 5;
$this->drupalPost('admin/structure/block/manage/blog/recent/configure', $edit, t('Save block'));
$this->assertEqual(variable_get('blog_block_count', 10), 5, 'Number of recent blog posts changed.');
// Do basic tests for each user.
$this->doBasicTests($this->any_user, TRUE);
$this->doBasicTests($this->own_user, FALSE);
// Create another blog node for the any blog user.
$node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->any_user->uid));
// Verify the own blog user only has access to the blog view node.
$this->verifyBlogs($this->any_user, $node, FALSE, 403);
// Create another blog node for the own blog user.
$node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->own_user->uid));
// Login the any blog user.
$this->drupalLogin($this->any_user);
// Verify the any blog user has access to all the blog nodes.
$this->verifyBlogs($this->own_user, $node, TRUE);
}
/**
* Run basic tests on the indicated user.
*
* @param object $user
* The logged in user.
* @param boolean $admin
* User has 'access administration pages' privilege.
*/
private function doBasicTests($user, $admin) {
// Login the user.
$this->drupalLogin($user);
// Create blog node.
$node = $this->drupalCreateNode(array('type' => 'blog'));
// Verify the user has access to all the blog nodes.
$this->verifyBlogs($user, $node, $admin);
// Create one more node to test the blog page with more than one node
$this->drupalCreateNode(array('type' => 'blog', 'uid' => $user->uid));
// Verify the blog links are displayed.
$this->verifyBlogLinks($user);
}
/**
* Verify the logged in user has the desired access to the various blog nodes.
*
* @param object $node_user
* The user who creates the node.
* @param object $node
* A node object.
* @param boolean $admin
* User has 'access administration pages' privilege.
* @param integer $response
* HTTP response code.
*/
private function verifyBlogs($node_user, $node, $admin, $response = 200) {
$response2 = ($admin) ? 200 : 403;
// View blog help node.
$this->drupalGet('admin/help/blog');
$this->assertResponse($response2);
if ($response2 == 200) {
$this->assertTitle(t('Blog | Drupal'), 'Blog help node was displayed');
$this->assertText(t('Blog'), 'Blog help node was displayed');
}
// Verify the blog block was displayed.
$this->drupalGet('');
$this->assertResponse(200);
$this->assertText(t('Recent blog posts'), 'Blog block was displayed');
// View blog node.
$this->drupalGet('node/' . $node->nid);
$this->assertResponse(200);
$this->assertTitle($node->title . ' | Drupal', 'Blog node was displayed');
$breadcrumb = array(
l(t('Home'), NULL),
l(t('Blogs'), 'blog'),
l(t("!name's blog", array('!name' => format_username($node_user))), 'blog/' . $node_user->uid),
);
$this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), 'Breadcrumbs were displayed');
// View blog edit node.
$this->drupalGet('node/' . $node->nid . '/edit');
$this->assertResponse($response);
if ($response == 200) {
$this->assertTitle('Edit Blog entry ' . $node->title . ' | Drupal', 'Blog edit node was displayed');
}
if ($response == 200) {
// Edit blog node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = 'node/' . $node->nid;
$edit["body[$langcode][0][value]"] = $this->randomName(256);
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertRaw(t('Blog entry %title has been updated.', array('%title' => $edit["title"])), 'Blog node was edited');
// Delete blog node.
$this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
$this->assertResponse($response);
$this->assertRaw(t('Blog entry %title has been deleted.', array('%title' => $edit["title"])), 'Blog node was deleted');
}
}
/**
* Verify the blog links are displayed to the logged in user.
*
* @param object $user
* The logged in user.
*/
private function verifyBlogLinks($user) {
// Confirm blog entries link exists on the user page.
$this->drupalGet('user/' . $user->uid);
$this->assertResponse(200);
$this->assertText(t('View recent blog entries'), 'View recent blog entries link was displayed');
// Confirm the recent blog entries link goes to the user's blog page.
$this->clickLink('View recent blog entries');
$this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), 'View recent blog entries link target was correct');
// Confirm a blog page was displayed.
$this->drupalGet('blog');
$this->assertResponse(200);
$this->assertTitle('Blogs | Drupal', 'Blog page was displayed');
$this->assertText(t('Home'), 'Breadcrumbs were displayed');
$this->assertLink(t('Create new blog entry'));
// Confirm a blog page was displayed per user.
$this->drupalGet('blog/' . $user->uid);
$this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), 'User blog node was displayed');
// Confirm a blog feed was displayed.
$this->drupalGet('blog/feed');
$this->assertTitle(t('Drupal blogs'), 'Blog feed was displayed');
// Confirm a blog feed was displayed per user.
$this->drupalGet('blog/' . $user->uid . '/feed');
$this->assertTitle(t("@name's blog", array('@name' => format_username($user))), 'User blog feed was displayed');
}
}