【问题标题】:Mediawiki php: how to get the name of user who uploaded a file?Mediawiki php:如何获取上传文件的用户名?
【发布时间】:2017-01-12 22:45:55
【问题描述】:

我正在更新一个 MediaWiki 扩展程序,该扩展程序显示一个类别 (CategoryGallery) 中的所有图像。

我想显示上传图片的用户的姓名,然后可能按用户过滤。

部分代码是这样的:

// Capitalize the first letter in the category argument, convert spaces to _
$params['cat'] = str_replace ( ' ', '_', ucfirst( $params['cat'] ) );

// Retrieve category members from database
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select( 'categorylinks', 'cl_from',
array ('cl_to' => $params['cat'],
                           'cl_type' => 'file'));
$ids = array();

foreach ( $res as $row ) {
    $ids[] = $row->cl_from;
}

// Create the gallery
$titles = Title::newFromIDs ( $ids );
$text = '';

foreach ( $titles as $title ) {
    $titlePrefixedDBKey = $title->getPrefixedDBKey();
    $text .= $titlePrefixedDBKey;
    $text .= "|**Username**:\n";
}

$output = $parser->renderImageGallery( $text, $params )

如何检索上传照片的用户的姓名以将其显示在图片库中(我在其中放置了用户名)?

【问题讨论】:

  • $title->getFirstRevision()->getUserText()
  • 感谢您的帮助@Tgr!

标签: php mediawiki


【解决方案1】:

您的$title 变量是Title object。您应该可以使用它来获取最新版本的作者:

$currentRevID = $title->getLatestRevID();
$revAuthors = $title->getAuthorsBetween($currentRevID, $currentRevID, 1, 'include_both'); //1=limit
$authorName = $revAuthors[0];

这使用了一个函数来返回两个修订之间的作者,但我们只是将当前修订作为最小和最大修订传递。

请注意,编辑图片说明会被视为一次修订,因此此代码可能会返回未上传文件的用户的名称。发生这种情况的可能性有多大可能取决于您的 wiki 的使用方式。

【讨论】:

  • FWIW 这和我上面的代码 sn-p 都假设第一个修订版的作者和文件的上传者是相同的,这在异国情调的情况下可能是不真实的。不是我担心的事情,只是注意。
猜你喜欢
  • 2015-06-08
  • 2017-11-19
  • 1970-01-01
  • 2013-07-18
  • 2018-04-22
  • 2011-11-21
  • 2013-04-10
  • 1970-01-01
  • 2020-10-27
相关资源
最近更新 更多